iwanhae il y a 2 ans
Parent
commit
4d0695470a
3 fichiers modifiés avec 86 ajouts et 15 suppressions
  1. 20 14
      controllers/bluegreen_controller.go
  2. 63 0
      controllers/bluegreen_controller_test.go
  3. 3 1
      go.mod

+ 20 - 14
controllers/bluegreen_controller.go

@@ -56,20 +56,9 @@ func (r *BlueGreenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
 
 	// Create or Update Service
 	svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: req.Name}}
-	if _, err := ctrl.CreateOrUpdate(ctx, r.Client, svc, func() error {
-		if err := ctrl.SetControllerReference(bluegreen, svc, r.Scheme); err != nil {
-			return err
-		}
-		svc.Spec.Ports = []corev1.ServicePort{
-			{Name: "http", Protocol: corev1.ProtocolTCP, Port: 80},
-		}
-		svc.Spec.Selector = map[string]string{
-			"app.kubernetes.io/managed-by": "app.demo.kakao.com",
-			"app.kubernetes.io/name":       req.Name,
-			"app.kubernetes.io/phase":      string(bluegreen.Spec.RouteTo),
-		}
-		return nil
-	}); err != nil {
+	if _, err := ctrl.CreateOrUpdate(ctx, r.Client, svc,
+		r.mutateBlueGreenService(bluegreen, svc, req.Name, bluegreen.Spec.RouteTo),
+	); err != nil {
 		return ctrl.Result{}, err
 	}
 
@@ -121,3 +110,20 @@ func (r *BlueGreenReconciler) SetupWithManager(mgr ctrl.Manager) error {
 func deploymentName(name string, phase v1.BlueOrGreen) string {
 	return fmt.Sprintf("%s-%s", name, strings.ToLower(string(phase)))
 }
+
+func (r *BlueGreenReconciler) mutateBlueGreenService(owner metav1.Object, svc *corev1.Service, appName string, routeTo v1.BlueOrGreen) func() error {
+	return func() error {
+		if err := ctrl.SetControllerReference(owner, svc, r.Scheme); err != nil {
+			return err
+		}
+		svc.Spec.Ports = []corev1.ServicePort{
+			{Name: "http", Protocol: corev1.ProtocolTCP, Port: 80},
+		}
+		svc.Spec.Selector = map[string]string{
+			"app.kubernetes.io/managed-by": "app.demo.kakao.com",
+			"app.kubernetes.io/name":       appName,
+			"app.kubernetes.io/phase":      string(routeTo),
+		}
+		return nil
+	}
+}

+ 63 - 0
controllers/bluegreen_controller_test.go

@@ -0,0 +1,63 @@
+/*
+Copyright 2022.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package controllers
+
+import (
+	"testing"
+
+	appv1 "github.com/kakao/bluegreen/api/v1"
+	v1 "github.com/kakao/bluegreen/api/v1"
+	"github.com/stretchr/testify/assert"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/runtime"
+	utilruntime "k8s.io/apimachinery/pkg/util/runtime"
+	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
+	"sigs.k8s.io/controller-runtime/pkg/client/fake"
+)
+
+var (
+	testScheme = runtime.NewScheme()
+)
+
+func init() {
+	utilruntime.Must(clientgoscheme.AddToScheme(testScheme))
+	utilruntime.Must(appv1.AddToScheme(testScheme))
+}
+
+func TestBlueGreenReconciler_mutateBlueGreenService(t *testing.T) {
+	t.Run("Should add proper labels and OwnerReferences", func(t *testing.T) {
+		r := &BlueGreenReconciler{
+			Client: fake.NewClientBuilder().WithScheme(testScheme).WithRuntimeObjects().Build(),
+			Scheme: testScheme,
+		}
+
+		const Name = "bluegreen-test"
+		const Namespace = "test"
+		svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: Namespace, Name: Name}}
+		err := r.mutateBlueGreenService(
+			&v1.BlueGreen{ObjectMeta: metav1.ObjectMeta{Namespace: Namespace, Name: Name, UID: "0000-0000"}},
+			svc, "test", appv1.Blue)()
+
+		assert.NoError(t, err)
+		assert.Equal(t, 1, len(svc.ObjectMeta.OwnerReferences))
+		assert.Equal(t, "app.demo.kakao.com", svc.Spec.Selector["app.kubernetes.io/managed-by"])
+		assert.Equal(t, "Blue", svc.Spec.Selector["app.kubernetes.io/phase"])
+		assert.Equal(t, "test", svc.Spec.Selector["app.kubernetes.io/name"])
+		/* more validation here */
+	})
+}

+ 3 - 1
go.mod

@@ -5,6 +5,8 @@ go 1.18
 require (
 	github.com/onsi/ginkgo v1.16.5
 	github.com/onsi/gomega v1.18.1
+	github.com/stretchr/testify v1.7.0
+	k8s.io/api v0.24.2
 	k8s.io/apimachinery v0.24.2
 	k8s.io/client-go v0.24.2
 	sigs.k8s.io/controller-runtime v0.12.2
@@ -49,6 +51,7 @@ require (
 	github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
 	github.com/nxadm/tail v1.4.8 // indirect
 	github.com/pkg/errors v0.9.1 // indirect
+	github.com/pmezard/go-difflib v1.0.0 // indirect
 	github.com/prometheus/client_golang v1.12.1 // indirect
 	github.com/prometheus/client_model v0.2.0 // indirect
 	github.com/prometheus/common v0.32.1 // indirect
@@ -71,7 +74,6 @@ require (
 	gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
 	gopkg.in/yaml.v2 v2.4.0 // indirect
 	gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
-	k8s.io/api v0.24.2 // indirect
 	k8s.io/apiextensions-apiserver v0.24.2 // indirect
 	k8s.io/component-base v0.24.2 // indirect
 	k8s.io/klog/v2 v2.60.1 // indirect