Skip to content

Commit

Permalink
Use typed ImageID for imageutils images
Browse files Browse the repository at this point in the history
  • Loading branch information
tallclair committed May 12, 2022
1 parent af99c75 commit ab29782
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 38 deletions.
7 changes: 1 addition & 6 deletions test/e2e/framework/pod/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ import (
imageutils "k8s.io/kubernetes/test/utils/image"
)

var (
// BusyBoxImage is the image URI of BusyBox.
BusyBoxImage = imageutils.GetE2EImage(imageutils.BusyBox)
)

// Config is a struct containing all arguments for creating a pod.
// SELinux testing requires to pass HostIPC and HostPID as boolean arguments.
type Config struct {
Expand All @@ -47,7 +42,7 @@ type Config struct {
SeLinuxLabel *v1.SELinuxOptions
FsGroup *int64
NodeSelection NodeSelection
ImageID int
ImageID imageutils.ImageID
PodFSGroupChangePolicy *v1.PodFSGroupChangePolicy
}

Expand Down
6 changes: 3 additions & 3 deletions test/e2e/framework/pod/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ func GetDefaultTestImage() string {
// If the node OS is windows, currently we return Agnhost image for Windows node
// due to the issue of #https://github.com/kubernetes-sigs/windows-testing/pull/35.
// If the node OS is linux, return busybox image
func GetDefaultTestImageID() int {
func GetDefaultTestImageID() imageutils.ImageID {
return GetTestImageID(imageutils.BusyBox)
}

// GetTestImage returns the image name with the given input
// If the Node OS is windows, currently we return Agnhost image for Windows node
// due to the issue of #https://github.com/kubernetes-sigs/windows-testing/pull/35.
func GetTestImage(id int) string {
func GetTestImage(id imageutils.ImageID) string {
if NodeOSDistroIs("windows") {
return imageutils.GetE2EImage(imageutils.Agnhost)
}
Expand All @@ -73,7 +73,7 @@ func GetTestImage(id int) string {
// GetTestImageID returns the image id with the given input
// If the Node OS is windows, currently we return Agnhost image for Windows node
// due to the issue of #https://github.com/kubernetes-sigs/windows-testing/pull/35.
func GetTestImageID(id int) int {
func GetTestImageID(id imageutils.ImageID) imageutils.ImageID {
if NodeOSDistroIs("windows") {
return imageutils.Agnhost
}
Expand Down
10 changes: 5 additions & 5 deletions test/utils/image/csi_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strings"

"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/kubernetes/test/e2e/testing-manifests"
e2etestingmanifests "k8s.io/kubernetes/test/e2e/testing-manifests"
)

// All of the image tags are of the format k8s.gcr.io/sig-storage/hostpathplugin:v1.7.3.
Expand All @@ -33,11 +33,11 @@ var imageRE = regexp.MustCompile(`^(.*)/([^/:]*):(.*)$`)
// appendCSIImageConfigs extracts image repo, name and version from
// the YAML files under test/e2e/testing-manifests/storage-csi and
// creates new config entries for them.
func appendCSIImageConfigs(configs map[int]Config) {
embeddedFS := testing_manifests.GetE2ETestingManifestsFS().EmbeddedFS
func appendCSIImageConfigs(configs map[ImageID]Config) {
embeddedFS := e2etestingmanifests.GetE2ETestingManifestsFS().EmbeddedFS

// We add our images with index numbers that start after the highest existing number.
index := 0
// We add our images with ImageID numbers that start after the highest existing number.
index := ImageID(0)
for i := range configs {
if i > index {
index = i
Expand Down
2 changes: 1 addition & 1 deletion test/utils/image/csi_manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestCSIImageConfigs(t *testing.T) {
configs := map[int]Config{}
configs := map[ImageID]Config{}
appendCSIImageConfigs(configs)

// We expect at least one entry for each of these images. There may be
Expand Down
42 changes: 22 additions & 20 deletions test/utils/image/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ var (
imageConfigs, originalImageConfigs = initImageConfigs(registry)
)

type ImageID int

const (
// None is to be used for unset/default images
None = iota
None ImageID = iota
// Agnhost image
Agnhost
// AgnhostPrivate image
Expand Down Expand Up @@ -229,8 +231,8 @@ const (
WindowsServer
)

func initImageConfigs(list RegistryList) (map[int]Config, map[int]Config) {
configs := map[int]Config{}
func initImageConfigs(list RegistryList) (map[ImageID]Config, map[ImageID]Config) {
configs := map[ImageID]Config{}
configs[Agnhost] = Config{list.PromoterE2eRegistry, "agnhost", "2.36"}
configs[AgnhostPrivate] = Config{list.PrivateRegistry, "agnhost", "2.6"}
configs[AuthenticatedAlpine] = Config{list.GcAuthenticatedRegistry, "alpine", "3.7"}
Expand Down Expand Up @@ -274,7 +276,7 @@ func initImageConfigs(list RegistryList) (map[int]Config, map[int]Config) {
configs[VolumeRBDServer] = Config{list.PromoterE2eRegistry, "volume/rbd", "1.0.4"}
configs[WindowsServer] = Config{list.MicrosoftRegistry, "windows", "1809"}

// This adds more config entries. Those have no pre-defined index number,
// This adds more config entries. Those have no pre-defined ImageID number,
// but will be used via ReplaceRegistryInImageURL when deploying
// CSI drivers (test/e2e/storage/util/create.go).
appendCSIImageConfigs(configs)
Expand All @@ -290,8 +292,8 @@ func initImageConfigs(list RegistryList) (map[int]Config, map[int]Config) {

// GetMappedImageConfigs returns the images if they were mapped to the provided
// image repository.
func GetMappedImageConfigs(originalImageConfigs map[int]Config, repo string) map[int]Config {
configs := make(map[int]Config)
func GetMappedImageConfigs(originalImageConfigs map[ImageID]Config, repo string) map[ImageID]Config {
configs := make(map[ImageID]Config)
for i, config := range originalImageConfigs {
switch i {
case InvalidRegistryImage, AuthenticatedAlpine,
Expand All @@ -303,7 +305,7 @@ func GetMappedImageConfigs(originalImageConfigs map[int]Config, repo string) map
continue
}

// Build a new tag with a the index, a hash of the image spec (to be unique) and
// Build a new tag with the ImageID, a hash of the image spec (to be unique) and
// shorten and make the pull spec "safe" so it will fit in the tag
configs[i] = getRepositoryMappedConfig(i, config, repo)
}
Expand All @@ -316,11 +318,11 @@ var (
)

// getRepositoryMappedConfig maps an existing image to the provided repo, generating a
// tag that is unique with the input config. The tag will contain the index, a hash of
// tag that is unique with the input config. The tag will contain the imageID, a hash of
// the image spec (to be unique) and shorten and make the pull spec "safe" so it will
// fit in the tag to allow a human to recognize the value. If index is -1, then no
// index will be added to the tag.
func getRepositoryMappedConfig(index int, config Config, repo string) Config {
// fit in the tag to allow a human to recognize the value. If imageID is None, then no
// imageID will be added to the tag.
func getRepositoryMappedConfig(imageID ImageID, config Config, repo string) Config {
parts := strings.SplitN(repo, "/", 2)
registry, name := parts[0], parts[1]

Expand All @@ -337,10 +339,10 @@ func getRepositoryMappedConfig(index int, config Config, repo string) Config {
shortName = shortName[len(shortName)-maxLength:]
}
var version string
if index == -1 {
if imageID == None {
version = fmt.Sprintf("e2e-%s-%s", shortName, hash)
} else {
version = fmt.Sprintf("e2e-%d-%s-%s", index, shortName, hash)
version = fmt.Sprintf("e2e-%d-%s-%s", imageID, shortName, hash)
}

return Config{
Expand All @@ -351,22 +353,22 @@ func getRepositoryMappedConfig(index int, config Config, repo string) Config {
}

// GetOriginalImageConfigs returns the configuration before any mapping rules.
func GetOriginalImageConfigs() map[int]Config {
func GetOriginalImageConfigs() map[ImageID]Config {
return originalImageConfigs
}

// GetImageConfigs returns the map of imageConfigs
func GetImageConfigs() map[int]Config {
func GetImageConfigs() map[ImageID]Config {
return imageConfigs
}

// GetConfig returns the Config object for an image
func GetConfig(image int) Config {
func GetConfig(image ImageID) Config {
return imageConfigs[image]
}

// GetE2EImage returns the fully qualified URI to an image (including version)
func GetE2EImage(image int) string {
func GetE2EImage(image ImageID) string {
return fmt.Sprintf("%s/%s:%s", imageConfigs[image].registry, imageConfigs[image].name, imageConfigs[image].version)
}

Expand Down Expand Up @@ -394,18 +396,18 @@ func replaceRegistryInImageURLWithList(imageURL string, reg RegistryList) (strin
registryAndUser := strings.Join(parts[:countParts-1], "/")

if repo := os.Getenv("KUBE_TEST_REPO"); len(repo) > 0 {
index := -1
imageID := None
for i, v := range originalImageConfigs {
if v.GetE2EImage() == imageURL {
index = i
imageID = i
break
}
}
last := strings.SplitN(parts[countParts-1], ":", 2)
if len(last) == 1 {
return "", fmt.Errorf("image %q is required to be in an image:tag format", imageURL)
}
config := getRepositoryMappedConfig(index, Config{
config := getRepositoryMappedConfig(imageID, Config{
registry: parts[0],
name: strings.Join([]string{strings.Join(parts[1:countParts-1], "/"), last[0]}, "/"),
version: last[1],
Expand Down
6 changes: 3 additions & 3 deletions test/utils/image/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ func TestGetOriginalImageConfigs(t *testing.T) {
}

func TestGetMappedImageConfigs(t *testing.T) {
originals := map[int]Config{
0: {registry: "docker.io", name: "source/repo", version: "1.0"},
originals := map[ImageID]Config{
10: {registry: "docker.io", name: "source/repo", version: "1.0"},
}
mapping := GetMappedImageConfigs(originals, "quay.io/repo/for-test")

Expand All @@ -174,7 +174,7 @@ func TestGetMappedImageConfigs(t *testing.T) {
actual[source.GetE2EImage()] = mapping.GetE2EImage()
}
expected := map[string]string{
"docker.io/source/repo:1.0": "quay.io/repo/for-test:e2e-0-docker-io-source-repo-1-0-72R4aXm7YnxQ4_ek",
"docker.io/source/repo:1.0": "quay.io/repo/for-test:e2e-10-docker-io-source-repo-1-0-72R4aXm7YnxQ4_ek",
}
if !reflect.DeepEqual(expected, actual) {
t.Fatal(diff.ObjectReflectDiff(expected, actual))
Expand Down

0 comments on commit ab29782

Please sign in to comment.