Skip to content

Commit

Permalink
feat: fix the matchselector issue and some little typos (#9)
Browse files Browse the repository at this point in the history
* feat: fix the matchselector issue and some little typos

* fix: lint

* Update README.md

Co-authored-by: ImOverlord <[email protected]>

---------

Co-authored-by: ImOverlord <[email protected]>
  • Loading branch information
Tchoupinax and ImOverlord authored Apr 11, 2024
1 parent 5787ed5 commit 384adf7
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 29 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ kubernetes-labels-migrator \

![Screenshot of a terminal with all the execution](.github/docs/demo.png "CLI demo")

## Tutorial

Let's say you are managing your application with the GitOps philosophy (ArgoCD is really great for this job). You want to add or edit a label. This change has already been made in your code, but ArgoCD refused to sync due to the "immutable field" error. Here's what you should do:

- ⚠️ First of all, ensure that nothing intervenes during the operation! kubernetes-labels-migrator should be free until the end. If you have tools like ArgoCD that synchronize your resources, please ensure this feature is disabled.
- Commit the changes to the resource and observe the difference.
- Launch the script and watch the magic happen.
- According to your changes, the difference should have been resolved.

## Motivation

Deployment's labels are immutable. That means, if you have to edit, rename or add labels to a deployed application, you have to delete the deployment. On a production application, it is very annoying.
Expand Down
5 changes: 4 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
default: up

up:
air go run *.go
air go run *.go

lint:
golangci-lint run
1 change: 1 addition & 0 deletions logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func logBlocking(msg string) {
magenta := color.New(color.Bold, color.FgHiMagenta).SprintFunc()
fmt.Println(magenta(fmt.Sprintf("⌛️ %s", msg)))
}

func logBlockingDot() {
magenta := color.New(color.Bold, color.FgHiMagenta).SprintFunc()
fmt.Print(magenta("."))
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func main() {
var labelToChangeValue = ""
var goalOfOperationIsToRemoveLabel = false

flag.StringVar(&deploymentName, "deployment", "" , "Name of the deployment to edit label")
flag.StringVar(&namespace, "namespace", "" , "Namespace of the deployment to edit label")
flag.StringVar(&deploymentName, "deployment", "", "Name of the deployment to edit label")
flag.StringVar(&namespace, "namespace", "", "Namespace of the deployment to edit label")
flag.BoolVar(&goalOfOperationIsToRemoveLabel, "remove-label", false, "If true, the label will be removed instead of be added/edited")
flag.StringVar(&labelToChangeKey, "label", "app.kubernetes.io/name", "Name of the label")
flag.StringVar(&labelToChangeValue, "value", "", "Value of the label")
Expand Down Expand Up @@ -62,7 +62,7 @@ func main() {
os.Exit(0)
}

Step1(
MigrationWorkflow(
namespace,
clientset,
deploymentName,
Expand Down
26 changes: 15 additions & 11 deletions steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/client-go/kubernetes"
)

func Step1(
func MigrationWorkflow(
namespace string,
clientset *kubernetes.Clientset,
deploymentName string,
Expand All @@ -33,7 +33,7 @@ func Step1(
// It's required because an error is thrown, we can not create a deployment with this property provided
temporalDeployment.ResourceVersion = ""

_, err = clientset.AppsV1().Deployments(namespace).Create(context.TODO(), &temporalDeployment, metav1.CreateOptions{});
_, err = clientset.AppsV1().Deployments(namespace).Create(context.TODO(), &temporalDeployment, metav1.CreateOptions{})
if err != nil {
if strings.Contains(err.Error(), "already exists, the server was not able to generate a unique name for the object") {
logWarning("1. Temporary deployment already created. Continue...")
Expand All @@ -45,7 +45,7 @@ func Step1(
logInfo("2. Updating the service...")
var temporalService = *currentService
delete(temporalService.Spec.Selector, changingLabelKey)
_, err = clientset.CoreV1().Services(namespace).Update(context.TODO(), &temporalService, metav1.UpdateOptions{});
_, err = clientset.CoreV1().Services(namespace).Update(context.TODO(), &temporalService, metav1.UpdateOptions{})
check(err)
logSuccess("2. Service updated")

Expand All @@ -54,9 +54,9 @@ func Step1(
for !areAllPodReady {
logBlockingDot()
time.Sleep(1 * time.Second)
areAllPodReady =
waitUntilAllPodAreReady(clientset, namespace, "api") &&
waitUntilAllPodAreReady(clientset, namespace, fmt.Sprintf("%s-%s", currentDeployment.Name, "changing-label-tmp"))
areAllPodReady =
waitUntilAllPodAreReady(clientset, namespace, currentDeployment.Name) &&
waitUntilAllPodAreReady(clientset, namespace, fmt.Sprintf("%s-%s", currentDeployment.Name, "changing-label-tmp"))
}
fmt.Println("")

Expand All @@ -78,12 +78,17 @@ func Step1(
if removeLabel {
delete(futureOfficialDeployment.ObjectMeta.Labels, changingLabelKey)
delete(futureOfficialDeployment.Spec.Template.ObjectMeta.Labels, changingLabelKey)
} else {
delete(futureOfficialDeployment.Spec.Selector.MatchLabels, changingLabelKey)
} else {
// Label of the deployment
futureOfficialDeployment.ObjectMeta.Labels[changingLabelKey] = changingLabelValue
// Label of the pod created by the deployment
futureOfficialDeployment.Spec.Template.ObjectMeta.Labels[changingLabelKey] = changingLabelValue
// Then we must include the label in the matchSelector for the deployment to find pods
futureOfficialDeployment.Spec.Selector.MatchLabels[changingLabelKey] = changingLabelValue
}

_, err = clientset.AppsV1().Deployments(namespace).Create(context.TODO(), &futureOfficialDeployment, metav1.CreateOptions{});
_, err = clientset.AppsV1().Deployments(namespace).Create(context.TODO(), &futureOfficialDeployment, metav1.CreateOptions{})
if err != nil {
if strings.Contains(err.Error(), "already exists, the server was not able to generate a unique name for the object") {
fmt.Println("⚠️ Temporary deployment already created. Continue...")
Expand All @@ -97,7 +102,7 @@ func Step1(
for !areAllPodReady {
logBlockingDot()
time.Sleep(1 * time.Second)
areAllPodReady = waitUntilAllPodAreReady(clientset, namespace, "api")
areAllPodReady = waitUntilAllPodAreReady(clientset, namespace, currentDeployment.Name)
}
fmt.Println("")

Expand All @@ -109,7 +114,7 @@ func Step1(
logSuccess("7. Temporary deployment deleted")
}

func AddLabelToServiceSelector (
func AddLabelToServiceSelector(
namespace string,
clientset *kubernetes.Clientset,
applicationName string,
Expand All @@ -123,7 +128,6 @@ func AddLabelToServiceSelector (
currentService, err := clientset.CoreV1().Services(namespace).Get(context.TODO(), applicationName, metav1.GetOptions{})
check(err)


var futureService = *currentService
if removeLabel {
// Update the value of the label
Expand Down
4 changes: 2 additions & 2 deletions summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ func displaySummary(
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Parameter", "Value"})
t.AppendRows([]table.Row{{"Deployement name", namespace}})
t.AppendRows([]table.Row{{"Namespace", deploymentName}})
t.AppendRows([]table.Row{{"Deployment name", deploymentName}})
t.AppendRows([]table.Row{{"Namespace", namespace}})
if goalOfOperationIsToRemoveLabel {
t.AppendRows([]table.Row{{"Label", labelToChangeKey}})
} else {
Expand Down
10 changes: 0 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ func stringInSlice(a string, list []string) bool {
return false
}

func concatLabels(labels map[string]string) string {
var concatenatedLabels []string

for key, value := range labels {
concatenatedLabels = append(concatenatedLabels, fmt.Sprintf("%s=%s", key, value))
}

return strings.Join(concatenatedLabels, ",")
}

// https://gist.github.com/r0l1/3dcbb0c8f6cfe9c66ab8008f55f8f28b
func askForConfirmation(s string) bool {
reader := bufio.NewReader(os.Stdin)
Expand Down
4 changes: 2 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/fatih/color"
)

const Version string = "0.1.1"
const BuildDate string = "2024-03-24"
const Version string = "0.1.2"
const BuildDate string = "2024-04-11"

func cliCommandDisplayHelp(args []string) {
displayVersion := stringInSlice("-v", args[1:]) || stringInSlice("--version", args[1:])
Expand Down

0 comments on commit 384adf7

Please sign in to comment.