Skip to content

Commit

Permalink
Respond with encoded string instead of bytes, enhance logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mccormickt committed Feb 6, 2022
1 parent f02adb6 commit de2c881
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 9 additions & 4 deletions pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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)
}

}
8 changes: 4 additions & 4 deletions pkg/zippo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit de2c881

Please sign in to comment.