Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ linters:
- errorlint # Checking for unchecked errors in Go code https://golangci-lint.run/docs/linters/configuration/#errorlint
- staticcheck
- unconvert
- unused
- unused # https://golangci-lint.run/docs/linters/configuration/#unused
- unparam # https://golangci-lint.run/docs/linters/configuration/#unparam
- usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library. https://golangci-lint.run/docs/linters/configuration/#usestdlibvars
- whitespace
- decorder # Check declaration order and count of types, constants, variables and functions. https://golangci-lint.run/docs/linters/configuration/#decorder
Expand Down
31 changes: 15 additions & 16 deletions controller/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ func TestHelperProcess(_ *testing.T) {
Execute()
}

// runExecuteSubprocess runs Execute in a separate process and returns exit code and output.
func runExecuteSubprocess(t *testing.T, args []string) (int, string, error) {
// runExecuteSubprocess runs Execute in a separate process and returns exit code.
func runExecuteSubprocess(t *testing.T, args []string) (int, error) {
t.Helper()
// make sure the subprocess does not run forever
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand All @@ -310,23 +310,22 @@ func runExecuteSubprocess(t *testing.T, args []string) (int, string, error) {
cmd.Stdout = &buf
cmd.Stderr = &buf
err := cmd.Run()
output := buf.String()
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return -1, output, ctx.Err()
return -1, ctx.Err()
}
if err == nil {
return 0, output, nil
return 0, nil
}
ee := &exec.ExitError{}
if errors.As(err, &ee) {
return ee.ExitCode(), output, nil
return ee.ExitCode(), nil
}
return -1, output, err
return -1, err
}

func TestExecuteOnceDryRunExitsZero(t *testing.T) {
// Use :0 for an ephemeral metrics port.
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--source", "fake",
"--provider", "inmemory",
"--once",
Expand All @@ -338,7 +337,7 @@ func TestExecuteOnceDryRunExitsZero(t *testing.T) {
}

func TestExecuteUnknownProviderExitsNonZero(t *testing.T) {
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--source", "fake",
"--provider", "unknown",
"--metrics-address", ":0",
Expand All @@ -348,7 +347,7 @@ func TestExecuteUnknownProviderExitsNonZero(t *testing.T) {
}

func TestExecuteValidationErrorNoSources(t *testing.T) {
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--provider", "inmemory",
"--metrics-address", ":0",
})
Expand All @@ -357,7 +356,7 @@ func TestExecuteValidationErrorNoSources(t *testing.T) {
}

func TestExecuteFlagParsingErrorInvalidLogFormat(t *testing.T) {
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--log-format", "invalid",
// Provide minimal required flags to keep errors focused on parsing
"--source", "fake",
Expand All @@ -370,7 +369,7 @@ func TestExecuteFlagParsingErrorInvalidLogFormat(t *testing.T) {

// Config validation failure triggers log.Fatalf.
func TestExecuteConfigValidationErrorExitsNonZero(t *testing.T) {
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--source", "fake",
// Choose a provider with validation that fails without required flags
"--provider", "azure",
Expand All @@ -385,7 +384,7 @@ func TestExecuteConfigValidationErrorExitsNonZero(t *testing.T) {
func TestExecuteBuildSourceErrorExitsNonZero(t *testing.T) {
// Use a valid source name (ingress) and an invalid kubeconfig path to
// force client creation failure inside buildSource.
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--source", "ingress",
"--kubeconfig", "this/path/does/not/exist",
"--provider", "inmemory",
Expand All @@ -398,7 +397,7 @@ func TestExecuteBuildSourceErrorExitsNonZero(t *testing.T) {
// RunOnce error exits non-zero.
func TestExecuteRunOnceErrorExitsNonZero(t *testing.T) {
// Connector source dials a TCP server; use a closed port to fail.
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--source", "connector",
"--connector-source-server", "127.0.0.1:1",
"--provider", "inmemory",
Expand All @@ -411,7 +410,7 @@ func TestExecuteRunOnceErrorExitsNonZero(t *testing.T) {

// Run loop error exits non-zero.
func TestExecuteRunLoopErrorExitsNonZero(t *testing.T) {
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--source", "connector",
"--connector-source-server", "127.0.0.1:1",
"--provider", "inmemory",
Expand All @@ -423,7 +422,7 @@ func TestExecuteRunLoopErrorExitsNonZero(t *testing.T) {

// buildController registry-creation failure triggers log.Fatal.
func TestExecuteBuildControllerErrorExitsNonZero(t *testing.T) {
code, _, err := runExecuteSubprocess(t, []string{
code, err := runExecuteSubprocess(t, []string{
"--source", "fake",
"--provider", "inmemory",
"--registry", "dynamodb",
Expand Down
36 changes: 14 additions & 22 deletions provider/alibabacloud/alibaba_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,8 @@ func (p *AlibabaCloudProvider) recordsForDNS() ([]*endpoint.Endpoint, error) {
return endpoints, nil
}

func getNextPageNumber(pageNumber, pageSize, totalCount int64) int64 {
if pageNumber*pageSize >= totalCount {
func getNextPageNumber(pageNumber, totalCount int64) int64 {
if pageNumber*defaultAlibabaCloudPageSize >= totalCount {
return 0
}
return pageNumber + 1
Expand Down Expand Up @@ -417,7 +417,7 @@ func (p *AlibabaCloudProvider) getDomainList() ([]string, error) {
for _, tmpDomain := range resp.Domains.Domain {
domainNames = append(domainNames, tmpDomain.DomainName)
}
nextPage := getNextPageNumber(resp.PageNumber, defaultAlibabaCloudPageSize, resp.TotalCount)
nextPage := getNextPageNumber(resp.PageNumber, resp.TotalCount)
if nextPage == 0 {
break
} else {
Expand Down Expand Up @@ -454,7 +454,7 @@ func (p *AlibabaCloudProvider) getDomainRecords(domainName string) ([]alidns.Rec
// TODO filter Locked record
results = append(results, record)
}
nextPage := getNextPageNumber(response.PageNumber, defaultAlibabaCloudPageSize, response.TotalCount)
nextPage := getNextPageNumber(response.PageNumber, response.TotalCount)
if nextPage == 0 {
break
} else {
Expand Down Expand Up @@ -544,13 +544,12 @@ func (p *AlibabaCloudProvider) createRecord(endpoint *endpoint.Endpoint, target
return err
}

func (p *AlibabaCloudProvider) createRecords(endpoints []*endpoint.Endpoint, hostedZoneDomains []string) error {
func (p *AlibabaCloudProvider) createRecords(endpoints []*endpoint.Endpoint, hostedZoneDomains []string) {
for _, endpoint := range endpoints {
for _, target := range endpoint.Targets {
p.createRecord(endpoint, target, hostedZoneDomains)
}
}
return nil
}

func (p *AlibabaCloudProvider) deleteRecord(recordID string) error {
Expand Down Expand Up @@ -591,7 +590,7 @@ func (p *AlibabaCloudProvider) updateRecord(record alidns.Record, endpoint *endp
return err
}

func (p *AlibabaCloudProvider) deleteRecords(recordMap map[string][]alidns.Record, endpoints []*endpoint.Endpoint) error {
func (p *AlibabaCloudProvider) deleteRecords(recordMap map[string][]alidns.Record, endpoints []*endpoint.Endpoint) {
for _, endpoint := range endpoints {
key := p.getRecordKeyByEndpoint(endpoint)
records := recordMap[key]
Expand All @@ -611,7 +610,6 @@ func (p *AlibabaCloudProvider) deleteRecords(recordMap map[string][]alidns.Recor
log.Errorf("Failed to find %s record named '%s' to delete for Alibaba Cloud DNS", endpoint.RecordType, endpoint.DNSName)
}
}
return nil
}

func (p *AlibabaCloudProvider) equals(record alidns.Record, endpoint *endpoint.Endpoint) bool {
Expand All @@ -628,7 +626,7 @@ func (p *AlibabaCloudProvider) equals(record alidns.Record, endpoint *endpoint.E
return ttl1 == ttl2
}

func (p *AlibabaCloudProvider) updateRecords(recordMap map[string][]alidns.Record, endpoints []*endpoint.Endpoint, hostedZoneDomains []string) error {
func (p *AlibabaCloudProvider) updateRecords(recordMap map[string][]alidns.Record, endpoints []*endpoint.Endpoint, hostedZoneDomains []string) {
for _, endpoint := range endpoints {
key := p.getRecordKeyByEndpoint(endpoint)
records := recordMap[key]
Expand Down Expand Up @@ -669,7 +667,6 @@ func (p *AlibabaCloudProvider) updateRecords(recordMap map[string][]alidns.Recor
}
}
}
return nil
}

func (p *AlibabaCloudProvider) splitDNSName(dnsName string, hostedZoneDomains []string) (string, string) {
Expand Down Expand Up @@ -747,7 +744,7 @@ func (p *AlibabaCloudProvider) privateZones() ([]pvtz.Zone, error) {
}
zones = append(zones, zone)
}
nextPage := getNextPageNumber(int64(response.PageNumber), defaultAlibabaCloudPageSize, int64(response.TotalItems))
nextPage := getNextPageNumber(int64(response.PageNumber), int64(response.TotalItems))
if nextPage == 0 {
break
} else {
Expand Down Expand Up @@ -799,7 +796,7 @@ func (p *AlibabaCloudProvider) getPrivateZones() (map[string]*alibabaPrivateZone
// TODO filter Locked
records = append(records, record)
}
nextPage := getNextPageNumber(int64(response.PageNumber), defaultAlibabaCloudPageSize, int64(response.TotalItems))
nextPage := getNextPageNumber(int64(response.PageNumber), int64(response.TotalItems))
if nextPage == 0 {
break
} else {
Expand Down Expand Up @@ -906,13 +903,12 @@ func (p *AlibabaCloudProvider) createPrivateZoneRecord(zones map[string]*alibaba
return err
}

func (p *AlibabaCloudProvider) createPrivateZoneRecords(zones map[string]*alibabaPrivateZone, endpoints []*endpoint.Endpoint) error {
func (p *AlibabaCloudProvider) createPrivateZoneRecords(zones map[string]*alibabaPrivateZone, endpoints []*endpoint.Endpoint) {
for _, endpoint := range endpoints {
for _, target := range endpoint.Targets {
_ = p.createPrivateZoneRecord(zones, endpoint, target)
}
}
return nil
}

func (p *AlibabaCloudProvider) deletePrivateZoneRecord(recordID int64) error {
Expand All @@ -934,15 +930,14 @@ func (p *AlibabaCloudProvider) deletePrivateZoneRecord(recordID int64) error {
return err
}

func (p *AlibabaCloudProvider) deletePrivateZoneRecords(zones map[string]*alibabaPrivateZone, endpoints []*endpoint.Endpoint) error {
func (p *AlibabaCloudProvider) deletePrivateZoneRecords(zones map[string]*alibabaPrivateZone, endpoints []*endpoint.Endpoint) {
zoneNames := keys(zones)
for _, endpoint := range endpoints {
rr, domain := p.splitDNSName(endpoint.DNSName, zoneNames)

zone := zones[domain]
if zone == nil {
err := fmt.Errorf("failed to find private zone '%s'", domain)
log.Errorf("Failed to delete %s record named '%s' for Alibaba Cloud Private Zone: %v", endpoint.RecordType, endpoint.DNSName, err)
log.Errorf("Failed to delete %s record named '%s' for Alibaba Cloud Private Zone: failed to find private zone '%s'", endpoint.RecordType, endpoint.DNSName, domain)
continue
}
found := false
Expand All @@ -962,7 +957,6 @@ func (p *AlibabaCloudProvider) deletePrivateZoneRecords(zones map[string]*alibab
log.Errorf("Failed to find %s record named '%s' to delete for Alibaba Cloud Private Zone", endpoint.RecordType, endpoint.DNSName)
}
}
return nil
}

// ApplyChanges applies the given changes.
Expand Down Expand Up @@ -1021,14 +1015,13 @@ func (p *AlibabaCloudProvider) equalsPrivateZone(record pvtz.Record, endpoint *e
return ttl1 == ttl2
}

func (p *AlibabaCloudProvider) updatePrivateZoneRecords(zones map[string]*alibabaPrivateZone, endpoints []*endpoint.Endpoint) error {
func (p *AlibabaCloudProvider) updatePrivateZoneRecords(zones map[string]*alibabaPrivateZone, endpoints []*endpoint.Endpoint) {
zoneNames := keys(zones)
for _, endpoint := range endpoints {
rr, domain := p.splitDNSName(endpoint.DNSName, zoneNames)
zone := zones[domain]
if zone == nil {
err := fmt.Errorf("failed to find private zone '%s'", domain)
log.Errorf("Failed to update %s record named '%s' for Alibaba Cloud Private Zone: %v", endpoint.RecordType, endpoint.DNSName, err)
log.Errorf("Failed to update %s record named '%s' for Alibaba Cloud Private Zone: failed to find private zone '%s'", endpoint.RecordType, endpoint.DNSName, domain)
continue
}

Expand Down Expand Up @@ -1070,7 +1063,6 @@ func (p *AlibabaCloudProvider) updatePrivateZoneRecords(zones map[string]*alibab
}
}
}
return nil
}

func keys[T any](value map[string]T) []string {
Expand Down
8 changes: 4 additions & 4 deletions provider/aws/aws_fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

func TestAWSRecordsV1(t *testing.T) {
var zones HostedZones
unmarshalTestHelper("/fixtures/160-plus-zones.yaml", &zones, t)
unmarshalZonesFixture(&zones, t)

stub := NewRoute53APIFixtureStub(&zones)
provider := providerFilters(stub,
Expand All @@ -49,7 +49,7 @@ func TestAWSRecordsV1(t *testing.T) {

func TestAWSZonesFilterWithTags(t *testing.T) {
var zones HostedZones
unmarshalTestHelper("/fixtures/160-plus-zones.yaml", &zones, t)
unmarshalZonesFixture(&zones, t)

stub := NewRoute53APIFixtureStub(&zones)
provider := providerFilters(stub,
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestAWSZonesFiltersWithTags(t *testing.T) {
tName := fmt.Sprintf("filters=%s and zones=%d", strings.Join(tt.filters, ","), tt.want)
t.Run(tName, func(t *testing.T) {
var zones HostedZones
unmarshalTestHelper("/fixtures/160-plus-zones.yaml", &zones, t)
unmarshalZonesFixture(&zones, t)

stub := NewRoute53APIFixtureStub(&zones)
provider := providerFilters(stub,
Expand All @@ -94,7 +94,7 @@ func TestAWSZonesFiltersWithTags(t *testing.T) {

func TestAWSZonesSecondRequestHitsTheCache(t *testing.T) {
var zones HostedZones
unmarshalTestHelper("/fixtures/160-plus-zones.yaml", &zones, t)
unmarshalZonesFixture(&zones, t)

stub := NewRoute53APIFixtureStub(&zones)
provider := providerFilters(stub)
Expand Down
4 changes: 2 additions & 2 deletions provider/aws/aws_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ func (r Route53APIFixtureStub) ListTagsForResources(_ context.Context, input *ro
return &route53.ListTagsForResourcesOutput{ResourceTagSets: sets}, nil
}

func unmarshalTestHelper(input string, obj any, t *testing.T) {
func unmarshalZonesFixture(obj any, t *testing.T) {
t.Helper()
path, _ := os.Getwd()
file, err := os.Open(path + input)
file, err := os.Open(path + "/fixtures/160-plus-zones.yaml")
assert.NoError(t, err)
defer file.Close()
dec := yaml.NewDecoder(file)
Expand Down
Loading
Loading