bluegreen_controller.go 3.0 KB

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