123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /*
- 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("event", "name", req.Name, "namespace", req.Namespace)
- bluegreen := new(v1.BlueGreen)
- if err := r.Client.Get(ctx, req.NamespacedName, bluegreen); err != nil {
- log.Error(err, "unable to fetch BlueGreen Resource")
- 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, func() error {
- if err := ctrl.SetControllerReference(bluegreen, 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": req.Name,
- "app.kubernetes.io/phase": string(bluegreen.Spec.RouteTo),
- }
- return nil
- }); err != nil {
- return ctrl.Result{}, err
- }
- // Create or Update BlueDeployment
- for _, bg := range []v1.BlueOrGreen{v1.Blue, v1.Green} {
- spec := bluegreen.Spec.BlueSpec
- if bg == v1.Green {
- spec = bluegreen.Spec.GreenSpec
- }
- if spec == nil {
- continue
- }
- deploy := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Namespace: req.Namespace, Name: deploymentName(req.Name, bg)}}
- 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(bg),
- }
- deploy.Spec.Selector = &metav1.LabelSelector{MatchLabels: label}
- deploy.Spec.Template = corev1.PodTemplateSpec{
- ObjectMeta: metav1.ObjectMeta{Labels: label},
- Spec: *spec,
- }
- return nil
- }); err != nil {
- return ctrl.Result{}, err
- }
- }
- return ctrl.Result{}, nil
- }
- // 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, bg v1.BlueOrGreen) string {
- return fmt.Sprintf("%s-%s", name, strings.ToLower(string(bg)))
- }
|