bluegreen_controller.go 4.0 KB

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