Skip to content

Commit

Permalink
feat: add a guard to prevent to start the migration when the targeted
Browse files Browse the repository at this point in the history
label is the only one.
  • Loading branch information
Tchoupinax committed Apr 16, 2024
1 parent 02c9cf1 commit 3b2c533
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"context"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

func isTheEditedLabelTheOnlyOne(
namespace string,
clientset *kubernetes.Clientset,
deploymentName string,
changingLabelKey string,
) bool {
deployment, _ := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, v1.GetOptions{})
labels := deployment.Spec.Template.ObjectMeta.Labels
return len(labels) == 1 && labels[changingLabelKey] != ""
}
12 changes: 12 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"os"

"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -48,6 +49,17 @@ func main() {
os.Exit(1)
}

onlyOneLabel := isTheEditedLabelTheOnlyOne(
namespace,
clientset,
deploymentName,
labelToChangeKey,
)
if onlyOneLabel {
logError(fmt.Sprintf("The label \"%s\" can not be edited because it's the only one in the matching set.", labelToChangeKey))
os.Exit(1)
}

displaySummary(
namespace,
deploymentName,
Expand Down

0 comments on commit 3b2c533

Please sign in to comment.