Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ func init() {

func main() {
ctx := ctrl.LoggerInto(ctrl.SetupSignalHandler(), setupLog)
defaultIpxeTemplateData := NewDefaultIPXETemplateData()
defaultHttpUKIURL := NewDefaultHTTPBootData()
skipControllerNameValidation := true

Expand Down Expand Up @@ -254,7 +253,7 @@ func main() {
}

setupLog.Info("starting boot-server")
go bootserver.RunBootServer(bootserverAddr, ipxeServiceURL, mgr.GetClient(), serverLog.WithName("bootserver"), *defaultIpxeTemplateData, *defaultHttpUKIURL)
go bootserver.RunBootServer(bootserverAddr, ipxeServiceURL, mgr.GetClient(), serverLog.WithName("bootserver"), *defaultHttpUKIURL)

setupLog.Info("starting image-proxy-server")
go bootserver.RunImageProxyServer(imageProxyServerAddr, mgr.GetClient(), serverLog.WithName("imageproxyserver"))
Expand Down Expand Up @@ -313,16 +312,6 @@ func IndexHTTPBootConfigBySystemIPs(ctx context.Context, mgr ctrl.Manager) error
)
}

func NewDefaultIPXETemplateData() *bootserver.IPXETemplateData {
var cfg bootserver.IPXETemplateData
flag.StringVar(&cfg.KernelURL, "default-kernel-url", "", "Default URL for the kernel")
flag.StringVar(&cfg.InitrdURL, "default-initrd-url", "", "Default URL for the initrd")
flag.StringVar(&cfg.SquashfsURL, "default-squashfs-url", "", "Default URL for the squashfs")
flag.StringVar(&cfg.IPXEServerURL, "default-ipxe-server-url", "", "Default IPXE Server URL to while generating ipxe-script")

return &cfg
}

func NewDefaultHTTPBootData() *string {
var defaultUKIURL string
flag.StringVar(&defaultUKIURL, "default-httpboot-uki-url", "", "Default UKI URL for http boot")
Expand Down
98 changes: 41 additions & 57 deletions server/bootserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ type IPXETemplateData struct {
IPXEServerURL string
}

func RunBootServer(ipxeServerAddr string, ipxeServiceURL string, k8sClient client.Client, log logr.Logger, defaultIpxeTemplateData IPXETemplateData, defaultUKIURL string) {
http.HandleFunc("/ipxe", func(w http.ResponseWriter, r *http.Request) {
handleIPXE(w, r, k8sClient, log, ipxeServiceURL, defaultIpxeTemplateData)
func RunBootServer(ipxeServerAddr string, ipxeServiceURL string, k8sClient client.Client, log logr.Logger, defaultUKIURL string) {
http.HandleFunc("/ipxe/", func(w http.ResponseWriter, r *http.Request) {
handleIPXE(w, r, k8sClient, log, ipxeServiceURL)
})

http.HandleFunc("/httpboot", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -79,75 +79,59 @@ func RunBootServer(ipxeServerAddr string, ipxeServiceURL string, k8sClient clien
}
}

func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client, log logr.Logger, ipxeServiceURL string, defaultIpxeTemplateData IPXETemplateData) {
func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client, log logr.Logger, ipxeServiceURL string) {
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)
uuid := strings.TrimPrefix(r.URL.Path, "/ipxe/")
if uuid == "" {
http.Error(w, "Bad Request: UUID is required", http.StatusBadRequest)
return
}

clientIPs := []string{clientIP}
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
for _, ip := range strings.Split(xff, ",") {
trimmedIP := strings.TrimSpace(ip)
if trimmedIP != "" {
clientIPs = append(clientIPs, trimmedIP)
}
}
ipxeBootConfigList := &bootv1alpha1.IPXEBootConfigList{}
err := k8sClient.List(ctx, ipxeBootConfigList, client.MatchingFields{bootv1alpha1.SystemUUIDIndexKey: uuid})
if client.IgnoreNotFound(err) != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

var ipxeConfigs bootv1alpha1.IPXEBootConfigList
for _, ip := range clientIPs {
if err := k8sClient.List(ctx, &ipxeConfigs, client.MatchingFields{bootv1alpha1.SystemIPIndexKey: ip}); err != nil {
log.Info("Failed to list IPXEBootConfig for IP", "IP", ip, "error", err)
continue
}

if len(ipxeConfigs.Items) > 0 {
log.Info("Found IPXEBootConfig", "IP", ip)
break
}
if len(ipxeBootConfigList.Items) == 0 {
log.Info("No IPXEBootConfig found for the given UUID")
http.Error(w, "Resource Not Found", http.StatusNotFound)
return
}

if len(ipxeConfigs.Items) == 0 {
log.Info("No IPXEBootConfig found for client IP, delivering default script", "clientIP", clientIP)
serveDefaultIPXETemplate(w, log, defaultIpxeTemplateData)
} else {
config := ipxeConfigs.Items[0]
if config.Spec.IPXEScriptSecretRef != nil {
secret := &corev1.Secret{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: config.Spec.IPXEScriptSecretRef.Name, Namespace: config.Namespace}, secret)
if err != nil {
log.Error(err, "Failed to fetch IPXE script from secret", "SecretName", config.Spec.IPXEScriptSecretRef.Name)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

ipxeScript, exists := secret.Data[bootv1alpha1.DefaultIPXEScriptKey]
if !exists {
log.Info("IPXE script not found in the secret", "ExpectedKey", bootv1alpha1.DefaultIPXEScriptKey)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
config := ipxeBootConfigList.Items[0]
if config.Spec.IPXEScriptSecretRef != nil {
secret := &corev1.Secret{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: config.Spec.IPXEScriptSecretRef.Name, Namespace: config.Namespace}, secret)
if err != nil {
log.Error(err, "Failed to fetch IPXE script from secret", "SecretName", config.Spec.IPXEScriptSecretRef.Name)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

if _, err := w.Write(ipxeScript); err != nil {
log.Info("Failed to write custom IPXE script", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
ipxeScript, exists := secret.Data[bootv1alpha1.DefaultIPXEScriptKey]
if !exists {
log.Info("IPXE script not found in the secret", "ExpectedKey", bootv1alpha1.DefaultIPXEScriptKey)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

serveDefaultIPXETemplate(w, log, IPXETemplateData{
KernelURL: config.Spec.KernelURL,
InitrdURL: config.Spec.InitrdURL,
SquashfsURL: config.Spec.SquashfsURL,
IPXEServerURL: ipxeServiceURL,
})
if _, err := w.Write(ipxeScript); err != nil {
log.Info("Failed to write custom IPXE script", "error", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
return
}

serveDefaultIPXETemplate(w, log, IPXETemplateData{
KernelURL: config.Spec.KernelURL,
InitrdURL: config.Spec.InitrdURL,
SquashfsURL: config.Spec.SquashfsURL,
IPXEServerURL: ipxeServiceURL,
})
}

func handleIgnitionIPXEBoot(w http.ResponseWriter, r *http.Request, k8sClient client.Client, log logr.Logger, uuid string) {
Expand Down
Loading