write krm metadata to Krmfile instead of Kustomization

This commit is contained in:
Phillip Wittrock
2020-05-28 12:27:14 -07:00
committed by Phillip Wittrock
parent c1a2bf14da
commit 0e5e2648b3
10 changed files with 335 additions and 42 deletions

View File

@@ -3,10 +3,14 @@
package ext
import "path/filepath"
import (
"path/filepath"
"sigs.k8s.io/kustomize/kyaml/krmfile"
)
// GetOpenAPIFile returns the path to the file containing supplementary OpenAPI definitions.
// Maybe be overridden to configure which file to read OpenAPI definitions from.
var GetOpenAPIFile = func(args []string) (string, error) {
return filepath.Join(args[0], "kustomization"), nil
return filepath.Join(args[0], krmfile.KrmfileName), nil
}

View File

@@ -126,13 +126,9 @@ func (r *ListSettersRunner) ListSubstitutions(c *cobra.Command, args []string) e
s.Name, s.Pattern, setters})
}
if len(r.List.Substitutions) == 0 {
// exit non-0 if no matching substitutions are found
if ExitOnError {
os.Exit(1)
return nil
}
} else {
table.Render()
}
return nil
}

View File

@@ -159,6 +159,7 @@ func lookup(l setters.LookupSetters, c *cobra.Command, args []string) error {
}
table.Render()
fmt.Println(l.SetterCounts)
if len(l.SetterCounts) == 0 {
// exit non-0 if no matching setters are found
os.Exit(1)

View File

@@ -0,0 +1,57 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package e2e
import (
"testing"
)
func TestCreateSetter(t *testing.T) {
tests := []test{
{
name: "create_setter",
args: []string{"create-setter", ".", "replicas", "3"},
files: map[string]string{
"deployment.yaml": `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
`,
"Krmfile": `
apiVersion: config.k8s.io/v1alpha1
kind: Krmfile
`,
},
expectedFiles: map[string]string{
"deployment.yaml": `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 # {"$openapi":"replicas"}
`,
"Krmfile": `
apiVersion: config.k8s.io/v1alpha1
kind: Krmfile
openAPI:
definitions:
io.k8s.cli.setters.replicas:
x-k8s-cli:
setter:
name: replicas
value: "3"
`,
},
},
}
runTests(t, tests)
}

View File

@@ -12,6 +12,7 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"
"testing"
"github.com/stretchr/testify/assert"
@@ -19,12 +20,7 @@ import (
)
func TestRunE2e(t *testing.T) {
binDir, err := ioutil.TempDir("", "kustomize-test-")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(binDir)
build(t, binDir)
binDir := build()
tests := []struct {
name string
@@ -661,6 +657,7 @@ metadata:
},
}
// TODO: dedup this with the shared version
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
@@ -710,23 +707,37 @@ metadata:
}
}
func build(t *testing.T, binDir string) {
var buildOnce sync.Once
var binDir string
func build() string {
// only build the binaries once
buildOnce.Do(func() {
var err error
binDir, err = ioutil.TempDir("", "kustomize-test-")
if err != nil {
panic(err)
}
build := exec.Command("go", "build", "-o",
filepath.Join(binDir, e2econtainerconfigBin))
build.Dir = "e2econtainerconfig"
build.Stdout = os.Stdout
build.Stderr = os.Stderr
build.Env = os.Environ()
if !assert.NoError(t, build.Run()) {
t.FailNow()
err = build.Run()
if err != nil {
panic(err)
}
build = exec.Command("go", "build", "-o", filepath.Join(binDir, kyamlBin))
build.Dir = filepath.Join("..", "..", "..")
build.Stdout = os.Stdout
build.Stderr = os.Stderr
if !assert.NoError(t, build.Run()) {
t.FailNow()
err = build.Run()
if err != nil {
panic(err)
}
if os.Getenv("KUSTOMIZE_DOCKER_E2E") == "false" {
@@ -737,9 +748,13 @@ func build(t *testing.T, binDir string) {
build.Dir = "e2econtainerconfig"
build.Stdout = os.Stdout
build.Stderr = os.Stderr
if !assert.NoError(t, build.Run()) {
t.FailNow()
err = build.Run()
if err != nil {
panic(err)
}
})
return binDir
}
var (

View File

@@ -0,0 +1,43 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package e2e
import "testing"
func TestListSetters(t *testing.T) {
tests := []test{
{
name: "set",
args: []string{"list-setters", "."},
files: map[string]string{
"deployment.yaml": `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 # {"$openapi":"replicas"}
`,
"Krmfile": `
apiVersion: config.k8s.io/v1alpha1
kind: Krmfile
openAPI:
definitions:
io.k8s.cli.setters.replicas:
x-k8s-cli:
setter:
name: replicas
value: "3"
`,
},
expectedStdOut: `
NAME VALUE SET BY DESCRIPTION COUNT
replicas 3 1
`,
},
}
runTests(t, tests)
}

View File

@@ -0,0 +1,62 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package e2e
import "testing"
func TestSet(t *testing.T) {
tests := []test{
{
name: "set",
args: []string{"set", ".", "replicas", "4"},
files: map[string]string{
"deployment.yaml": `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 # {"$openapi":"replicas"}
`,
"Krmfile": `
apiVersion: config.k8s.io/v1alpha1
kind: Krmfile
openAPI:
definitions:
io.k8s.cli.setters.replicas:
x-k8s-cli:
setter:
name: replicas
value: "3"
`,
},
expectedFiles: map[string]string{
"deployment.yaml": `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 4 # {"$openapi":"replicas"}
`,
"Krmfile": `
apiVersion: config.k8s.io/v1alpha1
kind: Krmfile
openAPI:
definitions:
io.k8s.cli.setters.replicas:
x-k8s-cli:
setter:
name: replicas
value: "4"
`,
},
},
}
runTests(t, tests)
}

View File

@@ -0,0 +1,86 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package e2e
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"sigs.k8s.io/kustomize/kyaml/testutil"
)
func TestMain(m *testing.M) {
d := build()
defer os.RemoveAll(d)
os.Exit(m.Run())
}
type test struct {
name string
args []string
files map[string]string
expectedFiles map[string]string
expectedErr string
expectedStdOut string
}
func runTests(t *testing.T, tests []test) {
dir := build()
bin := filepath.Join(dir, kyamlBin)
for i := range tests {
tt := tests[i]
t.Run(tt.name, func(t *testing.T) {
dataDir, err := ioutil.TempDir("", "kustomize-test-data-")
if !assert.NoError(t, err) {
t.FailNow()
}
defer os.RemoveAll(dataDir)
os.Chdir(dataDir)
// write the input
for path, data := range tt.files {
err := ioutil.WriteFile(path, []byte(data), 0600)
testutil.AssertNoError(t, err)
}
cmd := exec.Command(bin, tt.args...)
cmd.Dir = dataDir
var stdErr, stdOut bytes.Buffer
cmd.Stdout = &stdOut
cmd.Stderr = &stdErr
cmd.Env = os.Environ()
err = cmd.Run()
if tt.expectedErr != "" {
if !assert.Contains(t, stdErr.String(), tt.expectedErr, stdErr.String()) {
t.FailNow()
}
return
}
testutil.AssertNoError(t, err, stdErr.String(), stdOut.String())
if tt.expectedStdOut != "" {
if !assert.Equal(t, strings.TrimSpace(stdOut.String()), strings.TrimSpace(tt.expectedStdOut)) {
t.FailNow()
}
}
for path, data := range tt.expectedFiles {
b, err := ioutil.ReadFile(path)
testutil.AssertNoError(t, err, stdErr.String())
if !assert.Equal(t, strings.TrimSpace(data), strings.TrimSpace(string(b)), stdErr.String()) {
t.FailNow()
}
}
})
}
}

19
kyaml/krmfile/doc.go Normal file
View File

@@ -0,0 +1,19 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Package krmfile provides functionality for working with Krmfiles.
//
// Example Krmfile
//
// apiVersion: config.k8s.io/v1alpha1
// kind: Krmfile
// openAPI:
// definitions:
// io.k8s.cli.setters.replicas:
// x-k8s-cli:
// setter:
// name: replicas
// value: "3"
// setBy: me
// description: "hello world"
package krmfile

10
kyaml/krmfile/krmfile.go Normal file
View File

@@ -0,0 +1,10 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package krmfile
// KRMFileName is the file where Krm metadata is stored
const (
// KrmfileName is the name of the file that KRM metadata is written to
KrmfileName = "Krmfile"
)