-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh.go
205 lines (179 loc) · 5.64 KB
/
refresh.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
package refresh
import (
"context"
"fmt"
"reflect"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/controller-runtime/pkg/client"
util "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/wantedly/resource-name-sanitizer"
)
// Builder is responsible for creating on Application
//
// Builder
// - collect information
// - should not contain any logic regarding generating objects
type Builder interface {
Build(ctx context.Context) (Lister, error)
}
// Lister is responsible for generating ObjectList for each resource
//
// Lister
// - generate objects
// - should not contain any logic accessing API servers
type Lister interface {
GenerateLists() []ObjectList
}
// Refresher reconciles objects by creating, updating, and deleting
type Refresher interface {
// Refresh syncs objects
//
// each object will be processed as described below
// - when it doesn't exist, it will be created
// - when the object exists, it will be updated
//
// Also an object that matches all the conditions below will be deleted
// - owned by parent
// - is not present in list
//
// when return value from Identify of two objects are the same they are considered to be a same object
Refresh(ctx context.Context, parent client.Object, list ObjectList) error
}
type refresher struct {
client client.Client
scheme *runtime.Scheme
}
type ObjectList struct {
Items []client.Object
GroupVersionKind schema.GroupVersionKind
Identity func(client.Object) (string, error)
}
func New(client client.Client, scheme *runtime.Scheme) Refresher {
return &refresher{client, scheme}
}
func (r refresher) Refresh(ctx context.Context, parent client.Object, list ObjectList) error {
existingObjs, err := r.handleExisting(ctx, parent, list)
if err != nil {
return errors.WithStack(err)
}
for _, obj := range list.Items {
objKey, err := list.Identity(obj)
if err != nil {
return errors.WithStack(err)
}
emptyObj := reflect.New(reflect.ValueOf(obj).Elem().Type()).Interface().(client.Object)
emptyObj.SetNamespace(parent.GetNamespace())
if existing, ok := existingObjs[objKey]; ok {
emptyObj.SetName(existing.GetName())
} else {
s := sanitizer.NewSubdomainLabelSafe()
if name := obj.GetName(); name != "" {
emptyObj.SetName(s.Sanitize(name))
} else {
name := createResourceName("%s-%s", objKey, parent.GetName())
emptyObj.SetName(s.Sanitize(name))
}
}
if _, err := util.CreateOrUpdate(ctx, r.client, emptyObj, func() error {
{
v := emptyObj.GetResourceVersion()
ns := emptyObj.GetNamespace()
n := emptyObj.GetName()
defer func() {
// to prevent resource version being nil
emptyObj.SetResourceVersion(v)
// because we cannot update namespace and name mutateFn
// we set those back
emptyObj.SetNamespace(ns)
emptyObj.SetName(n)
}()
}
// TODO: if the resource already exists and not owned by this, it should return error
if err := r.scheme.Convert(obj, emptyObj, nil); err != nil {
return errors.WithStack(err)
}
return errors.WithStack(util.SetControllerReference(parent, emptyObj, r.scheme))
}); err != nil {
return errors.WithStack(err)
}
}
return nil
}
// handleExisting is responsible for two things
// - collect information about existing objects to be updated
// - delete outdated objects
func (r refresher) handleExisting(ctx context.Context, parent client.Object, list ObjectList) (map[string]unstructured.Unstructured, error) {
desiredIds := sets.String{}
for _, obj := range list.Items {
id, err := list.Identity(obj)
if err != nil {
return nil, errors.WithStack(err)
}
desiredIds.Insert(id)
}
// k: identify
// v: object
existingMap := map[string]unstructured.Unstructured{}
{ // Delete outdated resource
currentObjs := &unstructured.UnstructuredList{}
currentObjs.SetGroupVersionKind(list.GroupVersionKind)
if err := r.client.List(ctx, currentObjs, &client.ListOptions{Namespace: parent.GetNamespace()}); err != nil {
return nil, errors.WithStack(err)
}
for _, obj := range currentObjs.Items {
if !r.ownedByParent(&obj, parent) {
continue
}
id, err := list.Identity(&obj)
if err != nil {
return nil, errors.WithStack(err)
}
if desiredIds.Has(id) {
existingMap[id] = obj
continue
}
// reaching here means the object is no longer required, because
// - it is owned by the parent object we are reconciling
// - and it's identity is not present in the given list
if err := r.client.Delete(ctx, &obj); err != nil {
return nil, errors.WithStack(err)
}
}
}
return existingMap, nil
}
func (r refresher) ownedByParent(dependent client.Object, parent client.Object) bool {
parentGVK := parent.GetObjectKind().GroupVersionKind()
for _, ref := range dependent.GetOwnerReferences() {
if ref.Name != parent.GetName() {
continue
}
if ref.APIVersion != parentGVK.GroupVersion().String() {
continue
}
if ref.Kind != parentGVK.Kind {
continue
}
return true
}
return false
}
func createResourceName(format, first, last string) string {
name := fmt.Sprintf(format, first, last)
length := float64(len(name))
// Margin so as not to exceed 63 characters
if length < 60 {
return name
}
restLen := length - 60
firstLen := float64(len(first))
lastLen := float64(len(last))
// Calculate the amount of text to be removed to get the same ratio.
firstPos := int(firstLen - (firstLen / length * restLen))
lastPos := int(lastLen - (lastLen / length * restLen))
return fmt.Sprintf(format, first[:firstPos], last[:lastPos])
}