Skip to content

Commit

Permalink
Add IdentfyingLabels to CreateOrUpdateWithOptions
Browse files Browse the repository at this point in the history
If the GenerateName field is set in the target resource, the
existing resource is searched for by the target's labels. However
this assumes the labels remain static for the lifetime of the
resource. If the labels are updated then the existing resource is
not found and a new one is created. To avoid this, allow the user
to specify which labels uniquely identify the resource by adding
an IdentfyingLabels field to CreateOrUpdateOptions.

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Oct 7, 2024
1 parent 141eb35 commit 4ce43e9
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions pkg/util/create_or_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,23 @@ var _ = Describe("CreateOrUpdate function", func() {
t := newCreateOrUpdateTestDiver()

createOrUpdate := func(expResult util.OperationResult) error {
result, err := util.CreateOrUpdate[*unstructured.Unstructured](context.TODO(), resource.ForDynamic(t.client),
resource.MustToUnstructured(t.pod), t.mutateFn)
options := util.CreateOrUpdateOptions[*unstructured.Unstructured]{
Client: resource.ForDynamic(t.client),
MutateOnUpdate: t.mutateFn,
}

if t.pod.GenerateName != "" {
options.IdentifyingLabels = map[string]string{}
for k, v := range t.pod.Labels {
options.IdentifyingLabels[k] = v
}

t.pod.Labels["new-label"] = "new-value"
}

options.Obj = resource.MustToUnstructured(t.pod)

result, _, err := util.CreateOrUpdateWithOptions[*unstructured.Unstructured](context.TODO(), options)
if err != nil && expResult != util.OperationResultNone {
return err
}
Expand All @@ -167,12 +182,6 @@ var _ = Describe("CreateOrUpdate function", func() {
})

Context("and a mutation function specified", func() {
BeforeEach(func() {
t.pod.Name = ""
t.pod.GenerateName = "name-prefix-"
t.pod.Labels = map[string]string{"label1": "value1", "label2": "value2"}
})

It("should invoke the function on create", func() {
result, created, err := util.CreateOrUpdateWithOptions[*unstructured.Unstructured](context.TODO(),
util.CreateOrUpdateOptions[*unstructured.Unstructured]{
Expand Down

0 comments on commit 4ce43e9

Please sign in to comment.