Skip to content

Commit 8316451

Browse files
committed
refactoring
Signed-off-by: Markus Blaschke <[email protected]>
1 parent 64a3174 commit 8316451

File tree

6 files changed

+41
-42
lines changed

6 files changed

+41
-42
lines changed

autopilot/lib.k8s.go

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,8 @@ func (r *AzureK8sAutopilot) getK8sNodeList() (nodeList *k8s.NodeList, err error)
6464
return
6565
}
6666

67-
func (r *AzureK8sAutopilot) k8sSetNodeLockAnnotation(node *k8s.Node, annotationName string, dur time.Duration) (err error) {
68-
lockValue := time.Now().Add(dur).Format(time.RFC3339)
69-
70-
patch := []k8s.PatchStringValue{{
71-
Op: "replace",
72-
Path: fmt.Sprintf("/metadata/annotations/%s", k8s.PatchPathEsacpe(annotationName)),
73-
Value: lockValue,
74-
}}
75-
76-
patchBytes, patchErr := json.Marshal(patch)
67+
func (r *AzureK8sAutopilot) k8sNodeApplyPatch(node *k8s.Node, patches []k8s.JsonPatch) (err error) {
68+
patchBytes, patchErr := json.Marshal(patches)
7769
if patchErr != nil {
7870
err = patchErr
7971
return
@@ -88,25 +80,25 @@ func (r *AzureK8sAutopilot) k8sSetNodeLockAnnotation(node *k8s.Node, annotationN
8880
return
8981
}
9082

91-
func (r *AzureK8sAutopilot) k8sRemoveNodeLockAnnotation(node *k8s.Node, annotationName string) (err error) {
92-
patch := []k8s.PatchRemove{{
93-
Op: "remove",
94-
Path: fmt.Sprintf("/metadata/annotations/%s", k8s.PatchPathEsacpe(annotationName)),
83+
func (r *AzureK8sAutopilot) k8sNodeSetLockAnnotation(node *k8s.Node, annotationName string, dur time.Duration) (err error) {
84+
lockValue := time.Now().Add(dur).Format(time.RFC3339)
85+
86+
patches := []k8s.JsonPatch{k8s.JsonPatchString{
87+
Op: "replace",
88+
Path: fmt.Sprintf("/metadata/annotations/%s", k8s.PatchPathEsacpe(annotationName)),
89+
Value: lockValue,
9590
}}
9691

97-
patchBytes, patchErr := json.Marshal(patch)
98-
if patchErr != nil {
99-
err = patchErr
100-
return
101-
}
92+
return r.k8sNodeApplyPatch(node, patches)
93+
}
10294

103-
_, k8sError := r.k8sClient.CoreV1().Nodes().Patch(r.ctx, node.Name, types.JSONPatchType, patchBytes, metav1.PatchOptions{})
104-
if k8sError != nil {
105-
err = k8sError
106-
return
107-
}
95+
func (r *AzureK8sAutopilot) k8sNodeRemoveAnnotation(node *k8s.Node, annotationName string) (err error) {
96+
patches := []k8s.JsonPatch{k8s.JsonPatchString{
97+
Op: "remove",
98+
Path: fmt.Sprintf("/metadata/annotations/%s", k8s.PatchPathEsacpe(annotationName)),
99+
}}
108100

109-
return
101+
return r.k8sNodeApplyPatch(node, patches)
110102
}
111103

112104
func (r *AzureK8sAutopilot) k8sGetNodeLockAnnotation(node *k8s.Node, annotationName string) (dur *time.Duration, exists bool) {

autopilot/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func (r *AzureK8sAutopilot) syncNodeLockCache(contextLogger *log.Entry, nodeList
347347
if lockDuration == nil || lockDuration.Seconds() <= 0 {
348348
// remove annotation
349349
contextLogger.Debugf("removing lock annotation \"%s\" from node %s", annotationName, node.Name)
350-
if err := r.k8sRemoveNodeLockAnnotation(node, annotationName); err != nil {
350+
if err := r.k8sNodeRemoveAnnotation(node, annotationName); err != nil {
351351
contextLogger.Error(err)
352352
}
353353
continue

autopilot/task.repair.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ nodeLoop:
102102
r.prometheus.general.errors.WithLabelValues("azure").Inc()
103103
nodeContextLogger.Errorf("node %s repair failed: %s", node.Name, err.Error())
104104
r.nodeRepairLock.Add(node.Name, true, r.Config.Repair.LockDurationError) //nolint:golint,errcheck
105-
if k8sErr := r.k8sSetNodeLockAnnotation(node, r.Config.Repair.NodeLockAnnotation, r.Config.Repair.LockDurationError); k8sErr != nil {
105+
if k8sErr := r.k8sNodeSetLockAnnotation(node, r.Config.Repair.NodeLockAnnotation, r.Config.Repair.LockDurationError); k8sErr != nil {
106106
nodeContextLogger.Error(k8sErr)
107107
}
108108
continue nodeLoop
109109
} else {
110110
// lock vm for next redeploy, can take up to 15 mins
111111
r.nodeRepairLock.Add(node.Name, true, r.Config.Repair.LockDuration) //nolint:golint,errcheck
112-
if k8sErr := r.k8sSetNodeLockAnnotation(node, r.Config.Repair.NodeLockAnnotation, r.Config.Repair.LockDuration); k8sErr != nil {
112+
if k8sErr := r.k8sNodeSetLockAnnotation(node, r.Config.Repair.NodeLockAnnotation, r.Config.Repair.LockDuration); k8sErr != nil {
113113
nodeContextLogger.Error(k8sErr)
114114
}
115115
nodeContextLogger.Infof("node %s successfully scheduled for repair", node.Name)

autopilot/task.update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ vmssLoop:
6666
continue vmssInstanceLoop
6767
}
6868

69-
// concurrency repair limit
69+
// concurrency update limit
7070
if r.Config.Update.Limit > 0 && r.nodeUpdateLock.ItemCount() >= r.Config.Update.Limit {
7171
vmssInstanceContextLogger.Infof("detected updateable node %s, skipping due to concurrent update limit", node.Name)
7272
continue vmssInstanceLoop
@@ -113,7 +113,7 @@ vmssLoop:
113113

114114
func (r *AzureK8sAutopilot) updateNodeLock(contextLogger *log.Entry, node *k8s.Node, dur time.Duration) {
115115
r.nodeUpdateLock.Add(node.Name, true, dur) //nolint:golint,errcheck
116-
if k8sErr := r.k8sSetNodeLockAnnotation(node, r.Config.Update.NodeLockAnnotation, dur); k8sErr != nil {
116+
if k8sErr := r.k8sNodeSetLockAnnotation(node, r.Config.Update.NodeLockAnnotation, dur); k8sErr != nil {
117117
contextLogger.Error(k8sErr)
118118
}
119119
}

config/opts.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ type (
4747

4848
// upgrade settings
4949
Update struct {
50-
Crontab string `long:"update.crontab" env:"UPDATE_CRONTAB" description:"Crontab of check runs" default:"@every 15m"`
51-
Limit int `long:"update.concurrency" env:"UPDATE_CONCURRENCY" description:"How many VMs should be updated concurrently" default:"1"`
52-
LockDuration time.Duration `long:"update.lock-duration" env:"UPDATE_LOCK_DURATION" description:"Duration how long should be waited for another update" default:"15m"`
53-
LockDurationError time.Duration `long:"update.lock-duration-error" env:"UPDATE_LOCK_DURATION_ERROR" description:"Duration how long should be waited for another update in case an error occurred" default:"5m"`
54-
AzureVmssAction string `long:"update.azure.vmss.action" env:"UPDATE_AZURE_VMSS_ACTION" description:"Defines the action which should be tried to update the node (VMSS)" default:"update+reimage" choice:"update" choice:"update+reimage"`
55-
ProvisioningState []string `long:"update.azure.provisioningstate" env:"UPDATE_AZURE_PROVISIONINGSTATE" description:"Azure VM provisioning states where update should be tried (eg. avoid repair in \"upgrading\" state; \"*\" to accept all states)" default:"succeeded" default:"failed" env-delim:" "`
56-
ProvisioningStateAll bool
57-
NodeLockAnnotation string `long:"update.lock-annotation" env:"UPDATE_LOCK_ANNOTATION" description:"Node annotation for update lock time" default:"autopilot.webdevops.io/update-lock"`
50+
Crontab string `long:"update.crontab" env:"UPDATE_CRONTAB" description:"Crontab of check runs" default:"@every 15m"`
51+
Limit int `long:"update.concurrency" env:"UPDATE_CONCURRENCY" description:"How many VMs should be updated concurrently" default:"1"`
52+
LockDuration time.Duration `long:"update.lock-duration" env:"UPDATE_LOCK_DURATION" description:"Duration how long should be waited for another update" default:"15m"`
53+
LockDurationError time.Duration `long:"update.lock-duration-error" env:"UPDATE_LOCK_DURATION_ERROR" description:"Duration how long should be waited for another update in case an error occurred" default:"5m"`
54+
NodeLockAnnotation string `long:"update.lock-annotation" env:"UPDATE_LOCK_ANNOTATION" description:"Node annotation for update lock time" default:"autopilot.webdevops.io/update-lock"`
55+
NodeOngoingAnnotation string `long:"update.ongoing-annotation" env:"UPDATE_ONGOING_ANNOTATION" description:"Node annotation for ongoing update lock" default:"autopilot.webdevops.io/update-ongoing"`
56+
AzureVmssAction string `long:"update.azure.vmss.action" env:"UPDATE_AZURE_VMSS_ACTION" description:"Defines the action which should be tried to update the node (VMSS)" default:"update+reimage" choice:"update" choice:"update+reimage"`
57+
ProvisioningState []string `long:"update.azure.provisioningstate" env:"UPDATE_AZURE_PROVISIONINGSTATE" description:"Azure VM provisioning states where update should be tried (eg. avoid repair in \"upgrading\" state; \"*\" to accept all states)" default:"succeeded" default:"failed" env-delim:" "`
58+
ProvisioningStateAll bool
5859
}
5960

6061
// drain settings

k8s/patch.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ package k8s
33
import "strings"
44

55
type (
6-
PatchStringValue struct {
6+
JsonPatch interface{}
7+
8+
JsonPatchString struct {
9+
JsonPatch
710
Op string `json:"op"`
811
Path string `json:"path"`
912
Value string `json:"value"`
1013
}
11-
PatchRemove struct {
12-
Op string `json:"op"`
13-
Path string `json:"path"`
14+
15+
JsonPatchObject struct {
16+
JsonPatch
17+
Op string `json:"op"`
18+
Path string `json:"path"`
19+
Value interface{} `json:"value"`
1420
}
1521
)
1622

0 commit comments

Comments
 (0)