Skip to content

Commit

Permalink
Update metadata:annotations: Too long error string detection (#6480)
Browse files Browse the repository at this point in the history
* Update `MetadataAnnotationsTooLongRe`

Update the regexp to match both pre and post Kubernetes 1.32 error
messages.

Signed-off-by: Andrew Farries <[email protected]>

* Update `TestUpsertAnnotationTooLong`

Add both error messages, pre and post Kubernetes v1.32.

Signed-off-by: Andrew Farries <[email protected]>

---------

Signed-off-by: Andrew Farries <[email protected]>
  • Loading branch information
andrew-farries authored Jan 3, 2025
1 parent 875606f commit ab51f91
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ func maybeImmutableFieldStderr(stderr string) bool {
strings.Contains(stderr, ForbiddenFieldsPrefix)
}

var MetadataAnnotationsTooLongRe = regexp.MustCompile(`metadata.annotations: Too long: must have at most \d+ bytes.*`)
var MetadataAnnotationsTooLongRe = regexp.MustCompile(`metadata.annotations: Too long: .* \d+ bytes.*`)

// kubectl apply sets an annotation containing the object's previous configuration.
// However, annotations have a max size of 256k. Large objects such as configmaps can exceed 256k, which makes
Expand Down
29 changes: 22 additions & 7 deletions internal/k8s/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,30 @@ func TestDeleteMissingKind(t *testing.T) {
}

func TestUpsertAnnotationTooLong(t *testing.T) {
f := newClientTestFixture(t)
postgres := MustParseYAMLFromString(t, testyaml.PostgresYAML)

f.resourceClient.updateErr = fmt.Errorf(`The ConfigMap "postgres-config" is invalid: metadata.annotations: Too long: must have at most 262144 bytes`)
_, err := f.k8sUpsert(f.ctx, postgres)
assert.Nil(t, err)
assert.Equal(t, 0, len(f.resourceClient.creates))
assert.Equal(t, 1, len(f.resourceClient.createOrReplaces))
assert.Equal(t, 4, len(f.resourceClient.updates))
// These error strings are from the Kubernetes API server which can have
// different error messages depending on the version.
//
// The error string was changed in K8S v1.32:
// See: https://github.com/kubernetes/kubernetes/pull/128553
errorMsgs := []string{
`The ConfigMap "postgres-config" is invalid: metadata.annotations: Too long: must have at most 262144 bytes`,
`The ConfigMap "postgres-config" is invalid: metadata.annotations: Too long: may not be more than 262144 bytes`,
}

for _, errorMsg := range errorMsgs {
t.Run(errorMsg, func(t *testing.T) {
f := newClientTestFixture(t)

f.resourceClient.updateErr = errors.New(errorMsg)
_, err := f.k8sUpsert(f.ctx, postgres)
assert.Nil(t, err)
assert.Equal(t, 0, len(f.resourceClient.creates))
assert.Equal(t, 1, len(f.resourceClient.createOrReplaces))
assert.Equal(t, 4, len(f.resourceClient.updates))
})
}
}

func TestUpsert413(t *testing.T) {
Expand Down

0 comments on commit ab51f91

Please sign in to comment.