Repository: appvia/kev
Pull request #498 (2021-04-29 16:27:44)

kev-458 move imagepull labels


Patch:
diff --git a/pkg/kev/config/extensions.go b/pkg/kev/config/extensions.go
index 2ca7c80..466d8fc 100644
--- a/pkg/kev/config/extensions.go
+++ b/pkg/kev/config/extensions.go
@@ -80,6 +80,9 @@ func DefaultK8SConfig() K8SConfiguration {
 			ReadinessProbe: DefaultReadinessProbe(),
 			Replicas:       1,
 			RestartPolicy:  RestartPolicyAlways,
+			ImagePull: ImagePull{
+				Policy: DefaultImagePullPolicy,
+			},
 		},
 		Service: Service{
 			Type: "None",
@@ -123,6 +126,13 @@ func K8SCfgFromCompose(svc *composego.ServiceConfig) (K8SConfiguration, error) {
 	cfg.Workload.LivenessProbe = LivenessProbeFromCompose(svc)
 	cfg.Workload.ReadinessProbe = DefaultReadinessProbe()
 
+	imagePull, err := ImagePullFromCompose(svc)
+	if err != nil {
+		return K8SConfiguration{}, err
+	}
+
+	cfg.Workload.ImagePull = imagePull
+
 	k8sext, err := ParseK8SCfgFromMap(svc.Extensions, DisableValidation())
 	if err != nil {
 		return K8SConfiguration{}, err
@@ -140,6 +150,13 @@ func K8SCfgFromCompose(svc *composego.ServiceConfig) (K8SConfiguration, error) {
 	return cfg, nil
 }
 
+func ImagePullFromCompose(svc *composego.ServiceConfig) (ImagePull, error) {
+	return ImagePull{
+		Policy: DefaultImagePullPolicy,
+		Secret: DefaultImagePullSecret,
+	}, nil
+}
+
 func ServiceTypeFromCompose(svc *composego.ServiceConfig) (string, error) {
 	serviceType := NoService
 
@@ -309,6 +326,12 @@ type Workload struct {
 	LivenessProbe  LivenessProbe  `yaml:"livenessProbe" validate:"required"`
 	ReadinessProbe ReadinessProbe `yaml:"readinessProbe,omitempty"`
 	RestartPolicy  string         `yaml:"restartPolicy,omitempty"`
+	ImagePull      ImagePull      `yaml:"imagePull,omitempty"`
+}
+
+type ImagePull struct {
+	Policy string `yaml:"policy,omitempty" validate:"oneof='' IfNotPresent Never Always"`
+	Secret string `yaml:"secret,omitempty"`
 }
 
 // Service will hold the service specific extensions in the future.
diff --git a/pkg/kev/config/labels.go b/pkg/kev/config/labels.go
index a40456a..46ebdce 100644
--- a/pkg/kev/config/labels.go
+++ b/pkg/kev/config/labels.go
@@ -52,12 +52,6 @@ const (
 	// LabelWorkloadSecurityContextFsGroup sets pod security context FsGroup attribute
 	LabelWorkloadSecurityContextFsGroup = LabelPrefix + "workload.pod-security-fs-group"
 
-	// LabelWorkloadImagePullPolicy defines when to pull images from registry
-	LabelWorkloadImagePullPolicy = LabelPrefix + "workload.image-pull-policy"
-
-	// LabelWorkloadImagePullSecret defines docker registry image pull secret
-	LabelWorkloadImagePullSecret = LabelPrefix + "workload.image-pull-secret"
-
 	// LabelWorkloadServiceAccountName defines service account name to be used by the workload
 	LabelWorkloadServiceAccountName = LabelPrefix + "workload.service-account-name"
 
diff --git a/pkg/kev/converter/kubernetes/project_service.go b/pkg/kev/converter/kubernetes/project_service.go
index 5751a17..d9261e4 100644
--- a/pkg/kev/converter/kubernetes/project_service.go
+++ b/pkg/kev/converter/kubernetes/project_service.go
@@ -366,33 +366,12 @@ func (p *ProjectService) fsGroup() string {
 
 // imagePullPolicy returns image PullPolicy for project service
 func (p *ProjectService) imagePullPolicy() v1.PullPolicy {
-	policy := config.DefaultImagePullPolicy
-
-	if val, ok := p.Labels[config.LabelWorkloadImagePullPolicy]; ok {
-		policy = val
-	}
-
-	pullPolicy, err := getImagePullPolicy(p.Name, policy)
-	if err != nil {
-		log.WarnfWithFields(log.Fields{
-			"project-service":   p.Name,
-			"image-pull-policy": policy,
-		}, "Invalid image pull policy passed in via %s label. Defaulting to `IfNotPresent`.",
-			config.LabelWorkloadImagePullPolicy)
-
-		return v1.PullPolicy(config.DefaultImagePullPolicy)
-	}
-
-	return pullPolicy
+	return v1.PullPolicy(p.K8SConfig.Workload.ImagePull.Policy)
 }
 
 // imagePullSecret returns image pull secret (for private registries)
 func (p *ProjectService) imagePullSecret() string {
-	if val, ok := p.Labels[config.LabelWorkloadImagePullSecret]; ok {
-		return val
-	}
-
-	return config.DefaultImagePullSecret
+	return p.K8SConfig.Workload.ImagePull.Secret
 }
 
 // serviceAccountName returns service account name to be used by the pod
diff --git a/pkg/kev/extract.go b/pkg/kev/extract.go
index efa48bb..cd01a3e 100644
--- a/pkg/kev/extract.go
+++ b/pkg/kev/extract.go
@@ -28,7 +28,6 @@ import (
 
 // setDefaultLabels sets sensible workload defaults as labels.
 func setDefaultLabels(target *ServiceConfig) {
-	target.Labels.Add(config.LabelWorkloadImagePullPolicy, config.DefaultImagePullPolicy)
 	target.Labels.Add(config.LabelWorkloadServiceAccountName, config.DefaultServiceAccountName)
 }
 


Test patch:
diff --git a/pkg/kev/converter/kubernetes/project_service_test.go b/pkg/kev/converter/kubernetes/project_service_test.go
index 4fc6124..1b2de67 100644
--- a/pkg/kev/converter/kubernetes/project_service_test.go
+++ b/pkg/kev/converter/kubernetes/project_service_test.go
@@ -850,9 +850,7 @@ var _ = Describe("ProjectService", func() {
 			policy := "Always"
 
 			BeforeEach(func() {
-				labels = composego.Labels{
-					config.LabelWorkloadImagePullPolicy: policy,
-				}
+				k8sconf.Workload.ImagePull.Policy = policy
 			})
 
 			It("returns label value", func() {
@@ -868,23 +866,22 @@ var _ = Describe("ProjectService", func() {
 
 		Context("for invalid image pull policy", func() {
 			policy := "invalid-policy-name"
+			extensions := make(map[string]interface{})
 
-			BeforeEach(func() {
-				labels = composego.Labels{
-					config.LabelWorkloadImagePullPolicy: policy,
-				}
+			JustBeforeEach(func() {
+				k8sconf.Workload.ImagePull.Policy = policy
+				m, err := k8sconf.ToMap()
+				Expect(err).NotTo(HaveOccurred())
+
+				extensions[config.K8SExtensionKey] = m
 			})
 
 			It("warn the user and returns default value", func() {
-				Expect(projectService.imagePullPolicy()).To(Equal(v1.PullPolicy(config.DefaultImagePullPolicy)))
-
-				assertLog(logrus.WarnLevel,
-					fmt.Sprintf("Invalid image pull policy passed in via %s label. Defaulting to `IfNotPresent`.", config.LabelWorkloadImagePullPolicy),
-					map[string]string{
-						"project-service":   projectServiceName,
-						"image-pull-policy": policy,
-					},
-				)
+				_, err := NewProjectService(composego.ServiceConfig{
+					Extensions: extensions,
+				})
+				Expect(err).To(HaveOccurred())
+				Expect(err.Error()).To(ContainSubstring("K8SConfiguration.Workload.ImagePull.Policy"))
 			})
 		})
 	})
@@ -895,9 +892,7 @@ var _ = Describe("ProjectService", func() {
 			secret := "image-pull-secret"
 
 			BeforeEach(func() {
-				labels = composego.Labels{
-					config.LabelWorkloadImagePullSecret: secret,
-				}
+				k8sconf.Workload.ImagePull.Secret = secret
 			})
 
 			It("returns label value", func() {
diff --git a/pkg/kev/converter/kubernetes/transform_test.go b/pkg/kev/converter/kubernetes/transform_test.go
index 43724e5..422bfb8 100644
--- a/pkg/kev/converter/kubernetes/transform_test.go
+++ b/pkg/kev/converter/kubernetes/transform_test.go
@@ -132,9 +132,18 @@ var _ = Describe("Transform", func() {
 
 		Context("with imaged pull secret specified via labels", func() {
 			BeforeEach(func() {
-				projectService.Labels = composego.Labels{
-					config.LabelWorkloadImagePullSecret: "my-pp-secret",
+				k8sconf := config.DefaultK8SConfig()
+				k8sconf.Workload.ImagePull.Secret = "my-pp-secret"
+
+				m, err := k8sconf.ToMap()
+				Expect(err).NotTo(HaveOccurred())
+
+				projectService.Extensions = map[string]interface{}{
+					config.K8SExtensionKey: m,
 				}
+
+				projectService, err = NewProjectService(projectService.ServiceConfig)
+				Expect(err).NotTo(HaveOccurred())
 			})
 
 			It("uses passed image pull secret in the spec", func() {
diff --git a/pkg/kev/kev_reconcile_test.go b/pkg/kev/kev_reconcile_test.go
index a4c9768..ed98f3a 100644
--- a/pkg/kev/kev_reconcile_test.go
+++ b/pkg/kev/kev_reconcile_test.go
@@ -550,6 +550,9 @@ func newDefaultServiceExtensions(name string, k8sconfs ...config.K8SConfiguratio
 			Type:           config.DefaultWorkload,
 			Replicas:       config.DefaultReplicaNumber,
 			RestartPolicy:  "always",
+			ImagePull: config.ImagePull{
+				Policy: config.DefaultImagePullPolicy,
+			},
 		},
 	}
 
diff --git a/pkg/kev/testdata/init-default/compose-yaml/output.yaml b/pkg/kev/testdata/init-default/compose-yaml/output.yaml
index 18597b8..e7725f8 100644
--- a/pkg/kev/testdata/init-default/compose-yaml/output.yaml
+++ b/pkg/kev/testdata/init-default/compose-yaml/output.yaml
@@ -6,6 +6,8 @@ services:
       service:
         type: None
       workload:
+        imagePull:
+          policy: IfNotPresent
         livenessProbe:
           exec:
             command: