diff --git a/api/v1alpha1/ipxebootconfig_types.go b/api/v1alpha1/ipxebootconfig_types.go index 8563bcc0..638a5b14 100644 --- a/api/v1alpha1/ipxebootconfig_types.go +++ b/api/v1alpha1/ipxebootconfig_types.go @@ -33,6 +33,7 @@ type IPXEBootConfigSpec struct { KernelURL string `json:"kernelURL,omitempty"` InitrdURL string `json:"initrdURL,omitempty"` SquashfsURL string `json:"squashfsURL,omitempty"` + IPXEServerURL string `json:"ipxeServerURL,omitempty"` IgnitionSecretRef *corev1.LocalObjectReference `json:"ignitionSecretRef,omitempty"` } diff --git a/cmd/main.go b/cmd/main.go index afa6a410..98027400 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -62,6 +62,7 @@ func init() { func main() { ctx := ctrl.LoggerInto(ctrl.SetupSignalHandler(), setupLog) + defaultIpxeTemplateData := NewDefaultIPXETemplateData() var metricsAddr string var enableLeaderElection bool @@ -70,6 +71,7 @@ func main() { var enableHTTP2 bool var ipxeServerAddr string var imageProxyServerAddr string + flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") flag.StringVar(&ipxeServerAddr, "ipxe-server-address", ":8082", "The address the ipxe-server binds to.") @@ -166,7 +168,7 @@ func main() { } setupLog.Info("starting ipxe-server") - go ipxeserver.RunIPXEServer(ipxeServerAddr, mgr.GetClient(), serverLog.WithName("ipxeserver")) + go ipxeserver.RunIPXEServer(ipxeServerAddr, mgr.GetClient(), serverLog.WithName("ipxeserver"), *defaultIpxeTemplateData) setupLog.Info("starting image-proxy-server") go ipxeserver.RunImageProxyServer(imageProxyServerAddr, mgr.GetClient(), serverLog.WithName("imageproxyserver")) @@ -200,3 +202,14 @@ func IndexIPXEBootConfigBySystemIP(ctx context.Context, mgr ctrl.Manager) error }, ) } + +func NewDefaultIPXETemplateData() *ipxeserver.IPXETemplateData { + var cfg ipxeserver.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") + flag.Parse() + + return &cfg +} diff --git a/server/ipxeserver.go b/server/ipxeserver.go index 8de072ed..b15096d7 100644 --- a/server/ipxeserver.go +++ b/server/ipxeserver.go @@ -21,12 +21,12 @@ type IPXETemplateData struct { InitrdURL string SquashfsURL string RegistryURL string - IpxeServerURL string + IPXEServerURL string } -func RunIPXEServer(ipxeServerAddr string, k8sClient client.Client, log logr.Logger) { +func RunIPXEServer(ipxeServerAddr string, k8sClient client.Client, log logr.Logger, defaultIpxeTemplateData IPXETemplateData) { http.HandleFunc("/ipxe", func(w http.ResponseWriter, r *http.Request) { - handleIPXE(w, r, k8sClient, ipxeServerAddr, log) + handleIPXE(w, r, k8sClient, log, defaultIpxeTemplateData) }) http.HandleFunc("/ignition/", func(w http.ResponseWriter, r *http.Request) { @@ -41,7 +41,7 @@ func RunIPXEServer(ipxeServerAddr string, k8sClient client.Client, log logr.Logg } } -func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client, ipxeServerAddr string, log logr.Logger) { +func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client, log logr.Logger, defaultIpxeTemplateData IPXETemplateData) { log.Info("Processing IPXE request", "method", r.Method, "path", r.URL.Path, "clientIP", r.RemoteAddr) ctx := r.Context() @@ -59,14 +59,19 @@ func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client, return } + data := defaultIpxeTemplateData if len(ipxeConfigs.Items) == 0 { - log.Info("No IPXEBootConfig found for client IP", "clientIP", clientIP) - http.NotFound(w, r) - return + 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: config.Spec.IPXEServerURL, + } } - config := ipxeConfigs.Items[0] - tmplPath := filepath.Join("templates", "ipxe-script.tpl") tmpl, err := template.ParseFiles(tmplPath) if err != nil { @@ -75,13 +80,6 @@ func handleIPXE(w http.ResponseWriter, r *http.Request, k8sClient client.Client, return } - data := IPXETemplateData{ - KernelURL: config.Spec.KernelURL, - InitrdURL: config.Spec.InitrdURL, - SquashfsURL: config.Spec.SquashfsURL, - IpxeServerURL: ipxeServerAddr, - } - if err := tmpl.Execute(w, data); err != nil { log.Info("Failed to execute template", "error", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) diff --git a/templates/ipxe-script.tpl b/templates/ipxe-script.tpl index a1886e27..90588979 100644 --- a/templates/ipxe-script.tpl +++ b/templates/ipxe-script.tpl @@ -1,11 +1,11 @@ #!ipxe -set ipxe-svc {{.IpxeServerURL}}/ignition +set ipxe-svc {{.IPXEServerURL}} set kernel-url {{.KernelURL}} set initrd-url {{.InitrdURL}} set squashfs-url {{.SquashfsURL}} -kernel ${kernel-url} initrd=rootfs.initrd gl.ovl=/:tmpfs gl.url=${squashfs-url} gl.live=1 ip=dhcp6 ignition.firstboot=1 ignition.config.url=${ipxe-svc}/${uuid} ignition.platform.id=metal console=ttyS0,115200 console=tty0 earlyprintk=ttyS0,115200 consoleblank=0 +kernel ${kernel-url} initrd=rootfs.initrd gl.ovl=/:tmpfs gl.url=${squashfs-url} gl.live=1 ip=dhcp6 ignition.firstboot=1 ignition.config.url=${ipxe-svc}/ignition/${uuid} ignition.platform.id=metal console=ttyS0,115200 console=tty0 earlyprintk=ttyS0,115200 consoleblank=0 initrd ${initrd-url} boot \ No newline at end of file