bluegreen_controller.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if err := r.CreateOrUpdateService(ctx, bluegreen); err != nil {
  47. return ctrl.Result{}, err
  48. }
  49. // Create or Update BlueDeployment
  50. for _, tgt := range []struct {
  51. Phase v1.BlueOrGreen
  52. Spec *corev1.PodSpec
  53. }{
  54. {Phase: v1.Blue, Spec: bluegreen.Spec.BlueSpec},
  55. {Phase: v1.Green, Spec: bluegreen.Spec.GreenSpec},
  56. } {
  57. if tgt.Spec == nil {
  58. continue
  59. }
  60. deploy := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: deploymentName(req.Name, tgt.Phase)}}
  61. if _, err := ctrl.CreateOrUpdate(ctx, r.Client, deploy, func() error {
  62. if err := ctrl.SetControllerReference(bluegreen, deploy, r.Scheme); err != nil {
  63. return err
  64. }
  65. label := map[string]string{
  66. "app.kubernetes.io/managed-by": "app.demo.kakao.com",
  67. "app.kubernetes.io/name": req.Name,
  68. "app.kubernetes.io/phase": string(tgt.Phase),
  69. }
  70. deploy.Spec.Selector = &metav1.LabelSelector{MatchLabels: label}
  71. deploy.Spec.Template = corev1.PodTemplateSpec{
  72. ObjectMeta: metav1.ObjectMeta{Labels: label},
  73. Spec: *tgt.Spec,
  74. }
  75. return nil
  76. }); err != nil {
  77. return ctrl.Result{}, err
  78. }
  79. }
  80. bluegreen.Status = v1.BlueGreenStatus{
  81. RouteTo: &bluegreen.Spec.RouteTo,
  82. }
  83. return ctrl.Result{}, r.Client.Status().Update(ctx, bluegreen)
  84. }
  85. // SetupWithManager sets up the controller with the Manager.
  86. func (r *BlueGreenReconciler) SetupWithManager(mgr ctrl.Manager) error {
  87. return ctrl.NewControllerManagedBy(mgr).
  88. For(&v1.BlueGreen{}).
  89. Complete(r)
  90. }
  91. func (r *BlueGreenReconciler) CreateOrUpdateService(ctx context.Context, owner *v1.BlueGreen) error {
  92. svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: owner.Namespace, Name: owner.Name}}
  93. if _, err := ctrl.CreateOrUpdate(ctx, r.Client, svc,
  94. func() error {
  95. if err := ctrl.SetControllerReference(owner, svc, r.Scheme); err != nil {
  96. return err
  97. }
  98. svc.Spec.Ports = []corev1.ServicePort{
  99. {Name: "http", Protocol: corev1.ProtocolTCP, Port: 80},
  100. }
  101. svc.Spec.Selector = map[string]string{
  102. "app.kubernetes.io/managed-by": "app.demo.kakao.com",
  103. "app.kubernetes.io/name": owner.Name,
  104. "app.kubernetes.io/phase": string(owner.Spec.RouteTo),
  105. }
  106. return nil
  107. },
  108. ); err != nil {
  109. return err
  110. }
  111. return nil
  112. }
  113. func deploymentName(name string, phase v1.BlueOrGreen) string {
  114. return fmt.Sprintf("%s-%s", name, strings.ToLower(string(phase)))
  115. }