-
Notifications
You must be signed in to change notification settings - Fork 9
/
op_apply.go
354 lines (301 loc) · 11.5 KB
/
op_apply.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2022 Namespace Labs Inc; 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.
package kubeops
import (
"context"
"encoding/json"
"fmt"
"google.golang.org/protobuf/types/known/timestamppb"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/discovery"
k8s "k8s.io/client-go/kubernetes"
"namespacelabs.dev/foundation/framework/kubernetes/kubedef"
"namespacelabs.dev/foundation/framework/kubernetes/kubeobj"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime/kubernetes"
"namespacelabs.dev/foundation/internal/runtime/kubernetes/client"
kobs "namespacelabs.dev/foundation/internal/runtime/kubernetes/kubeobserver"
fnschema "namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/schema/orchestration"
"namespacelabs.dev/foundation/std/execution"
"namespacelabs.dev/foundation/std/tasks"
)
func registerApply() {
execution.RegisterVFuncs(execution.VFuncs[*kubedef.OpApply, *parsedApply]{
Parse: func(ctx context.Context, def *fnschema.SerializedInvocation, apply *kubedef.OpApply) (*parsedApply, error) {
if apply.BodyJson == "" {
return nil, fnerrors.InternalError("apply.Body is required")
}
var parsed unstructured.Unstructured
if err := json.Unmarshal([]byte(apply.BodyJson), &parsed); err != nil {
return nil, fnerrors.BadInputError("kubernetes.apply: failed to parse resource: %w", err)
}
return &parsedApply{obj: &parsed, spec: apply}, nil
},
Handle: func(ctx context.Context, d *fnschema.SerializedInvocation, parsed *parsedApply) (*execution.HandleResult, error) {
return apply(ctx, d.Description, fnschema.PackageNames(d.Scope...), parsed.obj, parsed.spec, nil)
},
PlanOrder: func(ctx context.Context, apply *parsedApply) (*fnschema.ScheduleOrder, error) {
return kubedef.PlanOrder(apply.obj.GroupVersionKind(), apply.obj.GetNamespace(), apply.obj.GetName()), nil
},
})
}
type parsedApply struct {
obj *unstructured.Unstructured
spec *kubedef.OpApply
}
func apply(ctx context.Context, desc string, scope []fnschema.PackageName, obj kubeobj.Object, spec *kubedef.OpApply, ch chan *orchestration.Event) (*execution.HandleResult, error) {
gv := obj.GroupVersionKind().GroupVersion()
if gv.Version == "" {
return nil, fnerrors.InternalError("%s: APIVersion is required", desc)
}
cluster, err := kubedef.InjectedKubeCluster(ctx)
if err != nil {
return nil, err
}
restcfg := cluster.PreparedClient().RESTConfig
var resource *schema.GroupVersionResource
var res unstructured.Unstructured
action := tasks.Action("kubernetes.apply").
Scope(scope...).
HumanReadablef(desc).
Arg("name", obj.GetName())
ns := obj.GetNamespace()
// We don't try to set namespaces to Namespaces. But for other resources, we use discovery to determine
// whether the resource is namespaced or not.
if spec.SetNamespace && ns == "" && !kubeobj.IsGVKNamespace(obj.GroupVersionKind()) {
// Don't know if the resource is namespace scoped or not -- need to go find out.
rawDisco, err := cluster.EnsureState(ctx, kubernetes.DiscoveryStateKey)
if err != nil {
return nil, err
}
disco := rawDisco.(discovery.DiscoveryInterface)
res, err := disco.ServerResourcesForGroupVersion(obj.GroupVersionKind().GroupVersion().String())
if err != nil {
return nil, err
}
namespaced := false
for _, resource := range res.APIResources {
if resource.Kind == obj.GroupVersionKind().Kind && resource.Namespaced {
c, err := kubedef.InjectedKubeClusterNamespace(ctx)
if err != nil {
return nil, err
}
ns = c.KubeConfig().Namespace
namespaced = true
break
}
}
fmt.Fprintf(console.Debug(ctx), "kubernetes: apply: namespace is not set, checking if %q in %q is namespaced: %v\n",
obj.GroupVersionKind().Kind, obj.GroupVersionKind().GroupKind().String(), namespaced)
}
if ns != "" {
action = action.Arg("namespace", ns)
}
if err := action.RunWithOpts(ctx, tasks.RunOpts{
Wait: func(ctx context.Context) (bool, error) {
var err error
resource, err = resolveResource(ctx, cluster, obj.GroupVersionKind())
if err != nil {
return false, err
}
tasks.Attachments(ctx).AddResult("resource", resource.Resource)
// Creating Deployments and Statefulsets that refer to the
// default service account, requires that the default service
// account actually exists. And creating the default service
// account takes a bit of time after creating a namespace.
waitOnNamespace, err := requiresWaitForNamespace(obj, spec)
if err != nil {
return false, fnerrors.InternalError("failed to determine object namespace: %w", err)
}
if waitOnNamespace {
if err := waitForDefaultServiceAccount(ctx, cluster.PreparedClient().Clientset, obj.GetNamespace()); err != nil {
return false, err
}
}
return false, nil
},
Run: func(ctx context.Context) error {
client, err := client.MakeGroupVersionBasedClient(ctx, restcfg, resource.GroupVersion())
if err != nil {
return fnerrors.InternalError("failed to create client: %w", err)
}
patchOpts := kubedef.Ego().ToPatchOptions()
req := client.Patch(types.ApplyPatchType)
if ns != "" {
req = req.Namespace(ns)
}
prepReq := req.Resource(resource.Resource).
Name(obj.GetName()).
VersionedParams(&patchOpts, metav1.ParameterCodec).
Body([]byte(spec.BodyJson))
if OutputKubeApiURLs {
fmt.Fprintf(console.Debug(ctx), "kubernetes: api patch call %q\n", prepReq.URL())
}
return prepReq.Do(ctx).Into(&res)
}}); err != nil {
return nil, fnerrors.InvocationError("kubernetes", "%s: failed to apply: %w", desc, err)
}
if err := checkResetCRDCache(ctx, cluster, obj.GroupVersionKind()); err != nil {
return nil, err
}
if spec.CheckGenerationCondition.GetType() != "" {
generation, found1, err1 := unstructured.NestedInt64(res.Object, "metadata", "generation")
if err1 != nil {
return nil, fnerrors.InternalError("failed to wait on resource: %w", err)
}
if !found1 {
return nil, fnerrors.InternalError("failed to wait on resource: no metadata.generation")
}
return &execution.HandleResult{
Waiter: kobs.WaitOnGenerationCondition{
RestConfig: restcfg,
Namespace: ns,
Name: obj.GetName(),
ExpectedGeneration: generation,
ConditionType: spec.CheckGenerationCondition.Type,
Resource: *resource,
}.WaitUntilReady,
}, nil
}
if spec.InhibitEvents {
return nil, nil
}
if ch != nil {
switch {
case kubeobj.IsDeployment(obj), kubeobj.IsStatefulSet(obj), kubeobj.IsPod(obj), kubeobj.IsDaemonSet(obj):
ev := kobs.PrepareEvent(obj.GroupVersionKind(), ns, obj.GetName(), desc, spec.Deployable)
ev.Stage = orchestration.Event_COMMITTED
ev.WaitStatus = append(ev.WaitStatus, &orchestration.Event_WaitStatus{
Description: "Committed...",
})
ch <- ev
}
}
switch {
case kubeobj.IsDeployment(obj), kubeobj.IsStatefulSet(obj), kubeobj.IsDaemonSet(obj):
generation, found1, err1 := unstructured.NestedInt64(res.Object, "metadata", "generation")
observedGen, found2, err2 := unstructured.NestedInt64(res.Object, "status", "observedGeneration")
if err2 != nil || !found2 {
observedGen = 0 // Assume no generation exists.
}
// XXX print a warning if expected fields are missing.
if err1 == nil && found1 {
w := kobs.WaitOnResource{
RestConfig: restcfg,
Description: desc,
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
GroupVersionKind: obj.GroupVersionKind(),
PreviousGen: observedGen,
ExpectedGen: generation,
}
if spec.Deployable != nil {
w.Scope = spec.Deployable.GetPackageRef().AsPackageName()
} else if spec.Creator != nil {
w.Scope = spec.Creator.AsPackageName()
}
return &execution.HandleResult{Waiter: w.WaitUntilReady}, nil
} else {
fmt.Fprintf(console.Warnings(ctx), "missing generation data from %s: %v / %v [found1=%v found2=%v]\n",
obj.GroupVersionKind().Kind, err1, err2, found1, found2)
}
case kubeobj.IsPod(obj):
return &execution.HandleResult{
Waiter: func(ctx context.Context, ch chan *orchestration.Event) error {
if ch != nil {
defer close(ch)
}
return kobs.WaitForCondition(ctx, cluster.PreparedClient().Clientset, tasks.Action("pod.wait").Scope(scope...),
kobs.WaitForPodConditition(ns, kobs.PickPod(obj.GetName()),
func(ps v1.PodStatus) (bool, error) {
meta, err := json.Marshal(ps)
if err != nil {
return false, fnerrors.InternalError("failed to marshal pod status: %w", err)
}
ev := kobs.PrepareEvent(obj.GroupVersionKind(), ns, obj.GetName(), desc, spec.Deployable)
ev.Stage = orchestration.Event_WAITING
ev.ImplMetadata = meta
ev.WaitStatus = append(ev.WaitStatus, kobs.PodStatusToWaitStatus(ns, obj.GetName(), ps))
ev.Timestamp = timestamppb.Now()
var done bool
if ps.Phase == v1.PodFailed || ps.Phase == v1.PodSucceeded {
// If the pod is finished, then don't wait further.
done = true
} else if ps.Phase == v1.PodRunning && spec.Deployable.IsOneShot() {
ev.Ready = orchestration.Event_READY
ev.Stage = orchestration.Event_RUNNING
} else {
if _, isReady := kobs.MatchPodCondition(ps, v1.PodReady); isReady {
done = true
}
}
if done {
ev.Ready = orchestration.Event_READY
ev.Stage = orchestration.Event_DONE
}
if ch != nil {
ch <- ev
}
return done, nil
}))
}}, nil
}
return nil, nil
}
func requiresWaitForNamespace(obj kubeobj.Object, spec *kubedef.OpApply) (bool, error) {
switch {
case kubeobj.IsDeployment(obj):
// XXX change to unstructured lookups.
var d appsv1.Deployment
if err := json.Unmarshal([]byte(spec.BodyJson), &d); err != nil {
return false, err
}
if d.Spec.Template.Spec.ServiceAccountName == "" || d.Spec.Template.Spec.ServiceAccountName == "default" {
return true, nil
}
case kubeobj.IsStatefulSet(obj):
var d appsv1.StatefulSet
if err := json.Unmarshal([]byte(spec.BodyJson), &d); err != nil {
return false, err
}
if d.Spec.Template.Spec.ServiceAccountName == "" || d.Spec.Template.Spec.ServiceAccountName == "default" {
return true, nil
}
case kubeobj.IsPod(obj):
var d v1.Pod
if err := json.Unmarshal([]byte(spec.BodyJson), &d); err != nil {
return false, err
}
if d.Spec.ServiceAccountName == "" || d.Spec.ServiceAccountName == "default" {
return true, nil
}
}
return false, nil
}
func waitForDefaultServiceAccount(ctx context.Context, c *k8s.Clientset, namespace string) error {
return tasks.Action("kubernetes.apply.wait-for-namespace").Arg("name", namespace).Run(ctx, func(ctx context.Context) error {
w, err := c.CoreV1().ServiceAccounts(namespace).Watch(ctx, metav1.ListOptions{})
if err != nil {
return fnerrors.InternalError("kubernetes: failed to wait until the namespace was ready: %w", err)
}
defer w.Stop()
// Wait until the default service account has been created.
for ev := range w.ResultChan() {
if account, ok := ev.Object.(*v1.ServiceAccount); ok && ev.Type == watch.Added {
if account.Name == "default" {
return nil // Service account is ready.
}
}
}
return nil
})
}