diff --git a/pkg/server.go b/pkg/server.go index 3e2376c..743a590 100644 --- a/pkg/server.go +++ b/pkg/server.go @@ -54,6 +54,9 @@ func (s *Server) Run() { } func (s *Server) serveButaneTranslator(w http.ResponseWriter, r *http.Request) { + // Set content type to json + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + // Set hostname from host header fqdn := strings.Split(r.Host, ":")[0] if fqdn == "" { @@ -66,16 +69,18 @@ func (s *Server) serveButaneTranslator(w http.ResponseWriter, r *http.Request) { httpError(w, "Error parsing hostname") return } - log.Debugf("Served ignition config for %s at %s", hostname, r.RemoteAddr) + log.Infof("Serving ignition config for %s at %s", hostname, r.RemoteAddr) + // Respond with ignition config including rendered hostname args := struct{ Hostname string }{Hostname: hostname} ignitionConfig, err := CreateIgnitionConfig(s.Config.TemplatePath, args) if err != nil { log.Error(err.Error()) - httpError(w, "failed to render ignition config") + httpError(w, "failed to render butane template") } else { - log.Infof("Successfully rendered ignition config") + log.Infof("Successfully rendered butane template into ignition config") w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(string(ignitionConfig)) + json.NewEncoder(w).Encode(ignitionConfig) } + } diff --git a/pkg/zippo.go b/pkg/zippo.go index 12eb5ea..b1c7878 100644 --- a/pkg/zippo.go +++ b/pkg/zippo.go @@ -34,16 +34,16 @@ func Render(templatePath string, args interface{}) (*bytes.Buffer, error) { } // CreateIgnitionConfig creates an ignition config from a rendered butane template with a given hostname -func CreateIgnitionConfig(butaneTemplate string, hostname interface{}) ([]byte, error) { +func CreateIgnitionConfig(butaneTemplate string, hostname interface{}) (string, error) { butaneConfig, err := Render(butaneTemplate, hostname) if err != nil { - return nil, err + return "", err } ignitionConfig, r, err := config.TranslateBytes(butaneConfig.Bytes(), common.TranslateBytesOptions{Pretty: true}) if err != nil { - return nil, errors.Wrapf(err, "error translating config: %s", r.String()) + return "", errors.Wrapf(err, "error translating config: %s", r.String()) } - return ignitionConfig, nil + return string(ignitionConfig), nil }