bluegreen_controller.go 3.9 KB

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