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,