123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /*
- 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"
- "fmt"
- "strings"
- v1 "github.com/kakao/bluegreen/api/v1"
- appsv1 "k8s.io/api/apps/v1"
- corev1 "k8s.io/api/core/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "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 {
- client.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
- svc := &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: req.Name}}
- if _, err := ctrl.CreateOrUpdate(ctx, r.Client, svc,
- r.mutateBlueGreenService(bluegreen, svc, req.Name, bluegreen.Spec.RouteTo),
- ); err != nil {
- return ctrl.Result{}, err
- }
- // Create or Update BlueDeployment
- for _, tgt := range []struct {
- Phase v1.BlueOrGreen
- Spec *corev1.PodSpec
- }{
- {Phase: v1.Blue, Spec: bluegreen.Spec.BlueSpec},
- {Phase: v1.Green, Spec: bluegreen.Spec.GreenSpec},
- } {
- if tgt.Spec == nil {
- continue
- }
- deploy := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: deploymentName(req.Name, tgt.Phase)}}
- if _, err := ctrl.CreateOrUpdate(ctx, r.Client, deploy, func() error {
- if err := ctrl.SetControllerReference(bluegreen, deploy, r.Scheme); err != nil {
- return err
- }
- label := map[string]string{
- "app.kubernetes.io/managed-by": "app.demo.kakao.com",
- "app.kubernetes.io/name": req.Name,
- "app.kubernetes.io/phase": string(tgt.Phase),
- }
- deploy.Spec.Selector = &metav1.LabelSelector{MatchLabels: label}
- deploy.Spec.Template = corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{Labels: label},
- Spec: *tgt.Spec,
- }
- return nil
- }); err != nil {
- return ctrl.Result{}, err
- }
- }
- bluegreen.Status = v1.BlueGreenStatus{
- RouteTo: &bluegreen.Spec.RouteTo,
- }
- return ctrl.Result{}, r.Client.Status().Update(ctx, bluegreen)
- }
- // 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)
- }
- func deploymentName(name string, phase v1.BlueOrGreen) string {
- return fmt.Sprintf("%s-%s", name, strings.ToLower(string(phase)))
- }
- func (r *BlueGreenReconciler) mutateBlueGreenService(owner metav1.Object, svc *corev1.Service, appName string, routeTo v1.BlueOrGreen) func() error {
- return func() error {
- if err := ctrl.SetControllerReference(owner, svc, r.Scheme); err != nil {
- return err
- }
- svc.Spec.Ports = []corev1.ServicePort{
- {Name: "http", Protocol: corev1.ProtocolTCP, Port: 80},
- }
- svc.Spec.Selector = map[string]string{
- "app.kubernetes.io/managed-by": "app.demo.kakao.com",
- "app.kubernetes.io/name": appName,
- "app.kubernetes.io/phase": string(routeTo),
- }
- return nil
- }
- }
|