aboutsummaryrefslogtreecommitdiffstats
path: root/src/ovnaction/pkg/module/resources.go
blob: 24c9833ecf02668ef7f0c6f06f0b02f1fc1a5c5b (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Copyright 2020 Intel Corporation, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package module

import (
	"encoding/json"
	"fmt"

	nettypes "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
	netutils "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils"
	log "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/logutils"
	v1 "k8s.io/api/apps/v1"
	batch "k8s.io/api/batch/v1"
	batchv1beta1 "k8s.io/api/batch/v1beta1"
	v1core "k8s.io/api/core/v1"
	_ "k8s.io/kubernetes/pkg/apis/apps/install"
	_ "k8s.io/kubernetes/pkg/apis/batch/install"
	_ "k8s.io/kubernetes/pkg/apis/core/install"
	_ "k8s.io/kubernetes/pkg/apis/extensions/install"

	pkgerrors "github.com/pkg/errors"
)

type NfnAnnotation struct {
	CniType   string
	Interface []WorkloadIfIntentSpec
}

const NfnAnnotationKey = "k8s.plugin.opnfv.org/nfn-network"

// ParsePodTemplateNetworkAnnotation parses Pod annotation in PodTemplate
func ParsePodTemplateNetworkAnnotation(pt *v1core.PodTemplateSpec) ([]*nettypes.NetworkSelectionElement, error) {
	netAnnot := pt.Annotations[nettypes.NetworkAttachmentAnnot]
	defaultNamespace := pt.Namespace

	if len(netAnnot) == 0 {
		return nil, pkgerrors.Errorf("No kubernetes network annotation found")
	}

	networks, err := netutils.ParseNetworkAnnotation(netAnnot, defaultNamespace)
	if err != nil {
		return nil, err
	}
	return networks, nil
}

// GetPodTemplateNetworkAnnotation gets Pod Nfn annotation in PodTemplate
func GetPodTemplateNfnAnnotation(pt *v1core.PodTemplateSpec) NfnAnnotation {
	var nfn NfnAnnotation

	nfnAnnot := pt.Annotations[NfnAnnotationKey]
	if len(nfnAnnot) == 0 {
		return nfn
	}

	err := json.Unmarshal([]byte(nfnAnnot), &nfn)
	if err != nil {
		log.Warn("Error unmarshalling nfn annotation", log.Fields{
			"annotation": nfnAnnot,
		})
	}
	return nfn
}

// GetPodNetworkAnnotation gets Pod Nfn annotation in PodTemplate
func GetPodNfnAnnotation(p *v1core.Pod) NfnAnnotation {
	var nfn NfnAnnotation

	nfnAnnot := p.Annotations[NfnAnnotationKey]
	if len(nfnAnnot) == 0 {
		return nfn
	}

	err := json.Unmarshal([]byte(nfnAnnot), &nfn)
	if err != nil {
		log.Warn("Error unmarshalling nfn annotation", log.Fields{
			"annotation": nfnAnnot,
		})
	}
	return nfn
}

func addNetworkAnnotation(a nettypes.NetworkSelectionElement, as []*nettypes.NetworkSelectionElement) []*nettypes.NetworkSelectionElement {
	var netElements []*nettypes.NetworkSelectionElement

	found := false
	for _, e := range as {
		if e.Name == a.Name {
			found = true
		}
		netElements = append(netElements, e)
	}
	if !found {
		netElements = append(netElements, &a)
	}

	return netElements
}

// Add the interfaces in the 'new' parameter to the nfn annotation
func newNfnIfs(nfn NfnAnnotation, new []WorkloadIfIntentSpec) NfnAnnotation {
	// Prepare a new interface list - combining the original plus new ones
	var newNfn NfnAnnotation

	if nfn.CniType != CNI_TYPE_OVN4NFV {
		if len(nfn.CniType) > 0 {
			log.Warn("Error existing nfn cnitype is invalid", log.Fields{
				"existing cnitype": nfn.CniType,
				"using cnitype":    CNI_TYPE_OVN4NFV,
			})
		}
	}
	newNfn.CniType = CNI_TYPE_OVN4NFV

	// update any interfaces already in the list with the updated interface
	for _, i := range nfn.Interface {
		for _, j := range new {
			if i.NetworkName == j.NetworkName && i.IfName == j.IfName {
				i.DefaultGateway = j.DefaultGateway
				i.IpAddr = j.IpAddr
				i.MacAddr = j.MacAddr
				break
			}
		}
		newNfn.Interface = append(newNfn.Interface, i)
	}

	// add new interfaces not present in original list
	for _, j := range new {
		found := false
		for _, i := range nfn.Interface {
			if i.NetworkName == j.NetworkName && i.IfName == j.IfName {
				found = true
				break
			}
		}
		if !found {
			newNfn.Interface = append(newNfn.Interface, j)
		}
	}
	return newNfn
}

func updatePodTemplateNetworkAnnotation(pt *v1core.PodTemplateSpec, a nettypes.NetworkSelectionElement) {
	netAnnotation, _ := ParsePodTemplateNetworkAnnotation(pt)
	elements := addNetworkAnnotation(a, netAnnotation)
	j, err := json.Marshal(elements)
	if err != nil {
		log.Error("Existing network annotation has invalid format", log.Fields{
			"error": err,
		})
		return
	}
	if pt.Annotations == nil {
		pt.Annotations = make(map[string]string)
	}
	pt.Annotations[nettypes.NetworkAttachmentAnnot] = string(j)
}

// Add a network annotation to the resource
func AddNetworkAnnotation(r interface{}, a nettypes.NetworkSelectionElement) {

	switch o := r.(type) {
	case *batch.Job:
		updatePodTemplateNetworkAnnotation(&o.Spec.Template, a)
	case *batchv1beta1.CronJob:
		updatePodTemplateNetworkAnnotation(&o.Spec.JobTemplate.Spec.Template, a)
	case *v1.DaemonSet:
		updatePodTemplateNetworkAnnotation(&o.Spec.Template, a)
	case *v1.Deployment:
		updatePodTemplateNetworkAnnotation(&o.Spec.Template, a)
	case *v1.ReplicaSet:
		updatePodTemplateNetworkAnnotation(&o.Spec.Template, a)
	case *v1.StatefulSet:
		updatePodTemplateNetworkAnnotation(&o.Spec.Template, a)
	case *v1core.Pod:
		netAnnotation, _ := netutils.ParsePodNetworkAnnotation(o)
		elements := addNetworkAnnotation(a, netAnnotation)
		j, err := json.Marshal(elements)
		if err != nil {
			log.Error("Existing network annotation has invalid format", log.Fields{
				"error": err,
			})
			break
		}
		if o.Annotations == nil {
			o.Annotations = make(map[string]string)
		}
		o.Annotations[nettypes.NetworkAttachmentAnnot] = string(j)
		return
	case *v1core.ReplicationController:
		updatePodTemplateNetworkAnnotation(o.Spec.Template, a)
	default:
		typeStr := fmt.Sprintf("%T", o)
		log.Warn("Network annotations not supported for resource type", log.Fields{
			"resource type": typeStr,
		})
	}
}

func updatePodTemplateNfnAnnotation(pt *v1core.PodTemplateSpec, new []WorkloadIfIntentSpec) {
	nfnAnnotation := GetPodTemplateNfnAnnotation(pt)
	newNfnAnnotation := newNfnIfs(nfnAnnotation, new)
	j, err := json.Marshal(newNfnAnnotation)
	if err != nil {
		log.Error("Network nfn annotation has invalid format", log.Fields{
			"error": err,
		})
		return
	}
	if pt.Annotations == nil {
		pt.Annotations = make(map[string]string)
	}
	pt.Annotations[NfnAnnotationKey] = string(j)
}

// Add an nfn annotation to the resource
func AddNfnAnnotation(r interface{}, new []WorkloadIfIntentSpec) {

	switch o := r.(type) {
	case *batch.Job:
		updatePodTemplateNfnAnnotation(&o.Spec.Template, new)
	case *batchv1beta1.CronJob:
		updatePodTemplateNfnAnnotation(&o.Spec.JobTemplate.Spec.Template, new)
	case *v1.DaemonSet:
		updatePodTemplateNfnAnnotation(&o.Spec.Template, new)
		return
	case *v1.Deployment:
		updatePodTemplateNfnAnnotation(&o.Spec.Template, new)
		return
	case *v1.ReplicaSet:
		updatePodTemplateNfnAnnotation(&o.Spec.Template, new)
	case *v1.StatefulSet:
		updatePodTemplateNfnAnnotation(&o.Spec.Template, new)
	case *v1core.Pod:
		nfnAnnotation := GetPodNfnAnnotation(o)
		newNfnAnnotation := newNfnIfs(nfnAnnotation, new)
		j, err := json.Marshal(newNfnAnnotation)
		if err != nil {
			log.Error("Network nfn annotation has invalid format", log.Fields{
				"error": err,
			})
			break
		}
		if o.Annotations == nil {
			o.Annotations = make(map[string]string)
		}
		o.Annotations[NfnAnnotationKey] = string(j)
		return
	case *v1core.ReplicationController:
		updatePodTemplateNfnAnnotation(o.Spec.Template, new)
		return
	default:
		typeStr := fmt.Sprintf("%T", o)
		log.Warn("Network nfn annotations not supported for resource type", log.Fields{
			"resource type": typeStr,
		})
	}
}