-
Notifications
You must be signed in to change notification settings - Fork 10
/
context_types.go
192 lines (160 loc) · 5.2 KB
/
context_types.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
/*
* Copyright (C) 2023 Appvia Ltd <info@appvia.io>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package v1alpha1
import (
"bytes"
"encoding/json"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
corev1alpha1 "github.com/appvia/terranetes-controller/pkg/apis/core/v1alpha1"
)
// ContextKind is the kind for a Context
const ContextKind = "Context"
const (
// ContextDescription is the description field name
ContextDescription = "description"
// ContextValue is the value field name
ContextValue = "value"
)
// NewContext creates a new Context
func NewContext(name string) *Context {
return &Context{
TypeMeta: metav1.TypeMeta{
Kind: ContextKind,
APIVersion: SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
}
// ContextSpec defines the desired state for a context
// +k8s:openapi-gen=true
type ContextSpec struct {
// Variables is a list of variables which can be used globally by Context resources.
// The structure of the variables is a map of key/value pairs, which MUST have both
// a description and a value.
// +kubebuilder:validation:Required
Variables map[string]runtime.RawExtension `json:"variables"`
}
// GetVariable returns the variable value if it exists
func (c *ContextSpec) GetVariable(key string) (interface{}, bool, error) {
if !c.HasVariables() {
return nil, false, nil
}
if c.Variables[key].Raw == nil {
return nil, false, nil
}
values := make(map[string]interface{})
if err := json.NewDecoder(bytes.NewReader(c.Variables[key].Raw)).Decode(&values); err != nil {
return nil, false, err
}
value, found := values["value"]
return value, found, nil
}
// GetVariableValue returns the string value of the a variable
func (c *ContextSpec) GetVariableValue(name string) (runtime.RawExtension, bool) {
if found := c.HasVariable(name); !found {
return runtime.RawExtension{}, false
}
return c.Variables[name], true
}
// HasVariables returns true if the context has variables defined
func (c *ContextSpec) HasVariables() bool {
return len(c.Variables) > 0
}
// HasVariable returns true if the context has variables defined
func (c *ContextSpec) HasVariable(name string) bool {
if !c.HasVariables() {
return false
}
value, found := c.Variables[name]
if !found {
return false
}
return len(value.Raw) > 0
}
// +kubebuilder:webhook:name=contexts.terraform.appvia.io,mutating=false,path=/validate/terraform.appvia.io/contexts,verbs=create;delete;update,groups="terraform.appvia.io",resources=contexts,versions=v1alpha1,failurePolicy=fail,sideEffects=None,admissionReviewVersions=v1
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// Context is the schema for the context type
// +k8s:openapi-gen=true
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=contexts,scope=Cluster,categories={terraform}
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
type Context struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ContextSpec `json:"spec,omitempty"`
Status ContextStatus `json:"status,omitempty"`
}
// ContextStatus defines the observed state of a terraform
// +k8s:openapi-gen=true
type ContextStatus struct {
corev1alpha1.CommonStatus `json:",inline"`
}
// GetNamespacedName returns the namespaced resource type
func (c *Context) GetNamespacedName() types.NamespacedName {
return types.NamespacedName{
Namespace: c.Namespace,
Name: c.Name,
}
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// ContextList contains a list of contexts
type ContextList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Context `json:"items"`
}
// HasItem returns true if the list contains the item name
func (c *ContextList) HasItem(name string) bool {
for _, item := range c.Items {
if item.Name == name {
return true
}
}
return false
}
// GetItem returns the item if the list contains the item name
func (c *ContextList) GetItem(name string) (Context, bool) {
for _, item := range c.Items {
if item.Name == name {
return item, true
}
}
return Context{}, false
}
// Merge is called to merge any items which don't exist in the list
func (c *ContextList) Merge(items []Context) {
if c.Items == nil {
c.Items = items
return
}
var adding []Context
for i := 0; i < len(items); i++ {
if c.HasItem(items[i].Name) {
continue
}
adding = append(adding, items[i])
}
if len(adding) > 0 {
c.Items = append(c.Items, adding...)
}
}