fix: hash scheduler-k3s cron-id label to fit Kubernetes' 63-byte cap
The `dokku.com/cron-id` label could exceed Kubernetes' 63-byte cap because the cron ID is `base36(appName === command === schedule)`, which expands roughly 1.5x per byte. The label is now keyed `dokku.com/cron-hash` and holds the `sha1` hex digest of the cron-id, a fixed 40-character value that always fits the cap. The same hex digest is mirrored into the `dokku.com/cron-hash` annotation, and the original base36 cron-id stays in the `dokku.com/cron-id` annotation that `cron:list` reads when surfacing user-facing IDs. Per-task lookups stay server-side via label selectors, so `cron:set --maintenance` and the forbid/replace concurrency checks on `dokku run --cron-id` keep working without any in-memory filtering.
This commit is contained in:
10 files changed
+235
-51
No files matched your search
@@ -15,7 +15,8 @@ import (
|
||||
)
|
||||
|
||||
// TestCronIDLabelValue asserts the hashed cron ID fits inside Kubernetes' 63
|
||||
// byte label cap and is deterministic. Without this the label can exceed the
|
||||
// byte label cap, is a fixed 40-character sha1 hex digest, and is
|
||||
// deterministic. Without this the dokku.com/cron-hash label can exceed the
|
||||
// cap and the Kubernetes API server rejects the manifest.
|
||||
func TestCronIDLabelValue(t *testing.T) {
|
||||
cases := []string{
|
||||
@@ -25,6 +26,9 @@ func TestCronIDLabelValue(t *testing.T) {
|
||||
}
|
||||
for _, in := range cases {
|
||||
out := cronIDLabelValue(in)
|
||||
if len(out) != 40 {
|
||||
t.Errorf("cronIDLabelValue(%q) = %q (len %d); want 40 hex chars", in, out, len(out))
|
||||
}
|
||||
if len(out) > 63 {
|
||||
t.Errorf("cronIDLabelValue(%q) = %q (len %d); must be <= 63", in, out, len(out))
|
||||
}
|
||||
@@ -35,11 +39,123 @@ func TestCronIDLabelValue(t *testing.T) {
|
||||
}
|
||||
|
||||
// TestCronJobTemplateQuotesAllDigitSuffix asserts that the rendered cron-job
|
||||
// manifest produces string-typed annotation values even when the suffix and
|
||||
// cron-id consist entirely of digits. Without `| quote` in the template, YAML
|
||||
// would coerce these to numbers and the manifest would be rejected by the
|
||||
// Kubernetes API server.
|
||||
// manifest produces string-typed annotation and label values even when the
|
||||
// suffix and cron-id consist entirely of digits. Without `| quote` in the
|
||||
// template, YAML would coerce these to numbers and the manifest would be
|
||||
// rejected by the Kubernetes API server.
|
||||
func TestCronJobTemplateQuotesAllDigitSuffix(t *testing.T) {
|
||||
manifest := renderCronJobTemplate(t, map[string]interface{}{
|
||||
"id": "1234567890",
|
||||
"hash": "0123456789abcdef0123456789abcdef01234567",
|
||||
"schedule": "5 5 5 5 5",
|
||||
"suffix": "1234567890",
|
||||
"suspend": false,
|
||||
"concurrency_policy": "Allow",
|
||||
})
|
||||
|
||||
forEachManifestMetadata(t, manifest, func(metadata map[string]interface{}) {
|
||||
annotations, _ := metadata["annotations"].(map[string]interface{})
|
||||
for key, value := range annotations {
|
||||
if _, isString := value.(string); !isString {
|
||||
t.Errorf("annotation %q has non-string value %v (type %T); helm template must apply | quote", key, value, value)
|
||||
}
|
||||
}
|
||||
labels, _ := metadata["labels"].(map[string]interface{})
|
||||
for key, value := range labels {
|
||||
if _, isString := value.(string); !isString {
|
||||
t.Errorf("label %q has non-string value %v (type %T); helm template must apply | quote", key, value, value)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestCronJobTemplateEmitsHashLabel asserts that the rendered manifest
|
||||
// emits the sha1 hash as the dokku.com/cron-hash label and matching
|
||||
// annotation, while keeping the original base36 cron-id in the
|
||||
// dokku.com/cron-id annotation so cron:list can still surface it to users.
|
||||
func TestCronJobTemplateEmitsHashLabel(t *testing.T) {
|
||||
originalID := "app===echo hello-from-cron===5 5 5 5 5"
|
||||
hashedID := cronIDLabelValue(originalID)
|
||||
|
||||
manifest := renderCronJobTemplate(t, map[string]interface{}{
|
||||
"id": originalID,
|
||||
"hash": hashedID,
|
||||
"schedule": "5 5 5 5 5",
|
||||
"suffix": "abcde",
|
||||
"suspend": false,
|
||||
"concurrency_policy": "Allow",
|
||||
})
|
||||
|
||||
sawHashLabel := false
|
||||
sawIDAnnotation := false
|
||||
forEachManifestMetadata(t, manifest, func(metadata map[string]interface{}) {
|
||||
if labels, ok := metadata["labels"].(map[string]interface{}); ok {
|
||||
if value, ok := labels["dokku.com/cron-hash"]; ok {
|
||||
sawHashLabel = true
|
||||
if value != hashedID {
|
||||
t.Errorf("dokku.com/cron-hash label = %q, want %q", value, hashedID)
|
||||
}
|
||||
}
|
||||
}
|
||||
annotations, _ := metadata["annotations"].(map[string]interface{})
|
||||
if value, ok := annotations["dokku.com/cron-hash"]; ok {
|
||||
if value != hashedID {
|
||||
t.Errorf("dokku.com/cron-hash annotation = %q, want %q (must mirror the label)", value, hashedID)
|
||||
}
|
||||
}
|
||||
if value, ok := annotations["dokku.com/cron-id"]; ok {
|
||||
sawIDAnnotation = true
|
||||
if value != originalID {
|
||||
t.Errorf("dokku.com/cron-id annotation = %q, want %q", value, originalID)
|
||||
}
|
||||
}
|
||||
})
|
||||
if !sawHashLabel {
|
||||
t.Errorf("rendered manifest did not include a dokku.com/cron-hash label")
|
||||
}
|
||||
if !sawIDAnnotation {
|
||||
t.Errorf("rendered manifest did not include a dokku.com/cron-id annotation")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCronJobTemplateRendersLongCronID is the direct regression test for
|
||||
// dokku/dokku#8594: a cron-id well over the 63-byte Kubernetes label cap
|
||||
// must render cleanly and every resulting label value must stay under the
|
||||
// cap.
|
||||
func TestCronJobTemplateRendersLongCronID(t *testing.T) {
|
||||
longCronID := strings.Repeat("a-very-long-cron-id-that-would-far-exceed-the-label-cap-", 10)
|
||||
manifest := renderCronJobTemplate(t, map[string]interface{}{
|
||||
"id": longCronID,
|
||||
"hash": cronIDLabelValue(longCronID),
|
||||
"schedule": "5 5 5 5 5",
|
||||
"suffix": "abcde",
|
||||
"suspend": false,
|
||||
"concurrency_policy": "Allow",
|
||||
})
|
||||
|
||||
forEachManifestMetadata(t, manifest, func(metadata map[string]interface{}) {
|
||||
labels, _ := metadata["labels"].(map[string]interface{})
|
||||
for key, value := range labels {
|
||||
str, ok := value.(string)
|
||||
if !ok {
|
||||
t.Errorf("label %q has non-string value %v (type %T)", key, value, value)
|
||||
continue
|
||||
}
|
||||
if len(str) > 63 {
|
||||
t.Errorf("label %q value %q exceeds Kubernetes' 63-byte cap (len %d)", key, str, len(str))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// renderCronJobTemplate renders templates/chart/cron-job.yaml with the
|
||||
// supplied cron-process values and returns the YAML manifest as a string.
|
||||
// The chart is materialised in a tempdir alongside the shared _helpers.tpl
|
||||
// so the helm engine resolves named templates the same way it does in
|
||||
// production.
|
||||
func renderCronJobTemplate(t *testing.T, cronValues map[string]interface{}) string {
|
||||
t.Helper()
|
||||
|
||||
chartDir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(chartDir, "templates"), 0o755); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
@@ -82,16 +198,9 @@ func TestCronJobTemplateQuotesAllDigitSuffix(t *testing.T) {
|
||||
},
|
||||
},
|
||||
"processes": map[string]interface{}{
|
||||
"cron-id-123": map[string]interface{}{
|
||||
"cron-process": map[string]interface{}{
|
||||
"args": []interface{}{"echo", "hello"},
|
||||
"cron": map[string]interface{}{
|
||||
"id": "1234567890",
|
||||
"hash": "abc123def",
|
||||
"schedule": "5 5 5 5 5",
|
||||
"suffix": "1234567890",
|
||||
"suspend": false,
|
||||
"concurrency_policy": "Allow",
|
||||
},
|
||||
"cron": cronValues,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -106,44 +215,49 @@ func TestCronJobTemplateQuotesAllDigitSuffix(t *testing.T) {
|
||||
t.Fatalf("render: %v", err)
|
||||
}
|
||||
|
||||
var manifest string
|
||||
for name, content := range rendered {
|
||||
if filepath.Base(name) == "cron-job.yaml" {
|
||||
manifest = content
|
||||
break
|
||||
return content
|
||||
}
|
||||
}
|
||||
if manifest == "" {
|
||||
t.Fatalf("cron-job.yaml not rendered; got: %v", rendered)
|
||||
}
|
||||
t.Fatalf("cron-job.yaml not rendered; got: %v", rendered)
|
||||
return ""
|
||||
}
|
||||
|
||||
// forEachManifestMetadata invokes fn on every metadata block in a multi-doc
|
||||
// YAML manifest, including the nested jobTemplate and pod template metadata
|
||||
// blocks inside a CronJob spec.
|
||||
func forEachManifestMetadata(t *testing.T, manifest string, fn func(metadata map[string]interface{})) {
|
||||
t.Helper()
|
||||
|
||||
decoder := yaml.NewDecoder(strings.NewReader(manifest))
|
||||
for {
|
||||
var doc map[string]interface{}
|
||||
if err := decoder.Decode(&doc); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
break
|
||||
return
|
||||
}
|
||||
t.Fatalf("yaml decode failed (would also fail in Kubernetes API): %v\nrendered:\n%s", err, manifest)
|
||||
}
|
||||
if doc == nil {
|
||||
continue
|
||||
}
|
||||
metadata, ok := doc["metadata"].(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
walkMetadata(doc, fn)
|
||||
}
|
||||
}
|
||||
|
||||
func walkMetadata(node interface{}, fn func(metadata map[string]interface{})) {
|
||||
switch typed := node.(type) {
|
||||
case map[string]interface{}:
|
||||
if metadata, ok := typed["metadata"].(map[string]interface{}); ok {
|
||||
fn(metadata)
|
||||
}
|
||||
annotations, _ := metadata["annotations"].(map[string]interface{})
|
||||
for key, value := range annotations {
|
||||
if _, isString := value.(string); !isString {
|
||||
t.Errorf("annotation %q has non-string value %v (type %T); helm template must apply | quote", key, value, value)
|
||||
}
|
||||
for _, value := range typed {
|
||||
walkMetadata(value, fn)
|
||||
}
|
||||
labels, _ := metadata["labels"].(map[string]interface{})
|
||||
for key, value := range labels {
|
||||
if _, isString := value.(string); !isString {
|
||||
t.Errorf("label %q has non-string value %v (type %T); helm template must apply | quote", key, value, value)
|
||||
}
|
||||
case []interface{}:
|
||||
for _, value := range typed {
|
||||
walkMetadata(value, fn)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user