Skip to content

Commit

Permalink
getConfigmap creates the kotsadm-redact configmap if it does not alre…
Browse files Browse the repository at this point in the history
…ady exist
  • Loading branch information
laverya committed May 22, 2020
1 parent 0a2d15d commit 5210ed8
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions kotsadm/pkg/redact/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,35 +142,9 @@ func SetRedactSpec(spec string) (string, error) {
return "failed to create kubernetes clientset", errors.Wrap(err, "failed to create kubernetes clientset")
}

configMap, err := clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Get("kotsadm-redact", metav1.GetOptions{})
configMap, errMsg, err := getConfigmap()
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
// not a not found error, so a real error
return "failed to get kotsadm-redact configMap", errors.Wrap(err, "failed to get kotsadm-redact configMap")
} else {
// not found, so create it fresh
newMap := v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "kotsadm-redact",
Namespace: os.Getenv("POD_NAMESPACE"),
Labels: map[string]string{
"kots.io/kotsadm": "true",
},
},
Data: map[string]string{
"kotsadm-redact": spec,
},
}
_, err = clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Create(&newMap)
if err != nil {
return "failed to create kotsadm-redact configMap", errors.Wrap(err, "failed to create kotsadm-redact configMap")
}
return "", nil
}
return errMsg, err
}

newMap, err := splitRedactors(spec, configMap.Data)
Expand Down Expand Up @@ -329,8 +303,27 @@ func getConfigmap() (*v1.ConfigMap, string, error) {
// not a not found error, so a real error
return nil, "failed to get kotsadm-redact configMap", errors.Wrap(err, "failed to get kotsadm-redact configMap")
} else {
// not found, so return empty string
return nil, "", nil
// not found, so create one and return it
newMap := v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "kotsadm-redact",
Namespace: os.Getenv("POD_NAMESPACE"),
Labels: map[string]string{
"kots.io/kotsadm": "true",
},
},
Data: map[string]string{},
}
createdMap, err := clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Create(&newMap)
if err != nil {
return nil, "failed to create kotsadm-redact configMap", errors.Wrap(err, "failed to create kotsadm-redact configMap")
}

return createdMap, "", nil
}
}
return configMap, "", nil
Expand Down

0 comments on commit 5210ed8

Please sign in to comment.