-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.go
157 lines (138 loc) · 4.34 KB
/
scope.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
package kgen
import (
"fmt"
"iter"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
type Scope interface {
// ID returns the identifier of the scope.
ID() string
// Namespace returns the namespace of the scope. It searches the current scope and its parents.
Namespace() string
// CreateScope creates a new scope, nested under the current scope.
CreateScope(id string, props ScopeProps) Scope
// GetContext returns the value of the given context key. It searches the current scope and its parents.
GetContext(key string) any
// SetContext sets the value of the given context key.
SetContext(key string, value any)
// AddApiObject adds a new API object to the scope.
AddApiObject(obj runtime.Object) ApiObject
// AddApiObjectFromMap adds a new API object to the scope from an arbitrary map.
AddApiObjectFromMap(props map[string]any) ApiObject
// WalkApiObjects walks through all the API objects in the scope and its children.
WalkApiObjects(walkFn func(ApiObject) error) error
// Children returns the child scopes of the current scope.
Children() iter.Seq[Scope]
// Logger returns the logger that was passed to the builder.
Logger() Logger
}
// ScopeProps is the properties for creating a new scope.
type ScopeProps struct {
// Namespace is the default kubernetes namespace that should be used for the k8s resources in the scope.
Namespace string
}
type scope struct {
id string
globalContext *globalContext
context map[string]any
parent *scope
children []*scope
objects []ApiObject
}
func newScope(id string, props ScopeProps, globalContext *globalContext) Scope {
scope := &scope{
id: id,
context: map[string]any{},
globalContext: globalContext,
}
if props.Namespace != "" {
scope.context[namespaceContextKey] = props.Namespace
}
return scope
}
func (s *scope) SetContext(key string, value any) {
s.context[key] = value
}
func (s *scope) GetContext(key string) any {
for s := s; s != nil; s = s.parent {
if ctx, ok := s.context[key]; ok {
return ctx
}
}
return s.context[key]
}
func (s *scope) ID() string {
return s.id
}
func (s *scope) CreateScope(id string, props ScopeProps) Scope {
childScope := newScope(id, props, s.globalContext).(*scope)
childScope.parent = s
s.children = append(s.children, childScope)
return childScope
}
func (s *scope) Namespace() string {
return s.GetContext(namespaceContextKey).(string)
}
func (s *scope) addApiObject(obj runtime.Object) (ApiObject, error) {
groupVersionKinds, _, err := s.globalContext.scheme.ObjectKinds(obj)
if err != nil {
return nil, fmt.Errorf("ObjectKinds: %w", err)
}
if len(groupVersionKinds) != 1 {
return nil, fmt.Errorf("expected 1 groupVersionKind, got %d: %v", len(groupVersionKinds), groupVersionKinds)
}
groupVersion := groupVersionKinds[0]
mobj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, fmt.Errorf("k8sObjectToMap: %w", err)
}
mobj["apiVersion"] = groupVersion.GroupVersion().String()
mobj["kind"] = groupVersion.Kind
return s.AddApiObjectFromMap(mobj), nil
}
func (s *scope) AddApiObject(obj runtime.Object) ApiObject {
if apiObject, err := s.addApiObject(obj); err != nil {
s.Logger().Panicf("failed to add api object: %v", err)
return nil
} else {
return apiObject
}
}
func (s *scope) AddApiObjectFromMap(obj map[string]any) ApiObject {
props := apiObjectProps{Unstructured: &unstructured.Unstructured{Object: obj}}
if props.GetNamespace() == "" {
namespaceCtx, _ := s.GetContext(namespaceContextKey).(string)
if namespaceCtx != "" {
props.SetNamespace(namespaceCtx)
}
}
apiObject := &apiObject{apiObjectProps: props, globalContext: s.globalContext}
s.objects = append(s.objects, apiObject)
return apiObject
}
func (s *scope) WalkApiObjects(walkFn func(ApiObject) error) error {
for _, object := range s.objects {
if err := walkFn(object); err != nil {
return fmt.Errorf("walk objects: %w", err)
}
}
for _, childNode := range s.children {
if err := childNode.WalkApiObjects(walkFn); err != nil {
return fmt.Errorf("walk children: %w", err)
}
}
return nil
}
func (s *scope) Children() iter.Seq[Scope] {
return func(yield func(Scope) bool) {
for _, child := range s.children {
if !yield(child) {
return
}
}
}
}
func (s *scope) Logger() Logger {
return s.globalContext.logger
}