Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

images: overhaul package, fix environment override #303

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ test-e2e-positive: build-e2e
test-e2e-negative: build-e2e
_out/e2e.test -ginkgo.focus='\[NegativeFlow\]'

.PHONY: test-e2e-local
test-e2e-local: build-e2e
_out/e2e.test -ginkgo.focus='\[Local\]'

.PHONY: gofmt
gofmt:
@echo "Running gofmt"
Expand Down
13 changes: 2 additions & 11 deletions pkg/commands/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/spf13/cobra"

"github.com/k8stopologyawareschedwg/deployer/pkg/deployer"
"github.com/k8stopologyawareschedwg/deployer/pkg/deployer/updaters"
"github.com/k8stopologyawareschedwg/deployer/pkg/images"
"github.com/k8stopologyawareschedwg/deployer/pkg/options"
)
Expand All @@ -39,13 +38,12 @@ func NewImagesCommand(env *deployer.Environment, commonOpts *options.Options) *c
Use: "images",
Short: "dump the container images used to deploy",
RunE: func(cmd *cobra.Command, args []string) error {
images.SetDefaults(opts.useSHA)
updaterImage := getUpdaterImage(commonOpts.UpdaterType)
imgs := images.GetWithFunc(opts.useSHA, os.LookupEnv)
fk := images.FormatText
if opts.jsonOutput {
fk = images.FormatJSON
}
imo := images.NewOutput(updaterImage)
imo := images.NewOutput(imgs, commonOpts.UpdaterType)
var of images.Formatter = imo
if opts.rawOutput {
of = imo.ToList()
Expand All @@ -60,10 +58,3 @@ func NewImagesCommand(env *deployer.Environment, commonOpts *options.Options) *c
images.Flags().BoolVarP(&opts.useSHA, "sha", "S", false, "emit SHA256 pullspects, not tag pullspecs.")
return images
}

func getUpdaterImage(updaterType string) string {
if updaterType == updaters.RTE {
return images.ResourceTopologyExporterImage
}
return images.NodeFeatureDiscoveryImage
}
9 changes: 5 additions & 4 deletions pkg/images/consts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,18 @@ package images
import "testing"

func TestImageURISanity(t *testing.T) {
imgs := Get()
// TODO: check these are valid pullSpecs
if SchedulerPluginSchedulerImage == "" {
if imgs.SchedulerPluginScheduler == "" {
t.Fatalf("invalid Scheduler Plugin Scheduler Image pull URL")
}
if SchedulerPluginControllerImage == "" {
if imgs.SchedulerPluginController == "" {
t.Fatalf("invalid Scheduler Plugin Controller Image pull URL")
}
if ResourceTopologyExporterImage == "" {
if imgs.ResourceTopologyExporter == "" {
t.Fatalf("invalid Resource Topology Exporter Image pull URL")
}
if NodeFeatureDiscoveryImage == "" {
if imgs.NodeFeatureDiscovery == "" {
t.Fatalf("invalid Node Feature Discovery Image pull URL")
}
}
56 changes: 30 additions & 26 deletions pkg/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,48 @@ package images

import "os"

func init() {
_, ok := os.LookupEnv("TAS_IMAGES_USE_SHA")
SetDefaults(ok)
Setup(os.LookupEnv)
type Images struct {
SchedulerPluginScheduler string
SchedulerPluginController string
ResourceTopologyExporter string
NodeFeatureDiscovery string
}

func SetDefaults(useSHA bool) {
func Defaults(useSHA bool) Images {
if useSHA {
SchedulerPluginSchedulerImage = SchedulerPluginSchedulerDefaultImageSHA
SchedulerPluginControllerImage = SchedulerPluginControllerDefaultImageSHA
ResourceTopologyExporterImage = ResourceTopologyExporterDefaultImageSHA
NodeFeatureDiscoveryImage = NodeFeatureDiscoveryDefaultImageSHA
} else {
SchedulerPluginSchedulerImage = SchedulerPluginSchedulerDefaultImageTag
SchedulerPluginControllerImage = SchedulerPluginControllerDefaultImageTag
ResourceTopologyExporterImage = ResourceTopologyExporterDefaultImageTag
NodeFeatureDiscoveryImage = NodeFeatureDiscoveryDefaultImageTag
return Images{
SchedulerPluginScheduler: SchedulerPluginSchedulerDefaultImageSHA,
SchedulerPluginController: SchedulerPluginControllerDefaultImageSHA,
ResourceTopologyExporter: ResourceTopologyExporterDefaultImageSHA,
NodeFeatureDiscovery: NodeFeatureDiscoveryDefaultImageSHA,
}
}
return Images{
SchedulerPluginScheduler: SchedulerPluginSchedulerDefaultImageTag,
SchedulerPluginController: SchedulerPluginControllerDefaultImageTag,
ResourceTopologyExporter: ResourceTopologyExporterDefaultImageTag,
NodeFeatureDiscovery: NodeFeatureDiscoveryDefaultImageTag,
}
}

func Setup(getImage func(string) (string, bool)) {
func Get() Images {
_, ok := os.LookupEnv("TAS_IMAGES_USE_SHA")
return GetWithFunc(ok, os.LookupEnv)
}

func GetWithFunc(useSHA bool, getImage func(string) (string, bool)) Images {
images := Defaults(useSHA)
if schedImage, ok := getImage("TAS_SCHEDULER_PLUGIN_IMAGE"); ok {
SchedulerPluginSchedulerImage = schedImage
images.SchedulerPluginScheduler = schedImage
}
if schedCtrlImage, ok := getImage("TAS_SCHEDULER_PLUGIN_CONTROLLER_IMAGE"); ok {
SchedulerPluginControllerImage = schedCtrlImage
images.SchedulerPluginController = schedCtrlImage
}
if rteImage, ok := getImage("TAS_RESOURCE_EXPORTER_IMAGE"); ok {
ResourceTopologyExporterImage = rteImage
images.ResourceTopologyExporter = rteImage
}
if nfdImage, ok := getImage("TAS_NODE_FEATURE_DISCOVERY_IMAGE"); ok {
NodeFeatureDiscoveryImage = nfdImage
images.NodeFeatureDiscovery = nfdImage
}
return images
}

var (
SchedulerPluginSchedulerImage = SchedulerPluginSchedulerDefaultImageTag
SchedulerPluginControllerImage = SchedulerPluginControllerDefaultImageTag
ResourceTopologyExporterImage = ResourceTopologyExporterDefaultImageTag
NodeFeatureDiscoveryImage = NodeFeatureDiscoveryDefaultImageTag
)
20 changes: 16 additions & 4 deletions pkg/images/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ import (
"strings"
)

const (
RTE string = "RTE"
NFD string = "NFD"
)

type Output struct {
TopologyUpdater string `json:"topology_updater"`
SchedulerPlugin string `json:"scheduler_plugin"`
SchedulerController string `json:"scheduler_controller"`
}

func NewOutput(updaterImage string) Output {
func NewOutput(imgs Images, updaterType string) Output {
imo := Output{
SchedulerPlugin: SchedulerPluginSchedulerImage,
SchedulerController: SchedulerPluginControllerImage,
TopologyUpdater: updaterImage,
SchedulerPlugin: imgs.SchedulerPluginScheduler,
SchedulerController: imgs.SchedulerPluginController,
TopologyUpdater: getUpdaterImage(imgs, updaterType),
}
return imo
}
Expand Down Expand Up @@ -91,3 +96,10 @@ func (imo Output) Format(kind int, w io.Writer) {
imo.EncodeText(w)
}
}

func getUpdaterImage(imgs Images, updaterType string) string {
if updaterType == RTE {
return imgs.ResourceTopologyExporter
}
return imgs.NodeFeatureDiscovery
}
48 changes: 24 additions & 24 deletions pkg/images/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
)

func TestOutputBasics(t *testing.T) {
Setup(testGetImage)
imgs := GetWithFunc(false, testGetImage)

imo := NewOutput("foobar")
imo := NewOutput(imgs, "foobar")
images := imo.ToList()
if len(images) != 3 {
t.Errorf("unexpected image list content: %#v", images)
Expand All @@ -34,44 +34,44 @@ func TestOutputBasics(t *testing.T) {

func TestOutput(t *testing.T) {
type testCase struct {
name string
kind int
updaterImage string
expected string
name string
kind int
updaterType string
expected string
}

testCases := []testCase{
{
name: "text/rte",
kind: FormatText,
updaterImage: ResourceTopologyExporterImage,
expected: "TAS_SCHEDULER_PLUGIN_IMAGE=sched_sched\nTAS_SCHEDULER_PLUGIN_CONTROLLER_IMAGE=sched_ctrl\nTAS_RESOURCE_EXPORTER_IMAGE=rte",
name: "text/rte",
kind: FormatText,
updaterType: "RTE",
expected: "TAS_SCHEDULER_PLUGIN_IMAGE=sched_sched\nTAS_SCHEDULER_PLUGIN_CONTROLLER_IMAGE=sched_ctrl\nTAS_RESOURCE_EXPORTER_IMAGE=rte",
},
{
name: "text/nfd",
kind: FormatText,
updaterImage: NodeFeatureDiscoveryImage,
expected: "TAS_SCHEDULER_PLUGIN_IMAGE=sched_sched\nTAS_SCHEDULER_PLUGIN_CONTROLLER_IMAGE=sched_ctrl\nTAS_RESOURCE_EXPORTER_IMAGE=nfd",
name: "text/nfd",
kind: FormatText,
updaterType: "NFD",
expected: "TAS_SCHEDULER_PLUGIN_IMAGE=sched_sched\nTAS_SCHEDULER_PLUGIN_CONTROLLER_IMAGE=sched_ctrl\nTAS_RESOURCE_EXPORTER_IMAGE=nfd",
},
{
name: "json/rte",
kind: FormatJSON,
updaterImage: ResourceTopologyExporterImage,
expected: `{"topology_updater":"rte","scheduler_plugin":"sched_sched","scheduler_controller":"sched_ctrl"}`,
name: "json/rte",
kind: FormatJSON,
updaterType: "RTE",
expected: `{"topology_updater":"rte","scheduler_plugin":"sched_sched","scheduler_controller":"sched_ctrl"}`,
},
{
name: "json/nfd",
kind: FormatJSON,
updaterImage: NodeFeatureDiscoveryImage,
expected: `{"topology_updater":"nfd","scheduler_plugin":"sched_sched","scheduler_controller":"sched_ctrl"}`,
name: "json/nfd",
kind: FormatJSON,
updaterType: "NFD",
expected: `{"topology_updater":"nfd","scheduler_plugin":"sched_sched","scheduler_controller":"sched_ctrl"}`,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Setup(testGetImage)
imgs := GetWithFunc(false, testGetImage)

imo := NewOutput(tc.updaterImage)
imo := NewOutput(imgs, tc.updaterType)
var buf bytes.Buffer
imo.Format(tc.kind, &buf)
got := strings.TrimSpace(buf.String())
Expand Down
3 changes: 2 additions & 1 deletion pkg/objectupdate/nfd/nfd.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func UpdaterDaemonSet(ds *appsv1.DaemonSet, opts options.DaemonSet) {

c.Args = flags.Argv()

c.Image = images.NodeFeatureDiscoveryImage
imgs := images.Get()
c.Image = imgs.NodeFeatureDiscovery
}

if opts.NodeSelector != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/objectupdate/rte/rte.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ func ContainerConfig(podSpec *corev1.PodSpec, cnt *corev1.Container, configMapNa
func DaemonSet(ds *appsv1.DaemonSet, plat platform.Platform, configMapName string, opts options.DaemonSet) {
podSpec := &ds.Spec.Template.Spec
if cntSpec := objectupdate.FindContainerByName(ds.Spec.Template.Spec.Containers, manifests.ContainerNameRTE); cntSpec != nil {
cntSpec.Image = images.ResourceTopologyExporterImage
imgs := images.Get()
cntSpec.Image = imgs.ResourceTopologyExporter

cntSpec.ImagePullPolicy = corev1.PullAlways
if opts.PullIfNotPresent {
Expand Down
6 changes: 4 additions & 2 deletions pkg/objectupdate/sched/sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ import (
)

func SchedulerDeployment(dp *appsv1.Deployment, pullIfNotPresent, ctrlPlaneAffinity bool, verbose int) {
imgs := images.Get()
cnt := &dp.Spec.Template.Spec.Containers[0] // shortcut

cnt.Image = images.SchedulerPluginSchedulerImage
cnt.Image = imgs.SchedulerPluginScheduler
cnt.ImagePullPolicy = pullPolicy(pullIfNotPresent)

flags := flagcodec.ParseArgvKeyValue(cnt.Args, flagcodec.WithFlagNormalization)
Expand All @@ -43,7 +44,8 @@ func SchedulerDeployment(dp *appsv1.Deployment, pullIfNotPresent, ctrlPlaneAffin
}

func ControllerDeployment(dp *appsv1.Deployment, pullIfNotPresent, ctrlPlaneAffinity bool) {
dp.Spec.Template.Spec.Containers[0].Image = images.SchedulerPluginControllerImage
imgs := images.Get()
dp.Spec.Template.Spec.Containers[0].Image = imgs.SchedulerPluginController
dp.Spec.Template.Spec.Containers[0].ImagePullPolicy = pullPolicy(pullIfNotPresent)

if ctrlPlaneAffinity {
Expand Down
Loading
Loading