From bf252e8144f49fb7323a7b9b6a79bca89570dbc4 Mon Sep 17 00:00:00 2001 From: Markus Blaschke Date: Sat, 17 Feb 2024 20:09:02 +0100 Subject: [PATCH] allow custom jsonpatches in configuration Signed-off-by: Markus Blaschke --- config/config.go | 10 ++++++++++ example.yaml | 8 +++++++- k8s/patch.go | 8 ++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 02725fe..b41bd96 100644 --- a/config/config.go +++ b/config/config.go @@ -34,6 +34,7 @@ type ( PoolConfigNode struct { Roles PoolConfigNodeValueMap `yaml:"roles"` + JsonPatches []k8s.JsonPatchObject `yaml:"jsonPatches"` ConfigSource *PoolConfigNodeConfigSource `yaml:"configSource"` Labels PoolConfigNodeValueMap `yaml:"labels"` Annotations PoolConfigNodeValueMap `yaml:"annotations"` @@ -145,6 +146,7 @@ func (p *PoolConfig) IsMatchingNode(logger *zap.SugaredLogger, node *corev1.Node func (p *PoolConfig) CreateJsonPatchSet(node *corev1.Node) (patchSet *k8s.JsonPatchSet) { patchSet = k8s.NewJsonPatchSet() + // node roles for roleName, roleValue := range p.Node.Roles.Entries() { label := fmt.Sprintf("node-role.kubernetes.io/%s", roleName) if roleValue != nil { @@ -162,6 +164,7 @@ func (p *PoolConfig) CreateJsonPatchSet(node *corev1.Node) (patchSet *k8s.JsonPa } } + // node config source if p.Node.ConfigSource != nil { patchSet.Add(k8s.JsonPatchObject{ Op: "replace", @@ -170,6 +173,7 @@ func (p *PoolConfig) CreateJsonPatchSet(node *corev1.Node) (patchSet *k8s.JsonPa }) } + // node labels for labelName, labelValue := range p.Node.Labels.Entries() { if labelValue != nil { value := *labelValue @@ -186,6 +190,7 @@ func (p *PoolConfig) CreateJsonPatchSet(node *corev1.Node) (patchSet *k8s.JsonPa } } + // node annotations for annotationName, annotationValue := range p.Node.Annotations.Entries() { if annotationValue != nil { value := *annotationValue @@ -202,5 +207,10 @@ func (p *PoolConfig) CreateJsonPatchSet(node *corev1.Node) (patchSet *k8s.JsonPa } } + // custom patches + for _, patch := range p.Node.JsonPatches { + patchSet.Add(patch) + } + return } diff --git a/example.yaml b/example.yaml index f80fba4..e8f44d9 100644 --- a/example.yaml +++ b/example.yaml @@ -8,6 +8,12 @@ pools: # sets the kubernetes node role roles: [linux] + jsonPatches: + # see https://en.wikipedia.org/wiki/JSON_Patch + - op: replace + path: /metadata/labels/foobar + value: barfoo + - pool: windows continue: true selector: @@ -46,7 +52,7 @@ pools: selector: - path: "{.spec.providerID}" # regexp match - regexp: "^.+virtualMachineScaleSets\\/aks-agents-35471996-vmss\\/.+$" + regexp: "^.+virtualMachineScaleSets\\/aks-agents-.+\\/.+$" node: # sets the kubernetes node role roles: diff --git a/k8s/patch.go b/k8s/patch.go index 786dfc9..a2dd6bb 100644 --- a/k8s/patch.go +++ b/k8s/patch.go @@ -16,10 +16,10 @@ type ( } JsonPatchObject struct { - JsonPatch - Op string `json:"op"` - Path string `json:"path"` - Value interface{} `json:"value"` + JsonPatch `json:"-"` + Op string `json:"op"` + Path string `json:"path"` + Value interface{} `json:"value,omitempty"` } JsonPatchSet struct {