Skip to content

Commit

Permalink
Fix json encoding for ignition config response
Browse files Browse the repository at this point in the history
  • Loading branch information
mccormickt committed Feb 6, 2022
1 parent de2c881 commit 260c8ff
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/coreos/butane v0.13.1
github.com/coreos/ignition/v2 v2.11.0
github.com/gorilla/mux v1.8.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
Expand Down
2 changes: 1 addition & 1 deletion pkg/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ 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")
w.Header().Set("Content-Type", "application/json")

// Set hostname from host header
fqdn := strings.Split(r.Host, ":")[0]
Expand Down
17 changes: 12 additions & 5 deletions pkg/zippo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package zippo

import (
"bytes"
"encoding/json"
"io/ioutil"
"text/template"

"github.com/coreos/butane/config"
"github.com/coreos/butane/config/common"
"github.com/coreos/ignition/v2/config/v3_1/types"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -34,16 +36,21 @@ 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{}) (string, error) {
func CreateIgnitionConfig(butaneTemplate string, hostname interface{}) (types.Config, error) {
var ignitionConfig types.Config
butaneConfig, err := Render(butaneTemplate, hostname)
if err != nil {
return "", err
return ignitionConfig, err
}

ignitionConfig, r, err := config.TranslateBytes(butaneConfig.Bytes(), common.TranslateBytesOptions{Pretty: true})
ignitionBytes, r, err := config.TranslateBytes(butaneConfig.Bytes(), common.TranslateBytesOptions{Pretty: true})
if err != nil {
return "", errors.Wrapf(err, "error translating config: %s", r.String())
return ignitionConfig, errors.Wrapf(err, "error translating config: %s", r.String())
}

return string(ignitionConfig), nil
err = json.Unmarshal(ignitionBytes, &ignitionConfig)
if err != nil {
return ignitionConfig, errors.Wrapf(err, "error translating config: %s", r.String())
}
return ignitionConfig, nil
}

0 comments on commit 260c8ff

Please sign in to comment.