-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
318 lines (298 loc) · 9.59 KB
/
controller.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
311
312
313
314
315
316
317
318
package compose
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
"github.com/rancher/norman/clientbase"
"github.com/rancher/norman/controller"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/controllers/management/compose/common"
"github.com/rancher/types/apis/management.cattle.io/v3"
clusterClient "github.com/rancher/types/client/cluster/v3"
managementClient "github.com/rancher/types/client/management/v3"
projectClient "github.com/rancher/types/client/project/v3"
"github.com/rancher/types/compose"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
const (
composeTokenPrefix = "compose-token-"
description = "token for compose"
url = "https://localhost:%v/v3"
)
// Lifecycle for GlobalComposeConfig is a controller which watches composeConfig and execute the yaml config and create a bunch of global resources. There is no sync logic between yaml file and resources, which means config is only executed once. And resource is not deleted even if the compose config is deleted.
type Lifecycle struct {
TokenClient v3.TokenInterface
UserClient v3.UserInterface
UserManager user.Manager
HTTPSPortGetter common.KubeConfigGetter
ComposeClient v3.ComposeConfigInterface
}
func Register(managementContext *config.ManagementContext, portGetter common.KubeConfigGetter) {
composeClient := managementContext.Management.ComposeConfigs("")
tokenClient := managementContext.Management.Tokens("")
userClient := managementContext.Management.Users("")
l := Lifecycle{
HTTPSPortGetter: portGetter,
UserManager: managementContext.UserManager,
TokenClient: tokenClient,
UserClient: userClient,
ComposeClient: composeClient,
}
composeClient.AddHandler("compose-controller", l.sync)
}
func (l Lifecycle) sync(key string, obj *v3.ComposeConfig) error {
if key == "" || obj == nil {
return nil
}
newObj, err := v3.ComposeConditionExecuted.Once(obj, func() (runtime.Object, error) {
obj, err := l.Create(obj)
if err != nil {
return obj, &controller.ForgetError{
Err: err,
}
}
return obj, nil
})
_, err = l.ComposeClient.Update(newObj.(*v3.ComposeConfig))
return err
}
func (l Lifecycle) Create(obj *v3.ComposeConfig) (*v3.ComposeConfig, error) {
userID := obj.Annotations["field.cattle.io/creatorId"]
user, err := l.UserClient.Get(userID, metav1.GetOptions{})
if err != nil {
return obj, err
}
token, err := l.UserManager.EnsureToken(composeTokenPrefix+user.Name, description, user.Name)
if err != nil {
return obj, err
}
config := &compose.Config{}
if err := yaml.Unmarshal([]byte(obj.Spec.RancherCompose), config); err != nil {
return obj, err
}
if err := up(token, l.HTTPSPortGetter.GetHTTPSPort(), config); err != nil {
return obj, err
}
v3.ComposeConditionExecuted.True(obj)
return obj, nil
}
func GetSchemas(token string, port int) (map[string]types.Schema, map[string]types.Schema, map[string]types.Schema, error) {
cc, err := clusterClient.NewClient(&clientbase.ClientOpts{
URL: fmt.Sprintf(url, port) + "/clusters",
TokenKey: token,
Insecure: true,
})
if err != nil {
return nil, nil, nil, err
}
mc, err := managementClient.NewClient(&clientbase.ClientOpts{
URL: fmt.Sprintf(url, port),
TokenKey: token,
Insecure: true,
})
if err != nil {
return nil, nil, nil, err
}
pc, err := projectClient.NewClient(&clientbase.ClientOpts{
URL: fmt.Sprintf(url, port) + "/projects",
TokenKey: token,
Insecure: true,
})
if err != nil {
return nil, nil, nil, err
}
return cc.Types, mc.Types, pc.Types, nil
}
func up(token string, port int, config *compose.Config) error {
clusterSchemas, managementSchemas, projectSchemas, err := GetSchemas(token, port)
if err != nil {
return err
}
// referenceMap is a map of schemaType with name -> id value
referenceMap := map[string]map[string]string{}
rawData, err := json.Marshal(config)
if err != nil {
return err
}
rawMap := map[string]interface{}{}
if err := json.Unmarshal(rawData, &rawMap); err != nil {
return err
}
delete(rawMap, "version")
allSchemas := getAllSchemas(clusterSchemas, managementSchemas, projectSchemas)
sortedSchemas := common.SortSchema(allSchemas)
baseClusterClient, err := clientbase.NewAPIClient(&clientbase.ClientOpts{
URL: fmt.Sprintf(url, port) + "/cluster",
TokenKey: token,
Insecure: true,
})
if err != nil {
return err
}
baseManagementClient, err := clientbase.NewAPIClient(&clientbase.ClientOpts{
URL: fmt.Sprintf(url, port),
TokenKey: token,
Insecure: true,
})
baseProjectClient, err := clientbase.NewAPIClient(&clientbase.ClientOpts{
URL: fmt.Sprintf(url, port) + "/project",
TokenKey: token,
Insecure: true,
})
if err != nil {
return err
}
baseURL := fmt.Sprintf(url, port)
configManager := configClientManager{
clusterSchemas: clusterSchemas,
managementSchemas: managementSchemas,
projectSchemas: projectSchemas,
baseClusterClient: &baseClusterClient,
baseManagementClient: &baseManagementClient,
baseProjectClient: &baseProjectClient,
baseURL: baseURL,
}
for _, schemaKey := range sortedSchemas {
key := allSchemas[schemaKey].PluralName
v, ok := rawMap[key]
if !ok {
continue
}
value, ok := v.(map[string]interface{})
if !ok {
continue
}
var baseClient *clientbase.APIBaseClient
for name, data := range value {
dataMap, ok := data.(map[string]interface{})
if !ok {
break
}
baseClient, err = configManager.ConfigBaseClient(schemaKey, dataMap, referenceMap, "")
if err != nil {
return err
}
if err := common.ReplaceGlobalReference(allSchemas[schemaKey], dataMap, referenceMap, &baseManagementClient); err != nil {
return err
}
clusterID := convert.ToString(dataMap["clusterId"])
baseClient, err = configManager.ConfigBaseClient(schemaKey, dataMap, referenceMap, clusterID)
if err != nil {
return err
}
dataMap["name"] = name
respObj := map[string]interface{}{}
// in here we have to make sure the same name won't be created twice
created := map[string]string{}
if err := baseClient.List(schemaKey, &types.ListOpts{}, &respObj); err != nil {
return err
}
if data, ok := respObj["data"]; ok {
if collections, ok := data.([]interface{}); ok {
for _, obj := range collections {
if objMap, ok := obj.(map[string]interface{}); ok {
createdName := common.GetValue(objMap, "name")
if createdName != "" {
created[createdName] = common.GetValue(objMap, "id")
}
}
}
}
}
id := ""
if v, ok := created[name]; ok {
id = v
existing := &types.Resource{}
if err := baseClient.ByID(schemaKey, id, existing); err != nil {
return err
}
if err := baseClient.Update(schemaKey, existing, dataMap, nil); err != nil {
return err
}
} else {
if err := baseClient.Create(schemaKey, dataMap, &respObj); err != nil && !strings.Contains(err.Error(), "already exist") {
return err
} else if err != nil && strings.Contains(err.Error(), "already exist") {
break
}
v, ok := respObj["id"]
if !ok {
return errors.Errorf("id is missing after creating %s obj", schemaKey)
}
id = v.(string)
}
if f, ok := WaitCondition[schemaKey]; ok {
if err := f(baseClient, id, schemaKey); err != nil {
return err
}
}
}
// fill in reference map name -> id
if err := common.FillInReferenceMap(baseClient, schemaKey, referenceMap, nil); err != nil {
return err
}
}
return nil
}
type configClientManager struct {
clusterSchemas map[string]types.Schema
managementSchemas map[string]types.Schema
projectSchemas map[string]types.Schema
baseClusterClient *clientbase.APIBaseClient
baseManagementClient *clientbase.APIBaseClient
baseProjectClient *clientbase.APIBaseClient
baseURL string
}
// GetBaseClient config a baseClient with a special base url based on schema type
func (c configClientManager) ConfigBaseClient(schemaType string, data map[string]interface{}, referenceMap map[string]map[string]string, clusterID string) (*clientbase.APIBaseClient, error) {
if _, ok := c.clusterSchemas[schemaType]; ok {
c.baseClusterClient.Opts.URL = c.baseURL + fmt.Sprintf("/cluster/%s", clusterID)
return c.baseClusterClient, nil
}
if _, ok := c.managementSchemas[schemaType]; ok {
return c.baseManagementClient, nil
}
if _, ok := c.projectSchemas[schemaType]; ok {
projectName := common.GetValue(data, "projectId")
if _, ok := referenceMap["project"]; !ok {
filter := map[string]string{
"clusterId": clusterID,
}
if err := common.FillInReferenceMap(c.baseManagementClient, "project", referenceMap, filter); err != nil {
return nil, err
}
}
projectID := referenceMap["project"][projectName]
c.baseProjectClient.Opts.URL = c.baseURL + fmt.Sprintf("/projects/%s", projectID)
return c.baseProjectClient, nil
}
return nil, errors.Errorf("schema type %s not supported", schemaType)
}
func getAllSchemas(clusterSchemas, managementSchemas, projectSchemas map[string]types.Schema) map[string]types.Schema {
r := map[string]types.Schema{}
for k, schema := range clusterSchemas {
if _, ok := schema.ResourceFields["creatorId"]; !ok {
continue
}
r[k] = schema
}
for k, schema := range managementSchemas {
if _, ok := schema.ResourceFields["creatorId"]; !ok {
continue
}
r[k] = schema
}
for k, schema := range projectSchemas {
if _, ok := schema.ResourceFields["creatorId"]; !ok {
continue
}
r[k] = schema
}
return r
}