external-dns
external-dns copied to clipboard
Help required to reduce cyclomatic complexity of the code
trafficstars
What would you like to be added:
In the file .golangci.yml enable cyclop linter
- cyclop # Checks function and package cyclomatic complexity. https://golangci-lint.run/usage/linters/#cyclop
Why is this needed:
What is cyclomatic complexity https://linearb.io/blog/reduce-cyclomatic-complexity
- Reduced Cognitive Load During Code Review: When a codebase adheres to consistent style guidelines, naming conventions, and architectural patterns, reviewers can focus more on the logic and potential issues within the code changes rather than spending time deciphering unfamiliar or inconsistent formatting and structures. This allows for more efficient and insightful reviews, ultimately leading to higher quality code.
- Enhanced Readability: A uniform codebase is more predictable, making it easier for contributors to follow and understand the code, thereby reducing the cognitive load.
- Easier Onboarding: When a codebase is consistent, new team members can get up to speed more quickly as they don’t have to adjust to different coding styles or paradigms in different parts of the codebase.
- Efficient Debugging and Maintenance: Uniformity in the codebase makes it easier to spot errors and maintain the code, as contributors know what to expect and where to look for potential issues.
To be fixed
NOTE: this is quite a challenging task, do not try to reduce complexity of all of the functions in single PR
What is expected
- Enable linter and run
make lint - Pick any function with complexity > 10
- Run
make cover-htmlto find out gaps if function is not covered with unit-tests - This is highly desirable to cover it with unit tests before refactoring. This will help with code review
- Reduce complexity of the function; most likely slice fucntion, reduce nesting of if statements, etc https://linearb.io/blog/reduce-cyclomatic-complexity
golangci-lint run --timeout=30m --fix ./...
controller/execute.go:76:1: calculated cyclomatic complexity for function Execute is 52, max is 10 (cyclop)
func Execute() {
^
endpoint/endpoint.go:138:1: calculated cyclomatic complexity for function IsLess is 14, max is 10 (cyclop)
func (t Targets) IsLess(o Targets) bool {
^
pkg/apis/externaldns/validation/validation.go:29:1: calculated cyclomatic complexity for function ValidateConfig is 30, max is 10 (cyclop)
func ValidateConfig(cfg *externaldns.Config) error {
^
pkg/rfc2317/arpa.go:30:1: calculated cyclomatic complexity for function CidrToInAddr is 15, max is 10 (cyclop)
func CidrToInAddr(cidr string) (string, error) {
^
plan/plan.go:167:1: calculated cyclomatic complexity for function Calculate is 31, max is 10 (cyclop)
func (p *Plan) Calculate() *Plan {
^
provider/akamai/akamai.go:90:1: calculated cyclomatic complexity for function NewAkamaiProvider is 12, max is 10 (cyclop)
func NewAkamaiProvider(akamaiConfig AkamaiConfig, akaService AkamaiDNSService) (provider.Provider, error) {
^
provider/alibabacloud/alibaba_cloud.go:621:1: calculated cyclomatic complexity for function updateRecords is 13, max is 10 (cyclop)
func (p *AlibabaCloudProvider) updateRecords(recordMap map[string][]alidns.Record, endpoints []*endpoint.Endpoint, hostedZoneDomains []string) error {
^
provider/alibabacloud/alibaba_cloud.go:1014:1: calculated cyclomatic complexity for function updatePrivateZoneRecords is 18, max is 10 (cyclop)
func (p *AlibabaCloudProvider) updatePrivateZoneRecords(zones map[string]*alibabaPrivateZone, endpoints []*endpoint.Endpoint) error {
^
provider/alibabacloud/alibaba_cloud_test.go:387:1: calculated cyclomatic complexity for function TestAlibabaCloudProvider_splitDNSName is 21, max is 10 (cyclop)
func TestAlibabaCloudProvider_splitDNSName(t *testing.T) {
^
provider/aws/aws.go:352:1: calculated cyclomatic complexity for function zones is 19, max is 10 (cyclop)
func (p *AWSProvider) zones(ctx context.Context) (map[string]*profiledZone, error) {
^
provider/aws/aws.go:466:1: calculated cyclomatic complexity for function records is 25, max is 10 (cyclop)
func (p *AWSProvider) records(ctx context.Context, zones map[string]*profiledZone) ([]*endpoint.Endpoint, error) {
^
provider/aws/aws.go:655:1: calculated cyclomatic complexity for function submitChanges is 18, max is 10 (cyclop)
func (p *AWSProvider) submitChanges(ctx context.Context, changes Route53Changes, zones map[string]*profiledZone) error {
^
provider/aws/aws.go:776:1: calculated cyclomatic complexity for function AdjustEndpoints is 14, max is 10 (cyclop)
func (p *AWSProvider) AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoint.Endpoint, error) {
^
provider/aws/aws.go:845:1: calculated cyclomatic complexity for function newChange is 18, max is 10 (cyclop)
func (p *AWSProvider) newChange(action route53types.ChangeAction, ep *endpoint.Endpoint) *Route53Change {
^
provider/aws/aws.go:1023:1: calculated cyclomatic complexity for function batchChangeSet is 13, max is 10 (cyclop)
func batchChangeSet(cs Route53Changes, batchSize int, batchSizeBytes int, batchSizeValues int) []Route53Changes {
^
provider/aws/aws_test.go:186:1: calculated cyclomatic complexity for function ChangeResourceRecordSets is 16, max is 10 (cyclop)
func (r *Route53APIStub) ChangeResourceRecordSets(ctx context.Context, input *route53.ChangeResourceRecordSetsInput, optFns ...func(options *route53.Options)) (*route53.ChangeResourceRecordSetsOutput, error) {
^
provider/azure/azure.go:109:1: calculated cyclomatic complexity for function Records is 13, max is 10 (cyclop)
func (p *AzureProvider) Records(ctx context.Context) (endpoints []*endpoint.Endpoint, _ error) {
^
provider/azure/azure.go:174:1: calculated cyclomatic complexity for function zones is 11, max is 10 (cyclop)
func (p *AzureProvider) zones(ctx context.Context) ([]dns.Zone, error) {
^
provider/azure/azure.go:342:1: calculated cyclomatic complexity for function newRecordSet is 13, max is 10 (cyclop)
func (p *AzureProvider) newRecordSet(endpoint *endpoint.Endpoint) (dns.RecordSet, error) {
^
provider/azure/azure.go:437:1: calculated cyclomatic complexity for function extractAzureTargets is 19, max is 10 (cyclop)
func extractAzureTargets(recordSet *dns.RecordSet) []string {
^
provider/azure/azure_private_dns.go:104:1: calculated cyclomatic complexity for function Records is 12, max is 10 (cyclop)
func (p *AzurePrivateDNSProvider) Records(ctx context.Context) (endpoints []*endpoint.Endpoint, _ error) {
^
provider/azure/azure_private_dns.go:184:1: calculated cyclomatic complexity for function zones is 11, max is 10 (cyclop)
func (p *AzurePrivateDNSProvider) zones(ctx context.Context) ([]privatedns.PrivateZone, error) {
^
provider/azure/azure_private_dns.go:349:1: calculated cyclomatic complexity for function newRecordSet is 11, max is 10 (cyclop)
func (p *AzurePrivateDNSProvider) newRecordSet(endpoint *endpoint.Endpoint) (privatedns.RecordSet, error) {
^
provider/azure/azure_private_dns.go:423:1: calculated cyclomatic complexity for function extractAzurePrivateDNSTargets is 16, max is 10 (cyclop)
func extractAzurePrivateDNSTargets(recordSet *privatedns.RecordSet) []string {
^
provider/azure/config.go:108:1: calculated cyclomatic complexity for function getCredentials is 12, max is 10 (cyclop)
func getCredentials(cfg config, maxRetries int) (azcore.TokenCredential, *arm.ClientOptions, error) {
^
provider/azure/config_test.go:78:1: calculated cyclomatic complexity for function TestCustomHeaderPolicyWithRetries is 17, max is 10 (cyclop)
func TestCustomHeaderPolicyWithRetries(t *testing.T) {
^
provider/civo/civo.go:176:1: calculated cyclomatic complexity for function submitChanges is 11, max is 10 (cyclop)
func (p *CivoProvider) submitChanges(ctx context.Context, changes CivoChanges) error {
^
provider/civo/civo_test.go:963:1: calculated cyclomatic complexity for function elementsMatch is 15, max is 10 (cyclop)
func elementsMatch(t *testing.T, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) {
^
provider/cloudflare/cloudflare.go:296:1: calculated cyclomatic complexity for function Zones is 11, max is 10 (cyclop)
func (p *CloudFlareProvider) Zones(ctx context.Context) ([]cloudflare.Zone, error) {
^
provider/cloudflare/cloudflare.go:374:1: calculated cyclomatic complexity for function ApplyChanges is 13, max is 10 (cyclop)
func (p *CloudFlareProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
^
provider/cloudflare/cloudflare.go:423:1: calculated cyclomatic complexity for function submitCustomHostnameChanges is 23, max is 10 (cyclop)
func (p *CloudFlareProvider) submitCustomHostnameChanges(ctx context.Context, zoneID string, change *cloudFlareChange, chs CustomHostnamesMap, logFields log.Fields) bool {
^
provider/cloudflare/cloudflare.go:498:1: calculated cyclomatic complexity for function submitDataLocalizationRegionalHostnameChanges is 19, max is 10 (cyclop)
func (p *CloudFlareProvider) submitDataLocalizationRegionalHostnameChanges(ctx context.Context, changes []DataLocalizationRegionalHostnameChange, resourceContainer *cloudflare.ResourceContainer) bool {
^
provider/cloudflare/cloudflare.go:578:1: calculated cyclomatic complexity for function dataLocalizationRegionalHostnamesChanges is 12, max is 10 (cyclop)
func dataLocalizationRegionalHostnamesChanges(changes []*cloudFlareChange) ([]DataLocalizationRegionalHostnameChange, error) {
^
provider/cloudflare/cloudflare.go:621:1: calculated cyclomatic complexity for function submitChanges is 23, max is 10 (cyclop)
func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloudFlareChange) error {
^
provider/cloudflare/cloudflare.go:970:1: calculated cyclomatic complexity for function groupByNameAndTypeWithCustomHostnames is 11, max is 10 (cyclop)
func groupByNameAndTypeWithCustomHostnames(records DNSRecordsMap, chs CustomHostnamesMap) []*endpoint.Endpoint {
^
provider/coredns/coredns.go:334:1: calculated cyclomatic complexity for function ApplyChanges is 27, max is 10 (cyclop)
func (p coreDNSProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
^
provider/digitalocean/digital_ocean.go:323:1: calculated cyclomatic complexity for function submitChanges is 13, max is 10 (cyclop)
func (p *DigitalOceanProvider) submitChanges(ctx context.Context, changes *digitalOceanChanges) error {
^
provider/digitalocean/digital_ocean_test.go:197:1: calculated cyclomatic complexity for function elementsMatch is 15, max is 10 (cyclop)
func elementsMatch(t *testing.T, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) {
^
provider/dnsimple/dnsimple.go:261:1: calculated cyclomatic complexity for function submitChanges is 15, max is 10 (cyclop)
func (p *dnsimpleProvider) submitChanges(ctx context.Context, changes []*dnsimpleChange) error {
^
provider/exoscale/exoscale.go:102:1: calculated cyclomatic complexity for function ApplyChanges is 24, max is 10 (cyclop)
func (ep *ExoscaleProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
^
provider/gandi/gandi.go:159:1: calculated cyclomatic complexity for function submitChanges is 15, max is 10 (cyclop)
func (p *GandiProvider) submitChanges(ctx context.Context, changes []*GandiChanges) error {
^
provider/godaddy/godaddy.go:370:1: calculated cyclomatic complexity for function ApplyChanges is 14, max is 10 (cyclop)
func (p *GDProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
^
provider/google/google.go:314:1: calculated cyclomatic complexity for function batchChange is 11, max is 10 (cyclop)
func batchChange(change *dns.Change, batchSize int) []*dns.Change {
^
provider/ibmcloud/ibmcloud.go:217:1: calculated cyclomatic complexity for function Validate is 24, max is 10 (cyclop)
func (c *ibmcloudConfig) Validate(authenticator core.Authenticator, domainFilter endpoint.DomainFilter, zoneIDFilter provider.ZoneIDFilter) (ibmcloudService, bool, error) {
^
provider/ibmcloud/ibmcloud.go:461:1: calculated cyclomatic complexity for function submitChangesForPrivateDNS is 11, max is 10 (cyclop)
func (p *IBMCloudProvider) submitChangesForPrivateDNS(ctx context.Context, changes []*ibmcloudChange) error {
^
provider/ibmcloud/ibmcloud.go:731:1: calculated cyclomatic complexity for function groupPrivateRecords is 13, max is 10 (cyclop)
func (p *IBMCloudProvider) groupPrivateRecords(records []dnssvcsv1.ResourceRecord) []*endpoint.Endpoint {
^
provider/inmemory/inmemory.go:147:1: calculated cyclomatic complexity for function ApplyChanges is 12, max is 10 (cyclop)
func (im *InMemoryProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
^
provider/inmemory/inmemory.go:308:1: calculated cyclomatic complexity for function validateChangeBatch is 15, max is 10 (cyclop)
func (c *inMemoryClient) validateChangeBatch(zone string, changes *plan.Changes) error {
^
provider/linode/linode.go:266:1: calculated cyclomatic complexity for function ApplyChanges is 25, max is 10 (cyclop)
func (p *LinodeProvider) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
^
provider/ns1/ns1.go:199:1: calculated cyclomatic complexity for function ns1SubmitChanges is 12, max is 10 (cyclop)
func (p *NS1Provider) ns1SubmitChanges(changes []*ns1Change) error {