Add subcommand: edit fix

This commit is contained in:
Jingfang Liu
2018-12-13 11:38:29 -08:00
parent aeda4172e4
commit 986c85e728
5 changed files with 128 additions and 3 deletions

View File

@@ -19,6 +19,7 @@ package edit
import ( import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/edit/add" "sigs.k8s.io/kustomize/pkg/commands/edit/add"
"sigs.k8s.io/kustomize/pkg/commands/edit/fix"
"sigs.k8s.io/kustomize/pkg/commands/edit/set" "sigs.k8s.io/kustomize/pkg/commands/edit/set"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc" "sigs.k8s.io/kustomize/pkg/ifc"
@@ -45,6 +46,7 @@ func NewCmdEdit(fsys fs.FileSystem, v ifc.Validator, kf ifc.KunstructuredFactory
c.AddCommand( c.AddCommand(
add.NewCmdAdd(fsys, v, kf), add.NewCmdAdd(fsys, v, kf),
set.NewCmdSet(fsys, v), set.NewCmdSet(fsys, v),
fix.NewCmdFix(fsys),
) )
return c return c
} }

View File

@@ -0,0 +1,62 @@
/*
Copyright 2018 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 fix
import (
"log"
"strings"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/fs"
)
// NewCmdFix returns an instance of 'fix' subcommand.
func NewCmdFix(fSys fs.FileSystem) *cobra.Command {
cmd := &cobra.Command{
Use: "fix",
Short: "Fix the missing fields in kustomization file",
Long: "",
Example: `
# Fix the missing and deprecated fields in kustomization file
kustomize fix
`,
RunE: func(cmd *cobra.Command, args []string) error {
return RunFix(fSys)
},
}
return cmd
}
func RunFix(fSys fs.FileSystem) error {
mf, err := kustfile.NewKustomizationFile(fSys)
if err != nil {
return err
}
m, err := mf.Read()
if err != nil {
return err
}
msgs := m.DealWithMissingFields()
if len(msgs) > 0 {
log.Printf(strings.Join(msgs, "\n"))
}
return mf.Write(m)
}

View File

@@ -0,0 +1,45 @@
/*
Copyright 2018 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 fix
import (
"strings"
"testing"
"sigs.k8s.io/kustomize/pkg/fs"
)
func TestFix(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteTestKustomizationWith([]byte(`nameprefix: some-prefix-`))
cmd := NewCmdFix(fakeFS)
err := cmd.RunE(cmd, nil)
if err != nil {
t.Errorf("unexpected cmd error: %v", err)
}
content, err := fakeFS.ReadTestKustomization()
if err != nil {
t.Errorf("unexpected read error: %v", err)
}
if !strings.Contains(string(content), "apiVersion: ") {
t.Errorf("expected apiVersion in kustomization")
}
if !strings.Contains(string(content), "kind: Kustomization") {
t.Errorf("expected kind in kustomization")
}
}

View File

@@ -40,7 +40,9 @@ func MakeFakeFS() *fakeFs {
} }
// kustomizationContent is used in tests. // kustomizationContent is used in tests.
const kustomizationContent = `namePrefix: some-prefix const kustomizationContent = `apiVersion: v1
kind: Kustomization
namePrefix: some-prefix
nameSuffix: some-suffix nameSuffix: some-suffix
# Labels to add to all objects and selectors. # Labels to add to all objects and selectors.
# These labels would also be used to form the selector for apply --prune # These labels would also be used to form the selector for apply --prune

View File

@@ -151,15 +151,29 @@ func (k *Kustomization) DealWithDeprecatedFields() {
} }
} }
// DealWithMissingFields fills the missing fields
func (k *Kustomization) DealWithMissingFields() []string {
var msgs []string
if k.APIVersion == "" {
k.APIVersion = KustomizationVersion
msgs = append(msgs, "Fixed the missing field by adding apiVersion: "+KustomizationVersion)
}
if k.Kind == "" {
k.Kind = KustomizationKind
msgs = append(msgs, "Fixed the missing field by adding apiKind: "+KustomizationKind)
}
return msgs
}
func (k *Kustomization) EnforceFields() ([]string, []string) { func (k *Kustomization) EnforceFields() ([]string, []string) {
var msgs, errs []string var msgs, errs []string
if k.APIVersion == "" { if k.APIVersion == "" {
msgs = append(msgs, "apiVersion is not defined. This will not be allowed in the next release.\nPlease add apiVersion: "+KustomizationVersion) msgs = append(msgs, "apiVersion is not defined. This will not be allowed in the next release.\nPlease run `kustomize edit fix`")
} else if k.APIVersion != KustomizationVersion { } else if k.APIVersion != KustomizationVersion {
errs = append(errs, "apiVersion should be "+KustomizationVersion) errs = append(errs, "apiVersion should be "+KustomizationVersion)
} }
if k.Kind == "" { if k.Kind == "" {
msgs = append(msgs, "kind is not defined. This will not be allowed in the next release.\nPlease add kind: "+KustomizationKind) msgs = append(msgs, "kind is not defined. This will not be allowed in the next release.\nPlease run `kustomize edit fix`")
} else if k.Kind != KustomizationKind { } else if k.Kind != KustomizationKind {
errs = append(errs, "kind should be "+KustomizationKind) errs = append(errs, "kind should be "+KustomizationKind)
} }