Repository: kedacore/http-add-on
Pull request #161 (2021-05-06 23:47:39)

validating pull policies
Validate pull policies as #155 didn't


Patch:
diff --git a/operator/controllers/config/config.go b/operator/controllers/config/config.go
index f37799f..0b2d4e9 100644
--- a/operator/controllers/config/config.go
+++ b/operator/controllers/config/config.go
@@ -4,6 +4,7 @@ import (
 	"fmt"
 
 	"github.com/kedacore/http-add-on/pkg/env"
+	corev1 "k8s.io/api/core/v1"
 )
 
 // Interceptor holds static configuration info for the interceptor
@@ -11,7 +12,16 @@ type Interceptor struct {
 	Image     string
 	ProxyPort int32
 	AdminPort int32
-	PullPolicy string
+	PullPolicy corev1.PullPolicy
+}
+
+func ensureValidPolicy (policy string) error {
+	converted := corev1.PullPolicy(policy)
+	switch (converted) {
+	case corev1.PullAlways, corev1.PullIfNotPresent, corev1.PullNever:
+		return nil
+	}
+	return fmt.Errorf("Policy %q is not a valid Pull Policy. Accepted values are: %s, %s, %s", policy, corev1.PullAlways, corev1.PullIfNotPresent, corev1.PullNever)
 }
 
 // NewInterceptorFromEnv gets interceptor configuration values from environment variables and/or
@@ -25,11 +35,15 @@ func NewInterceptorFromEnv() (*Interceptor, error) {
 	adminPort := env.GetInt32Or("KEDAHTTP_OPERATOR_INTERCEPTOR_ADMIN_PORT", 8090)
 	proxyPort := env.GetInt32Or("KEDAHTTP_OPERATOR_INTERCEPTOR_PROXY_PORT", 8091)
 	pullPolicy := env.GetOr("INTERCEPTOR_PULL_POLICY", "Always")
+	if policyErr := ensureValidPolicy(pullPolicy); policyErr != nil {
+		return nil, policyErr
+	}
+
 	return &Interceptor{
 		Image:     image,
 		AdminPort: adminPort,
 		ProxyPort: proxyPort,
-		PullPolicy: pullPolicy,
+		PullPolicy: corev1.PullPolicy(pullPolicy),
 	}, nil
 }
 
@@ -37,7 +51,7 @@ func NewInterceptorFromEnv() (*Interceptor, error) {
 type ExternalScaler struct {
 	Image string
 	Port  int32
-	PullPolicy string
+	PullPolicy corev1.PullPolicy
 }
 
 // NewExternalScalerFromEnv gets external scaler configuration values from environment variables and/or
@@ -46,13 +60,18 @@ type ExternalScaler struct {
 func NewExternalScalerFromEnv() (*ExternalScaler, error) {
 	image, err := env.Get("KEDAHTTP_OPERATOR_EXTERNAL_SCALER_IMAGE")
 	port := env.GetInt32Or("KEDAHTTP_OPERATOR_EXTERNAL_SCALER_PORT", 8091)
-	pullPolicy := env.GetOr("SCALER_PULL_POLICY", "Always")
 	if err != nil {
 		return nil, fmt.Errorf("Missing KEDAHTTP_EXTERNAL_SCALER_IMAGE")
 	}
+
+	pullPolicy := env.GetOr("SCALER_PULL_POLICY", "Always")
+	if policyErr := ensureValidPolicy(pullPolicy); policyErr != nil {
+		return nil, policyErr
+	}
+
 	return &ExternalScaler{
 		Image: image,
 		Port:  port,
-		PullPolicy: pullPolicy,
+		PullPolicy: corev1.PullPolicy(pullPolicy),
 	}, nil
 }
diff --git a/pkg/k8s/deployment.go b/pkg/k8s/deployment.go
index 31f7775..caedcb4 100644
--- a/pkg/k8s/deployment.go
+++ b/pkg/k8s/deployment.go
@@ -35,7 +35,7 @@ func NewDeployment(
 	ports []int32,
 	env []corev1.EnvVar,
 	labels map[string]string,
-	pullPolicy string,
+	pullPolicy corev1.PullPolicy,
 ) *appsv1.Deployment {
 	containerPorts := make([]corev1.ContainerPort, len(ports))
 	for i, port := range ports {
@@ -66,7 +66,7 @@ func NewDeployment(
 						{
 							Image:           image,
 							Name:            name,
-							ImagePullPolicy: corev1.PullPolicy(pullPolicy),
+							ImagePullPolicy: pullPolicy,
 							Ports:           containerPorts,
 							Env:             env,
 						},


Test patch:
diff --git a/interceptor/forward_wait_func_test.go b/interceptor/forward_wait_func_test.go
index 29b4b6c..a9602e5 100644
--- a/interceptor/forward_wait_func_test.go
+++ b/interceptor/forward_wait_func_test.go
@@ -9,6 +9,7 @@ import (
 	"github.com/stretchr/testify/require"
 	"golang.org/x/sync/errgroup"
 	appsv1 "k8s.io/api/apps/v1"
+	corev1 "k8s.io/api/core/v1"
 	"k8s.io/apimachinery/pkg/watch"
 )
 
@@ -26,7 +27,7 @@ func TestForwardWaitFuncOneReplica(t *testing.T) {
 			[]int32{123},
 			nil,
 			map[string]string{},
-			"Always",
+			corev1.PullAlways,
 		),
 	})
 	waitFunc := newDeployReplicasForwardWaitFunc(
@@ -55,7 +56,7 @@ func TestForwardWaitFuncNoReplicas(t *testing.T) {
 		[]int32{123},
 		nil,
 		map[string]string{},
-		"Always",
+		corev1.PullAlways,
 	)
 	deployment.Spec.Replicas = k8s.Int32P(0)
 	cache := k8s.NewMemoryDeploymentCache(map[string]*appsv1.Deployment{
@@ -86,7 +87,7 @@ func TestWaitFuncWaitsUntilReplicas(t *testing.T) {
 		[]int32{123},
 		nil,
 		map[string]string{},
-		"Always",
+		corev1.PullAlways,
 	)
 	deployment.Spec.Replicas = k8s.Int32P(0)
 	cache := k8s.NewMemoryDeploymentCache(map[string]*appsv1.Deployment{
diff --git a/pkg/k8s/deployment_cache_test.go b/pkg/k8s/deployment_cache_test.go
index cfcc9c5..03c1814 100644
--- a/pkg/k8s/deployment_cache_test.go
+++ b/pkg/k8s/deployment_cache_test.go
@@ -7,6 +7,7 @@ import (
 
 	"github.com/stretchr/testify/require"
 	appsv1 "k8s.io/api/apps/v1"
+	core "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"k8s.io/apimachinery/pkg/runtime/schema"
 	k8sfake "k8s.io/client-go/kubernetes/fake"
@@ -25,7 +26,7 @@ func TestK8DeploymentCacheGet(t *testing.T) {
 		nil,
 		nil,
 		make(map[string]string),
-		"Always",
+		core.PullAlways,
 	)
 	fakeClientset := k8sfake.NewSimpleClientset(expectedDepl)
 	fakeApps := fakeClientset.AppsV1()
@@ -55,7 +56,7 @@ func TestK8sDeploymentCacheWatch(t *testing.T) {
 		nil,
 		nil,
 		make(map[string]string),
-		"Always",
+		core.PullAlways,
 	)
 	fakeClientset := k8sfake.NewSimpleClientset()
 	fakeDeployments := fakeClientset.AppsV1().Deployments(ns)