Skip to content

Commit

Permalink
Merge pull request #4816 from k0sproject/backport-4794-to-release-1.28
Browse files Browse the repository at this point in the history
[Backport release-1.28] bug: add support for image tag and digest
  • Loading branch information
twz123 authored Jul 29, 2024
2 parents 0db12ef + 46bedaa commit 286952f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
27 changes: 27 additions & 0 deletions pkg/apis/k0s/v1beta1/clusterconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package v1beta1

import (
"encoding/json"
"fmt"
"testing"

"github.com/k0sproject/k0s/internal/pkg/iface"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -65,6 +67,31 @@ func TestEmptyClusterSpec(t *testing.T) {
assert.Nil(t, errs)
}

func TestClusterSpecCustomImages(t *testing.T) {
underTest := ClusterConfig{
Spec: &ClusterSpec{
Images: &ClusterImages{
DefaultPullPolicy: string(corev1.PullIfNotPresent),
Konnectivity: ImageSpec{
Image: "foo",
Version: "v1",
},
PushGateway: ImageSpec{
Image: "bar",
Version: "v2@sha256:0000000000000000000000000000000000000000000000000000000000000000",
},
MetricsServer: ImageSpec{
Image: "baz",
Version: "sha256:0000000000000000000000000000000000000000000000000000000000000000",
},
},
},
}

errs := underTest.Validate()
assert.Nil(t, errs, fmt.Sprintf("%v", errs))
}

func TestEtcdDefaults(t *testing.T) {
yamlData := `
apiVersion: k0s.k0sproject.io/v1beta1
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/k0s/v1beta1/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func (s *ImageSpec) Validate(path *field.Path) (errs field.ErrorList) {
errs = append(errs, field.Invalid(path.Child("image"), s.Image, "must not have leading or trailing whitespace"))
}

versionRe := regexp.MustCompile(`^` + docker.TagRegexp.String() + `$`)
// Validate the image contains a tag and optional digest
versionRe := regexp.MustCompile(`^` + docker.TagRegexp.String() + `(?:@` + docker.DigestRegexp.String() + `)?$`)
if !versionRe.MatchString(s.Version) {
errs = append(errs, field.Invalid(path.Child("version"), s.Version, "must match regular expression: "+versionRe.String()))
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/apis/k0s/v1beta1/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/k0sproject/k0s/pkg/constant"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -117,3 +118,53 @@ func TestOverrideFunction(t *testing.T) {
assert.Equal(t, tc.Output, overrideRepository(repository, tc.Input))
}
}

func TestImageSpec_Validate(t *testing.T) {
validTestCases := []struct {
Image string
Version string
}{
{"my.registry/repo/image", "v1.0.0"},
{"my.registry/repo/image", "latest"},
{"my.registry/repo/image", "v1.0.0-rc1"},
{"my.registry/repo/image", "v1.0.0@sha256:0000000000000000000000000000000000000000000000000000000000000000"},
}
for _, tc := range validTestCases {
t.Run(tc.Image+":"+tc.Version+"_valid", func(t *testing.T) {
s := &ImageSpec{
Image: tc.Image,
Version: tc.Version,
}
errs := s.Validate(field.NewPath("image"))
assert.Empty(t, errs)
})
}

errVersionRe := `must match regular expression: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$`

invalidTestCases := []struct {
Image string
Version string
Errs field.ErrorList
}{
{
"my.registry/repo/image", "",
field.ErrorList{field.Invalid(field.NewPath("image").Child("version"), "", errVersionRe)},
},
// digest only is currently not supported
{
"my.registry/repo/image", "sha256:0000000000000000000000000000000000000000000000000000000000000000",
field.ErrorList{field.Invalid(field.NewPath("image").Child("version"), "sha256:0000000000000000000000000000000000000000000000000000000000000000", errVersionRe)},
},
}
for _, tc := range invalidTestCases {
t.Run(tc.Image+":"+tc.Version+"_valid", func(t *testing.T) {
s := &ImageSpec{
Image: tc.Image,
Version: tc.Version,
}
errs := s.Validate(field.NewPath("image"))
assert.Equal(t, tc.Errs, errs)
})
}
}
2 changes: 1 addition & 1 deletion pkg/apis/k0s/v1beta1/nllb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ spec:
},
{
"version", `"*"`,
`network: nodeLocalLoadBalancing.envoyProxy.image.version: Invalid value: "*": must match regular expression: ^[\w][\w.-]{0,127}$`,
`network: nodeLocalLoadBalancing.envoyProxy.image.version: Invalid value: "*": must match regular expression: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$`,
},
} {
t.Run(test.field+"_invalid", func(t *testing.T) {
Expand Down

0 comments on commit 286952f

Please sign in to comment.