Skip to content

Commit 986c85e

Browse files
committed
Add subcommand: edit fix
1 parent aeda417 commit 986c85e

File tree

5 files changed

+128
-3
lines changed

5 files changed

+128
-3
lines changed

pkg/commands/edit/all.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package edit
1919
import (
2020
"github.com/spf13/cobra"
2121
"sigs.k8s.io/kustomize/pkg/commands/edit/add"
22+
"sigs.k8s.io/kustomize/pkg/commands/edit/fix"
2223
"sigs.k8s.io/kustomize/pkg/commands/edit/set"
2324
"sigs.k8s.io/kustomize/pkg/fs"
2425
"sigs.k8s.io/kustomize/pkg/ifc"
@@ -45,6 +46,7 @@ func NewCmdEdit(fsys fs.FileSystem, v ifc.Validator, kf ifc.KunstructuredFactory
4546
c.AddCommand(
4647
add.NewCmdAdd(fsys, v, kf),
4748
set.NewCmdSet(fsys, v),
49+
fix.NewCmdFix(fsys),
4850
)
4951
return c
5052
}

pkg/commands/edit/fix/fix.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package fix
18+
19+
import (
20+
"log"
21+
"strings"
22+
23+
"github.com/spf13/cobra"
24+
"sigs.k8s.io/kustomize/pkg/commands/kustfile"
25+
"sigs.k8s.io/kustomize/pkg/fs"
26+
)
27+
28+
// NewCmdFix returns an instance of 'fix' subcommand.
29+
func NewCmdFix(fSys fs.FileSystem) *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: "fix",
32+
Short: "Fix the missing fields in kustomization file",
33+
Long: "",
34+
Example: `
35+
# Fix the missing and deprecated fields in kustomization file
36+
kustomize fix
37+
38+
`,
39+
RunE: func(cmd *cobra.Command, args []string) error {
40+
return RunFix(fSys)
41+
},
42+
}
43+
return cmd
44+
}
45+
46+
func RunFix(fSys fs.FileSystem) error {
47+
mf, err := kustfile.NewKustomizationFile(fSys)
48+
if err != nil {
49+
return err
50+
}
51+
52+
m, err := mf.Read()
53+
if err != nil {
54+
return err
55+
}
56+
msgs := m.DealWithMissingFields()
57+
if len(msgs) > 0 {
58+
log.Printf(strings.Join(msgs, "\n"))
59+
}
60+
61+
return mf.Write(m)
62+
}

pkg/commands/edit/fix/fix_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package fix
18+
19+
import (
20+
"strings"
21+
"testing"
22+
23+
"sigs.k8s.io/kustomize/pkg/fs"
24+
)
25+
26+
func TestFix(t *testing.T) {
27+
fakeFS := fs.MakeFakeFS()
28+
fakeFS.WriteTestKustomizationWith([]byte(`nameprefix: some-prefix-`))
29+
30+
cmd := NewCmdFix(fakeFS)
31+
err := cmd.RunE(cmd, nil)
32+
if err != nil {
33+
t.Errorf("unexpected cmd error: %v", err)
34+
}
35+
content, err := fakeFS.ReadTestKustomization()
36+
if err != nil {
37+
t.Errorf("unexpected read error: %v", err)
38+
}
39+
if !strings.Contains(string(content), "apiVersion: ") {
40+
t.Errorf("expected apiVersion in kustomization")
41+
}
42+
if !strings.Contains(string(content), "kind: Kustomization") {
43+
t.Errorf("expected kind in kustomization")
44+
}
45+
}

pkg/fs/fakefs.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ func MakeFakeFS() *fakeFs {
4040
}
4141

4242
// kustomizationContent is used in tests.
43-
const kustomizationContent = `namePrefix: some-prefix
43+
const kustomizationContent = `apiVersion: v1
44+
kind: Kustomization
45+
namePrefix: some-prefix
4446
nameSuffix: some-suffix
4547
# Labels to add to all objects and selectors.
4648
# These labels would also be used to form the selector for apply --prune

pkg/types/kustomization.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,29 @@ func (k *Kustomization) DealWithDeprecatedFields() {
151151
}
152152
}
153153

154+
// DealWithMissingFields fills the missing fields
155+
func (k *Kustomization) DealWithMissingFields() []string {
156+
var msgs []string
157+
if k.APIVersion == "" {
158+
k.APIVersion = KustomizationVersion
159+
msgs = append(msgs, "Fixed the missing field by adding apiVersion: "+KustomizationVersion)
160+
}
161+
if k.Kind == "" {
162+
k.Kind = KustomizationKind
163+
msgs = append(msgs, "Fixed the missing field by adding apiKind: "+KustomizationKind)
164+
}
165+
return msgs
166+
}
167+
154168
func (k *Kustomization) EnforceFields() ([]string, []string) {
155169
var msgs, errs []string
156170
if k.APIVersion == "" {
157-
msgs = append(msgs, "apiVersion is not defined. This will not be allowed in the next release.\nPlease add apiVersion: "+KustomizationVersion)
171+
msgs = append(msgs, "apiVersion is not defined. This will not be allowed in the next release.\nPlease run `kustomize edit fix`")
158172
} else if k.APIVersion != KustomizationVersion {
159173
errs = append(errs, "apiVersion should be "+KustomizationVersion)
160174
}
161175
if k.Kind == "" {
162-
msgs = append(msgs, "kind is not defined. This will not be allowed in the next release.\nPlease add kind: "+KustomizationKind)
176+
msgs = append(msgs, "kind is not defined. This will not be allowed in the next release.\nPlease run `kustomize edit fix`")
163177
} else if k.Kind != KustomizationKind {
164178
errs = append(errs, "kind should be "+KustomizationKind)
165179
}

0 commit comments

Comments
 (0)