Skip to content

Commit bd8e678

Browse files
committed
Add :latest tag to renderstatus when no tag provided
Signed-off-by: Aravindhan Ayyanathan <aravindhan.a@est.tech>
1 parent 359f50f commit bd8e678

4 files changed

Lines changed: 54 additions & 5 deletions

File tree

e2e/testdata/fn-render/missing-fn-image/.expected/diff.patch

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/Kptfile b/Kptfile
2-
index 11012de..9fadb6e 100644
2+
index 11012de..a0f4634 100644
33
--- a/Kptfile
44
+++ b/Kptfile
55
@@ -7,6 +7,26 @@ pipeline:
@@ -22,11 +22,11 @@ index 11012de..9fadb6e 100644
2222
+ mutationSteps:
2323
+ - image: ghcr.io/kptdev/krm-functions-catalog/set-namespace:v0.2.0
2424
+ exitCode: 0
25-
+ - image: ghcr.io/kptdev/krm-functions-catalog/dne
25+
+ - image: ghcr.io/kptdev/krm-functions-catalog/dne:latest
2626
+ stderr: |-
2727
+ docker: Error response from daemon: error from registry: denied
2828
+ denied
2929
+
3030
+ Run 'docker run --help' for more information
3131
+ exitCode: 125
32-
+ errorSummary: 'ghcr.io/kptdev/krm-functions-catalog/dne: exit code 125'
32+
+ errorSummary: 'ghcr.io/kptdev/krm-functions-catalog/dne:latest: exit code 125'

e2e/testdata/fn-render/no-op/.expected/diff.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/Kptfile b/Kptfile
2-
index a7a2d0b..ed39ce3 100644
2+
index a7a2d0b..3dbfee4 100644
33
--- a/Kptfile
44
+++ b/Kptfile
55
@@ -5,3 +5,12 @@ metadata:
@@ -13,5 +13,5 @@ index a7a2d0b..ed39ce3 100644
1313
+ reason: RenderSuccess
1414
+ renderStatus:
1515
+ mutationSteps:
16-
+ - image: ghcr.io/kptdev/krm-functions-catalog/no-op
16+
+ - image: ghcr.io/kptdev/krm-functions-catalog/no-op:latest
1717
+ exitCode: 0

internal/fnruntime/runner.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ func NewRunner(
7575
if err != nil {
7676
return nil, err
7777
}
78+
} else if !hasTagOrDigest(f.Image) {
79+
f.Image = f.Image + ":latest"
7880
}
7981
}
8082

@@ -520,3 +522,15 @@ func newFnConfig(fsys filesys.FileSystem, f *kptfilev1.Function, pkgPath types.U
520522
// no need to return ConfigMap if no config given
521523
return nil, nil
522524
}
525+
526+
// hasTagOrDigest reports whether the image reference contains an explicit tag or digest.
527+
func hasTagOrDigest(image string) bool {
528+
if strings.Contains(image, "@") {
529+
return true
530+
}
531+
lastSlash := strings.LastIndex(image, "/")
532+
if lastSlash == -1 {
533+
return strings.Contains(image, ":")
534+
}
535+
return strings.Contains(image[lastSlash:], ":")
536+
}

internal/fnruntime/runner_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,38 @@ func getExpectedPrefix(prefix string) string {
658658
}
659659
return prefix
660660
}
661+
662+
func TestHasTagOrDigest(t *testing.T) {
663+
tests := []struct {
664+
name string
665+
image string
666+
want bool
667+
}{
668+
// With explicit tag
669+
{"tag", "nginx:1.25", true},
670+
{"latest tag", "nginx:latest", true},
671+
{"full path with tag", "ghcr.io/kptdev/krm-functions-catalog/set-labels:v0.1.5", true},
672+
673+
// With digest
674+
{"digest only", "nginx@sha256:abc123", true},
675+
{"full path with digest", "ghcr.io/kptdev/krm-functions-catalog/set-labels@sha256:abc123", true},
676+
{"tag and digest", "nginx:1.25@sha256:abc123", true},
677+
678+
// Without tag or digest
679+
{"bare image", "nginx", false},
680+
{"full path no tag", "ghcr.io/kptdev/krm-functions-catalog/set-labels", false},
681+
{"two-part no tag", "library/nginx", false},
682+
683+
// Registry with port (should not confuse port colon with tag colon)
684+
{"registry port no tag", "localhost:5000/myimage", false},
685+
{"registry port with tag", "localhost:5000/myimage:v1", true},
686+
{"registry port with digest", "localhost:5000/myimage@sha256:abc123", true},
687+
}
688+
for _, tt := range tests {
689+
t.Run(tt.name, func(t *testing.T) {
690+
if got := hasTagOrDigest(tt.image); got != tt.want {
691+
t.Errorf("hasTagOrDigest(%q) = %v, want %v", tt.image, got, tt.want)
692+
}
693+
})
694+
}
695+
}

0 commit comments

Comments
 (0)