/
bootserver.go
235 lines (198 loc) · 7.76 KB
/
bootserver.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package server
import (
"fmt"
"net"
"net/http"
"path"
"path/filepath"
"text/template"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
butaneconfig "github.com/coreos/butane/config"
butanecommon "github.com/coreos/butane/config/common"
"github.com/go-logr/logr"
bootv1alpha1 "github.com/ironcore-dev/ipxe-operator/api/v1alpha1"
)
type IPXETemplateData struct {
KernelURL string
InitrdURL string
SquashfsURL string
RegistryURL string
IPXEServerURL string
}
func RunBootServer(ipxeServerAddr string, ipxeServiceURL string, k8sClient client.Client, log logr.Logger, defaultIpxeTemplateData IPXETemplateData) {
http.HandleFunc("/ipxe", func(w http.ResponseWriter, r *http.Request) {
handleIPXE(w, r, k8sClient, log, ipxeServiceURL, defaultIpxeTemplateData)
})
http.HandleFunc("/ignition/", func(w http.ResponseWriter, r *http.Request) {
uuid := path.Base(r.URL.Path)
if uuid == "" {
http.Error(w, "Bad Request: UUID is required", http.StatusBadRequest)
return
}
ipxeBootConfigList := &bootv1alpha1.IPXEBootConfigList{}
err := k8sClient.List(r.Context(), ipxeBootConfigList, client.MatchingFields{"spec.systemUUID": uuid})
if client.IgnoreNotFound(err) != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if len(ipxeBootConfigList.Items) == 0 {
log.Info("No IPXEBootConfig found with given UUID")
handleIgnitionHTTPBoot(w, r, k8sClient, log, uuid)
} else {
handleIgnitionIPXEBoot(w, r, k8sClient, log, uuid)
}
})
log.Info("Starting boot server", "address", ipxeServerAddr)
if err := http.ListenAndServe(ipxeServerAddr, nil); err != nil {
log.Error(err, "failed to start boot server")
panic(err)
}
}
func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client, log logr.Logger, ipxeServiceURL string, defaultIpxeTemplateData IPXETemplateData) {
log.Info("Processing IPXE request", "method", r.Method, "path", r.URL.Path, "clientIP", r.RemoteAddr)
ctx := r.Context()
clientIP, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Error(err, "Failed to parse client IP address", "clientIP", r.RemoteAddr)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
var ipxeConfigs bootv1alpha1.IPXEBootConfigList
if err := k8sClient.List(ctx, &ipxeConfigs, client.MatchingFields{"spec.systemIP": clientIP}); err != nil {
log.Info("Failed to list IPXEBootConfig", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
data := defaultIpxeTemplateData
if len(ipxeConfigs.Items) == 0 {
log.Info("No IPXEBootConfig found for client IP, delivering default script", "clientIP", clientIP)
} else {
config := ipxeConfigs.Items[0]
data = IPXETemplateData{
KernelURL: config.Spec.KernelURL,
InitrdURL: config.Spec.InitrdURL,
SquashfsURL: config.Spec.SquashfsURL,
IPXEServerURL: ipxeServiceURL,
}
}
tmplPath := filepath.Join("templates", "ipxe-script.tpl")
tmpl, err := template.ParseFiles(tmplPath)
if err != nil {
log.Info("Failed to parse iPXE script template", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
if err := tmpl.Execute(w, data); err != nil {
log.Info("Failed to execute template", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
log.Info("Successfully generated iPXE script", "clientIP", clientIP)
_, err = w.Write(nil)
if err != nil {
log.Info("Failed to write the ipxe http response", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
func handleIgnitionIPXEBoot(w http.ResponseWriter, r *http.Request, k8sClient client.Client, log logr.Logger, uuid string) {
log.Info("Processing Ignition request", "method", r.Method, "path", r.URL.Path, "clientIP", r.RemoteAddr)
ctx := r.Context()
ipxeBootConfigList := &bootv1alpha1.IPXEBootConfigList{}
if err := k8sClient.List(ctx, ipxeBootConfigList, client.MatchingFields{"spec.systemUUID": uuid}); err != nil {
http.Error(w, "Resource Not Found", http.StatusNotFound)
log.Info("Failed to find IPXEBootConfig", "error", err.Error())
return
}
if len(ipxeBootConfigList.Items) == 0 {
http.Error(w, "Resource Not Found", http.StatusNotFound)
log.Info("No IPXEBootConfig found with given UUID")
return
}
// TODO: Assuming UUID is unique.
ipxeBootConfig := ipxeBootConfigList.Items[0]
ignitionSecret := &corev1.Secret{}
if err := k8sClient.Get(ctx, client.ObjectKey{Name: ipxeBootConfig.Spec.IgnitionSecretRef.Name, Namespace: ipxeBootConfig.Namespace}, ignitionSecret); err != nil {
http.Error(w, "Resource Not Found", http.StatusNotFound)
log.Info("Error: Failed to get Ignition secret", "error", err.Error())
return
}
ignitionData, ok := ignitionSecret.Data[bootv1alpha1.DefaultIgnitionKey]
if !ok {
http.Error(w, "Resource Not Found", http.StatusNotFound)
log.Info("Error: Ignition data not found in secret")
return
}
ignitionJSONData, err := renderIgnition(ignitionData)
if err != nil {
log.Info("Failed to render the ignition data to json", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(ignitionJSONData)
if err != nil {
log.Info("Failed to write the ignition http response", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
func handleIgnitionHTTPBoot(w http.ResponseWriter, r *http.Request, k8sClient client.Client, log logr.Logger, uuid string) {
log.Info("Processing Ignition request", "method", r.Method, "path", r.URL.Path, "clientIP", r.RemoteAddr)
ctx := r.Context()
HTTPBootConfigList := &bootv1alpha1.HTTPBootConfigList{}
if err := k8sClient.List(ctx, HTTPBootConfigList, client.MatchingFields{"spec.systemUUID": uuid}); err != nil {
http.Error(w, "Resource Not Found", http.StatusNotFound)
log.Info("Failed to find HTTPBootConfigList", "error", err.Error())
return
}
if len(HTTPBootConfigList.Items) == 0 {
http.Error(w, "Resource Not Found", http.StatusNotFound)
log.Info("No HTTPBootConfig found with given UUID")
return
}
// TODO: Assuming UUID is unique.
HTTPBootConfig := HTTPBootConfigList.Items[0]
ignitionSecret := &corev1.Secret{}
if err := k8sClient.Get(ctx, client.ObjectKey{Name: HTTPBootConfig.Spec.IgnitionSecretRef.Name, Namespace: HTTPBootConfig.Namespace}, ignitionSecret); err != nil {
http.Error(w, "Resource Not Found", http.StatusNotFound)
log.Info("Error: Failed to get Ignition secret", "error", err.Error())
return
}
ignitionData, ok := ignitionSecret.Data[bootv1alpha1.DefaultIgnitionKey]
if !ok {
http.Error(w, "Resource Not Found", http.StatusNotFound)
log.Info("Error: Ignition data not found in secret")
return
}
ignitionJSONData, err := renderIgnition(ignitionData)
if err != nil {
log.Info("Failed to render the ignition data to json", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
_, err = w.Write(ignitionJSONData)
if err != nil {
log.Info("Failed to write the ignition http response", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
}
func renderIgnition(yamlData []byte) ([]byte, error) {
translateOptions := butanecommon.TranslateBytesOptions{
Raw: true,
Pretty: false,
TranslateOptions: butanecommon.TranslateOptions{
NoResourceAutoCompression: true,
},
}
jsonData, _, err := butaneconfig.TranslateBytes(yamlData, translateOptions)
if err != nil {
return nil, fmt.Errorf("translation error from butane %w", err)
}
return jsonData, nil
}