forked from yamt/midonet-kubernetes
/
translation.go
310 lines (296 loc) · 9.18 KB
/
translation.go
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright (C) 2018 Midokura SARL.
// All rights reserved.
//
// 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 converter
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"fmt"
"strings"
jsonpatch "github.com/evanphx/json-patch"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
mnv1 "github.com/midonet/midonet-kubernetes/pkg/apis/midonet/v1"
mncli "github.com/midonet/midonet-kubernetes/pkg/client/clientset/versioned"
"github.com/midonet/midonet-kubernetes/pkg/k8s"
)
type translationUpdater struct {
client mncli.Interface
recorder record.EventRecorder
}
// NewTranslationUpdater returns an updater to store Translation resources.
func NewTranslationUpdater(client mncli.Interface, recorder record.EventRecorder) Updater {
return &translationUpdater{
client: client,
recorder: recorder,
}
}
func (u *translationUpdater) Update(parentKind schema.GroupVersionKind, parentObjInterface interface{}, resources map[Key][]BackendResource) error {
var parentObj runtime.Object
var parentRef *v1.ObjectReference
// REVISIT: Make the caller pass runtime.Object
if parentObjInterface != nil {
parentObj = parentObjInterface.(runtime.Object)
ref, err := k8s.GetReferenceForEvent(parentObj)
if err != nil {
return err
}
parentRef = ref
} else {
parentObj = nil
parentRef = nil
}
var owners []metav1.OwnerReference
var ownerlabels map[string]string
var requirement *labels.Requirement
var ns string
if parentObj != nil {
pmeta, err := meta.Accessor(parentObj)
if err != nil {
log.WithError(err).Error("Accessor")
return err
}
puid := pmeta.GetUID()
owners = []metav1.OwnerReference{
{
APIVersion: parentKind.GroupVersion().String(),
Kind: parentKind.Kind,
Name: pmeta.GetName(),
UID: puid,
},
}
ownerlabels = map[string]string{OwnerUIDLabel: string(puid)}
r, err := labels.NewRequirement(OwnerUIDLabel, selection.Equals, []string{string(puid)})
if err != nil {
return err
}
requirement = r
ns = pmeta.GetNamespace()
if ns == "" {
// Namespace-less resource like Node. Use the default
// namespace for the corresponding Translation resources.
ns = metav1.NamespaceDefault
}
} else {
owners = nil
ownerlabels = map[string]string{GlobalLabel: ""}
r, err := labels.NewRequirement(GlobalLabel, selection.Exists, nil)
if err != nil {
return err
}
requirement = r
ns = metav1.NamespaceSystem
}
finalizers := []string{MidoNetAPIDeleter}
var uids []types.UID
for k, res := range resources {
name := k.translationName()
name = makeDNS(name)
uid, err := u.updateOne(parentRef, ns, name, owners, ownerlabels, finalizers, res)
if err != nil {
return err
}
uids = append(uids, uid)
}
// Remove stale translations
return u.deleteTranslations(parentRef, requirement, uids)
}
func contains(xs []types.UID, y types.UID) bool {
for _, x := range xs {
if x == y {
return true
}
}
return false
}
func (u *translationUpdater) deleteTranslations(parentRef *v1.ObjectReference, req *labels.Requirement, keepUIDs []types.UID) error {
// Get a list of Translations owned by the parentUID synchronously.
// REVISIT: Maybe it's more efficient to use the cache in the informer
// but it might be tricky to avoid races with ourselves:
// consider updating a Service twice.
// the first update adds a Translation and the second update
// deletes it. when the controller processes the second update,
// it's possible that its informer have not seen the Translation
// addition from the first update yet. in that case, it might
// fail to delete the Translation.
clog := log.WithFields(log.Fields{
"parentRef": parentRef,
"req": req,
"keepUIDs": keepUIDs,
})
clog.Debug("Deleting stale translations")
selector := labels.NewSelector()
selector = selector.Add(*req)
opts := metav1.ListOptions{LabelSelector: selector.String()}
objList, err := u.client.MidonetV1().Translations(metav1.NamespaceAll).List(opts)
if err != nil {
return err
}
clog.WithField("objList", objList).Debug("Got Translations")
for _, tr := range objList.Items {
if contains(keepUIDs, tr.ObjectMeta.UID) {
continue
}
err = u.deleteTranslation(tr)
if err != nil {
return err
}
if parentRef != nil {
u.recorder.Eventf(parentRef, v1.EventTypeNormal, "TranslationDeleted", "Translation %s/%s UID %s Deleted", tr.ObjectMeta.Namespace, tr.ObjectMeta.Name, tr.ObjectMeta.UID)
} else {
log.WithFields(log.Fields{
"namespace": tr.ObjectMeta.Namespace,
"name": tr.ObjectMeta.Name,
}).Info("Global Translation Deleted")
}
}
return nil
}
func (u *translationUpdater) deleteTranslation(tr mnv1.Translation) error {
namespace := tr.ObjectMeta.Namespace
name := tr.ObjectMeta.Name
err := u.client.MidonetV1().Translations(namespace).Delete(name, &metav1.DeleteOptions{})
if err != nil {
return err
}
return nil
}
func (u *translationUpdater) updateOne(parentRef *v1.ObjectReference, ns, name string, owners []metav1.OwnerReference, labels map[string]string, finalizers []string, resources []BackendResource) (types.UID, error) {
clog := log.WithFields(log.Fields{
"namespace": ns,
"name": name,
})
var v1rs []mnv1.BackendResource
for _, res := range resources {
r, err := toAPI(res)
if err != nil {
return "", err
}
v1rs = append(v1rs, *r)
}
obj := &mnv1.Translation{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
Resources: v1rs,
}
meta, err := meta.Accessor(obj)
if err != nil {
clog.WithError(err).Error("Accessor(obj)")
return "", err
}
meta.SetOwnerReferences(owners)
meta.SetLabels(labels)
meta.SetFinalizers(finalizers)
clog = clog.WithField("obj", obj)
newObj, err := u.client.MidonetV1().Translations(ns).Create(obj)
if err == nil {
if parentRef != nil {
u.recorder.Eventf(parentRef, v1.EventTypeNormal, "TranslationCreated", "Translation %s/%s UID %s Created", ns, name, newObj.ObjectMeta.UID)
} else {
log.WithFields(log.Fields{
"namespace": ns,
"name": name,
}).Info("Global Translation Created")
}
return newObj.ObjectMeta.UID, nil
}
if !errors.IsAlreadyExists(err) {
clog.WithError(err).Error("Create")
return "", err
}
// NOTE: CRs have AllowUnconditionalUpdate=false
// NOTE: CRs don't support strategic merge patch
existingObj, err := u.client.MidonetV1().Translations(ns).Get(name, metav1.GetOptions{})
if err != nil {
clog.WithError(err).Error("Get")
return "", err
}
oldData, err := json.Marshal(existingObj)
if err != nil {
return "", err
}
desiredObj := existingObj.DeepCopy()
desiredObj.Resources = obj.Resources
desiredData, err := json.Marshal(desiredObj)
if err != nil {
return "", err
}
patchBytes, err := jsonpatch.CreateMergePatch(oldData, desiredData)
if err != nil {
return "", err
}
if jsonpatch.Equal(patchBytes, []byte(`{}`)) {
log.WithFields(log.Fields{
"namespace": ns,
"name": name,
}).Debug("Skipping no-op update of Translation")
return existingObj.ObjectMeta.UID, nil
}
clog = clog.WithField("patch", string(patchBytes))
newObj, err = u.client.MidonetV1().Translations(ns).Patch(name, types.MergePatchType, patchBytes)
if err != nil {
clog.WithError(err).Error("Patch")
return "", err
}
checkTranslationUpdate(existingObj, desiredObj)
log.WithFields(log.Fields{
"namespace": ns,
"name": name,
"patch": string(patchBytes),
}).Debug("Patched Translation")
if parentRef != nil {
u.recorder.Eventf(parentRef, v1.EventTypeNormal, "TranslationUpdated", "Translation %s/%s UID %s Updated", ns, name, newObj.ObjectMeta.UID)
} else {
log.WithFields(log.Fields{
"namespace": ns,
"name": name,
}).Info("Global Translation Updated")
}
return newObj.ObjectMeta.UID, nil
}
func checkTranslationUpdate(old *mnv1.Translation, new *mnv1.Translation) {
clog := log.WithFields(log.Fields{
"old": "old",
"new": "new",
})
if len(old.Resources) > len(new.Resources) {
clog.Fatal("The list of resources shrunk")
}
}
// makeDNS tweaks the given name so that it's usable as a Kubernetes
// resource name.
// REVISIT: probabaly we should truncate it when too long.
func makeDNS(name string) string {
n := strings.Replace(name, "/", "-", -1)
n = strings.ToLower(n)
// If we changed anything, append the hash of the original string
// to maintain uniqueness.
if name != n {
h := sha1.New()
h.Write([]byte(name))
n = fmt.Sprintf("%s-%s", n, hex.EncodeToString(h.Sum(nil)))
}
return n
}