1
0
Fork 0

Merge current branch v2.4 into master

This commit is contained in:
Jean-Baptiste Doumenjou 2021-03-09 11:32:01 +01:00
commit 702e301990
100 changed files with 561 additions and 557 deletions

View file

@ -63,27 +63,24 @@ func (c *ChallengeTLSALPN) Present(domain, _, keyAuth string) error {
timer := time.NewTimer(c.Timeout)
var errC error
select {
case t := <-timer.C:
timer.Stop()
close(c.chans[string(certPEMBlock)])
c.muChans.Lock()
c.cleanChan(string(certPEMBlock))
c.muChans.Unlock()
err = c.CleanUp(domain, "", keyAuth)
if err != nil {
logger.Errorf("Failed to clean up TLS challenge: %v", err)
}
errC = fmt.Errorf("timeout %s", t)
return fmt.Errorf("timeout %s", t)
case <-ch:
// noop
return nil
}
c.muChans.Lock()
delete(c.chans, string(certPEMBlock))
c.muChans.Unlock()
return errC
}
// CleanUp cleans the challenges when certificate is obtained.
@ -115,16 +112,23 @@ func (c *ChallengeTLSALPN) Provide(configurationChan chan<- dynamic.Message, _ *
// ListenConfiguration sets a new Configuration into the configurationChan.
func (c *ChallengeTLSALPN) ListenConfiguration(conf dynamic.Configuration) {
c.muChans.Lock()
for _, certificate := range conf.TLS.Certificates {
if !containsACMETLS1(certificate.Stores) {
continue
}
c.muChans.Lock()
if _, ok := c.chans[certificate.CertFile.String()]; ok {
close(c.chans[certificate.CertFile.String()])
}
c.muChans.Unlock()
c.cleanChan(certificate.CertFile.String())
}
c.muChans.Unlock()
}
func (c *ChallengeTLSALPN) cleanChan(key string) {
if _, ok := c.chans[key]; ok {
close(c.chans[key])
delete(c.chans, key)
}
}

View file

@ -2,7 +2,7 @@ package acme
import (
"encoding/json"
"io/ioutil"
"io"
"os"
"sync"
@ -60,7 +60,7 @@ func (s *LocalStore) get(resolverName string) (*StoredData, error) {
}
defer f.Close()
file, err := ioutil.ReadAll(f)
file, err := io.ReadAll(f)
if err != nil {
return nil, err
}
@ -108,7 +108,7 @@ func (s *LocalStore) listenSaveAction() {
logger.Error(err)
}
err = ioutil.WriteFile(s.filename, data, 0o600)
err = os.WriteFile(s.filename, data, 0o600)
if err != nil {
logger.Error(err)
}

View file

@ -2,7 +2,7 @@ package acme
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
@ -23,7 +23,7 @@ func TestLocalStore_GetAccount(t *testing.T) {
}
}`, email)
err := ioutil.WriteFile(acmeFile, []byte(filePayload), 0o600)
err := os.WriteFile(acmeFile, []byte(filePayload), 0o600)
require.NoError(t, err)
testCases := []struct {
@ -68,7 +68,7 @@ func TestLocalStore_SaveAccount(t *testing.T) {
time.Sleep(100 * time.Millisecond)
file, err := ioutil.ReadFile(acmeFile)
file, err := os.ReadFile(acmeFile)
require.NoError(t, err)
expected := `{

View file

@ -119,10 +119,6 @@ func (p ProviderAggregator) Init() error {
// Provide calls the provide method of every providers.
func (p ProviderAggregator) Provide(configurationChan chan<- dynamic.Message, pool *safe.Pool) error {
if p.internalProvider != nil {
launchProvider(configurationChan, pool, p.internalProvider)
}
if p.fileProvider != nil {
launchProvider(configurationChan, pool, p.fileProvider)
}
@ -134,6 +130,12 @@ func (p ProviderAggregator) Provide(configurationChan chan<- dynamic.Message, po
})
}
// internal provider must be the last because we use it to know if all the providers are loaded.
// ConfigurationWatcher will wait for this requiredProvider before applying configurations.
if p.internalProvider != nil {
launchProvider(configurationChan, pool, p.internalProvider)
}
return nil
}

View file

@ -32,12 +32,11 @@ func TestProviderAggregator_Provide(t *testing.T) {
errCh <- aggregator.Provide(cfgCh, pool)
}()
// Make sure the internal provider is always called first, followed by the file provider.
requireReceivedMessageFromProviders(t, cfgCh, []string{"internal"})
// Make sure the file provider is always called first.
requireReceivedMessageFromProviders(t, cfgCh, []string{"file"})
// Check if all providers have been called, the order doesn't matter.
requireReceivedMessageFromProviders(t, cfgCh, []string{"salad", "tomato", "onion"})
requireReceivedMessageFromProviders(t, cfgCh, []string{"salad", "tomato", "onion", "internal"})
require.NoError(t, <-errCh)
}
@ -52,7 +51,8 @@ func requireReceivedMessageFromProviders(t *testing.T, cfgCh <-chan dynamic.Mess
for range names {
select {
case <-time.After(10 * time.Millisecond):
case <-time.After(100 * time.Millisecond):
require.Fail(t, "Timeout while waiting for configuration.")
case msg = <-cfgCh:
receivedMessagesFrom = append(receivedMessagesFrom, msg.ProviderName)
}

View file

@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -197,7 +196,7 @@ func flattenCertificates(ctx context.Context, tlsConfig *dynamic.TLSConfiguratio
}
func (p *Provider) loadFileConfigFromDirectory(ctx context.Context, directory string, configuration *dynamic.Configuration) (*dynamic.Configuration, error) {
fileList, err := ioutil.ReadDir(directory)
fileList, err := os.ReadDir(directory)
if err != nil {
return configuration, fmt.Errorf("unable to read directory %s: %w", directory, err)
}
@ -436,7 +435,7 @@ func (p *Provider) decodeConfiguration(filePath, content string) (*dynamic.Confi
func readFile(filename string) (string, error) {
if len(filename) > 0 {
buf, err := ioutil.ReadFile(filename)
buf, err := os.ReadFile(filename)
if err != nil {
return "", err
}

View file

@ -3,7 +3,6 @@ package file
import (
"context"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@ -27,13 +26,12 @@ type ProvideTestCase struct {
}
func TestTLSContent(t *testing.T) {
tempDir := createTempDir(t, "testdir")
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()
fileTLS, err := createTempFile("./fixtures/toml/tls_file.cert", tempDir)
require.NoError(t, err)
fileConfig, err := ioutil.TempFile(tempDir, "temp*.toml")
fileConfig, err := os.CreateTemp(tempDir, "temp*.toml")
require.NoError(t, err)
content := `
@ -245,7 +243,7 @@ func getTestCases() []ProvideTestCase {
func createProvider(t *testing.T, test ProvideTestCase, watch bool) *Provider {
t.Helper()
tempDir := createTempDir(t, "testdir")
tempDir := t.TempDir()
provider := &Provider{}
provider.Watch = true
@ -265,7 +263,7 @@ func createProvider(t *testing.T, test ProvideTestCase, watch bool) *Provider {
var file *os.File
if watch {
var err error
file, err = ioutil.TempFile(tempDir, "temp*"+filepath.Ext(test.filePath))
file, err = os.CreateTemp(tempDir, "temp*"+filepath.Ext(test.filePath))
require.NoError(t, err)
} else {
var err error
@ -283,17 +281,6 @@ func createProvider(t *testing.T, test ProvideTestCase, watch bool) *Provider {
return provider
}
// createTempDir Helper.
func createTempDir(t *testing.T, dir string) string {
t.Helper()
d, err := ioutil.TempDir("", dir)
if err != nil {
t.Fatal(err)
}
return d
}
func copyFile(srcPath, dstPath string) error {
dst, err := os.OpenFile(dstPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0o666)
if err != nil {
@ -312,7 +299,7 @@ func copyFile(srcPath, dstPath string) error {
}
func createTempFile(srcPath, tempDir string) (*os.File, error) {
file, err := ioutil.TempFile(tempDir, "temp*"+filepath.Ext(srcPath))
file, err := os.CreateTemp(tempDir, "temp*"+filepath.Ext(srcPath))
if err != nil {
return nil, err
}

View file

@ -4,7 +4,7 @@ import (
"context"
"fmt"
"hash/fnv"
"io/ioutil"
"io"
"net/http"
"time"
@ -139,7 +139,7 @@ func (p *Provider) fetchConfigurationData() ([]byte, error) {
return nil, fmt.Errorf("received non-ok response code: %d", res.StatusCode)
}
return ioutil.ReadAll(res.Body)
return io.ReadAll(res.Body)
}
// decodeConfiguration decodes and returns the dynamic configuration from the given data.

View file

@ -3,7 +3,6 @@ package crd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -147,7 +146,7 @@ func newExternalClusterClient(endpoint, token, caFilePath string) (*clientWrappe
}
if caFilePath != "" {
caData, err := ioutil.ReadFile(caFilePath)
caData, err := os.ReadFile(caFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read CA file %s: %w", caFilePath, err)
}

View file

@ -2,7 +2,7 @@ package crd
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/traefik/traefik/v2/pkg/provider/kubernetes/crd/traefik/v1alpha1"
@ -46,7 +46,7 @@ func newClientMock(paths ...string) clientMock {
var c clientMock
for _, path := range paths {
yamlContent, err := ioutil.ReadFile(filepath.FromSlash("./fixtures/" + path))
yamlContent, err := os.ReadFile(filepath.FromSlash("./fixtures/" + path))
if err != nil {
panic(err)
}

View file

@ -2,7 +2,7 @@ package crd
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
@ -4537,7 +4537,7 @@ func TestCrossNamespace(t *testing.T) {
var k8sObjects []runtime.Object
var crdObjects []runtime.Object
for _, path := range test.paths {
yamlContent, err := ioutil.ReadFile(filepath.FromSlash("./fixtures/" + path))
yamlContent, err := os.ReadFile(filepath.FromSlash("./fixtures/" + path))
if err != nil {
panic(err)
}

View file

@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/traefik/traefik/v2/pkg/log"
@ -138,7 +138,7 @@ func newExternalClusterClient(endpoint, token, caFilePath string) (*clientWrappe
}
if caFilePath != "" {
caData, err := ioutil.ReadFile(caFilePath)
caData, err := os.ReadFile(caFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read CA file %s: %w", caFilePath, err)
}

View file

@ -2,7 +2,7 @@ package gateway
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/traefik/traefik/v2/pkg/provider/kubernetes/k8s"
@ -43,7 +43,7 @@ func newClientMock(paths ...string) clientMock {
var c clientMock
for _, path := range paths {
yamlContent, err := ioutil.ReadFile(filepath.FromSlash("./fixtures/" + path))
yamlContent, err := os.ReadFile(filepath.FromSlash("./fixtures/" + path))
if err != nil {
panic(err)
}

View file

@ -550,23 +550,24 @@ func (p *Provider) makeGatewayStatus(listenerStatuses []v1alpha1.ListenerStatus)
gatewayStatus.Listeners = listenerStatuses
// update "Scheduled" status with "ResourcesAvailable" reason
gatewayStatus.Conditions = append(gatewayStatus.Conditions, metav1.Condition{
Type: string(v1alpha1.GatewayConditionScheduled),
Status: metav1.ConditionTrue,
Reason: "ResourcesAvailable",
Message: "Resources available",
LastTransitionTime: metav1.Now(),
})
// update "Ready" status with "ListenersValid" reason
gatewayStatus.Conditions = append(gatewayStatus.Conditions, metav1.Condition{
Type: string(v1alpha1.GatewayConditionReady),
Status: metav1.ConditionTrue,
Reason: "ListenersValid",
Message: "Listeners valid",
LastTransitionTime: metav1.Now(),
})
gatewayStatus.Conditions = append(gatewayStatus.Conditions,
// update "Scheduled" status with "ResourcesAvailable" reason
metav1.Condition{
Type: string(v1alpha1.GatewayConditionScheduled),
Status: metav1.ConditionTrue,
Reason: "ResourcesAvailable",
Message: "Resources available",
LastTransitionTime: metav1.Now(),
},
// update "Ready" status with "ListenersValid" reason
metav1.Condition{
Type: string(v1alpha1.GatewayConditionReady),
Status: metav1.ConditionTrue,
Reason: "ListenersValid",
Message: "Listeners valid",
LastTransitionTime: metav1.Now(),
},
)
return gatewayStatus, nil
}

View file

@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -112,7 +111,7 @@ func newExternalClusterClient(endpoint, token, caFilePath string) (*clientWrappe
}
if caFilePath != "" {
caData, err := ioutil.ReadFile(caFilePath)
caData, err := os.ReadFile(caFilePath)
if err != nil {
return nil, fmt.Errorf("failed to read CA file %s: %w", caFilePath, err)
}
@ -341,7 +340,7 @@ func (c *clientWrapper) updateIngressStatusOld(src *networkingv1beta1.Ingress, i
}
// isLoadBalancerIngressEquals returns true if the given slices are equal, false otherwise.
func isLoadBalancerIngressEquals(aSlice []corev1.LoadBalancerIngress, bSlice []corev1.LoadBalancerIngress) bool {
func isLoadBalancerIngressEquals(aSlice, bSlice []corev1.LoadBalancerIngress) bool {
if len(aSlice) != len(bSlice) {
return false
}

View file

@ -2,7 +2,7 @@ package ingress
import (
"fmt"
"io/ioutil"
"os"
"github.com/hashicorp/go-version"
"github.com/traefik/traefik/v2/pkg/provider/kubernetes/k8s"
@ -36,7 +36,7 @@ func newClientMock(serverVersion string, paths ...string) clientMock {
c.serverVersion = version.Must(version.NewVersion(serverVersion))
for _, path := range paths {
yamlContent, err := ioutil.ReadFile(path)
yamlContent, err := os.ReadFile(path)
if err != nil {
panic(err)
}

View file

@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"flag"
"io/ioutil"
"os"
"path/filepath"
"testing"
@ -271,11 +271,11 @@ func Test_createConfiguration(t *testing.T) {
newJSON, err := json.MarshalIndent(cfg, "", " ")
require.NoError(t, err)
err = ioutil.WriteFile(filename, newJSON, 0o644)
err = os.WriteFile(filename, newJSON, 0o644)
require.NoError(t, err)
}
expectedJSON, err := ioutil.ReadFile(filename)
expectedJSON, err := os.ReadFile(filename)
require.NoError(t, err)
actualJSON, err := json.MarshalIndent(cfg, "", " ")