func()

in pkg/cni/conf/confmanager.go [157:196]


func (mgr *Manager) removeCNIPluginConf() error {
	log := logger.GetLogger()
	cm := &corev1.ConfigMap{}
	cmKey := client.ObjectKey{Name: mgr.cniUninstallConfigMapName, Namespace: os.Getenv(consts.PodNamespaceEnvKey)}
	err := mgr.k8sClient.Get(context.Background(), cmKey, cm)
	if err == nil {
		if cm.Data["uninstall"] == "false" {
			log.Info("Uninstall flag is NOT set, skip removing cni configuration file")
			return nil
		}
	} else if apierrors.IsNotFound(err) {
		log.Info(fmt.Sprintf("CNI uninstall configMap (%s/%s) is not found, skip removing cni configuration file", cmKey.Namespace, cmKey.Name))
		return nil
	} else {
		return fmt.Errorf("failed to get cni uninstall configMap (%s/%s): %w", cmKey.Namespace, cmKey.Name, err)
	}

	// only remove the cniConf file when the uninstall configMap is found and uninstall flag is set to true
	log.Info("Removing cni configuration file...")
	file := filepath.Join(mgr.cniConfDir, mgr.cniConfFile)
	if _, err := os.Stat(file); err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return fmt.Errorf("failed to stat file %s: %w", file, err)
	}

	return retry.OnError(
		// retry for 10 times, 5s each
		wait.Backoff{Duration: 5 * time.Second, Steps: 10},
		func(err error) bool { return true },
		func() error {
			if err := os.Remove(file); err != nil {
				return fmt.Errorf("failed to delete file %s: %w", file, err)
			}
			log.Info(fmt.Sprintf("successfully removed cni configuration file %s", file))
			return nil
		},
	)
}