4
4
package localize
5
5
6
6
import (
7
+ "bytes"
7
8
"log"
9
+ "path/filepath"
8
10
9
11
"github.com/spf13/cobra"
10
12
lclzr "sigs.k8s.io/kustomize/api/krusty/localizer"
13
+ "sigs.k8s.io/kustomize/kustomize/v5/commands/build"
14
+ "sigs.k8s.io/kustomize/kyaml/copyutil"
11
15
"sigs.k8s.io/kustomize/kyaml/errors"
12
16
"sigs.k8s.io/kustomize/kyaml/filesys"
13
17
)
@@ -20,23 +24,26 @@ type arguments struct {
20
24
}
21
25
22
26
type flags struct {
23
- scope string
27
+ scope string
28
+ noVerify bool
24
29
}
25
30
26
31
// NewCmdLocalize returns a new localize command.
27
32
func NewCmdLocalize (fs filesys.FileSystem ) * cobra.Command {
28
33
var f flags
34
+ var buildBuffer bytes.Buffer
35
+ buildCmd := build .NewCmdBuild (fs , & build.Help {}, & buildBuffer )
29
36
cmd := & cobra.Command {
30
37
Use : "localize [target [destination]]" ,
31
38
Short : "[Alpha] Creates localized copy of target kustomization root at destination" ,
32
- Long : `[Alpha] Creates copy of target kustomization directory or
33
- versioned URL at destination, where remote references in the original
39
+ Long : `[Alpha] Creates copy of target kustomization directory or
40
+ versioned URL at destination, where remote references in the original
34
41
are replaced by local references to the downloaded remote content.
35
42
36
- If target is not specified, the current working directory will be used.
37
- Destination is a path to a new directory in an existing directory. If
38
- destination is not specified, a new directory will be created in the current
39
- working directory.
43
+ If target is not specified, the current working directory will be used.
44
+ Destination is a path to a new directory in an existing directory. If
45
+ destination is not specified, a new directory will be created in the current
46
+ working directory.
40
47
41
48
For details, see: https://kubectl.docs.kubernetes.io/references/kustomize/cmd/
42
49
@@ -46,7 +53,7 @@ alphabetizes kustomization fields in the localized copy.
46
53
` ,
47
54
Example : `
48
55
# Localize the current working directory, with default scope and destination
49
- kustomize localize
56
+ kustomize localize
50
57
51
58
# Localize some local directory, with scope and default destination
52
59
kustomize localize /home/path/scope/target --scope /home/path/scope
@@ -62,6 +69,30 @@ kustomize localize https://github.com/kubernetes-sigs/kustomize//api/krusty/test
62
69
if err != nil {
63
70
return errors .Wrap (err )
64
71
}
72
+
73
+ if ! f .noVerify {
74
+ originalBuild , err := runBuildCmd (buildBuffer , buildCmd , args .target )
75
+ if err != nil {
76
+ return errors .Wrap (err )
77
+ }
78
+
79
+ buildDst := dst
80
+ if f .scope != "" && f .scope != args .target {
81
+ buildDst = filepath .Join (dst , filepath .Base (args .target ))
82
+ }
83
+
84
+ localizedBuild , err := runBuildCmd (buildBuffer , buildCmd , buildDst )
85
+ if err != nil {
86
+ return errors .Wrap (err )
87
+ }
88
+
89
+ if localizedBuild != originalBuild {
90
+ copyutil .PrettyFileDiff (originalBuild , localizedBuild )
91
+ log .Fatalf ("VERIFICATION FAILED: `kustomize build` for %s and %s are different after localization.\n " , args .target , dst )
92
+ }
93
+ log .Printf ("VERIFICATION SUCCESS: `kustomize build` for %s and %s are the same after localization.\n " , args .target , dst )
94
+ }
95
+
65
96
log .Printf ("SUCCESS: localized %q to directory %s\n " , args .target , dst )
66
97
return nil
67
98
},
@@ -74,6 +105,12 @@ kustomize localize https://github.com/kubernetes-sigs/kustomize//api/krusty/test
74
105
Cannot specify for remote targets, as scope is by default the containing repo.
75
106
If not specified for local target, scope defaults to target.
76
107
` )
108
+ cmd .Flags ().BoolVar (& f .noVerify ,
109
+ "no-verify" ,
110
+ false ,
111
+ `Does not verify that the outputs of kustomize build for target and newDir are the same after localization.
112
+ If not specified, this flag defaults to false and will run kustomize build.
113
+ ` )
77
114
return cmd
78
115
}
79
116
@@ -92,3 +129,16 @@ func matchArgs(rawArgs []string) arguments {
92
129
}
93
130
return args
94
131
}
132
+
133
+ func runBuildCmd (buffer bytes.Buffer , cmd * cobra.Command , folder string ) (buildOutput string , err error ) {
134
+ buffer .Reset ()
135
+ buildErr := cmd .RunE (cmd , []string {folder })
136
+ if buildErr != nil {
137
+ log .Printf ("If your target directory requires flags to build: \n " +
138
+ "1. Add executable permissions for the downloaded exec binaries in '%s'. \n " +
139
+ "2. Run kustomize build with the necessary flags and self-verify the outputs." , folder )
140
+ return "" , errors .Wrap (buildErr )
141
+ }
142
+
143
+ return buffer .String (), nil
144
+ }
0 commit comments