alibaba/kt-connect

View on GitHub

Showing 109 of 109 total issues

Function getPodNameAndPort has 51 lines of code (exceeds 50 allowed). Consider refactoring.
Open

func getPodNameAndPort(serviceName string, remotePort int, namespace string) (string, int, int, error) {
    svc, err := cluster.Ins().GetService(serviceName, namespace)
    if err != nil {
        if k8sErrors.IsNotFound(err) {
            return "", 0, 0, fmt.Errorf("service '%s' is not found in namespace %s", serviceName, namespace)
Severity: Minor
Found in pkg/kt/command/forward/common.go - About 1 hr to fix

    Function HandleMeshedByAutoService has 51 lines of code (exceeds 50 allowed). Consider refactoring.
    Open

    func HandleMeshedByAutoService(svc *coreV1.Service, deployment *appV1.Deployment, pod *coreV1.Pod) error {
        // shadow pods, shadow deployments, shadow services
        if deployment != nil {
            return fmt.Errorf("service '%s' is meshed but selecting more than a router pod, cannot auto recover", svc.Name)
        } else if pod == nil {
    Severity: Minor
    Found in pkg/kt/command/recover/common.go - About 1 hr to fix

      Function getKtRouteRecords has 51 lines of code (exceeds 50 allowed). Consider refactoring.
      Open

      func getKtRouteRecords(s *Cli) ([]RouteRecord, error) {
          records := []RouteRecord{}
      
          // run command: netsh interface ipv4 show route store=persistent
          out, _, err := util.RunAndWait(exec.Command("netsh",
      Severity: Minor
      Found in pkg/kt/service/tun/tun_windows.go - About 1 hr to fix

        Method Kubernetes.tryGetExistingShadows has 10 return statements (exceeds 4 allowed).
        Open

        func (k *Kubernetes) tryGetExistingShadows(resourceMeta *ResourceMeta, sshKeyMeta *SSHkeyMeta) (*coreV1.Pod, *util.SSHGenerator, error) {
            var app *appV1.Deployment
            var pod *coreV1.Pod
            if opt.Get().Global.UseShadowDeployment {
                app2, err := k.GetDeployment(resourceMeta.Name, resourceMeta.Namespace)
        Severity: Major
        Found in pkg/kt/service/cluster/shadow_pod.go - About 1 hr to fix

          Function AutoMesh has 10 return statements (exceeds 4 allowed).
          Open

          func AutoMesh(svc *coreV1.Service) error {
              // Lock service to avoid conflict, must be first step
              svc, err := general.LockService(svc.Name, opt.Get().Global.Namespace, 0)
              if err != nil {
                  return err
          Severity: Major
          Found in pkg/kt/command/mesh/auto.go - About 1 hr to fix

            Similar blocks of code found in 3 locations. Consider refactoring.
            Open

            func (k *Kubernetes) GetDeploymentsByLabel(labels map[string]string, namespace string) (pods *appV1.DeploymentList, err error) {
                return k.Clientset.AppsV1().Deployments(namespace).List(context.TODO(), metav1.ListOptions{
                    LabelSelector:  labelApi.SelectorFromSet(labels).String(),
                    TimeoutSeconds: &apiTimeout,
                })
            Severity: Major
            Found in pkg/kt/service/cluster/deployment.go and 2 other locations - About 55 mins to fix
            pkg/kt/service/cluster/configmap.go on lines 19..24
            pkg/kt/service/cluster/service.go on lines 42..47

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 127.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 3 locations. Consider refactoring.
            Open

            func (k *Kubernetes) GetConfigMapsByLabel(labels map[string]string, namespace string) (pods *coreV1.ConfigMapList, err error) {
                return k.Clientset.CoreV1().ConfigMaps(namespace).List(context.TODO(), metav1.ListOptions{
                    LabelSelector:  labelApi.SelectorFromSet(labels).String(),
                    TimeoutSeconds: &apiTimeout,
                })
            Severity: Major
            Found in pkg/kt/service/cluster/configmap.go and 2 other locations - About 55 mins to fix
            pkg/kt/service/cluster/deployment.go on lines 20..25
            pkg/kt/service/cluster/service.go on lines 42..47

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 127.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Similar blocks of code found in 3 locations. Consider refactoring.
            Open

            func (k *Kubernetes) GetServicesByLabel(labels map[string]string, namespace string) (svcs *coreV1.ServiceList, err error) {
                return k.Clientset.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{
                    LabelSelector:  labelApi.SelectorFromSet(labels).String(),
                    TimeoutSeconds: &apiTimeout,
                })
            Severity: Major
            Found in pkg/kt/service/cluster/service.go and 2 other locations - About 55 mins to fix
            pkg/kt/service/cluster/configmap.go on lines 19..24
            pkg/kt/service/cluster/deployment.go on lines 20..25

            Duplicated Code

            Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

            Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

            When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

            Tuning

            This issue has a mass of 127.

            We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

            The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

            If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

            See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

            Refactorings

            Further Reading

            Function GetTargetPorts has a Cognitive Complexity of 24 (exceeds 20 allowed). Consider refactoring.
            Open

            func GetTargetPorts(svc *coreV1.Service) map[int]string {
                var pod *coreV1.Pod = nil
                svcPorts := svc.Spec.Ports
                targetPorts := map[int]string{}
                for _, p := range svcPorts {
            Severity: Minor
            Found in pkg/kt/command/general/resource.go - About 55 mins 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

            Function BySelector has 9 return statements (exceeds 4 allowed).
            Open

            func BySelector(resourceName string) error {
                // Get service to exchange
                svc, err := general.GetServiceByResourceName(resourceName, opt.Get().Global.Namespace)
                if err != nil {
                    return err
            Severity: Major
            Found in pkg/kt/command/exchange/selector.go - About 55 mins to fix

              Function getPodNameAndPort has 9 return statements (exceeds 4 allowed).
              Open

              func getPodNameAndPort(serviceName string, remotePort int, namespace string) (string, int, int, error) {
                  svc, err := cluster.Ins().GetService(serviceName, namespace)
                  if err != nil {
                      if k8sErrors.IsNotFound(err) {
                          return "", 0, 0, fmt.Errorf("service '%s' is not found in namespace %s", serviceName, namespace)
              Severity: Major
              Found in pkg/kt/command/forward/common.go - About 55 mins to fix

                Function GetServiceStatus has a Cognitive Complexity of 24 (exceeds 20 allowed). Consider refactoring.
                Open

                func GetServiceStatus(ktSvcs []coreV1.Service, pods []coreV1.Pod, svcs []coreV1.Service) [][]string {
                    allServices := make([][]string, 0)
                    for _, svc := range ktSvcs {
                        for _, p := range pods {
                            if p.Labels[util.KtRole] == util.RolePreviewShadow && util.MapContains(svc.Spec.Selector, p.Labels) {
                Severity: Minor
                Found in pkg/kt/command/birdseye/common.go - About 55 mins 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

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                func analysisExpiredServices(svc coreV1.Service, cleanThresholdInMinus int64, resourceToClean *ResourceToClean) {
                    lastHeartBeat := util.ParseTimestamp(svc.Annotations[util.KtLastHeartBeat])
                    if lastHeartBeat < 0 {
                        log.Debug().Msgf("Service %s does no have heart beat annotation", svc.Name)
                    } else if isExpired(lastHeartBeat, cleanThresholdInMinus) {
                Severity: Minor
                Found in pkg/kt/command/clean/common.go and 1 other location - About 55 mins to fix
                pkg/kt/command/clean/common.go on lines 227..234

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 123.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                func analysisExpiredConfigmaps(cf coreV1.ConfigMap, cleanThresholdInMinus int64, resourceToClean *ResourceToClean) {
                    lastHeartBeat := util.ParseTimestamp(cf.Annotations[util.KtLastHeartBeat])
                    if lastHeartBeat < 0 {
                        log.Debug().Msgf("Configmap %s does no have heart beat annotation", cf.Name)
                    } else if isExpired(lastHeartBeat, cleanThresholdInMinus) {
                Severity: Minor
                Found in pkg/kt/command/clean/common.go and 1 other location - About 55 mins to fix
                pkg/kt/command/clean/common.go on lines 246..253

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 123.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Function setupDns has 8 return statements (exceeds 4 allowed).
                Open

                func setupDns(shadowPodName, shadowPodIp string) error {
                    if strings.HasPrefix(opt.Get().Connect.DnsMode, util.DnsModeHosts) {
                        log.Info().Msgf("Setting up dns in hosts mode")
                        dump2HostsNamespaces := ""
                        pos := len(util.DnsModeHosts)
                Severity: Major
                Found in pkg/kt/command/connect/common.go - About 50 mins to fix

                  Function UpdateServiceSelector has 8 return statements (exceeds 4 allowed).
                  Open

                  func UpdateServiceSelector(svcName, namespace string, selector map[string]string) error {
                      svc, err := cluster.Ins().GetService(svcName, namespace)
                      if err != nil {
                          return err
                      }
                  Severity: Major
                  Found in pkg/kt/command/general/resource.go - About 50 mins to fix

                    Function ByTun2Socks has 8 return statements (exceeds 4 allowed).
                    Open

                    func ByTun2Socks() error {
                        podIP, podName, privateKeyPath, err := getOrCreateShadow()
                        if err != nil {
                            return err
                        }
                    Severity: Major
                    Found in pkg/kt/command/connect/tun2socks.go - About 50 mins to fix

                      Similar blocks of code found in 2 locations. Consider refactoring.
                      Open

                      func handleServiceEvent(obj any, status string, f func(*coreV1.Service)) {
                          switch obj.(type) {
                          case *coreV1.Service:
                              if f != nil {
                                  log.Debug().Msgf("Service %s %s", obj.(*coreV1.Service).Name, status)
                      Severity: Minor
                      Found in pkg/kt/service/cluster/service.go and 1 other location - About 50 mins to fix
                      pkg/kt/service/cluster/pod.go on lines 175..185

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 119.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 2 locations. Consider refactoring.
                      Open

                      func handlePodEvent(obj any, status string, f func(*coreV1.Pod)) {
                          switch obj.(type) {
                          case *coreV1.Pod:
                              if f != nil {
                                  log.Debug().Msgf("Pod %s %s", obj.(*coreV1.Pod).Name, status)
                      Severity: Minor
                      Found in pkg/kt/service/cluster/pod.go and 1 other location - About 50 mins to fix
                      pkg/kt/service/cluster/service.go on lines 107..117

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 119.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Avoid deeply nested control flow statements.
                      Open

                                      } else if index == 5 {
                                          iface = parts[i]
                                      } else if index > 5 {
                                          iface = fmt.Sprintf("%s %s", iface, parts[i])
                                      }
                      Severity: Major
                      Found in pkg/kt/service/tun/tun_windows.go - About 45 mins to fix
                        Severity
                        Category
                        Status
                        Source
                        Language