aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/controller/pod/pod_controller.go
blob: 23a847eec6a89d239153878488e5a328c31f6c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package pod

import (
	"context"
	"encoding/json"
	"fmt"
	corev1 "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/errors"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/types"
	"ovn4nfv-k8s-plugin/internal/pkg/ovn"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/controller"
	"sigs.k8s.io/controller-runtime/pkg/event"
	"sigs.k8s.io/controller-runtime/pkg/handler"
	"sigs.k8s.io/controller-runtime/pkg/manager"
	"sigs.k8s.io/controller-runtime/pkg/predicate"
	"sigs.k8s.io/controller-runtime/pkg/reconcile"
	logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
	"sigs.k8s.io/controller-runtime/pkg/source"
)

var log = logf.Log.WithName("controller_pod")

const (
	nfnNetworkAnnotation = "k8s.plugin.opnfv.org/nfn-network"
)

type nfnNetwork struct {
	Type      string                   "json:\"type\""
	Interface []map[string]interface{} "json:\"interface\""
}

// Add creates a new Pod Controller and adds it to the Manager. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
	return add(mgr, newReconciler(mgr))
}

// newReconciler returns a new reconcile.Reconciler
func newReconciler(mgr manager.Manager) reconcile.Reconciler {
	return &ReconcilePod{client: mgr.GetClient(), scheme: mgr.GetScheme()}
}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {

	// Create a new Controller that will call the provided Reconciler function in response
	// to events.
	c, err := controller.New("pod-controller", mgr, controller.Options{Reconciler: r})
	if err != nil {
		return err
	}
	// Define Predicates On Create and Update function
	p := predicate.Funcs{
		UpdateFunc: func(e event.UpdateEvent) bool {
			annotaion := e.MetaNew.GetAnnotations()
			// The object doesn't contain annotation ,nfnNetworkAnnotation so the event will be
			// ignored.
			if _, ok := annotaion[nfnNetworkAnnotation]; !ok {
				return false
			}
			// If pod is already processed by OVN don't add event
			if _, ok := annotaion[ovn.Ovn4nfvAnnotationTag]; ok {
				return false
			}
			return true
		},
		CreateFunc: func(e event.CreateEvent) bool {
			// The object doesn't contain annotation ,nfnNetworkAnnotation so the event will be
			// ignored.
			annotaion := e.Meta.GetAnnotations()
			if _, ok := annotaion[nfnNetworkAnnotation]; !ok {
				return false
			}
			return true
		},
		DeleteFunc: func(e event.DeleteEvent) bool {
			// The object doesn't contain annotation ,nfnNetworkAnnotation so the event will be
			// ignored.
			annotaion := e.Meta.GetAnnotations()
			if _, ok := annotaion[nfnNetworkAnnotation]; !ok {
				return false
			}
			return true
		},
	}

	// Watch for Pod create / update / delete events and call Reconcile
	err = c.Watch(&source.Kind{Type: &corev1.Pod{}}, &handler.EnqueueRequestForObject{}, p)
	if err != nil {
		return err
	}
	return nil
}

// blank assignment to verify that ReconcuilePod implements reconcile.Reconciler
var _ reconcile.Reconciler = &ReconcilePod{}

// ReconcilePod reconciles a ProviderNetwork object
type ReconcilePod struct {
	// This client, initialized using mgr.Client() above, is a split client
	// that reads objects from the cache and writes to the apiserver
	client client.Client
	scheme *runtime.Scheme
}

// Reconcile function
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ReconcilePod) Reconcile(request reconcile.Request) (reconcile.Result, error) {
	reqLogger := log.WithValues("Request.Namespace", request.Namespace, "Request.Name", request.Name)
	reqLogger.Info("Enter Reconciling Pod")

	// Fetch the Pod instance
	instance := &corev1.Pod{}
	err := r.client.Get(context.TODO(), request.NamespacedName, instance)

	if err != nil {
		if errors.IsNotFound(err) {
			// Request object not found, could have been deleted after reconcile request.
			// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
			// Return and don't requeue
			if instance.Name == "" || instance.Namespace == "" {
				return reconcile.Result{}, nil
			}
			r.deleteLogicalPorts(request.Name, request.Namespace)
			return reconcile.Result{}, nil
		}
		// Error reading the object - requeue the request.
		return reconcile.Result{}, err
	}
	if instance.Name == "" || instance.Namespace == "" {
		return reconcile.Result{}, nil
	}
	err = r.addLogicalPorts(instance)
	if err != nil && err.Error() == "Failed to add ports" {
		// Requeue the object
		return reconcile.Result{}, err
	}
	reqLogger.Info("Exit Reconciling Pod")
	return reconcile.Result{}, nil
}

// annotatePod annotates pod with the given annotations
func (r *ReconcilePod) setPodAnnotation(pod *corev1.Pod, key, value string) error {

	patchData := fmt.Sprintf(`{"metadata":{"annotations":{"%s":"%s"}}}`, key, value)
	err := r.client.Patch(context.TODO(), pod, client.ConstantPatch(types.MergePatchType, []byte(patchData)))
	if err != nil {
		log.Error(err, "Updating pod failed", "pod", pod, "key", key, "value", value)
		return err
	}
	return nil
}

func (r *ReconcilePod) addLogicalPorts(pod *corev1.Pod) error {

	nfn, err := r.readPodAnnotation(pod)
	if err != nil {
		return err
	}

	switch {
	case nfn.Type == "ovn4nfv":
		ovnCtl, err := ovn.GetOvnController()
		if err != nil {
			return err
		}
                if _, ok := pod.Annotations[ovn.Ovn4nfvAnnotationTag]; ok {
			return fmt.Errorf("Pod annotation found")
		}
		key, value := ovnCtl.AddLogicalPorts(pod, nfn.Interface)
		if len(key) > 0 {
			return r.setPodAnnotation(pod, key, value)
		}
		return fmt.Errorf("Failed to add ports")
	default:
		return fmt.Errorf("Unsupported Networking type %s", nfn.Type)
	// Add other types here
	}
}

func (r *ReconcilePod) deleteLogicalPorts(name, namesapce string) error {

	// Run delete for all controllers; pod annonations inaccessible
	ovnCtl, err := ovn.GetOvnController()
	if err != nil {
		return err
	}
	ovnCtl.DeleteLogicalPorts(name, namesapce)
	return nil
	// Add other types here
}

func (r *ReconcilePod) readPodAnnotation(pod *corev1.Pod) (*nfnNetwork, error) {
	annotaion, ok := pod.Annotations[nfnNetworkAnnotation]
	if !ok {
		return nil, fmt.Errorf("Invalid annotations")
	}
	var nfn nfnNetwork
	err := json.Unmarshal([]byte(annotaion), &nfn)
	if err != nil {
		log.Error(err, "Invalid nfn annotaion", "annotaiton", annotaion)
		return nil, err
	}
	return &nfn, nil
}