|
| 1 | +/* |
| 2 | +Copyright 2025. |
| 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 controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + corev1 "k8s.io/api/core/v1" |
| 23 | + "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/apimachinery/pkg/runtime" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 32 | + |
| 33 | + sandboxv1alpha1 "sigs.k8s.io/agent-sandbox/api/v1alpha1" |
| 34 | +) |
| 35 | + |
| 36 | +// SandboxReconciler reconciles a Sandbox object |
| 37 | +type SandboxReconciler struct { |
| 38 | + client.Client |
| 39 | + Scheme *runtime.Scheme |
| 40 | +} |
| 41 | + |
| 42 | +//+kubebuilder:rbac:groups=agents.x-k8s.io,resources=sandboxes,verbs=get;list;watch;create;update;patch;delete |
| 43 | +//+kubebuilder:rbac:groups=agents.x-k8s.io,resources=sandboxes/status,verbs=get;update;patch |
| 44 | + |
| 45 | +//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;create;update;patch;delete |
| 46 | + |
| 47 | +// Reconcile is part of the main kubernetes reconciliation loop which aims to |
| 48 | +// move the current state of the cluster closer to the desired state. |
| 49 | +// TODO(user): Modify the Reconcile function to compare the state specified by |
| 50 | +// the Sandbox object against the actual cluster state, and then |
| 51 | +// perform operations to make the cluster state reflect the state specified by |
| 52 | +// the user. |
| 53 | +// |
| 54 | +// For more details, check Reconcile and its Result here: |
| 55 | +// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile |
| 56 | +func (r *SandboxReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 57 | + log := log.FromContext(ctx) |
| 58 | + |
| 59 | + sandbox := &sandboxv1alpha1.Sandbox{} |
| 60 | + if err := r.Get(ctx, req.NamespacedName, sandbox); err != nil { |
| 61 | + if errors.IsNotFound(err) { |
| 62 | + log.Info("sandbox resource not found. Ignoring since object must be deleted") |
| 63 | + return ctrl.Result{}, nil |
| 64 | + } |
| 65 | + return ctrl.Result{}, err |
| 66 | + } |
| 67 | + |
| 68 | + if !sandbox.ObjectMeta.DeletionTimestamp.IsZero() { |
| 69 | + log.Info("Sandbox is being deleted") |
| 70 | + return ctrl.Result{}, nil |
| 71 | + } |
| 72 | + |
| 73 | + // Check if the pod already exists, if not create a new one |
| 74 | + podInCluster := &corev1.Pod{} |
| 75 | + found := true |
| 76 | + err := r.Get(ctx, types.NamespacedName{Name: sandbox.Name, Namespace: sandbox.Namespace}, podInCluster) |
| 77 | + if err != nil { |
| 78 | + if !errors.IsNotFound(err) { |
| 79 | + log.Error(err, "Failed to get Pod") |
| 80 | + return ctrl.Result{}, err |
| 81 | + } |
| 82 | + found = false |
| 83 | + } |
| 84 | + |
| 85 | + // Create a pod object from the sandbox |
| 86 | + pod, err := r.podForSandbox(sandbox) |
| 87 | + if err != nil { |
| 88 | + return ctrl.Result{}, err |
| 89 | + } |
| 90 | + if !found { |
| 91 | + log.Info("Creating a new Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name) |
| 92 | + if err = r.Create(ctx, pod, client.FieldOwner("sandbox-controller")); err != nil { |
| 93 | + log.Error(err, "Failed to create", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name) |
| 94 | + return ctrl.Result{}, err |
| 95 | + } |
| 96 | + } else { |
| 97 | + log.Info("Found Pod", "Pod.Namespace", pod.Namespace, "Pod.Name", pod.Name) |
| 98 | + // TODO - Do we enfore (change) spec if a pod exists ? |
| 99 | + // r.Patch(ctx, pod, client.Apply, client.ForceOwnership, client.FieldOwner("sandbox-controller")) |
| 100 | + } |
| 101 | + return ctrl.Result{}, nil |
| 102 | +} |
| 103 | + |
| 104 | +func (r *SandboxReconciler) podForSandbox(s *sandboxv1alpha1.Sandbox) (*corev1.Pod, error) { |
| 105 | + // TODO we need to handle this better. |
| 106 | + // We are enforcing the length limitation of label values |
| 107 | + // https://kubernetes.io/docs/concepts/overview/working-with-objects/names/ |
| 108 | + // https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set |
| 109 | + labelValue := s.Name |
| 110 | + if len(labelValue) > 63 { |
| 111 | + labelValue = labelValue[:63] |
| 112 | + } |
| 113 | + pod := &corev1.Pod{ |
| 114 | + ObjectMeta: metav1.ObjectMeta{ |
| 115 | + Name: s.Name, |
| 116 | + Namespace: s.Namespace, |
| 117 | + Labels: map[string]string{ |
| 118 | + "agents.x-k8s.io/sandbox-name": labelValue, |
| 119 | + }, |
| 120 | + }, |
| 121 | + Spec: s.Spec.Template.Spec, |
| 122 | + } |
| 123 | + pod.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Pod")) |
| 124 | + if err := ctrl.SetControllerReference(s, pod, r.Scheme); err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + return pod, nil |
| 128 | +} |
| 129 | + |
| 130 | +// SetupWithManager sets up the controller with the Manager. |
| 131 | +func (r *SandboxReconciler) SetupWithManager(mgr ctrl.Manager) error { |
| 132 | + labelSelectorPredicate := predicate.NewPredicateFuncs(func(object client.Object) bool { |
| 133 | + // Filter for pods with the agent label |
| 134 | + if pod, ok := object.(*corev1.Pod); ok { |
| 135 | + if _, exists := pod.Labels["agents.x-k8s.io/sandbox-name"]; exists { |
| 136 | + return true |
| 137 | + } |
| 138 | + } |
| 139 | + return false |
| 140 | + }) |
| 141 | + return ctrl.NewControllerManagedBy(mgr). |
| 142 | + For(&sandboxv1alpha1.Sandbox{}). |
| 143 | + Owns(&corev1.Pod{}, builder.WithPredicates(labelSelectorPredicate)). |
| 144 | + Complete(r) |
| 145 | +} |
0 commit comments