Skip to content

Commit

Permalink
Merge pull request #1865 from hj-johannes-lee/PR-2024-010
Browse files Browse the repository at this point in the history
operator: fix upgradeImages
  • Loading branch information
tkatila authored Oct 16, 2024
2 parents f31ff37 + 6915c7d commit d2279d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/controllers/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions pkg/controllers/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package controllers

import (
"context"
"os"
"testing"

v1 "k8s.io/api/core/v1"
Expand All @@ -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
Expand Down Expand Up @@ -61,18 +63,48 @@ 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) {
test.Errorf("expectedUpgrade: %v, received: %v", t.upgrade, upgrade)
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)
}
}
}

Expand Down

0 comments on commit d2279d1

Please sign in to comment.