-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from mallardduck/new-charts/pt2
- Loading branch information
Showing
18 changed files
with
395 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rancherProjectMonitoringVersion: 0.3.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// This program generates a Go file containing a set of exported constants that represent | ||
// configuration variables of Rancher at build-time. | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"text/template" | ||
) | ||
|
||
func main() { | ||
if err := generateGoConstantsFile(); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func generateGoConstantsFile() error { | ||
in, err := os.OpenFile("build.yaml", os.O_RDONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
out, err := os.OpenFile("pkg/buildconfig/constants.go", os.O_TRUNC|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
const raw = `// Code generated by pkg/codegen/config/main.go. DO NOT EDIT. | ||
// Package buildconfig contains a set of exported constants that represent configuration variables of Rancher at build-time. | ||
package buildconfig | ||
const ( | ||
{{ . }}) | ||
` | ||
tmpl, err := template.New("").Parse(raw) | ||
if err != nil { | ||
return err | ||
} | ||
writer := GoConstantsWriter{ | ||
Tmpl: tmpl, | ||
Input: in, | ||
Output: out, | ||
} | ||
return writer.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"go/format" | ||
"io" | ||
"sort" | ||
"strings" | ||
"text/template" | ||
|
||
"golang.org/x/text/cases" | ||
"golang.org/x/text/language" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type GoConstantsWriter struct { | ||
Input io.Reader | ||
Output io.Writer | ||
Tmpl *template.Template | ||
buf []byte | ||
cfg map[string]string | ||
} | ||
|
||
// Run loads YAML data from the pre-configured Input source, processes it, and outputs a template with formatted | ||
// Go constants in the pre-configured Output source. This method can only be run once, since the Input source gets fully read. | ||
func (f *GoConstantsWriter) Run() error { | ||
if err := f.load(); err != nil { | ||
return err | ||
} | ||
if err := f.process(); err != nil { | ||
return err | ||
} | ||
if err := f.write(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (f *GoConstantsWriter) load() error { | ||
if f.Input == nil { | ||
return errors.New("nil input") | ||
} | ||
b, err := io.ReadAll(f.Input) | ||
if err != nil { | ||
return fmt.Errorf("failed to read input: %w", err) | ||
} | ||
if len(b) == 0 { | ||
return errors.New("nothing was read") | ||
} | ||
if err := yaml.Unmarshal(b, &f.cfg); err != nil { | ||
return fmt.Errorf("failed to unmarshal raw YAML from input: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (f *GoConstantsWriter) process() error { | ||
if f.Tmpl == nil { | ||
return errors.New("nil template") | ||
} | ||
// This sorts the keys alphabetically to process the map in a fixed order. | ||
keys := make([]string, 0, len(f.cfg)) | ||
for k := range f.cfg { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
|
||
capitalize := cases.Title(language.English, cases.NoLower) | ||
var builder strings.Builder | ||
for _, k := range keys { | ||
v := f.cfg[k] | ||
// Capitalize the key to make the constant exported in the generated Go file. | ||
k = capitalize.String(k) | ||
s := fmt.Sprintf("\t%s = %q\n", k, v) | ||
builder.WriteString(s) | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
if err := f.Tmpl.Execute(buf, builder.String()); err != nil { | ||
return err | ||
} | ||
f.buf = buf.Bytes() | ||
return nil | ||
} | ||
|
||
func (f *GoConstantsWriter) write() error { | ||
if f.Output == nil { | ||
return errors.New("nil output") | ||
} | ||
formatted, err := format.Source(f.buf) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = f.Output.Write(formatted) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
"text/template" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
mainpkg "github.com/rancher/prometheus-federator/pkg/codegen/buildconfig" | ||
) | ||
|
||
func TestGoConstantsWriterRun(t *testing.T) { | ||
t.Parallel() | ||
const contents = `b: 3 | ||
a: foo | ||
c: 3.14` | ||
in := bytes.NewBufferString(contents) | ||
out := new(bytes.Buffer) | ||
|
||
const rawTemplate = ` | ||
package buildconfig | ||
const ( | ||
{{ . }}) | ||
` | ||
tmpl, err := template.New("").Parse(rawTemplate) | ||
require.NoError(t, err) | ||
w := &mainpkg.GoConstantsWriter{ | ||
Tmpl: tmpl, | ||
Input: in, | ||
Output: out, | ||
} | ||
require.NoError(t, w.Run()) | ||
|
||
want := | ||
`package buildconfig | ||
const ( | ||
A = "foo" | ||
B = "3" | ||
C = "3.14" | ||
) | ||
` | ||
got := out.String() | ||
require.Equal(t, want, got) | ||
|
||
// Running a second time with the same Input source must fail. | ||
require.Error(t, w.Run()) | ||
} | ||
|
||
func TestGoConstantsWriterFailsWithBadConfiguration(t *testing.T) { | ||
t.Parallel() | ||
const rawTemplate = ` | ||
package buildconfig | ||
const ( | ||
{{ . }}) | ||
` | ||
tmpl, err := template.New("").Parse(rawTemplate) | ||
require.NoError(t, err) | ||
const contents = `a: foo | ||
b: 3 | ||
c: 3.14` | ||
output := new(bytes.Buffer) | ||
|
||
tests := []struct { | ||
name string | ||
tmpl *template.Template | ||
input io.Reader | ||
output io.Writer | ||
}{ | ||
{ | ||
name: "nil template", | ||
tmpl: nil, | ||
input: bytes.NewBufferString(contents), | ||
output: output, | ||
}, | ||
{ | ||
name: "empty template", | ||
tmpl: template.New(""), | ||
input: bytes.NewBufferString(contents), | ||
output: output, | ||
}, | ||
{ | ||
name: "nil input", | ||
tmpl: tmpl, | ||
input: nil, | ||
output: output, | ||
}, | ||
{ | ||
name: "empty input", | ||
tmpl: tmpl, | ||
input: bytes.NewBufferString(""), | ||
output: output, | ||
}, | ||
{ | ||
name: "nil output", | ||
tmpl: tmpl, | ||
input: bytes.NewBufferString(contents), | ||
output: nil, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
w := mainpkg.GoConstantsWriter{ | ||
Input: test.input, | ||
Output: test.output, | ||
Tmpl: test.tmpl, | ||
} | ||
require.Error(t, w.Run()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,4 @@ cd $(dirname $0) | |
./test | ||
./validate | ||
./validate-ci | ||
./validate-packages | ||
./validate-chart | ||
./validate-charts | ||
./package |
Oops, something went wrong.