alibaba/kt-connect

View on GitHub
pkg/kt/service/dns/hosts.go

Summary

Maintainability
A
3 hrs
Test Coverage

Function dropHosts has a Cognitive Complexity of 26 (exceeds 20 allowed). Consider refactoring.
Open

func dropHosts(rawLines []string, namespaceToDrop string) ([]string, []string, error) {
    escapeBegin := -1
    escapeEnd := -1
    midDomain := fmt.Sprintf(".%s", namespaceToDrop)
    fullDomain := fmt.Sprintf(".%s.svc.%s", namespaceToDrop, opt.Get().Connect.ClusterDomain)
Severity: Minor
Found in pkg/kt/service/dns/hosts.go - About 1 hr to fix

Cognitive Complexity

Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

A method's cognitive complexity is based on a few simple rules:

  • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
  • Code is considered more complex for each "break in the linear flow of the code"
  • Code is considered more complex when "flow breaking structures are nested"

Further reading

Avoid deeply nested control flow statements.
Open

            } else if !strings.HasSuffix(l, midDomain) && !strings.HasSuffix(l, fullDomain) {
                recordsToKeep = append(recordsToKeep, l)
            }
Severity: Major
Found in pkg/kt/service/dns/hosts.go - About 45 mins to fix

    Avoid deeply nested control flow statements.
    Open

                    if keepShortDomain {
                        recordsToKeep = append(recordsToKeep, l)
                    }
    Severity: Major
    Found in pkg/kt/service/dns/hosts.go - About 45 mins to fix

      Function updateHostsFile has 5 return statements (exceeds 4 allowed).
      Open

      func updateHostsFile(lines []string) error {
          lock := flock.New(fmt.Sprintf("%s/hosts.lock", util.KtLockDir))
          timeoutContext, cancel := context.WithTimeout(context.TODO(), 2 * time.Second)
          defer cancel()
          if ok, err := lock.TryLockContext(timeoutContext, 100 * time.Millisecond); !ok {
      Severity: Major
      Found in pkg/kt/service/dns/hosts.go - About 35 mins to fix

        Function DumpHosts has 5 return statements (exceeds 4 allowed).
        Open

        func DumpHosts(hostsMap map[string]string, namespaceToDrop string) error {
            if doNotDump {
                return nil
            }
            lines, err := loadHostsFile()
        Severity: Major
        Found in pkg/kt/service/dns/hosts.go - About 35 mins to fix

          There are no issues that match your filters.

          Category
          Status