Repository: istio/istio
File: cni/pkg/plugin/plugin.go (Go)

Code before:
				// present both.
				msg = fmt.Sprintf("%s: %v", msg, err)
			}
			err = errors.New(msg)
		}
		if err != nil {
			log.Errorf("istio-cni cmdAdd error: %v", err)
		}
	}()

	conf, err := parseConfig(args.StdinData)
	if err != nil {
		log.Errorf("istio-cni cmdAdd failed to parse config %v %v", string(args.StdinData), err)
		return err
	}

	// Preemptively check if the pod is a CNI pod.
	// It is possible that the kubeconfig is not available if it hasn't been written yet
	// by the CNI pod or it is invalid which cause the CNI pod to be unable to start if
	// on the creation of a new K8s client. We preemptively check if the pod is a CNI pod
	// to avoid a deadlock on the kubeconfig when the k8s client is unnecessary to process
	// the CNI add event for the CNI pod itself.
	if conf.AmbientEnabled {
		k8sArgs := K8sArgs{}
		if err := types.LoadArgs(args.Args, &k8sArgs); err != nil {
			return fmt.Errorf("failed to load args after failed attempt to get client: %v", err)
		}
		if isCNIPod(conf, &k8sArgs) {
			// If we are in a degraded state and this is our own agent pod, skip
			return pluginResponse(conf)
		}
	}

	// Create a kube client
	client, err := newK8sClient(*conf)
	if err != nil {
		return fmt.Errorf("failed to createNewK8sClient: %v", err)
	}

	// Actually do the add
	mgr := IptablesInterceptRuleMgr()
	if conf.NativeNftables {
		mgr = NftablesInterceptRuleMgr()
	}
	if err := doAddRun(args, conf, client, mgr); err != nil {
		return err
	}
	return pluginResponse(conf)
}

func doAddRun(args *skel.CmdArgs, conf *Config, kClient kubernetes.Interface, rulesMgr InterceptRuleMgr) error {

Review comment (bug):
If we move this to before we try to create a client, then the error message becomes misleading and should be updated.
```suggestion
			return fmt.Errorf("failed to load args: %v", err)
```

Code after:
				// present both.
				msg = fmt.Sprintf("%s: %v", msg, err)
			}
			err = errors.New(msg)
		}
		if err != nil {
			log.Errorf("istio-cni cmdAdd error: %v", err)
		}
	}()

	conf, err := parseConfig(args.StdinData)
	if err != nil {
		log.Errorf("istio-cni cmdAdd failed to parse config %v %v", string(args.StdinData), err)
		return err
	}

	// Preemptively check if the pod is a CNI pod.
	// It is possible that the kubeconfig is not available if it hasn't been written yet
	// by the CNI pod or it is invalid which cause the CNI pod to be unable to start if
	// on the creation of a new K8s client. We preemptively check if the pod is a CNI pod
	// to avoid a deadlock on the kubeconfig when the k8s client is unnecessary to process
	// the CNI add event for the CNI pod itself.
	if conf.AmbientEnabled {
		k8sArgs := K8sArgs{}
		if err := types.LoadArgs(args.Args, &k8sArgs); err != nil {
			return fmt.Errorf("failed to load args: %v", err)
		}
		if isCNIPod(conf, &k8sArgs) {
			// If we are in a degraded state and this is our own agent pod, skip
			return pluginResponse(conf)
		}
	}

	// Create a kube client
	client, err := newK8sClient(*conf)
	if err != nil {
		return fmt.Errorf("failed to createNewK8sClient: %v", err)
	}

	// Actually do the add
	mgr := IptablesInterceptRuleMgr()
	if conf.NativeNftables {
		mgr = NftablesInterceptRuleMgr()
	}
	if err := doAddRun(args, conf, client, mgr); err != nil {
		return err
	}
	return pluginResponse(conf)
}

func doAddRun(args *skel.CmdArgs, conf *Config, kClient kubernetes.Interface, rulesMgr InterceptRuleMgr) error {