From 3b2c53313d9455d662e6a2a3c0ace0461726ddad Mon Sep 17 00:00:00 2001 From: Tchoupinax Date: Tue, 16 Apr 2024 22:22:41 +0200 Subject: [PATCH] feat: add a guard to prevent to start the migration when the targeted label is the only one. --- checks.go | 19 +++++++++++++++++++ main.go | 12 ++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 checks.go diff --git a/checks.go b/checks.go new file mode 100644 index 0000000..8a67b49 --- /dev/null +++ b/checks.go @@ -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] != "" +} diff --git a/main.go b/main.go index 1a647e2..c442fa6 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "os" "k8s.io/client-go/kubernetes" @@ -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,