-
Notifications
You must be signed in to change notification settings - Fork 9
/
generation.go
97 lines (81 loc) · 2.86 KB
/
generation.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
// 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 kubeobserver
import (
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"namespacelabs.dev/foundation/framework/rpcerrors/multierr"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/runtime/kubernetes/client"
"namespacelabs.dev/foundation/schema/orchestration"
"namespacelabs.dev/foundation/std/tasks"
)
type WaitOnGenerationCondition struct {
RestConfig *rest.Config
Name, Namespace string
ExpectedGeneration int64
ConditionType string
Resource schema.GroupVersionResource
}
func (w WaitOnGenerationCondition) WaitUntilReady(ctx context.Context, ch chan *orchestration.Event) error {
if ch != nil {
defer close(ch)
}
return tasks.Action("kubernetes.wait-on-condition").
Arg("group_version", w.Resource.GroupVersion()).
Arg("resource", w.Resource.Resource).
Arg("name", w.Name).
Arg("namespace", w.Namespace).
Run(ctx, func(ctx context.Context) error {
cli, err := client.MakeGroupVersionBasedClient(ctx, w.RestConfig, w.Resource.GroupVersion())
if err != nil {
return err
}
// XXX use watch.
return client.PollImmediateWithContext(ctx, 500*time.Millisecond, 5*time.Minute, func(c context.Context) (bool, error) {
opts := metav1.GetOptions{}
req := cli.Get().Resource(w.Resource.Resource).Namespace(w.Namespace).Name(w.Name).Body(&opts)
var res unstructured.Unstructured
if err := req.Do(ctx).Into(&res); err != nil {
if errors.IsNotFound(err) {
return false, nil
} else {
return false, err
}
}
conditions, has, err := unstructured.NestedSlice(res.Object, "status", "conditions")
if err != nil {
return false, err
}
if !has {
return false, err
}
for _, cond := range conditions {
if m, ok := cond.(map[string]interface{}); ok {
gen, has1, err1 := unstructured.NestedInt64(m, "observedGeneration")
typ, has2, err2 := unstructured.NestedString(m, "type")
if err1 == nil && err2 == nil && has1 && has2 {
return w.ExpectedGeneration == gen && w.ConditionType == typ, nil
}
errs := []error{err1, err2}
if !has1 {
errs = append(errs, fnerrors.BadInputError("expected observedGeneration"))
}
if !has2 {
errs = append(errs, fnerrors.BadInputError("expected type"))
}
fmt.Fprintf(console.Debug(ctx), "WaitOnGenerationCondition cycle failed: %v\n", multierr.New(errs...))
}
}
return false, nil
})
})
}