Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
fix: Fix adding a label to namespace if the labels field is absent (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Czekajlo authored Nov 18, 2021
1 parent 503eae4 commit a361982
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ archives:
- darwin
- linux
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
wrap_in_directory: true
wrap_in_directory: "true"
files:
- README.md
- LICENSE
Expand Down
25 changes: 12 additions & 13 deletions pkg/k8s/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,23 @@ func (k *Kubernetes) IsNamespaceManageable() bool {
// AddNamespaceLabels adds an extra label to a given namespace that indicates that the namespace
// is managed by rasactl.
func (k *Kubernetes) AddNamespaceLabel() error {
type patch struct {
Op string `json:"op"`
Path string `json:"path"`
Value string `json:"value"`

ns, err := k.clientset.CoreV1().Namespaces().Get(context.TODO(), k.Namespace, metav1.GetOptions{})
if err != nil {
return err
}

payload := []patch{{
Op: "add",
Path: "/metadata/labels/rasactl",
Value: "true",
}}
var mergedLabels = make(map[string]string)
for key, value := range ns.Labels {
mergedLabels[key] = value
}
mergedLabels["rasactl"] = "true"
ns.Labels = mergedLabels

payloadBytes, _ := json.Marshal(payload)
k.Log.V(1).Info("Adding label", "namespace", k.Namespace, "payload", string(payloadBytes))
if _, err := k.clientset.CoreV1().Namespaces().Patch(context.TODO(), k.Namespace,
ktypes.JSONPatchType, payloadBytes, metav1.PatchOptions{}); err != nil {
if _, err := k.clientset.CoreV1().Namespaces().Update(context.TODO(), ns, metav1.UpdateOptions{}); err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit a361982

Please sign in to comment.