-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.go
131 lines (113 loc) · 4.45 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
package api
import (
"fmt"
infrastructurev1beta1 "github.com/rancher-sandbox/cluster-api-provider-elemental/api/v1beta1"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type HostCreateRequest struct {
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"`
}
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,
},
}
}
type HostDeleteRequest struct {
Namespace string `path:"namespace"`
RegistrationName string `path:"registrationName"`
HostName string `path:"hostName"`
}
type HostPatchRequest struct {
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) {
elementalHost.Annotations = h.Annotations
elementalHost.Labels = h.Labels
if h.Installed != nil {
elementalHost.Status.Installed = *h.Installed
}
if h.Bootstrapped != nil {
elementalHost.Status.Bootstrapped = *h.Bootstrapped
}
if h.Reset != nil {
elementalHost.Status.Reset = *h.Reset
}
}
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
h.Bootstrapped = elementalHost.Status.Bootstrapped
h.Installed = elementalHost.Status.Installed
h.NeedsReset = elementalHost.Status.NeedsReset
}
type RegistrationGetRequest struct {
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 {
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
}