From f4241b8cb282c7b09d46b20496da08d0dab51daf Mon Sep 17 00:00:00 2001 From: Hyeongju Johannes Lee Date: Fri, 4 Oct 2024 16:50:52 +0300 Subject: [PATCH 1/2] operator: fix upgradeImages Operatorhub bundle can have sha256 image tags that are put through env vars. When operator controller manager gets upgraded, its operands (plugin daemonsets) should be updated to the image in the env vars. But it has not been working properly because of wrong parsing. Fix it to parse the image names that have sha256 tags correctly so env vars in operator can be used as intended. Additionatlly, add comments with an example result to the part where parsing, trimming, or transforming the name of images happens in UpgradImages to make the process intuitive. Signed-off-by: Hyeongju Johannes Lee --- pkg/controllers/reconciler.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkg/controllers/reconciler.go b/pkg/controllers/reconciler.go index d1c38d3c6..eee7d7940 100644 --- a/pkg/controllers/reconciler.go +++ b/pkg/controllers/reconciler.go @@ -158,10 +158,13 @@ func UpgradeImages(ctx context.Context, image *string, initimage *string) (upgra if s == nil { continue } - + // e.g. intel-dsa-plugin@sha256:hash -> [intel-dsa-plugin@sha256, hash] if parts := strings.SplitN(*s, ":", 2); len(parts) == 2 && len(parts[0]) > 0 { - name, version := parts[0], parts[1] + // e.g. [intel-dsa-plugin@sha256, hash] -> [intel-dsa-plugin, hash] + name, version := strings.TrimSuffix(parts[0], "@sha256"), parts[1] + // e.g. intel-dsa-plugin -> INTEL_DSA_PLUGIN_SHA + // and get the value of the env var INTEL_DSA_PLUGIN_SHA envVarValue := os.Getenv(strings.ReplaceAll(strings.ToUpper(filepath.Base(name)), "-", "_") + "_SHA") if envVarValue != "" && *s != envVarValue { From 6915c7d162f55cc7342006b0045de2ef633088cf Mon Sep 17 00:00:00 2001 From: Hyeongju Johannes Lee Date: Sat, 5 Oct 2024 16:22:23 +0300 Subject: [PATCH 2/2] operator: add sha256 image upgrade test cases in the unit test Signed-off-by: Hyeongju Johannes Lee --- pkg/controllers/reconciler_test.go | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pkg/controllers/reconciler_test.go b/pkg/controllers/reconciler_test.go index c2ff136b9..7dc176634 100644 --- a/pkg/controllers/reconciler_test.go +++ b/pkg/controllers/reconciler_test.go @@ -16,6 +16,7 @@ package controllers import ( "context" + "os" "testing" v1 "k8s.io/api/core/v1" @@ -27,6 +28,7 @@ func TestUpgrade(test *testing.T) { version := ":" + ImageMinVersion.String() prevVersion := ":" + ImageMinVersion.WithMinor(ImageMinVersion.Minor()-1).String() tests := []struct { + envVars map[string]string image string initimage string expectedImage string @@ -61,11 +63,37 @@ func TestUpgrade(test *testing.T) { expectedInitimage: initimage, upgrade: false, }, + { + envVars: map[string]string{ + "INTEL_DSA_PLUGIN_SHA": "intel/intel-dsa-plugin@sha256:000000000000000000000000000000000000000000000000000000000000000b", + "INTEL_IDXD_CONFIG_INITCONTAINER_SHA": "intel/intel-idxd-config-initcontainer@sha256:000000000000000000000000000000000000000000000000000000000000000b", + }, + image: image + "@sha256:000000000000000000000000000000000000000000000000000000000000000a", + expectedImage: image + "@sha256:000000000000000000000000000000000000000000000000000000000000000b", + initimage: initimage + "@sha256:000000000000000000000000000000000000000000000000000000000000000a", + expectedInitimage: initimage + "@sha256:000000000000000000000000000000000000000000000000000000000000000b", + upgrade: true, + }, + { + envVars: map[string]string{ + "INTEL_DSA_PLUGIN_SHA": "intel/intel-dsa-plugin@sha256:000000000000000000000000000000000000000000000000000000000000000a", + "INTEL_IDXD_CONFIG_INITCONTAINER_SHA": "intel/intel-idxd-config-initcontainer@sha256:000000000000000000000000000000000000000000000000000000000000000a", + }, + image: image + "@sha256:000000000000000000000000000000000000000000000000000000000000000a", + expectedImage: image + "@sha256:000000000000000000000000000000000000000000000000000000000000000a", + initimage: initimage + "@sha256:000000000000000000000000000000000000000000000000000000000000000a", + expectedInitimage: initimage + "@sha256:000000000000000000000000000000000000000000000000000000000000000a", + upgrade: false, + }, } for i := range tests { t := tests[i] + for key, value := range t.envVars { + os.Setenv(key, value) + } + upgrade := UpgradeImages(context.Background(), &t.image, &t.initimage) if !(upgrade == t.upgrade && t.image == t.expectedImage && t.initimage == t.expectedInitimage) { @@ -73,6 +101,10 @@ func TestUpgrade(test *testing.T) { test.Errorf("expectedImage: %s, received: %s", t.expectedImage, t.image) test.Errorf("expectedInitimage: %s, received: %s", t.expectedInitimage, t.initimage) } + + for key := range t.envVars { + os.Unsetenv(key) + } } }