bluegreen_controller.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. v1 "github.com/kakao/bluegreen/api/v1"
  17. "github.com/kakao/bluegreen/controllers/bluegreen"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. ctrl "sigs.k8s.io/controller-runtime"
  20. "sigs.k8s.io/controller-runtime/pkg/client"
  21. "sigs.k8s.io/controller-runtime/pkg/log"
  22. )
  23. // BlueGreenReconciler reconciles a BlueGreen object
  24. type BlueGreenReconciler struct {
  25. bluegreen.Client
  26. Scheme *runtime.Scheme
  27. }
  28. //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens,verbs=get;list;watch;create;update;patch;delete
  29. //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens/status,verbs=get;update;patch
  30. //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens/finalizers,verbs=update
  31. //+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;watch;create;update;patch
  32. //+kubebuilder:rbac:groups=,resources=services,verbs=get;create;update;patch
  33. func (r *BlueGreenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  34. log := log.FromContext(ctx)
  35. log.Info("Reconcile Start", "name", req.Name, "namespace", req.Namespace)
  36. defer log.Info("Reconcile Finished", "name", req.Name, "namespace", req.Namespace)
  37. bluegreen := new(v1.BlueGreen)
  38. if err := r.Client.Get(ctx, req.NamespacedName, bluegreen); err != nil {
  39. return ctrl.Result{}, client.IgnoreNotFound(err)
  40. }
  41. // Create or Update Service
  42. if err := r.CreateOrUpdateService(ctx, bluegreen); err != nil {
  43. return ctrl.Result{}, err
  44. }
  45. // Create or Update BlueDeployment
  46. if spec := bluegreen.Spec.BlueSpec; spec != nil {
  47. if err := r.CreateOrUpdateDeployment(ctx, bluegreen, v1.Blue, *spec); err != nil {
  48. return ctrl.Result{}, err
  49. }
  50. }
  51. // Create or Update GreenDeployment
  52. if spec := bluegreen.Spec.GreenSpec; spec != nil {
  53. if err := r.CreateOrUpdateDeployment(ctx, bluegreen, v1.Green, *spec); err != nil {
  54. return ctrl.Result{}, err
  55. }
  56. }
  57. bluegreen.Status = v1.BlueGreenStatus{
  58. RouteTo: &bluegreen.Spec.RouteTo,
  59. }
  60. return ctrl.Result{}, r.Client.UpdateStatus(ctx, req, bluegreen.Status)
  61. }
  62. // SetupWithManager sets up the controller with the Manager.
  63. func (r *BlueGreenReconciler) SetupWithManager(mgr ctrl.Manager) error {
  64. return ctrl.NewControllerManagedBy(mgr).
  65. For(&v1.BlueGreen{}).
  66. Complete(r)
  67. }