|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package reconciler |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/apimachinery/pkg/util/sets" |
| 29 | + |
| 30 | + ctrl "sigs.k8s.io/controller-runtime" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 32 | + "sigs.k8s.io/controller-runtime/pkg/clusterconnector" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 34 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/source" |
| 36 | +) |
| 37 | + |
| 38 | +func Add(mgr ctrl.Manager, mirrorCluster clusterconnector.ClusterConnector) error { |
| 39 | + return ctrl.NewControllerManagedBy(mgr). |
| 40 | + // Watch Pods in the reference cluster |
| 41 | + For(&corev1.Pod{}). |
| 42 | + // Watch pods in the mirror cluster |
| 43 | + Watches( |
| 44 | + source.NewKindWithCache(&corev1.Pod{}, mirrorCluster.GetCache()), |
| 45 | + &handler.EnqueueRequestForObject{}, |
| 46 | + ). |
| 47 | + Complete(&reconciler{ |
| 48 | + referenceClusterClient: mgr.GetClient(), |
| 49 | + mirrorClusterClient: mirrorCluster.GetClient(), |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +type reconciler struct { |
| 54 | + referenceClusterClient client.Client |
| 55 | + mirrorClusterClient client.Client |
| 56 | +} |
| 57 | + |
| 58 | +func (r *reconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) { |
| 59 | + return reconcile.Result{}, r.reconcile(req) |
| 60 | +} |
| 61 | + |
| 62 | +const podFinalizerName = "pod-finalzer.mirror.org/v1" |
| 63 | + |
| 64 | +func (r *reconciler) reconcile(req reconcile.Request) error { |
| 65 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 66 | + defer cancel() |
| 67 | + |
| 68 | + referencePod := &corev1.Pod{} |
| 69 | + if err := r.referenceClusterClient.Get(ctx, req.NamespacedName, referencePod); err != nil { |
| 70 | + if apierrors.IsNotFound(err) { |
| 71 | + return nil |
| 72 | + } |
| 73 | + return fmt.Errorf("failed to get pod from reference clsuster: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + if referencePod.DeletionTimestamp != nil && sets.NewString(referencePod.Finalizers...).Has(podFinalizerName) { |
| 77 | + mirrorClusterPod := &corev1.Pod{ObjectMeta: metav1.ObjectMeta{ |
| 78 | + Namespace: referencePod.Namespace, |
| 79 | + Name: referencePod.Name, |
| 80 | + }} |
| 81 | + if err := r.mirrorClusterClient.Delete(ctx, mirrorClusterPod); err != nil && !apierrors.IsNotFound(err) { |
| 82 | + return fmt.Errorf("failed to delete pod in mirror cluster: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + referencePod.Finalizers = sets.NewString(referencePod.Finalizers...).Delete(podFinalizerName).UnsortedList() |
| 86 | + if err := r.referenceClusterClient.Update(ctx, referencePod); err != nil { |
| 87 | + return fmt.Errorf("failed to update pod in refernce cluster after removing finalizer: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | + } |
| 92 | + |
| 93 | + if !sets.NewString(referencePod.Finalizers...).Has(podFinalizerName) { |
| 94 | + referencePod.Finalizers = append(referencePod.Finalizers, podFinalizerName) |
| 95 | + if err := r.referenceClusterClient.Update(ctx, referencePod); err != nil { |
| 96 | + return fmt.Errorf("failed to update pod after adding finalizer: %w", err) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Check if pod already exists |
| 101 | + podName := types.NamespacedName{Namespace: referencePod.Namespace, Name: referencePod.Name} |
| 102 | + podExists := true |
| 103 | + if err := r.mirrorClusterClient.Get(ctx, podName, &corev1.Pod{}); err != nil { |
| 104 | + if !apierrors.IsNotFound(err) { |
| 105 | + return fmt.Errorf("failed to check in mirror cluster if pod exists: %w", err) |
| 106 | + } |
| 107 | + podExists = false |
| 108 | + } |
| 109 | + if podExists { |
| 110 | + return nil |
| 111 | + } |
| 112 | + |
| 113 | + mirrorPod := &corev1.Pod{ |
| 114 | + ObjectMeta: metav1.ObjectMeta{ |
| 115 | + Namespace: referencePod.Namespace, |
| 116 | + Name: referencePod.Name, |
| 117 | + }, |
| 118 | + Spec: *referencePod.Spec.DeepCopy(), |
| 119 | + } |
| 120 | + if err := r.mirrorClusterClient.Create(ctx, mirrorPod); err != nil { |
| 121 | + return fmt.Errorf("failed to create mirror pod: %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + return nil |
| 125 | +} |
0 commit comments