-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.go
191 lines (164 loc) · 6.44 KB
/
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
package api
import (
"errors"
infrastructurev1beta1 "github.com/rancher-sandbox/cluster-api-provider-elemental/api/v1beta1"
"golang.org/x/exp/maps"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
)
var ErrBootstrapSecretNoConfig = errors.New("CAPI bootstrap secret does not contain any config")
type HostCreateRequest struct {
Auth string `header:"Authorization"`
RegAuth string `header:"Registration-Authorization"`
Namespace string `path:"namespace"`
RegistrationName string `path:"registrationName"`
Name string `json:"name,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
PubKey string `json:"pubKey,omitempty"`
}
func (h *HostCreateRequest) toElementalHost(namespace string) infrastructurev1beta1.ElementalHost {
return infrastructurev1beta1.ElementalHost{
ObjectMeta: metav1.ObjectMeta{
Name: h.Name,
Namespace: namespace,
Labels: h.Labels,
Annotations: h.Annotations,
},
Spec: infrastructurev1beta1.ElementalHostSpec{
PubKey: h.PubKey,
},
}
}
type HostDeleteRequest struct {
Auth string `header:"Authorization"`
Namespace string `path:"namespace"`
RegistrationName string `path:"registrationName"`
HostName string `path:"hostName"`
}
type HostPatchRequest struct {
Auth string `header:"Authorization"`
Namespace string `path:"namespace"`
RegistrationName string `path:"registrationName"`
HostName string `path:"hostName"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Bootstrapped *bool `json:"bootstrapped,omitempty"`
Installed *bool `json:"installed,omitempty"`
Reset *bool `json:"reset,omitempty"`
Condition *clusterv1.Condition `json:"condition,omitempty"`
}
func (h *HostPatchRequest) SetCondition(conditionType clusterv1.ConditionType, status corev1.ConditionStatus, severity clusterv1.ConditionSeverity, reason string, message string) {
h.Condition = &clusterv1.Condition{
Type: conditionType,
Status: status,
Severity: severity,
Reason: reason,
Message: message,
}
}
func (h *HostPatchRequest) applyToElementalHost(elementalHost *infrastructurev1beta1.ElementalHost) {
if elementalHost.Annotations == nil {
elementalHost.Annotations = map[string]string{}
}
if elementalHost.Labels == nil {
elementalHost.Labels = map[string]string{}
}
maps.Copy(elementalHost.Annotations, h.Annotations)
maps.Copy(elementalHost.Labels, h.Labels)
// Map request values to ElementalHost labels
if h.Installed != nil {
elementalHost.Labels[infrastructurev1beta1.LabelElementalHostInstalled] = "true"
}
if h.Bootstrapped != nil {
elementalHost.Labels[infrastructurev1beta1.LabelElementalHostBootstrapped] = "true"
}
if h.Reset != nil {
elementalHost.Labels[infrastructurev1beta1.LabelElementalHostReset] = "true"
}
if elementalHost.Status.Conditions == nil {
elementalHost.Status.Conditions = clusterv1.Conditions{}
}
// Set the patch condition to the ElementalHost object.
conditions.Set(elementalHost, h.Condition)
// Always update the Summary after conditions change
conditions.SetSummary(elementalHost)
}
type HostResponse struct {
Name string `json:"name,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
BootstrapReady bool `json:"bootstrapReady,omitempty"`
Bootstrapped bool `json:"bootstrapped,omitempty"`
Installed bool `json:"installed,omitempty"`
NeedsReset bool `json:"needsReset,omitempty"`
}
func (h *HostResponse) fromElementalHost(elementalHost infrastructurev1beta1.ElementalHost) {
h.Name = elementalHost.Name
h.Annotations = elementalHost.Annotations
h.Labels = elementalHost.Labels
h.BootstrapReady = elementalHost.Spec.BootstrapSecret != nil
if elementalHost.Labels == nil {
return
}
// Map ElementalHost labels to response values
if value, found := elementalHost.Labels[infrastructurev1beta1.LabelElementalHostBootstrapped]; found && value == "true" {
h.Bootstrapped = true
}
if value, found := elementalHost.Labels[infrastructurev1beta1.LabelElementalHostInstalled]; found && value == "true" {
h.Installed = true
}
if value, found := elementalHost.Labels[infrastructurev1beta1.LabelElementalHostNeedsReset]; found && value == "true" {
h.NeedsReset = true
}
}
type RegistrationGetRequest struct {
RegAuth string `header:"Registration-Authorization"`
Namespace string `path:"namespace"`
RegistrationName string `path:"registrationName"`
}
type RegistrationResponse struct {
// HostLabels are labels propagated to each ElementalHost object linked to this registration.
// +optional
HostLabels map[string]string `json:"hostLabels,omitempty"`
// HostAnnotations are labels propagated to each ElementalHost object linked to this registration.
// +optional
HostAnnotations map[string]string `json:"hostAnnotations,omitempty"`
// Config points to Elemental machine configuration.
// +optional
Config infrastructurev1beta1.Config `json:"config,omitempty"`
}
func (r *RegistrationResponse) fromElementalRegistration(elementalRegistration infrastructurev1beta1.ElementalRegistration) {
r.HostLabels = elementalRegistration.Spec.HostLabels
r.HostAnnotations = elementalRegistration.Spec.HostAnnotations
r.Config = elementalRegistration.Spec.Config
}
type BootstrapGetRequest struct {
Auth string `header:"Authorization"`
Namespace string `path:"namespace"`
RegistrationName string `path:"registrationName"`
HostName string `path:"hostName"`
}
type BootstrapResponse struct {
Format string `json:"format"`
Config string `json:"config"`
}
type WriteFile struct {
Path string `json:"path" yaml:"path"`
Owner string `json:"owner" yaml:"owner"`
Permissions string `json:"permissions" yaml:"permissions"`
Content string `json:"content" yaml:"content"`
}
func (b *BootstrapResponse) fromSecret(secret *corev1.Secret) error {
b.Format = "cloud-config" // Assume 'cloud-config' by default.
if format, found := secret.Data["format"]; found {
b.Format = string(format)
}
if config, found := secret.Data["value"]; found {
b.Config = string(config)
return nil
}
return ErrBootstrapSecretNoConfig
}