7 corev1 "k8s.io/api/core/v1"
8 "k8s.io/apimachinery/pkg/api/errors"
9 "k8s.io/apimachinery/pkg/runtime"
10 "k8s.io/apimachinery/pkg/types"
11 "ovn4nfv-k8s-plugin/internal/pkg/ovn"
12 "sigs.k8s.io/controller-runtime/pkg/client"
13 "sigs.k8s.io/controller-runtime/pkg/controller"
14 "sigs.k8s.io/controller-runtime/pkg/event"
15 "sigs.k8s.io/controller-runtime/pkg/handler"
16 "sigs.k8s.io/controller-runtime/pkg/manager"
17 "sigs.k8s.io/controller-runtime/pkg/predicate"
18 "sigs.k8s.io/controller-runtime/pkg/reconcile"
19 logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
20 "sigs.k8s.io/controller-runtime/pkg/source"
23 var log = logf.Log.WithName("controller_pod")
26 nfnNetworkAnnotation = "k8s.plugin.opnfv.org/nfn-network"
29 type nfnNetwork struct {
30 Type string "json:\"type\""
31 Interface []map[string]interface{} "json:\"interface\""
34 // Add creates a new Pod Controller and adds it to the Manager. The Manager will set fields on the Controller
35 // and Start it when the Manager is Started.
36 func Add(mgr manager.Manager) error {
37 return add(mgr, newReconciler(mgr))
40 // newReconciler returns a new reconcile.Reconciler
41 func newReconciler(mgr manager.Manager) reconcile.Reconciler {
42 return &ReconcilePod{client: mgr.GetClient(), scheme: mgr.GetScheme()}
45 // add adds a new Controller to mgr with r as the reconcile.Reconciler
46 func add(mgr manager.Manager, r reconcile.Reconciler) error {
48 // Create a new Controller that will call the provided Reconciler function in response
50 c, err := controller.New("pod-controller", mgr, controller.Options{Reconciler: r})
54 // Define Predicates On Create and Update function
56 UpdateFunc: func(e event.UpdateEvent) bool {
57 annotaion := e.MetaNew.GetAnnotations()
58 // The object doesn't contain annotation ,nfnNetworkAnnotation so the event will be
60 if _, ok := annotaion[nfnNetworkAnnotation]; !ok {
63 // If pod is already processed by OVN don't add event
64 if _, ok := annotaion[ovn.Ovn4nfvAnnotationTag]; ok {
69 CreateFunc: func(e event.CreateEvent) bool {
70 // The object doesn't contain annotation ,nfnNetworkAnnotation so the event will be
72 annotaion := e.Meta.GetAnnotations()
73 if _, ok := annotaion[nfnNetworkAnnotation]; !ok {
78 DeleteFunc: func(e event.DeleteEvent) bool {
79 // The object doesn't contain annotation ,nfnNetworkAnnotation so the event will be
81 annotaion := e.Meta.GetAnnotations()
82 if _, ok := annotaion[nfnNetworkAnnotation]; !ok {
89 // Watch for Pod create / update / delete events and call Reconcile
90 err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForObject{}, p)
97 // blank assignment to verify that ReconcuilePod implements reconcile.Reconciler
98 var _ reconcile.Reconciler = &ReconcilePod{}
100 // ReconcilePod reconciles a ProviderNetwork object
101 type ReconcilePod struct {
102 // This client, initialized using mgr.Client() above, is a split client
103 // that reads objects from the cache and writes to the apiserver
105 scheme *runtime.Scheme
108 // Reconcile function
109 // The Controller will requeue the Request to be processed again if the returned error is non-nil or
110 // Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
111 func (r *ReconcilePod) Reconcile(request reconcile.Request) (reconcile.Result, error) {
112 reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
113 reqLogger.Info("Enter Reconciling Pod")
115 // Fetch the Pod instance
116 instance := &corev1.Pod{}
117 err := r.client.Get(context.TODO(), request.NamespacedName, instance)
120 if errors.IsNotFound(err) {
121 // Request object not found, could have been deleted after reconcile request.
122 // Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
123 // Return and don't requeue
124 if instance.Name == "" || instance.Namespace == "" {
125 return reconcile.Result{}, nil
127 r.deleteLogicalPorts(request.Name, request.Namespace)
128 return reconcile.Result{}, nil
130 // Error reading the object - requeue the request.
131 return reconcile.Result{}, err
133 if instance.Name == "" || instance.Namespace == "" {
134 return reconcile.Result{}, nil
136 err = r.addLogicalPorts(instance)
137 if err != nil && err.Error() == "Failed to add ports" {
138 // Requeue the object
139 return reconcile.Result{}, err
141 reqLogger.Info("Exit Reconciling Pod")
142 return reconcile.Result{}, nil
145 // annotatePod annotates pod with the given annotations
146 func (r *ReconcilePod) setPodAnnotation(pod *corev1.Pod, key, value string) error {
148 patchData := fmt.Sprintf(`{"metadata":{"annotations":{"%s":"%s"}}}`, key, value)
149 err := r.client.Patch(context.TODO(), pod, client.ConstantPatch(types.MergePatchType, []byte(patchData)))
151 log.Error(err, "Updating pod failed", "pod", pod, "key", key, "value", value)
157 func (r *ReconcilePod) addLogicalPorts(pod *corev1.Pod) error {
159 nfn, err := r.readPodAnnotation(pod)
165 case nfn.Type == "ovn4nfv":
166 ovnCtl, err := ovn.GetOvnController()
170 if _, ok := pod.Annotations[ovn.Ovn4nfvAnnotationTag]; ok {
171 return fmt.Errorf("Pod annotation found")
173 key, value := ovnCtl.AddLogicalPorts(pod, nfn.Interface)
175 return r.setPodAnnotation(pod, key, value)
177 return fmt.Errorf("Failed to add ports")
179 return fmt.Errorf("Unsupported Networking type %s", nfn.Type)
180 // Add other types here
184 func (r *ReconcilePod) deleteLogicalPorts(name, namesapce string) error {
186 // Run delete for all controllers; pod annonations inaccessible
187 ovnCtl, err := ovn.GetOvnController()
191 ovnCtl.DeleteLogicalPorts(name, namesapce)
193 // Add other types here
196 func (r *ReconcilePod) readPodAnnotation(pod *corev1.Pod) (*nfnNetwork, error) {
197 annotaion, ok := pod.Annotations[nfnNetworkAnnotation]
199 return nil, fmt.Errorf("Invalid annotations")
202 err := json.Unmarshal([]byte(annotaion), &nfn)
204 log.Error(err, "Invalid nfn annotaion", "annotaiton", annotaion)