|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 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 controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + "k8s.io/apimachinery/pkg/runtime" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + "k8s.io/klog/v2" |
| 27 | + "k8s.io/utils/ptr" |
| 28 | + ctrl "sigs.k8s.io/controller-runtime" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 32 | + |
| 33 | + api "github.com/inftyai/manta/api/v1alpha1" |
| 34 | +) |
| 35 | + |
| 36 | +// PodReconciler reconciles a Torrent object |
| 37 | +type PodReconciler struct { |
| 38 | + client.Client |
| 39 | + Scheme *runtime.Scheme |
| 40 | +} |
| 41 | + |
| 42 | +func NewPodReconciler(client client.Client, scheme *runtime.Scheme) *PodReconciler { |
| 43 | + return &PodReconciler{ |
| 44 | + Client: client, |
| 45 | + Scheme: scheme, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch |
| 50 | + |
| 51 | +func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 52 | + logger := log.FromContext(ctx) |
| 53 | + |
| 54 | + pod := &corev1.Pod{} |
| 55 | + if err := r.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: req.Name}, pod); err != nil { |
| 56 | + return ctrl.Result{}, client.IgnoreNotFound(err) |
| 57 | + } |
| 58 | + |
| 59 | + logger.Info("reconcile Pod") |
| 60 | + // This should not happen, double check here. |
| 61 | + if pod.Labels == nil || pod.Labels[api.TorrentNameLabelKey] == "" { |
| 62 | + return ctrl.Result{}, nil |
| 63 | + } |
| 64 | + |
| 65 | + torrentName := pod.Labels[api.TorrentNameLabelKey] |
| 66 | + |
| 67 | + torrent := &api.Torrent{} |
| 68 | + if err := r.Get(ctx, types.NamespacedName{Name: torrentName}, torrent); err != nil { |
| 69 | + return ctrl.Result{}, err |
| 70 | + } |
| 71 | + |
| 72 | + newTorrent := constructTorrent(torrent, pod) |
| 73 | + if err := r.Client.Create(ctx, &newTorrent); err != nil { |
| 74 | + logger.Error(err, "failed to create Torrent", "Torrent", klog.KObj(&newTorrent)) |
| 75 | + return ctrl.Result{}, err |
| 76 | + } |
| 77 | + |
| 78 | + return ctrl.Result{}, nil |
| 79 | +} |
| 80 | + |
| 81 | +func (r *PodReconciler) Create(e event.CreateEvent) bool { |
| 82 | + pod, match := e.Object.(*corev1.Pod) |
| 83 | + if !match { |
| 84 | + return false |
| 85 | + } |
| 86 | + |
| 87 | + // Pod should be managed by Manta. |
| 88 | + if pod.Labels == nil || pod.Labels[api.TorrentNameLabelKey] == "" { |
| 89 | + return false |
| 90 | + } |
| 91 | + |
| 92 | + return true |
| 93 | +} |
| 94 | + |
| 95 | +func (r *PodReconciler) Update(e event.UpdateEvent) bool { |
| 96 | + return false |
| 97 | +} |
| 98 | + |
| 99 | +func (r *PodReconciler) Delete(e event.DeleteEvent) bool { |
| 100 | + return false |
| 101 | +} |
| 102 | + |
| 103 | +func (r *PodReconciler) Generic(e event.GenericEvent) bool { |
| 104 | + return false |
| 105 | +} |
| 106 | + |
| 107 | +func (r *PodReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 108 | + return ctrl.NewControllerManagedBy(mgr). |
| 109 | + For(&corev1.Pod{}). |
| 110 | + WithEventFilter(r). |
| 111 | + Complete(r) |
| 112 | +} |
| 113 | + |
| 114 | +func constructTorrent(torrent *api.Torrent, pod *corev1.Pod) api.Torrent { |
| 115 | + newTorrent := api.Torrent{} |
| 116 | + newTorrent.ObjectMeta.Name = torrent.Name + "--tmp--" + pod.Spec.NodeName |
| 117 | + newTorrent.TypeMeta = torrent.TypeMeta |
| 118 | + newTorrent.Annotations = map[string]string{api.ParentPodNameAnnoKey: pod.Namespace + "/" + pod.Name} |
| 119 | + newTorrent.Spec = torrent.Spec |
| 120 | + newTorrent.Spec.Preheat = ptr.To[bool](true) |
| 121 | + newTorrent.Spec.Replicas = ptr.To[int32](1) |
| 122 | + newTorrent.Spec.ReclaimPolicy = ptr.To[api.ReclaimPolicy](api.RetainReclaimPolicy) |
| 123 | + newTorrent.Spec.TTLSecondsAfterReady = ptr.To[time.Duration](0) |
| 124 | + newTorrent.Spec.NodeSelector = map[string]string{"kubernetes.io/hostname": pod.Spec.NodeName} |
| 125 | + |
| 126 | + return newTorrent |
| 127 | +} |
0 commit comments