Skip to content
Snippets Groups Projects
Commit de3b70a8 authored by Mitchell Nielsen's avatar Mitchell Nielsen
Browse files

Truncate secret annotation key

Truncates the key used in the secret hash annotation, ensuring
a maximum length of 63 characters per the Kubernetes spec.

Reference for helper function:
https://stackoverflow.com/a/69549487

Closes #1026

Changelog: fixed
parent 3934aa77
No related branches found
No related tags found
1 merge request!569Truncate secret annotation key
Pipeline #755250104 passed with warnings
......@@ -60,6 +60,7 @@ import (
const (
defaultRequeueDelay = 10 * time.Second
maxKeyLength = 63
)
// GitLabReconciler reconciles a GitLab object.
......@@ -792,7 +793,14 @@ func (r *GitLabReconciler) annotateSecretsChecksum(ctx context.Context, adapter
template.ObjectMeta.Annotations = map[string]string{}
}
template.ObjectMeta.Annotations[fmt.Sprintf("checksum/secret-%s", secretName)] = hash
key := fmt.Sprintf("checksum/secret-%s", secretName)
truncatedKey, err := internal.Truncate(key, maxKeyLength)
if err != nil {
return err
}
template.ObjectMeta.Annotations[truncatedKey] = hash
}
return nil
......
......@@ -172,3 +172,23 @@ func SecretChecksum(secret v1.Secret, keys map[string]struct{}) string {
return fmt.Sprintf("%x", hash.Sum(nil))
}
// Truncate trims a string to a given length.
// Reqires a positive integer.
// If provided length is equal to or lower than the
// provided text length, then the text will be returned
// unmodified.
func Truncate(text string, length int) (string, error) {
if length < 0 {
return "", fmt.Errorf("invalid width size: %d", length)
}
if len(text) <= length {
return text, nil
}
r := []rune(text)
trunc := r[:length]
return string(trunc), nil
}
package internal
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Internal helpers", func() {
Context("Truncating strings", func() {
testString := "abcdefghijklmnopqrstuvwxyz"
testStringLen := len(testString)
When("Passing a negative number", func() {
It("Should return an error", func() {
result, err := Truncate(testString, -1)
Expect(err).NotTo(BeNil())
Expect(result).To(Equal(""))
})
})
When("Passing a number equal to the string length", func() {
It("Should return the input string", func() {
result, err := Truncate(testString, testStringLen)
Expect(err).To(BeNil())
Expect(result).To(Equal(testString))
})
})
When("Passing a number greater than the string length", func() {
It("Should return the input string", func() {
result, err := Truncate(testString, testStringLen)
Expect(err).To(BeNil())
Expect(result).To(Equal(testString))
})
})
When("Passing a number greater than zero and less than the string length", func() {
It("Should return the input string truncated to the given number", func() {
result, err := Truncate(testString, 5)
Expect(err).To(BeNil())
Expect(result).To(Equal("abcde"))
})
})
})
})
package internal
import (
"testing"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestGitlabOperator(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "controllers/internal")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment