1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- /*
- Copyright 2022.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package controllers
- import (
- "context"
- v1 "github.com/kakao/bluegreen/api/v1"
- "github.com/kakao/bluegreen/controllers/bluegreen"
- "k8s.io/apimachinery/pkg/runtime"
- ctrl "sigs.k8s.io/controller-runtime"
- "sigs.k8s.io/controller-runtime/pkg/client"
- "sigs.k8s.io/controller-runtime/pkg/log"
- )
- // BlueGreenReconciler reconciles a BlueGreen object
- type BlueGreenReconciler struct {
- bluegreen.Client
- Scheme *runtime.Scheme
- }
- //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens,verbs=get;list;watch;create;update;patch;delete
- //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens/status,verbs=get;update;patch
- //+kubebuilder:rbac:groups=app.demo.kakao.com,resources=bluegreens/finalizers,verbs=update
- //+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;watch;create;update;patch
- //+kubebuilder:rbac:groups=,resources=services,verbs=get;create;update;patch
- func (r *BlueGreenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
- log := log.FromContext(ctx)
- log.Info("Reconcile Start", "name", req.Name, "namespace", req.Namespace)
- defer log.Info("Reconcile Finished", "name", req.Name, "namespace", req.Namespace)
- bluegreen := new(v1.BlueGreen)
- if err := r.Client.Get(ctx, req.NamespacedName, bluegreen); err != nil {
- return ctrl.Result{}, client.IgnoreNotFound(err)
- }
- // Create or Update Service
- if err := r.CreateOrUpdateService(ctx, bluegreen); err != nil {
- return ctrl.Result{}, err
- }
- // Create or Update BlueDeployment
- if spec := bluegreen.Spec.BlueSpec; spec != nil {
- if err := r.CreateOrUpdateDeployment(ctx, bluegreen, v1.Blue, *spec); err != nil {
- return ctrl.Result{}, err
- }
- }
- // Create or Update GreenDeployment
- if spec := bluegreen.Spec.GreenSpec; spec != nil {
- if err := r.CreateOrUpdateDeployment(ctx, bluegreen, v1.Green, *spec); err != nil {
- return ctrl.Result{}, err
- }
- }
- bluegreen.Status = v1.BlueGreenStatus{
- RouteTo: &bluegreen.Spec.RouteTo,
- }
- return ctrl.Result{}, r.Client.UpdateStatus(ctx, req, bluegreen.Status)
- }
- // SetupWithManager sets up the controller with the Manager.
- func (r *BlueGreenReconciler) SetupWithManager(mgr ctrl.Manager) error {
- return ctrl.NewControllerManagedBy(mgr).
- For(&v1.BlueGreen{}).
- Complete(r)
- }
|