Skip to content

Commit

Permalink
Merge pull request k0sproject#4792 from emosbaugh/add-support-for-ima…
Browse files Browse the repository at this point in the history
…ge-tag-digest

bug: add support for image tag and digest
  • Loading branch information
twz123 authored Jul 24, 2024
2 parents 57297a7 + f7f641b commit 6d083b0
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 15 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
5 changes: 3 additions & 2 deletions pkg/apis/k0s/v1beta1/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ImageSpec struct {
// +kubebuilder:validation:MinLength=1
Image string `json:"image"`

// +kubebuilder:validation:Pattern="[\\w][\\w.-]{0,127}"
// +kubebuilder:validation:Pattern="^[\\w][\\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$"
Version string `json:"version"`
}

Expand All @@ -51,7 +51,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(`^` + reference.TagRegexp.String() + `$`)
// Validate the image contains a tag and optional digest
versionRe := regexp.MustCompile(`^` + reference.TagRegexp.String() + `(?:@` + reference.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
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -245,7 +245,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -258,7 +258,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -272,7 +272,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -292,7 +292,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -305,7 +305,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -322,7 +322,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -335,7 +335,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -349,7 +349,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -362,7 +362,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand All @@ -375,7 +375,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand Down Expand Up @@ -775,7 +775,7 @@ spec:
minLength: 1
type: string
version:
pattern: '[\w][\w.-]{0,127}'
pattern: ^[\w][\w.-]{0,127}(?:@[A-Za-z][A-Za-z0-9]*(?:[-_+.][A-Za-z][A-Za-z0-9]*)*[:][[:xdigit:]]{32,})?$
type: string
required:
- image
Expand Down

0 comments on commit 6d083b0

Please sign in to comment.