mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Drop k8s metatype fields from kustomize.yaml struct
This commit is contained in:
@@ -22,11 +22,11 @@ import (
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
||||
interror "k8s.io/kubectl/pkg/kustomize/internal/error"
|
||||
"k8s.io/kubectl/pkg/kustomize/resource"
|
||||
"k8s.io/kubectl/pkg/kustomize/transformers"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
"k8s.io/kubectl/pkg/loader"
|
||||
)
|
||||
|
||||
@@ -35,8 +35,10 @@ type Application interface {
|
||||
Resources() (resource.ResourceCollection, error)
|
||||
// SemiResources computes and returns the resources without name hash and name reference for the app
|
||||
SemiResources() (resource.ResourceCollection, error)
|
||||
// RawResources computes and returns the raw resources from the manifest.
|
||||
// It contains resources from 1) untransformed resources from current manifest 2) transformed resources from sub packages
|
||||
// RawResources computes and returns the raw resources from the kustomize config file.
|
||||
// It contains resources from
|
||||
// 1) untransformed resources from current kustomize config file
|
||||
// 2) transformed resources from sub packages
|
||||
RawResources() (resource.ResourceCollection, error)
|
||||
}
|
||||
|
||||
@@ -44,19 +46,19 @@ var _ Application = &applicationImpl{}
|
||||
|
||||
// Private implementation of the Application interface
|
||||
type applicationImpl struct {
|
||||
manifest *manifest.Manifest
|
||||
manifest *types.Manifest
|
||||
loader loader.Loader
|
||||
}
|
||||
|
||||
// NewApp parses the manifest at the path using the loader.
|
||||
// NewApp parses the kustomize config file at the path using the loader.
|
||||
func New(loader loader.Loader) (Application, error) {
|
||||
// load the manifest using the loader
|
||||
// load the kustomize config file using the loader
|
||||
manifestBytes, err := loader.Load(constants.KustomizeFileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var m manifest.Manifest
|
||||
var m types.Manifest
|
||||
err = unmarshal(manifestBytes, &m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -64,7 +66,7 @@ func New(loader loader.Loader) (Application, error) {
|
||||
return &applicationImpl{manifest: &m, loader: loader}, nil
|
||||
}
|
||||
|
||||
// Resources computes and returns the resources from the manifest.
|
||||
// Resources computes and returns the resources from the kustomize config file.
|
||||
// The namehashing for configmap/secrets and resolving name reference is only done
|
||||
// in the most top overlay once at the end of getting resources.
|
||||
func (a *applicationImpl) Resources() (resource.ResourceCollection, error) {
|
||||
@@ -130,7 +132,7 @@ func (a *applicationImpl) SemiResources() (resource.ResourceCollection, error) {
|
||||
return allRes, nil
|
||||
}
|
||||
|
||||
// RawResources computes and returns the raw resources from the manifest.
|
||||
// RawResources computes and returns the raw resources from the kustomize config file.
|
||||
// The namehashing for configmap/secrets and resolving name reference is only done
|
||||
// in the most top overlay once at the end of getting resources.
|
||||
func (a *applicationImpl) RawResources() (resource.ResourceCollection, error) {
|
||||
|
||||
@@ -32,10 +32,7 @@ import (
|
||||
)
|
||||
|
||||
func setupTest(t *testing.T) loader.Loader {
|
||||
manifestContent := []byte(`apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: nginx-app
|
||||
manifestContent := []byte(`kustomizationName: nginx-app
|
||||
namePrefix: foo-
|
||||
objectLabels:
|
||||
app: nginx
|
||||
|
||||
@@ -22,9 +22,9 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/configmapandsecret"
|
||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
"k8s.io/kubectl/pkg/kustomize/util/fs"
|
||||
)
|
||||
|
||||
@@ -32,13 +32,13 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
var config dataConfig
|
||||
cmd := &cobra.Command{
|
||||
Use: "configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1]",
|
||||
Short: "Adds a configmap to the manifest.",
|
||||
Short: "Adds a configmap to the kustomize config file.",
|
||||
Long: "",
|
||||
Example: `
|
||||
# Adds a configmap to the Manifest (with a specified key)
|
||||
# Adds a configmap to the kustomize config file (with a specified key)
|
||||
kustomize edit add configmap my-configmap --from-file=my-key=file/path --from-literal=my-literal=12345
|
||||
|
||||
# Adds a configmap to the Manifest (key is the filename)
|
||||
# Adds a configmap to the kustomize config file (key is the filename)
|
||||
kustomize edit add configmap my-configmap --from-file=file/path
|
||||
|
||||
# Adds a configmap from env-file
|
||||
@@ -50,7 +50,7 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
// Load in the manifest file.
|
||||
// Load in the kustomize config file.
|
||||
mf, err := newManifestFile(constants.KustomizeFileName, fsys)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -61,13 +61,13 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add the config map to the manifest.
|
||||
// Add the config map to the kustomize config file.
|
||||
err = addConfigMap(m, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Write out the manifest with added configmap.
|
||||
// Write out the kustomize config file with added configmap.
|
||||
return mf.write(m)
|
||||
},
|
||||
}
|
||||
@@ -79,10 +79,10 @@ func newCmdAddConfigMap(errOut io.Writer, fsys fs.FileSystem) *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
// addConfigMap updates a configmap within a manifest, using the data in config.
|
||||
// Note: error may leave manifest in an undefined state. Suggest passing a copy
|
||||
// of manifest.
|
||||
func addConfigMap(m *manifest.Manifest, config dataConfig) error {
|
||||
// addConfigMap updates a configmap within a kustomize config file, using the data in config.
|
||||
// Note: error may leave kustomize config file in an undefined state. Suggest passing a copy
|
||||
// of kustomize config file.
|
||||
func addConfigMap(m *types.Manifest, config dataConfig) error {
|
||||
cm := getOrCreateConfigMap(m, config.Name)
|
||||
|
||||
err := mergeData(&cm.DataSources, config)
|
||||
@@ -90,7 +90,7 @@ func addConfigMap(m *manifest.Manifest, config dataConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Validate manifest's configmap by trying to create corev1.configmap.
|
||||
// Validate by trying to create corev1.configmap.
|
||||
_, _, err = configmapandsecret.MakeConfigmapAndGenerateName(*cm)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -99,19 +99,19 @@ func addConfigMap(m *manifest.Manifest, config dataConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getOrCreateConfigMap(m *manifest.Manifest, name string) *manifest.ConfigMapArgs {
|
||||
func getOrCreateConfigMap(m *types.Manifest, name string) *types.ConfigMapArgs {
|
||||
for i, v := range m.ConfigMapGenerator {
|
||||
if name == v.Name {
|
||||
return &m.ConfigMapGenerator[i]
|
||||
}
|
||||
}
|
||||
// config map not found, create new one and add it to the manifest.
|
||||
cm := &manifest.ConfigMapArgs{Name: name}
|
||||
// config map not found, create new one and add it to the kustomize config file.
|
||||
cm := &types.ConfigMapArgs{Name: name}
|
||||
m.ConfigMapGenerator = append(m.ConfigMapGenerator, *cm)
|
||||
return &m.ConfigMapGenerator[len(m.ConfigMapGenerator)-1]
|
||||
}
|
||||
|
||||
func mergeData(src *manifest.DataSources, config dataConfig) error {
|
||||
func mergeData(src *types.DataSources, config dataConfig) error {
|
||||
src.LiteralSources = append(src.LiteralSources, config.LiteralSources...)
|
||||
src.FileSources = append(src.FileSources, config.FileSources...)
|
||||
if src.EnvSource != "" && src.EnvSource != config.EnvFileSource {
|
||||
|
||||
@@ -19,7 +19,7 @@ package commands
|
||||
import (
|
||||
"testing"
|
||||
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
"k8s.io/kubectl/pkg/kustomize/util/fs"
|
||||
)
|
||||
|
||||
@@ -32,7 +32,7 @@ func TestNewAddConfigMapIsNotNil(t *testing.T) {
|
||||
func TestGetOrCreateConfigMap(t *testing.T) {
|
||||
cmName := "test-config-name"
|
||||
|
||||
manifest := &manifest.Manifest{
|
||||
manifest := &types.Manifest{
|
||||
NamePrefix: "test-name-prefix",
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ func TestGetOrCreateConfigMap(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMergeData_LiteralSources(t *testing.T) {
|
||||
ds := &manifest.DataSources{}
|
||||
ds := &types.DataSources{}
|
||||
|
||||
err := mergeData(ds, dataConfig{LiteralSources: []string{"k1=v1"}})
|
||||
if err != nil {
|
||||
@@ -87,7 +87,7 @@ func TestMergeData_LiteralSources(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMergeData_FileSources(t *testing.T) {
|
||||
ds := &manifest.DataSources{}
|
||||
ds := &types.DataSources{}
|
||||
|
||||
err := mergeData(ds, dataConfig{FileSources: []string{"file1"}})
|
||||
if err != nil {
|
||||
@@ -111,7 +111,7 @@ func TestMergeData_FileSources(t *testing.T) {
|
||||
func TestMergeData_EnvSource(t *testing.T) {
|
||||
envFileName := "env1"
|
||||
envFileName2 := "env2"
|
||||
ds := &manifest.DataSources{}
|
||||
ds := &types.DataSources{}
|
||||
|
||||
err := mergeData(ds, dataConfig{EnvFileSource: envFileName})
|
||||
if err != nil {
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: nginx-app
|
||||
kustomizationName: nginx-app
|
||||
namePrefix: team-foo-
|
||||
objectLabels:
|
||||
app: mynginx
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: nginx-app
|
||||
kustomizationName: nginx-app
|
||||
namePrefix: staging-
|
||||
objectLabels:
|
||||
env: staging
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: nginx-app
|
||||
kustomizationName: nginx-app
|
||||
namePrefix: team-foo-
|
||||
objectLabels:
|
||||
app: mynginx
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: nginx-app
|
||||
kustomizationName: nginx-app
|
||||
namePrefix: staging-
|
||||
objectLabels:
|
||||
env: staging
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: nginx-app
|
||||
kustomizationName: nginx-app
|
||||
namePrefix: team-foo-
|
||||
objectLabels:
|
||||
app: mynginx
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: nginx-app
|
||||
kustomizationName: nginx-app
|
||||
namePrefix: staging-
|
||||
objectLabels:
|
||||
env: staging
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: nginx-app
|
||||
kustomizationName: nginx-app
|
||||
namePrefix: team-foo-
|
||||
objectLabels:
|
||||
app: mynginx
|
||||
|
||||
@@ -24,9 +24,9 @@ import (
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/constants"
|
||||
interror "k8s.io/kubectl/pkg/kustomize/internal/error"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
"k8s.io/kubectl/pkg/kustomize/util/fs"
|
||||
)
|
||||
|
||||
@@ -47,7 +47,7 @@ func newManifestFile(mPath string, fsys fs.FileSystem) (*manifestFile, error) {
|
||||
func (mf *manifestFile) validate() error {
|
||||
f, err := mf.fsys.Stat(mf.mPath)
|
||||
if err != nil {
|
||||
errorMsg := fmt.Sprintf("Manifest (%s) missing\nRun `kustomize init` first", mf.mPath)
|
||||
errorMsg := fmt.Sprintf("Missing kustomize config file '%s'.\n", mf.mPath)
|
||||
merr := interror.ManifestError{ManifestFilepath: mf.mPath, ErrorMsg: errorMsg}
|
||||
return merr
|
||||
}
|
||||
@@ -55,13 +55,13 @@ func (mf *manifestFile) validate() error {
|
||||
mf.mPath = path.Join(mf.mPath, constants.KustomizeFileName)
|
||||
_, err = mf.fsys.Stat(mf.mPath)
|
||||
if err != nil {
|
||||
errorMsg := fmt.Sprintf("Manifest (%s) missing\nRun `kustomize init` first", mf.mPath)
|
||||
errorMsg := fmt.Sprintf("Missing kustomize config file '%s'.\n", mf.mPath)
|
||||
merr := interror.ManifestError{ManifestFilepath: mf.mPath, ErrorMsg: errorMsg}
|
||||
return merr
|
||||
}
|
||||
} else {
|
||||
if !strings.HasSuffix(mf.mPath, constants.KustomizeFileName) {
|
||||
errorMsg := fmt.Sprintf("Manifest file (%s) should have %s suffix\n", mf.mPath, constants.KustomizeSuffix)
|
||||
errorMsg := fmt.Sprintf("Kustomize config file path (%s) should have %s suffix\n", mf.mPath, constants.KustomizeSuffix)
|
||||
merr := interror.ManifestError{ManifestFilepath: mf.mPath, ErrorMsg: errorMsg}
|
||||
return merr
|
||||
}
|
||||
@@ -69,12 +69,12 @@ func (mf *manifestFile) validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mf *manifestFile) read() (*manifest.Manifest, error) {
|
||||
func (mf *manifestFile) read() (*types.Manifest, error) {
|
||||
bytes, err := mf.fsys.ReadFile(mf.mPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var manifest manifest.Manifest
|
||||
var manifest types.Manifest
|
||||
err = yaml.Unmarshal(bytes, &manifest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -82,9 +82,9 @@ func (mf *manifestFile) read() (*manifest.Manifest, error) {
|
||||
return &manifest, err
|
||||
}
|
||||
|
||||
func (mf *manifestFile) write(manifest *manifest.Manifest) error {
|
||||
func (mf *manifestFile) write(manifest *types.Manifest) error {
|
||||
if manifest == nil {
|
||||
return errors.New("util: failed to write passed-in nil manifest")
|
||||
return errors.New("util: kustomize config file arg is nil.")
|
||||
}
|
||||
bytes, err := yaml.Marshal(manifest)
|
||||
if err != nil {
|
||||
|
||||
@@ -21,12 +21,12 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
"k8s.io/kubectl/pkg/kustomize/util/fs"
|
||||
)
|
||||
|
||||
func TestWriteAndRead(t *testing.T) {
|
||||
manifest := &manifest.Manifest{
|
||||
manifest := &types.Manifest{
|
||||
NamePrefix: "prefix",
|
||||
}
|
||||
|
||||
@@ -67,21 +67,23 @@ func TestNewNotExist(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "Run `kustomize init` first") {
|
||||
t.Fatalf("expect an error contains %q, but got %v", "does not exist", err)
|
||||
contained := "Missing kustomize config file"
|
||||
if !strings.Contains(err.Error(), contained) {
|
||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||
}
|
||||
_, err = newManifestFile("kustomize.yaml", fakeFS)
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "Run `kustomize init` first") {
|
||||
t.Fatalf("expect an error contains %q, but got %v", "does not exist", err)
|
||||
if !strings.Contains(err.Error(), contained) {
|
||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||
}
|
||||
_, err = newManifestFile(badSuffix, fakeFS)
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "should have .yaml suffix") {
|
||||
t.Fatalf("expect an error contains %q, but got %v", "does not exist", err)
|
||||
contained = "should have .yaml suffix"
|
||||
if !strings.Contains(err.Error(), contained) {
|
||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
cutil "k8s.io/kubectl/pkg/kustomize/configmapandsecret/util"
|
||||
"k8s.io/kubectl/pkg/kustomize/hash"
|
||||
"k8s.io/kubectl/pkg/kustomize/resource"
|
||||
@@ -36,7 +35,7 @@ import (
|
||||
)
|
||||
|
||||
// MakeConfigmapAndGenerateName makes a configmap and returns the configmap and the name appended with a hash.
|
||||
func MakeConfigmapAndGenerateName(cm manifest.ConfigMapArgs) (*unstructured.Unstructured, string, error) {
|
||||
func MakeConfigmapAndGenerateName(cm types.ConfigMapArgs) (*unstructured.Unstructured, string, error) {
|
||||
corev1CM, err := makeConfigMap(cm)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
@@ -51,7 +50,7 @@ func MakeConfigmapAndGenerateName(cm manifest.ConfigMapArgs) (*unstructured.Unst
|
||||
}
|
||||
|
||||
// MakeSecretAndGenerateName returns a secret with the name appended with a hash.
|
||||
func MakeSecretAndGenerateName(secret manifest.SecretArgs, path string) (*unstructured.Unstructured, string, error) {
|
||||
func MakeSecretAndGenerateName(secret types.SecretArgs, path string) (*unstructured.Unstructured, string, error) {
|
||||
corev1Secret, err := makeSecret(secret, path)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
@@ -75,7 +74,7 @@ func objectToUnstructured(in runtime.Object) (*unstructured.Unstructured, error)
|
||||
return &out, err
|
||||
}
|
||||
|
||||
func makeConfigMap(cm manifest.ConfigMapArgs) (*corev1.ConfigMap, error) {
|
||||
func makeConfigMap(cm types.ConfigMapArgs) (*corev1.ConfigMap, error) {
|
||||
corev1cm := &corev1.ConfigMap{}
|
||||
corev1cm.APIVersion = "v1"
|
||||
corev1cm.Kind = "ConfigMap"
|
||||
@@ -101,7 +100,7 @@ func makeConfigMap(cm manifest.ConfigMapArgs) (*corev1.ConfigMap, error) {
|
||||
return corev1cm, nil
|
||||
}
|
||||
|
||||
func makeSecret(secret manifest.SecretArgs, path string) (*corev1.Secret, error) {
|
||||
func makeSecret(secret types.SecretArgs, path string) (*corev1.Secret, error) {
|
||||
corev1secret := &corev1.Secret{}
|
||||
corev1secret.APIVersion = "v1"
|
||||
corev1secret.Kind = "Secret"
|
||||
@@ -137,7 +136,7 @@ func populateMap(m resource.ResourceCollection, obj *unstructured.Unstructured,
|
||||
}
|
||||
|
||||
// MakeConfigMapsResourceCollection returns a map of <GVK, oldName> -> unstructured object.
|
||||
func MakeConfigMapsResourceCollection(maps []manifest.ConfigMapArgs) (resource.ResourceCollection, error) {
|
||||
func MakeConfigMapsResourceCollection(maps []types.ConfigMapArgs) (resource.ResourceCollection, error) {
|
||||
m := resource.ResourceCollection{}
|
||||
for _, cm := range maps {
|
||||
unstructuredConfigMap, nameWithHash, err := MakeConfigmapAndGenerateName(cm)
|
||||
@@ -153,7 +152,7 @@ func MakeConfigMapsResourceCollection(maps []manifest.ConfigMapArgs) (resource.R
|
||||
}
|
||||
|
||||
// MakeSecretsResourceCollection returns a map of <GVK, oldName> -> unstructured object.
|
||||
func MakeSecretsResourceCollection(secrets []manifest.SecretArgs, path string) (resource.ResourceCollection, error) {
|
||||
func MakeSecretsResourceCollection(secrets []types.SecretArgs, path string) (resource.ResourceCollection, error) {
|
||||
m := resource.ResourceCollection{}
|
||||
for _, secret := range secrets {
|
||||
unstructuredSecret, nameWithHash, err := MakeSecretAndGenerateName(secret, path)
|
||||
|
||||
@@ -24,7 +24,7 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
)
|
||||
|
||||
func makeEnvConfigMap(name string) *corev1.ConfigMap {
|
||||
@@ -131,16 +131,16 @@ func makeUnstructuredSecret(name string) *unstructured.Unstructured {
|
||||
func TestConstructConfigMap(t *testing.T) {
|
||||
type testCase struct {
|
||||
description string
|
||||
input manifest.ConfigMapArgs
|
||||
input types.ConfigMapArgs
|
||||
expected *corev1.ConfigMap
|
||||
}
|
||||
|
||||
testCases := []testCase{
|
||||
{
|
||||
description: "construct config map from env",
|
||||
input: manifest.ConfigMapArgs{
|
||||
input: types.ConfigMapArgs{
|
||||
Name: "envConfigMap",
|
||||
DataSources: manifest.DataSources{
|
||||
DataSources: types.DataSources{
|
||||
EnvSource: "../examples/simple/instances/exampleinstance/configmap/app.env",
|
||||
},
|
||||
},
|
||||
@@ -148,9 +148,9 @@ func TestConstructConfigMap(t *testing.T) {
|
||||
},
|
||||
{
|
||||
description: "construct config map from file",
|
||||
input: manifest.ConfigMapArgs{
|
||||
input: types.ConfigMapArgs{
|
||||
Name: "fileConfigMap",
|
||||
DataSources: manifest.DataSources{
|
||||
DataSources: types.DataSources{
|
||||
FileSources: []string{"../examples/simple/instances/exampleinstance/configmap/app-init.ini"},
|
||||
},
|
||||
},
|
||||
@@ -158,9 +158,9 @@ func TestConstructConfigMap(t *testing.T) {
|
||||
},
|
||||
{
|
||||
description: "construct config map from literal",
|
||||
input: manifest.ConfigMapArgs{
|
||||
input: types.ConfigMapArgs{
|
||||
Name: "literalConfigMap",
|
||||
DataSources: manifest.DataSources{
|
||||
DataSources: types.DataSources{
|
||||
LiteralSources: []string{"a=x", "b=y"},
|
||||
},
|
||||
},
|
||||
@@ -180,7 +180,7 @@ func TestConstructConfigMap(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConstructSecret(t *testing.T) {
|
||||
secret := manifest.SecretArgs{
|
||||
secret := types.SecretArgs{
|
||||
Name: "secret",
|
||||
Commands: map[string]string{
|
||||
"DB_USERNAME": "printf admin",
|
||||
@@ -199,7 +199,7 @@ func TestConstructSecret(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFailConstructSecret(t *testing.T) {
|
||||
secret := manifest.SecretArgs{
|
||||
secret := types.SecretArgs{
|
||||
Name: "secret",
|
||||
Commands: map[string]string{
|
||||
"FAILURE": "false", // This will fail.
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
# This example is from https://docs.google.com/document/d/1cLPGweVEYrVqQvBLJg6sxV-TrE5Rm2MNOBA_cxZP2WU/edit#heading=h.dr88tktf0e99
|
||||
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: test-infra-mungebot
|
||||
kustomizationName: test-infra-mungebot
|
||||
namePrefix: test-infra-
|
||||
# Labels to add to all objects and selectors.
|
||||
# These labels would also be used to form the selector for apply --prune
|
||||
|
||||
@@ -1,11 +1,4 @@
|
||||
# This example is from https://docs.google.com/document/d/1cLPGweVEYrVqQvBLJg6sxV-TrE5Rm2MNOBA_cxZP2WU/edit#heading=h.dr88tktf0e99
|
||||
|
||||
# Inspired by https://github.com/kubernetes/helm/blob/master/docs/charts.md
|
||||
# But Kubernetes API style
|
||||
apiVersion: manifest.k8s.io/v1alpha1
|
||||
kind: Manifest
|
||||
metadata:
|
||||
name: mungebot
|
||||
kustomizationName: mungebot
|
||||
namePrefix: baseprefix-
|
||||
# Labels to add to all objects and selectors.
|
||||
# These labels would also be used to form the selector for apply --prune
|
||||
|
||||
@@ -22,12 +22,12 @@ import (
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
cutil "k8s.io/kubectl/pkg/kustomize/configmapandsecret/util"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
"k8s.io/kubectl/pkg/loader"
|
||||
)
|
||||
|
||||
func newFromConfigMap(l loader.Loader, cm manifest.ConfigMapArgs) (*Resource, error) {
|
||||
func newFromConfigMap(l loader.Loader, cm types.ConfigMapArgs) (*Resource, error) {
|
||||
corev1CM, err := makeConfigMap(l, cm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -40,7 +40,7 @@ func newFromConfigMap(l loader.Loader, cm manifest.ConfigMapArgs) (*Resource, er
|
||||
return &Resource{Data: data, Behavior: cm.Behavior}, nil
|
||||
}
|
||||
|
||||
func makeConfigMap(l loader.Loader, cm manifest.ConfigMapArgs) (*corev1.ConfigMap, error) {
|
||||
func makeConfigMap(l loader.Loader, cm types.ConfigMapArgs) (*corev1.ConfigMap, error) {
|
||||
var envPairs, literalPairs, filePairs []kvPair
|
||||
var err error
|
||||
|
||||
@@ -130,8 +130,8 @@ func addKV(m map[string]string, kv kvPair) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewFromConfigMaps returns a Resource slice given a configmap metadata slice from manifest file.
|
||||
func NewFromConfigMaps(loader loader.Loader, cmList []manifest.ConfigMapArgs) (ResourceCollection, error) {
|
||||
// NewFromConfigMaps returns a Resource slice given a configmap metadata slice from kustomize config file.
|
||||
func NewFromConfigMaps(loader loader.Loader, cmList []types.ConfigMapArgs) (ResourceCollection, error) {
|
||||
allResources := []*Resource{}
|
||||
for _, cm := range cmList {
|
||||
res, err := newFromConfigMap(loader, cm)
|
||||
|
||||
@@ -22,15 +22,15 @@ import (
|
||||
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/resource"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
"k8s.io/kubectl/pkg/loader/loadertest"
|
||||
)
|
||||
|
||||
func TestNewFromConfigMaps(t *testing.T) {
|
||||
type testCase struct {
|
||||
description string
|
||||
input []manifest.ConfigMapArgs
|
||||
input []types.ConfigMapArgs
|
||||
filepath string
|
||||
content string
|
||||
expected resource.ResourceCollection
|
||||
@@ -40,10 +40,10 @@ func TestNewFromConfigMaps(t *testing.T) {
|
||||
testCases := []testCase{
|
||||
{
|
||||
description: "construct config map from env",
|
||||
input: []manifest.ConfigMapArgs{
|
||||
input: []types.ConfigMapArgs{
|
||||
{
|
||||
Name: "envConfigMap",
|
||||
DataSources: manifest.DataSources{
|
||||
DataSources: types.DataSources{
|
||||
EnvSource: "app.env",
|
||||
},
|
||||
},
|
||||
@@ -74,9 +74,9 @@ func TestNewFromConfigMaps(t *testing.T) {
|
||||
},
|
||||
{
|
||||
description: "construct config map from file",
|
||||
input: []manifest.ConfigMapArgs{{
|
||||
input: []types.ConfigMapArgs{{
|
||||
Name: "fileConfigMap",
|
||||
DataSources: manifest.DataSources{
|
||||
DataSources: types.DataSources{
|
||||
FileSources: []string{"app-init.ini"},
|
||||
},
|
||||
},
|
||||
@@ -108,10 +108,10 @@ BAR=baz
|
||||
},
|
||||
{
|
||||
description: "construct config map from literal",
|
||||
input: []manifest.ConfigMapArgs{
|
||||
input: []types.ConfigMapArgs{
|
||||
{
|
||||
Name: "literalConfigMap",
|
||||
DataSources: manifest.DataSources{
|
||||
DataSources: types.DataSources{
|
||||
LiteralSources: []string{"a=x", "b=y"},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -24,10 +24,10 @@ import (
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
)
|
||||
|
||||
func newFromSecretGenerator(p string, s manifest.SecretArgs) (*Resource, error) {
|
||||
func newFromSecretGenerator(p string, s types.SecretArgs) (*Resource, error) {
|
||||
corev1secret := &corev1.Secret{}
|
||||
corev1secret.APIVersion = "v1"
|
||||
corev1secret.Kind = "Secret"
|
||||
@@ -70,7 +70,7 @@ func createSecretKey(wd string, command string) ([]byte, error) {
|
||||
|
||||
// NewFromSecretGenerators takes a SecretGenerator slice and executes its command in directory p
|
||||
// then writes the output to a Resource slice and return it.
|
||||
func NewFromSecretGenerators(p string, secretList []manifest.SecretArgs) (ResourceCollection, error) {
|
||||
func NewFromSecretGenerators(p string, secretList []types.SecretArgs) (ResourceCollection, error) {
|
||||
allResources := []*Resource{}
|
||||
for _, secret := range secretList {
|
||||
res, err := newFromSecretGenerator(p, secret)
|
||||
|
||||
@@ -24,11 +24,11 @@ import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
manifest "k8s.io/kubectl/pkg/apis/manifest/v1alpha1"
|
||||
"k8s.io/kubectl/pkg/kustomize/types"
|
||||
)
|
||||
|
||||
func TestNewFromSecretGenerators(t *testing.T) {
|
||||
secrets := []manifest.SecretArgs{
|
||||
secrets := []types.SecretArgs{
|
||||
{
|
||||
Name: "secret",
|
||||
Commands: map[string]string{
|
||||
|
||||
137
types/manifest.go
Normal file
137
types/manifest.go
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
Copyright 2017 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package types
|
||||
|
||||
// Manifest has all the information to expand of generate the k8s api resources.
|
||||
// It can be used by kubectl or some other tooling.
|
||||
// A manifest could be either a Base or an Overlay.
|
||||
// TODO: rename Manifest to Kustomization
|
||||
type Manifest struct {
|
||||
// KustomizationName is a documentation field - a name for the customizations
|
||||
// in a marshalled version of this struct, that survives YAML unmarshalling
|
||||
// that discards comments.
|
||||
KustomizationName string `json:"kustomizationName,omitempty" yaml:"kustomizationName,omitempty"`
|
||||
|
||||
// NamePrefix will prefix the names of all resources mentioned in the manifest
|
||||
// including generated configmaps and secrets.
|
||||
NamePrefix string `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"`
|
||||
|
||||
// Labels to add to all objects and selectors.
|
||||
// These labels would also be used to form the selector for apply --prune
|
||||
// Named differently than “labels” to avoid confusion with metadata for
|
||||
// this object
|
||||
ObjectLabels map[string]string `json:"objectLabels,omitempty" yaml:"objectLabels,omitempty"`
|
||||
|
||||
// Annotations to add to all objects.
|
||||
ObjectAnnotations map[string]string `json:"objectAnnotations,omitempty" yaml:"objectAnnotations,omitempty"`
|
||||
|
||||
// Bases contain the paths to other packages that this manifest depends on.
|
||||
// Each path should be either a path to a kustomize.yaml or a path of
|
||||
// a directory that contains a kustomize.yaml file.
|
||||
Bases []string `json:"bases,omitempty" yaml:"bases,omitempty"`
|
||||
|
||||
// Resources specifies the relative paths within the package.
|
||||
// It could be any format that kubectl -f allows, i.e. files, directories,
|
||||
// URLs and globs.
|
||||
Resources []string `json:"resources,omitempty" yaml:"resources,omitempty"`
|
||||
|
||||
// An Patch entry is very similar to an Resource entry.
|
||||
// It specifies the relative paths within the package, and could be any
|
||||
// format that kubectl -f allows.
|
||||
// It should be able to be merged by Strategic Merge Patch on top of its
|
||||
// corresponding base resource.
|
||||
Patches []string `json:"patches,omitempty" yaml:"patches,omitempty"`
|
||||
|
||||
// List of configmaps to generate from configuration sources.
|
||||
// Base/overlay concept doesn't apply to this field.
|
||||
// If a configmap want to have a base and an overlay, it should go to Bases
|
||||
// and Overlays fields.
|
||||
ConfigMapGenerator []ConfigMapArgs `json:"configMapGenerator,omitempty" yaml:"configMapGenerator,omitempty"`
|
||||
|
||||
// List of secrets to generate from secret commands.
|
||||
// Base/overlay concept doesn't apply to this field.
|
||||
// If a secret want to have a base and an overlay, it should go to Bases and
|
||||
// Overlays fields.
|
||||
SecretGenerator []SecretArgs `json:"secretGenerator,omitempty" yaml:"secretGenerator,omitempty"`
|
||||
}
|
||||
|
||||
// ConfigMapArg contains the metadata of how to generate a configmap.
|
||||
type ConfigMapArgs struct {
|
||||
// Name of the configmap.
|
||||
// The full name should be Manifest.NamePrefix + Configmap.Name +
|
||||
// hash(content of configmap).
|
||||
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||
|
||||
// Behavior of configmap, must be one of create, merge and replace
|
||||
// 'create': create a new one;
|
||||
// 'replace': replace the existing one;
|
||||
// 'merge': merge the existing one.
|
||||
Behavior string `json:"behavior,omitempty" yaml:"behavior,omitempty"`
|
||||
|
||||
// DataSources for configmap.
|
||||
DataSources `json:",inline,omitempty" yaml:",inline,omitempty"`
|
||||
}
|
||||
|
||||
// SecretGenerator contains the metadata of how to generate a secret.
|
||||
type SecretArgs struct {
|
||||
// Name of the secret.
|
||||
// The full name should be Manifest.NamePrefix + SecretGenerator.Name +
|
||||
// hash(content of secret).
|
||||
Name string `json:"name,omitempty" yaml:"name,omitempty"`
|
||||
|
||||
// Behavior of secretGenerator, must be one of create, merge and replace
|
||||
// 'create': create a new one;
|
||||
// 'replace': replace the existing one;
|
||||
// 'merge': merge the existing one.
|
||||
Behavior string `json:"behavior,omitempty" yaml:"behavior,omitempty"`
|
||||
|
||||
// Type of the secret.
|
||||
//
|
||||
// This is the same field as the secret type field in v1/Secret:
|
||||
// It can be "Opaque" (default), or "kubernetes.io/tls".
|
||||
//
|
||||
// If type is "kubernetes.io/tls", then "Commands" must have exactly two
|
||||
// keys: "tls.key" and "tls.crt"
|
||||
Type string `json:"type,omitempty" yaml:"type,omitempty"`
|
||||
|
||||
// Map of keys to commands to generate the values
|
||||
Commands map[string]string `json:",commands,omitempty" yaml:",inline,omitempty"`
|
||||
}
|
||||
|
||||
// DataSources contains some generic sources for configmap or secret.
|
||||
// Only one field can be set.
|
||||
type DataSources struct {
|
||||
// LiteralSources is a list of literal sources.
|
||||
// Each literal source should be a key and literal value,
|
||||
// e.g. `somekey=somevalue`
|
||||
// It will be similar to kubectl create configmap|secret --from-literal
|
||||
LiteralSources []string `json:"literals,omitempty" yaml:"literals,omitempty"`
|
||||
|
||||
// FileSources is a list of file sources.
|
||||
// Each file source can be specified using its file path, in which case file
|
||||
// basename will be used as configmap key, or optionally with a key and file
|
||||
// path, in which case the given key will be used.
|
||||
// Specifying a directory will iterate each named file in the directory
|
||||
// whose basename is a valid configmap key.
|
||||
// It will be similar to kubectl create configmap|secret --from-file
|
||||
FileSources []string `json:"files,omitempty" yaml:"files,omitempty"`
|
||||
|
||||
// EnvSource format should be a path to a file to read lines of key=val
|
||||
// pairs to create a configmap.
|
||||
// i.e. a Docker .env file or a .ini file.
|
||||
EnvSource string `json:"env,omitempty" yaml:"env,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user