-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.go
164 lines (141 loc) · 5.58 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
package api
import (
"fmt"
infrastructurev1beta1 "github.com/rancher-sandbox/cluster-api-provider-elemental/api/v1beta1"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
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"`
}
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"
}
}
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 {
Files []WriteFile `json:"write_files" yaml:"write_files"` //nolint:tagliatelle //Matching cloud-init schema
Commands []string `json:"runcmd" yaml:"runcmd"`
}
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 {
data := secret.Data["value"]
if err := yaml.Unmarshal(data, b); err != nil {
return fmt.Errorf("unmarshalling bootstrap secret value: %w", err)
}
return nil
}