Skip to content

Commit

Permalink
can create the directory for the customized cni conf and remove the c…
Browse files Browse the repository at this point in the history
…ni conf file in cleanup command

Signed-off-by: Su Fei <[email protected]>
  • Loading branch information
sofat1989 authored and julianwiedmann committed Sep 12, 2023
1 parent 6180087 commit 2e894f3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
36 changes: 26 additions & 10 deletions cilium/cmd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ working condition after uninstalling the Cilium agent.`,
}

var (
cleanAll bool
cleanBPF bool
force bool
cleanAll bool
cleanBPF bool
force bool
customCNIConf string
)

const (
allFlagName = "all-state"
bpfFlagName = "bpf-state"
forceFlagName = "force"

cleanCiliumEnvVar = "CLEAN_CILIUM_STATE"
cleanBpfEnvVar = "CLEAN_CILIUM_BPF_STATE"
cleanCiliumEnvVar = "CLEAN_CILIUM_STATE"
cleanBpfEnvVar = "CLEAN_CILIUM_BPF_STATE"
writeCNIConfWhenReadyEnvVar = "WRITE_CNI_CONF_WHEN_READY"

tcFilterParentIngress = 0xfffffff2
tcFilterParentEgress = 0xfffffff3
Expand Down Expand Up @@ -83,6 +85,7 @@ func init() {

bindEnv(cleanCiliumEnvVar, cleanCiliumEnvVar)
bindEnv(cleanBpfEnvVar, cleanBpfEnvVar)
bindEnv(writeCNIConfWhenReadyEnvVar, writeCNIConfWhenReadyEnvVar)

if err := vp.BindPFlags(cleanupCmd.Flags()); err != nil {
Fatalf("viper failed to bind to flags: %v\n", err)
Expand Down Expand Up @@ -232,8 +235,13 @@ func (c ciliumCleanup) whatWillBeRemoved() []string {
defaults.LibraryPath))
toBeRemoved = append(toBeRemoved, fmt.Sprintf("endpoint state in %s",
defaults.RuntimePath))
toBeRemoved = append(toBeRemoved, fmt.Sprintf("CNI configuration at %s, %s, %s, %s, %s",
cniConfigV1, cniConfigV2, cniConfigV3, cniConfigV4, cniConfigV5))
if customCNIConf != "" {
toBeRemoved = append(toBeRemoved, fmt.Sprintf("CNI configuration at %s, %s, %s, %s, %s, %s",
cniConfigV1, cniConfigV2, cniConfigV3, cniConfigV4, cniConfigV5, customCNIConf))
} else {
toBeRemoved = append(toBeRemoved, fmt.Sprintf("CNI configuration at %s, %s, %s, %s, %s",
cniConfigV1, cniConfigV2, cniConfigV3, cniConfigV4, cniConfigV5))
}
return toBeRemoved
}

Expand Down Expand Up @@ -276,6 +284,8 @@ func runCleanup() {
os.Exit(1)
}

customCNIConf = vp.GetString(writeCNIConfWhenReadyEnvVar)

cleanAll = vp.GetBool(allFlagName) || vp.GetBool(cleanCiliumEnvVar)
cleanBPF = vp.GetBool(bpfFlagName) || vp.GetBool(cleanBpfEnvVar)

Expand Down Expand Up @@ -346,10 +356,16 @@ func removeCNI() error {
os.Remove(cniConfigV4)

err := os.Remove(cniConfigV5)
if os.IsNotExist(err) {
return nil
if err != nil && !os.IsNotExist(err) {
return err
}
return err
if customCNIConf != "" {
err = os.Remove(customCNIConf)
if err != nil && !os.IsNotExist(err) {
return err
}
}
return nil
}

// revertCNIBackup removes the ".cilium_bak" suffix from all files in cniPath,
Expand Down
13 changes: 13 additions & 0 deletions daemon/cmd/cni/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ func (c *cniConfigManager) setupCNIConfFile() error {
}
}

err = ensureDirExists(c.cniConfDir)
if err != nil {
return fmt.Errorf("failed to create the dir %s of the CNI configuration file: %w", c.cniConfDir, err)
}

dest := path.Join(c.cniConfDir, c.cniConfFile)

// Check to see if existing file is the same; if so, do nothing
Expand Down Expand Up @@ -465,3 +470,11 @@ func (c *cniConfigManager) findCNINetwork(wantNetwork string) ([]byte, error) {
}
return nil, fmt.Errorf("no matching CNI configurations found (will retry)")
}

func ensureDirExists(dir string) error {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil && os.IsExist(err) {
return nil
}
return err
}
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ spec:
name: cilium-config
key: clean-cilium-bpf-state
optional: true
- name: WRITE_CNI_CONF_WHEN_READY
valueFrom:
configMapKeyRef:
name: cilium-config
key: write-cni-conf-when-ready
optional: true
{{- if .Values.k8sServiceHost }}
- name: KUBERNETES_SERVICE_HOST
value: {{ .Values.k8sServiceHost | quote }}
Expand Down

0 comments on commit 2e894f3

Please sign in to comment.