Skip to content

Commit

Permalink
Merge pull request #5260 from makhov/strip-defaults-images
Browse files Browse the repository at this point in the history
Strip default images from the config
  • Loading branch information
twz123 authored Nov 26, 2024
2 parents 1cf7f97 + 7a6b7b6 commit 33fc07e
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 55 deletions.
35 changes: 35 additions & 0 deletions pkg/apis/k0s/v1beta1/clusterconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,54 @@ func (c *ClusterConfig) StripDefaults() *ClusterConfig {
}
if reflect.DeepEqual(copy.Spec.Network, DefaultNetwork()) {
copy.Spec.Network = nil
} else {
if copy.Spec.Network.NodeLocalLoadBalancing != nil &&
copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy != nil &&
reflect.DeepEqual(copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy.Image, DefaultEnvoyProxyImage()) {
copy.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy.Image = nil
}
}
if reflect.DeepEqual(copy.Spec.Telemetry, DefaultClusterTelemetry()) {
copy.Spec.Telemetry = nil
}
if reflect.DeepEqual(copy.Spec.Images, DefaultClusterImages()) {
copy.Spec.Images = nil
} else {
stripDefaultImages(copy.Spec.Images, DefaultClusterImages())
}
if reflect.DeepEqual(copy.Spec.Konnectivity, DefaultKonnectivitySpec()) {
copy.Spec.Konnectivity = nil
}
return copy
}

func stripDefaultImages(cfgImages, defaultImages *ClusterImages) {
cfgVal := reflect.ValueOf(cfgImages).Elem()
defaultVal := reflect.ValueOf(defaultImages).Elem()
stripDefaults(cfgVal, defaultVal)
}

func stripDefaults(cfgVal, defaultVal reflect.Value) {
for i := range cfgVal.NumField() {
f1 := cfgVal.Field(i)
f2 := defaultVal.Field(i)
switch f1.Kind() {
case reflect.Pointer:
if f1.Elem().Equal(f2.Elem()) {
f1.Set(reflect.Zero(f1.Type()))
} else {
stripDefaults(f1.Elem(), f2.Elem())
}
case reflect.Struct:
stripDefaults(f1, f2)
default:
if f1.Equal(f2) {
f1.Set(reflect.Zero(f1.Type()))
}
}
}
}

// InstallSpec defines the required fields for the `k0s install` command
type InstallSpec struct {
SystemUsers *SystemUser `json:"users,omitempty"`
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/k0s/v1beta1/clusterconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func TestClusterSpecCustomImages(t *testing.T) {
},
}
validTestCase.Spec.Images.DefaultPullPolicy = string(corev1.PullIfNotPresent)
validTestCase.Spec.Images.Konnectivity = ImageSpec{
validTestCase.Spec.Images.Konnectivity = &ImageSpec{
Image: "foo",
Version: "v1",
}
validTestCase.Spec.Images.PushGateway = ImageSpec{
validTestCase.Spec.Images.PushGateway = &ImageSpec{
Image: "bar",
Version: "v2@sha256:0000000000000000000000000000000000000000000000000000000000000000",
}
Expand All @@ -101,17 +101,17 @@ func TestClusterSpecCustomImages(t *testing.T) {
Images: DefaultClusterImages(),
},
}
invalidTestCase.Spec.Images.MetricsServer = ImageSpec{
invalidTestCase.Spec.Images.MetricsServer = &ImageSpec{
Image: "baz",
// digest only is currently not supported
Version: "sha256:0000000000000000000000000000000000000000000000000000000000000000",
}
invalidTestCase.Spec.Images.Calico.CNI = ImageSpec{
invalidTestCase.Spec.Images.Calico.CNI = &ImageSpec{
Image: "qux",
// digest only is currently not supported
Version: "sha256:0000000000000000000000000000000000000000000000000000000000000000",
}
invalidTestCase.Spec.Images.KubeRouter.CNI = ImageSpec{
invalidTestCase.Spec.Images.KubeRouter.CNI = &ImageSpec{
Image: "quux",
// digest only is currently not supported
Version: "sha256:0000000000000000000000000000000000000000000000000000000000000000",
Expand Down
72 changes: 36 additions & 36 deletions pkg/apis/k0s/v1beta1/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func (s *ImageSpec) URI() string {

// ClusterImages sets docker images for addon components
type ClusterImages struct {
Konnectivity ImageSpec `json:"konnectivity,omitempty"`
PushGateway ImageSpec `json:"pushgateway,omitempty"`
MetricsServer ImageSpec `json:"metricsserver,omitempty"`
KubeProxy ImageSpec `json:"kubeproxy,omitempty"`
CoreDNS ImageSpec `json:"coredns,omitempty"`
Pause ImageSpec `json:"pause,omitempty"`
Konnectivity *ImageSpec `json:"konnectivity,omitempty"`
PushGateway *ImageSpec `json:"pushgateway,omitempty"`
MetricsServer *ImageSpec `json:"metricsserver,omitempty"`
KubeProxy *ImageSpec `json:"kubeproxy,omitempty"`
CoreDNS *ImageSpec `json:"coredns,omitempty"`
Pause *ImageSpec `json:"pause,omitempty"`

Calico CalicoImageSpec `json:"calico,omitempty"`
KubeRouter KubeRouterImageSpec `json:"kuberouter,omitempty"`
Calico *CalicoImageSpec `json:"calico,omitempty"`
KubeRouter *KubeRouterImageSpec `json:"kuberouter,omitempty"`

Repository string `json:"repository,omitempty"`

Expand Down Expand Up @@ -134,23 +134,23 @@ func (ci *ClusterImages) overrideImageRepositories() {
override := func(dst *ImageSpec) {
dst.Image = overrideRepository(ci.Repository, dst.Image)
}
override(&ci.Konnectivity)
override(&ci.MetricsServer)
override(&ci.KubeProxy)
override(&ci.CoreDNS)
override(&ci.Calico.CNI)
override(&ci.Calico.Node)
override(&ci.Calico.KubeControllers)
override(&ci.KubeRouter.CNI)
override(&ci.KubeRouter.CNIInstaller)
override(&ci.Pause)
override(ci.Konnectivity)
override(ci.MetricsServer)
override(ci.KubeProxy)
override(ci.CoreDNS)
override(ci.Calico.CNI)
override(ci.Calico.Node)
override(ci.Calico.KubeControllers)
override(ci.KubeRouter.CNI)
override(ci.KubeRouter.CNIInstaller)
override(ci.Pause)
}

// CalicoImageSpec config group for calico related image settings
type CalicoImageSpec struct {
CNI ImageSpec `json:"cni,omitempty"`
Node ImageSpec `json:"node,omitempty"`
KubeControllers ImageSpec `json:"kubecontrollers,omitempty"`
CNI *ImageSpec `json:"cni,omitempty"`
Node *ImageSpec `json:"node,omitempty"`
KubeControllers *ImageSpec `json:"kubecontrollers,omitempty"`
}

func (s *CalicoImageSpec) Validate(path *field.Path) (errs field.ErrorList) {
Expand All @@ -165,8 +165,8 @@ func (s *CalicoImageSpec) Validate(path *field.Path) (errs field.ErrorList) {

// KubeRouterImageSpec config group for kube-router related images
type KubeRouterImageSpec struct {
CNI ImageSpec `json:"cni,omitempty"`
CNIInstaller ImageSpec `json:"cniInstaller,omitempty"`
CNI *ImageSpec `json:"cni,omitempty"`
CNIInstaller *ImageSpec `json:"cniInstaller,omitempty"`
}

func (s *KubeRouterImageSpec) Validate(path *field.Path) (errs field.ErrorList) {
Expand All @@ -182,51 +182,51 @@ func (s *KubeRouterImageSpec) Validate(path *field.Path) (errs field.ErrorList)
func DefaultClusterImages() *ClusterImages {
return &ClusterImages{
DefaultPullPolicy: "IfNotPresent",
Konnectivity: ImageSpec{
Konnectivity: &ImageSpec{
Image: constant.KonnectivityImage,
Version: constant.KonnectivityImageVersion,
},
PushGateway: ImageSpec{
PushGateway: &ImageSpec{
Image: constant.PushGatewayImage,
Version: constant.PushGatewayImageVersion,
},
MetricsServer: ImageSpec{
MetricsServer: &ImageSpec{
Image: constant.MetricsImage,
Version: constant.MetricsImageVersion,
},
KubeProxy: ImageSpec{
KubeProxy: &ImageSpec{
Image: constant.KubeProxyImage,
Version: constant.KubeProxyImageVersion,
},
CoreDNS: ImageSpec{
CoreDNS: &ImageSpec{
Image: constant.CoreDNSImage,
Version: constant.CoreDNSImageVersion,
},
Calico: CalicoImageSpec{
CNI: ImageSpec{
Calico: &CalicoImageSpec{
CNI: &ImageSpec{
Image: constant.CalicoImage,
Version: constant.CalicoComponentImagesVersion,
},
Node: ImageSpec{
Node: &ImageSpec{
Image: constant.CalicoNodeImage,
Version: constant.CalicoComponentImagesVersion,
},
KubeControllers: ImageSpec{
KubeControllers: &ImageSpec{
Image: constant.KubeControllerImage,
Version: constant.CalicoComponentImagesVersion,
},
},
KubeRouter: KubeRouterImageSpec{
CNI: ImageSpec{
KubeRouter: &KubeRouterImageSpec{
CNI: &ImageSpec{
Image: constant.KubeRouterCNIImage,
Version: constant.KubeRouterCNIImageVersion,
},
CNIInstaller: ImageSpec{
CNIInstaller: &ImageSpec{
Image: constant.KubeRouterCNIInstallerImage,
Version: constant.KubeRouterCNIInstallerImageVersion,
},
},
Pause: ImageSpec{
Pause: &ImageSpec{
Image: constant.KubePauseContainerImage,
Version: constant.KubePauseContainerImageVersion,
},
Expand Down
24 changes: 24 additions & 0 deletions pkg/apis/k0s/v1beta1/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ spec:
assert.Equal(t, "registry.k8s.io/metrics-server/metrics-server", a.MetricsServer.Image)
}

func TestStripDefaultsForDefaultImageList(t *testing.T) {
yamlData := `
apiVersion: k0s.k0sproject.io/v1beta1s
kind: ClusterConfig
spec:
images:
default_pull_policy: Never
network:
nodeLocalLoadBalancing:
enabled: true
type: EnvoyProxy
`

cfg, err := ConfigFromString(yamlData)
require.NoError(t, err)

strippedCfg := cfg.StripDefaults()

require.Equal(t, "Never", strippedCfg.Spec.Images.DefaultPullPolicy)
require.Nil(t, strippedCfg.Spec.Images.Konnectivity)
require.True(t, strippedCfg.Spec.Network.NodeLocalLoadBalancing.Enabled)
require.Nil(t, strippedCfg.Spec.Network.NodeLocalLoadBalancing.EnvoyProxy.Image)
}

func TestImagesRepoOverrideInConfiguration(t *testing.T) {
t.Run("if_has_repository_not_empty_add_prefix_to_all_images", func(t *testing.T) {
t.Run("default_config", func(t *testing.T) {
Expand Down
80 changes: 66 additions & 14 deletions pkg/apis/k0s/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 33fc07e

Please sign in to comment.