Browse Source

Before Test Code

iwanhae 2 năm trước cách đây
mục cha
commit
78d5bb0401
1 tập tin đã thay đổi với 13 bổ sung11 xóa
  1. 13 11
      controllers/bluegreen_controller.go

+ 13 - 11
controllers/bluegreen_controller.go

@@ -74,15 +74,17 @@ func (r *BlueGreenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
 	}
 
 	// Create or Update BlueDeployment
-	for _, bg := range []v1.BlueOrGreen{v1.Blue, v1.Green} {
-		spec := bluegreen.Spec.BlueSpec
-		if bg == v1.Green {
-			spec = bluegreen.Spec.GreenSpec
-		}
-		if spec == nil {
+	for _, tgt := range []struct {
+		Phase v1.BlueOrGreen
+		Spec  *corev1.PodSpec
+	}{
+		{Phase: v1.Blue, Spec: bluegreen.Spec.BlueSpec},
+		{Phase: v1.Green, Spec: bluegreen.Spec.GreenSpec},
+	} {
+		if tgt.Spec == nil {
 			continue
 		}
-		deploy := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: deploymentName(req.Name, bg)}}
+		deploy := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: deploymentName(req.Name, tgt.Phase)}}
 		if _, err := ctrl.CreateOrUpdate(ctx, r.Client, deploy, func() error {
 			if err := ctrl.SetControllerReference(bluegreen, deploy, r.Scheme); err != nil {
 				return err
@@ -90,12 +92,12 @@ func (r *BlueGreenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
 			label := map[string]string{
 				"app.kubernetes.io/managed-by": "app.demo.kakao.com",
 				"app.kubernetes.io/name":       req.Name,
-				"app.kubernetes.io/phase":      string(bg),
+				"app.kubernetes.io/phase":      string(tgt.Phase),
 			}
 			deploy.Spec.Selector = &metav1.LabelSelector{MatchLabels: label}
 			deploy.Spec.Template = corev1.PodTemplateSpec{
 				ObjectMeta: metav1.ObjectMeta{Labels: label},
-				Spec:       *spec,
+				Spec:       *tgt.Spec,
 			}
 			return nil
 		}); err != nil {
@@ -116,6 +118,6 @@ func (r *BlueGreenReconciler) SetupWithManager(mgr ctrl.Manager) error {
 		Complete(r)
 }
 
-func deploymentName(name string, bg v1.BlueOrGreen) string {
-	return fmt.Sprintf("%s-%s", name, strings.ToLower(string(bg)))
+func deploymentName(name string, phase v1.BlueOrGreen) string {
+	return fmt.Sprintf("%s-%s", name, strings.ToLower(string(phase)))
 }