bluegreen_controller.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright 2022.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package controllers
  14. import (
  15. "context"
  16. "fmt"
  17. "strings"
  18. v1 "github.com/kakao/bluegreen/api/v1"
  19. appsv1 "k8s.io/api/apps/v1"
  20. corev1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/runtime"
  23. ctrl "sigs.k8s.io/controller-runtime"
  24. "sigs.k8s.io/controller-runtime/pkg/client"
  25. "sigs.k8s.io/controller-runtime/pkg/log"
  26. )
  27. // BlueGreenReconciler reconciles a BlueGreen object
  28. type BlueGreenReconciler struct {
  29. client.Client
  30. Scheme *runtime.Scheme
  31. }
  32. //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens,verbs=get;list;watch;create;update;patch;delete
  33. //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens/status,verbs=get;update;patch
  34. //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens/finalizers,verbs=update
  35. //+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;watch;create;update;patch
  36. //+kubebuilder:rbac:groups=,resources=services,verbs=get;create;update;patch
  37. func (r *BlueGreenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  38. log := log.FromContext(ctx)
  39. log.Info("Reconcile Start", "name", req.Name, "namespace", req.Namespace)
  40. defer log.Info("Reconcile Finished", "name", req.Name, "namespace", req.Namespace)
  41. bluegreen := new(v1.BlueGreen)
  42. if err := r.Client.Get(ctx, req.NamespacedName, bluegreen); err != nil {
  43. return ctrl.Result{}, client.IgnoreNotFound(err)
  44. }
  45. // Create or Update Service
  46. svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: req.Name}}
  47. if _, err := ctrl.CreateOrUpdate(ctx, r.Client, svc,
  48. r.mutateBlueGreenService(bluegreen, svc, req.Name, bluegreen.Spec.RouteTo),
  49. ); err != nil {
  50. return ctrl.Result{}, err
  51. }
  52. // Create or Update BlueDeployment
  53. for _, tgt := range []struct {
  54. Phase v1.BlueOrGreen
  55. Spec *corev1.PodSpec
  56. }{
  57. {Phase: v1.Blue, Spec: bluegreen.Spec.BlueSpec},
  58. {Phase: v1.Green, Spec: bluegreen.Spec.GreenSpec},
  59. } {
  60. if tgt.Spec == nil {
  61. continue
  62. }
  63. deploy := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: deploymentName(req.Name, tgt.Phase)}}
  64. if _, err := ctrl.CreateOrUpdate(ctx, r.Client, deploy, func() error {
  65. if err := ctrl.SetControllerReference(bluegreen, deploy, r.Scheme); err != nil {
  66. return err
  67. }
  68. label := map[string]string{
  69. "app.kubernetes.io/managed-by": "app.demo.kakao.com",
  70. "app.kubernetes.io/name": req.Name,
  71. "app.kubernetes.io/phase": string(tgt.Phase),
  72. }
  73. deploy.Spec.Selector = &metav1.LabelSelector{MatchLabels: label}
  74. deploy.Spec.Template = corev1.PodTemplateSpec{
  75. ObjectMeta: metav1.ObjectMeta{Labels: label},
  76. Spec: *tgt.Spec,
  77. }
  78. return nil
  79. }); err != nil {
  80. return ctrl.Result{}, err
  81. }
  82. }
  83. bluegreen.Status = v1.BlueGreenStatus{
  84. RouteTo: &bluegreen.Spec.RouteTo,
  85. }
  86. return ctrl.Result{}, r.Client.Status().Update(ctx, bluegreen)
  87. }
  88. // SetupWithManager sets up the controller with the Manager.
  89. func (r *BlueGreenReconciler) SetupWithManager(mgr ctrl.Manager) error {
  90. return ctrl.NewControllerManagedBy(mgr).
  91. For(&v1.BlueGreen{}).
  92. Complete(r)
  93. }
  94. func deploymentName(name string, phase v1.BlueOrGreen) string {
  95. return fmt.Sprintf("%s-%s", name, strings.ToLower(string(phase)))
  96. }
  97. func (r *BlueGreenReconciler) mutateBlueGreenService(owner metav1.Object, svc *corev1.Service, appName string, routeTo v1.BlueOrGreen) func() error {
  98. return func() error {
  99. if err := ctrl.SetControllerReference(owner, svc, r.Scheme); err != nil {
  100. return err
  101. }
  102. svc.Spec.Ports = []corev1.ServicePort{
  103. {Name: "http", Protocol: corev1.ProtocolTCP, Port: 80},
  104. }
  105. svc.Spec.Selector = map[string]string{
  106. "app.kubernetes.io/managed-by": "app.demo.kakao.com",
  107. "app.kubernetes.io/name": appName,
  108. "app.kubernetes.io/phase": string(routeTo),
  109. }
  110. return nil
  111. }
  112. }