-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85cf74e
commit 543a25c
Showing
5 changed files
with
293 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package controller | ||
|
||
import ( | ||
"strings" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
) | ||
|
||
func getImages(dp appsv1.Deployment) []string { | ||
images := []string{} | ||
|
||
ignoreDP := dp.ObjectMeta.Annotations["kube-image-prefetch/ignore"] | ||
if ignoreDP == "true" { | ||
return images | ||
} | ||
|
||
ignoreContainersStr := dp.ObjectMeta.Annotations["kube-image-prefetch/ignore-containers"] | ||
ignoreContainers := strings.Split(ignoreContainersStr, ",") | ||
|
||
for _, container := range append(dp.Spec.Template.Spec.InitContainers, dp.Spec.Template.Spec.Containers...) { | ||
if !contains(ignoreContainers, container.Name) { | ||
images = append(images, container.Image) | ||
} | ||
} | ||
|
||
return images | ||
} | ||
|
||
func contains(arr []string, str string) bool { | ||
for _, c := range arr { | ||
if strings.TrimSpace(strings.ToLower(str)) == strings.TrimSpace(strings.ToLower(c)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
package controller | ||
|
||
import ( | ||
"testing" | ||
"reflect" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestGetImages_SingleImage(t *testing.T) { | ||
dp := appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "test-deployment", | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "container-1", | ||
Image: "image:1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expected := []string{"image:1"} | ||
actual := getImages(dp) | ||
|
||
if ! reflect.DeepEqual(expected, actual) { | ||
t.Errorf("Unexpected images returned - %v", actual) | ||
} | ||
} | ||
|
||
func TestGetImages_MultipleImage(t *testing.T) { | ||
dp := appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "test-deployment", | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
Containers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "container-1", | ||
Image: "image:1", | ||
}, | ||
corev1.Container{ | ||
Name: "container-2", | ||
Image: "image:2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expected := []string{"image:1","image:2"} | ||
actual := getImages(dp) | ||
|
||
if ! reflect.DeepEqual(expected, actual) { | ||
t.Errorf("Unexpected images returned - %v", actual) | ||
} | ||
} | ||
|
||
func TestGetImages_InitContainers(t *testing.T) { | ||
dp := appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "test-deployment", | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "initcontainer-1", | ||
Image: "initimage:1", | ||
}, | ||
}, | ||
Containers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "container-1", | ||
Image: "image:1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expected := []string{"initimage:1","image:1"} | ||
actual := getImages(dp) | ||
|
||
if ! reflect.DeepEqual(expected, actual) { | ||
t.Errorf("Unexpected images returned - %v", actual) | ||
} | ||
} | ||
|
||
func TestGetImages_IgnoreDeployment(t *testing.T) { | ||
dp := appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "test-deployment", | ||
Annotations: map[string]string{ | ||
"kube-image-prefetch/ignore": "true", | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "initcontainer-1", | ||
Image: "initimage:1", | ||
}, | ||
}, | ||
Containers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "container-1", | ||
Image: "image:1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expected := []string{} | ||
actual := getImages(dp) | ||
|
||
if ! reflect.DeepEqual(expected, actual) { | ||
t.Errorf("Unexpected images returned - %v", actual) | ||
} | ||
} | ||
|
||
func TestGetImages_IgnoreContainer(t *testing.T) { | ||
dp := appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "test-deployment", | ||
Annotations: map[string]string{ | ||
"kube-image-prefetch/ignore-containers": "initcontainer-1", | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "initcontainer-1", | ||
Image: "initimage:1", | ||
}, | ||
}, | ||
Containers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "container-1", | ||
Image: "image:1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expected := []string{"image:1"} | ||
actual := getImages(dp) | ||
|
||
if ! reflect.DeepEqual(expected, actual) { | ||
t.Errorf("Unexpected images returned - %v", actual) | ||
} | ||
} | ||
|
||
func TestGetImages_IgnoreMultipleContainer(t *testing.T) { | ||
dp := appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "test-deployment", | ||
Annotations: map[string]string{ | ||
"kube-image-prefetch/ignore-containers": "initcontainer-1,container-1", | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "initcontainer-1", | ||
Image: "initimage:1", | ||
}, | ||
}, | ||
Containers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "container-1", | ||
Image: "image:1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expected := []string{} | ||
actual := getImages(dp) | ||
|
||
if ! reflect.DeepEqual(expected, actual) { | ||
t.Errorf("Unexpected images returned - %v", actual) | ||
} | ||
} | ||
|
||
func TestGetImages_IgnoreContainerSpaces(t *testing.T) { | ||
dp := appsv1.Deployment{ | ||
ObjectMeta: v1.ObjectMeta{ | ||
Name: "test-deployment", | ||
Annotations: map[string]string{ | ||
"kube-image-prefetch/ignore-containers": "initcontainer-1, container-1", | ||
}, | ||
}, | ||
Spec: appsv1.DeploymentSpec{ | ||
Template: corev1.PodTemplateSpec{ | ||
Spec: corev1.PodSpec{ | ||
InitContainers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "initcontainer-1", | ||
Image: "initimage:1", | ||
}, | ||
}, | ||
Containers: []corev1.Container{ | ||
corev1.Container{ | ||
Name: "container-1", | ||
Image: "image:1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
expected := []string{} | ||
actual := getImages(dp) | ||
|
||
if ! reflect.DeepEqual(expected, actual) { | ||
t.Errorf("Unexpected images returned - %v", actual) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters