-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (132 loc) · 3.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"fmt"
"os"
utils "github.com/Tchoupinax/k8s-labels-migrator/utils"
"github.com/mbndr/figlet4go"
istio "istio.io/client-go/pkg/clientset/versioned"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
// Check if the helper is asked by flag
cliCommandDisplayHelp(os.Args)
config, err := clientcmd.BuildConfigFromFlags("", os.Getenv("KUBECONFIG"))
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
istioClient, err := istio.NewForConfig(config)
if err != nil {
panic(err.Error())
}
crdClient, err := dynamic.NewForConfig(config)
if err != nil {
panic(err.Error())
}
var deploymentName = ""
var namespace = ""
var labelToChangeKey = ""
var labelToChangeValue = ""
var goalOfOperationIsToRemoveLabel = false
var matcherLabels = []string{
"app.kubernetes.io/instance",
"app.kubernetes.io/name",
}
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")
flag.Parse()
if deploymentName == "" {
utils.LogError("Deployment name is mandatory")
os.Exit(1)
}
if namespace == "" {
utils.LogError("Namespace is mandatory")
os.Exit(1)
}
if labelToChangeValue == "" && !goalOfOperationIsToRemoveLabel {
utils.LogError("label value is mandatory")
os.Exit(1)
}
labelKeyErr := utils.CheckLabelKey(labelToChangeKey)
if labelKeyErr != nil {
utils.LogError(labelKeyErr.Error())
os.Exit(1)
}
labelValueErr := utils.CheckLabelValue(labelToChangeValue)
if labelValueErr != nil {
utils.LogError(labelValueErr.Error())
os.Exit(1)
}
fmt.Print("\033[H\033[2J")
ascii := figlet4go.NewAsciiRender()
renderStr, _ := ascii.Render("k8s labels migrator")
fmt.Print(renderStr)
utils.LogInfo("Analyzing your cluster...")
resourcesAnalyze(clientset, istioClient, crdClient, namespace, deploymentName, labelToChangeKey)
utils.LogSuccess("Cluster ready")
displaySummary(
namespace,
deploymentName,
labelToChangeKey,
labelToChangeValue,
goalOfOperationIsToRemoveLabel,
)
c := utils.AskForConfirmation("Do you validate these parameters?")
if !c {
utils.LogInfo("Operation aborted by the user")
os.Exit(0)
}
c2 := utils.AskForConfirmation("I confirm that I have no gitops tool overriding my config (e.g. ArgoCD auto-sync)")
if !c2 {
utils.LogInfo("Operation aborted by the user")
os.Exit(0)
}
fmt.Println("")
fmt.Println("")
fmt.Println("")
utils.LogWarning("PLEASE DO NOT INTERRUPT THE PROCESS UNTIL THE END!")
fmt.Println("")
fmt.Println("")
fmt.Println("")
MigrationWorkflow(
namespace,
clientset,
istioClient,
crdClient,
deploymentName,
labelToChangeKey,
labelToChangeValue,
goalOfOperationIsToRemoveLabel,
)
if utils.ArrayContains(matcherLabels, labelToChangeKey) {
AddLabelToServiceSelector(
namespace,
clientset,
deploymentName,
labelToChangeKey,
labelToChangeValue,
goalOfOperationIsToRemoveLabel,
)
AddLabelToIstioDestinatonRulesSelector(
namespace,
clientset,
istioClient,
deploymentName,
labelToChangeKey,
labelToChangeValue,
goalOfOperationIsToRemoveLabel,
)
}
fmt.Println("")
utils.LogSuccess("Migration terminated with success!")
fmt.Println("")
}