Skip to content

Commit

Permalink
allow custom jsonpatches in configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Feb 17, 2024
1 parent 4bbc771 commit bf252e8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
8 changes: 7 additions & 1 deletion example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions k8s/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bf252e8

Please sign in to comment.