Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,6 @@ require (
github.com/stoewer/go-strcase v1.3.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
golang.org/x/exp/typeparams v0.0.0-20250210185358-939b2ce775ac // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20251202230838-ff82c1b0f217 // indirect
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,6 @@ github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3 h1:f+jULpR
github.com/tenntenn/text/transform v0.0.0-20200319021203-7eef512accb3/go.mod h1:ON8b8w4BN/kE1EOhwT0o+d62W65a6aPw1nouo9LMgyY=
github.com/tetafro/godot v1.5.0 h1:aNwfVI4I3+gdxjMgYPus9eHmoBeJIbnajOyqZYStzuw=
github.com/tetafro/godot v1.5.0/go.mod h1:2oVxTBSftRTh4+MVfUaUXR6bn2GDXCaMcOG4Dk3rfio=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
Expand All @@ -1036,8 +1035,6 @@ github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhV
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/timakin/bodyclose v0.0.0-20241017074812-ed6a65f985e3 h1:y4mJRFlM6fUyPhoXuFg/Yu02fg/nIPFMOY8tOqppoFg=
github.com/timakin/bodyclose v0.0.0-20241017074812-ed6a65f985e3/go.mod h1:mkjARE7Yr8qU23YcGMSALbIxTQ9r9QBVahQOBRfU460=
github.com/timonwong/loggercheck v0.10.1 h1:uVZYClxQFpw55eh+PIoqM7uAOHMrhVcDoWDery9R8Lg=
Expand Down
38 changes: 28 additions & 10 deletions pkg/installmanager/installmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
"sigs.k8s.io/yaml"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -1032,16 +1031,13 @@ func patchLabelMachineSetManifest(manifestBytes []byte, poolsByType map[string]*
}
logger.Infof("Matching machineset %s of type %s with pool %s", msetName, msetType, poolName)

manifestBytesJson, err = sjson.SetBytes(manifestBytesJson, `metadata.labels.hive\.openshift\.io/managed`, "true")
if err != nil {
logger.WithError(err).Error("error applying hive managed label patch")
return nil, err
}

// Add a pool-name label to the MachineSet containing the pool.Spec.Name which is equivalent to the value of the machine.openshift.io/cluster-api-machineset label
manifestBytesJson, err = sjson.SetBytes(manifestBytesJson, `metadata.labels.hive\.openshift\.io/machine-pool`, msetType)
// Set hive labels on the MachineSet metadata
manifestBytesJson, err = setJSONLabels(manifestBytesJson, map[string]string{
"hive.openshift.io/managed": "true",
"hive.openshift.io/machine-pool": msetType,
})
if err != nil {
logger.WithError(err).Error("error applying machine pool label patch")
logger.WithError(err).Error("error setting labels on manifest")
return nil, err
}

Expand All @@ -1059,6 +1055,28 @@ func patchLabelMachineSetManifest(manifestBytes []byte, poolsByType map[string]*

}

// setJSONLabels sets the given labels on a JSON-encoded Kubernetes resource's metadata.
func setJSONLabels(jsonData []byte, newLabels map[string]string) ([]byte, error) {
var manifest map[string]interface{}
if err := json.Unmarshal(jsonData, &manifest); err != nil {
return nil, fmt.Errorf("error unmarshalling manifest JSON: %w", err)
}
metadata, _ := manifest["metadata"].(map[string]interface{})
if metadata == nil {
metadata = map[string]interface{}{}
manifest["metadata"] = metadata
}
labels, _ := metadata["labels"].(map[string]interface{})
if labels == nil {
labels = map[string]interface{}{}
metadata["labels"] = labels
}
for k, v := range newLabels {
labels[k] = v
}
return json.Marshal(manifest)
}
Comment on lines +1058 to +1078
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Is there a way to call an existing/already-imported upstream lib to decode into a metav1.Object on which we can SetLabels()? What you've done here should be pretty straightforward and stable, but it feels... dirty.


// patchWorkerMachineSetManifest accepts a yaml manifest as []byte and patches the manifest to include an additional
// security group filter if that manifest is the definition of a worker MachineSet. The security group is obtained from
// an annotation (constants.ExtraWorkerSecurityGroupAnnotation) on the provided MachinePool and is the value of the
Expand Down
74 changes: 74 additions & 0 deletions pkg/installmanager/installmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,80 @@ data:
}
}

func Test_setJSONLabels(t *testing.T) {
cases := []struct {
name string
input string
labels map[string]string
wantErr bool
expectLabels map[string]string
expectName string
}{
{
name: "add labels to object with no metadata",
input: `{"apiVersion":"machine.openshift.io/v1beta1","kind":"MachineSet"}`,
labels: map[string]string{
"hive.openshift.io/managed": "true",
},
expectLabels: map[string]string{
"hive.openshift.io/managed": "true",
},
},
{
name: "add labels preserving existing metadata and labels",
input: `{"apiVersion":"machine.openshift.io/v1beta1","kind":"MachineSet","metadata":{"name":"test-ms","labels":{"existing":"label"}}}`,
labels: map[string]string{
"hive.openshift.io/managed": "true",
"hive.openshift.io/machine-pool": "worker",
},
expectName: "test-ms",
expectLabels: map[string]string{
"existing": "label",
"hive.openshift.io/managed": "true",
"hive.openshift.io/machine-pool": "worker",
},
},
{
name: "add labels to object with metadata but no labels",
input: `{"apiVersion":"machine.openshift.io/v1beta1","kind":"MachineSet","metadata":{"name":"test-ms"}}`,
labels: map[string]string{
"foo": "bar",
},
expectName: "test-ms",
expectLabels: map[string]string{
"foo": "bar",
},
},
{
name: "invalid json",
input: `not json`,
labels: map[string]string{"a": "b"},
wantErr: true,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
output, err := setJSONLabels([]byte(tc.input), tc.labels)
if tc.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)

var ms machineapi.MachineSet
require.NoError(t, json.Unmarshal(output, &ms), "output should unmarshal into a MachineSet")

if tc.expectName != "" {
assert.Equal(t, tc.expectName, ms.Name)
}
for k, v := range tc.expectLabels {
assert.Equal(t, v, ms.Labels[k], "label %s", k)
}
})
}
}

func Test_scrubMetadataJSON(t *testing.T) {
cases := []struct {
initial string
Expand Down
21 changes: 0 additions & 21 deletions vendor/github.com/tidwall/sjson/LICENSE

This file was deleted.

Loading