@@ -29,24 +29,26 @@ var (
29
29
)
30
30
31
31
func main () {
32
+ start := time .Now ()
33
+
32
34
homeDir , err := os .UserHomeDir ()
33
35
if err != nil {
34
36
log .Fatalf ("failed getting user home dir: %v\n " , err )
35
37
}
36
38
37
39
var (
38
- start = time . Now ( )
39
-
40
- kubeConfigPath = flag .String ("config " , filepath . Join ( homeDir , ".kube " , "config" ), "path to the kubeconfig " )
41
- kubeContext = flag .String ("context " , "" , "context from the kubeconfig, empty for default " )
42
- outdirFlag = flag .String ("dir " , "dump " , "output directory for the dumps " )
43
- resourcesFlag = flag .String ("resources " , "" , "resource to dump (e.g. 'configmaps,secrets '), empty for all" )
44
- namespacesFlag = flag .String ("namespaces" , "" , "namespace to dump (e.g. 'ns1,ns2'), empty for all " )
45
- clusterscopedFlag = flag .Bool ("clusterscoped" , true , "dump cluster-wide resources" )
46
- namespacedFlag = flag .Bool ("namespaced" , true , "dump namespaced resources" )
47
- statelessFlag = flag .Bool ("stateless" , true , "remove fields containing a state of the resource" )
48
- verboseFlag = flag .Bool ("verbose" , false , "output the current progress" )
49
- versionFlag = flag .Bool ("version" , false , fmt .Sprintf ("print version information of this release (%v)" , version ))
40
+ kubeConfigPath = flag . String ( "config" , filepath . Join ( homeDir , ".kube" , "config" ), "path to the kubeconfig" )
41
+ kubeContext = flag . String ( "context" , "" , "context from the kubeconfig, empty for default" )
42
+ outdirFlag = flag .String ("dir " , "dump " , "output directory for the dumps " )
43
+ resourcesFlag = flag .String ("resources " , "" , "resource to dump (e.g. 'configmaps,secrets'), empty for all " )
44
+ ignoreResourcesFlag = flag .String ("ignore-resources " , "" , "resource to ignore (e.g. 'configmaps,secrets') " )
45
+ namespacesFlag = flag .String ("namespaces " , "" , "namespace to dump (e.g. 'ns1,ns2 '), empty for all" )
46
+ ignoreNamespacesFlag = flag .String ("ignore- namespaces" , "" , "namespace to ignore (e.g. 'ns1,ns2')" )
47
+ clusterscopedFlag = flag .Bool ("clusterscoped" , true , "dump cluster-wide resources" )
48
+ namespacedFlag = flag .Bool ("namespaced" , true , "dump namespaced resources" )
49
+ statelessFlag = flag .Bool ("stateless" , true , "remove fields containing a state of the resource" )
50
+ verboseFlag = flag .Bool ("verbose" , false , "output the current progress" )
51
+ versionFlag = flag .Bool ("version" , false , fmt .Sprintf ("print version information of this release (%v)" , version ))
50
52
)
51
53
flag .Parse ()
52
54
@@ -58,8 +60,10 @@ func main() {
58
60
}
59
61
60
62
var (
61
- wantResources = strings .Split (strings .ToLower (* resourcesFlag ), "," )
62
- wantNamespaces = strings .Split (strings .ToLower (* namespacesFlag ), "," )
63
+ wantResources = strings .Split (strings .ToLower (* resourcesFlag ), "," )
64
+ wantNamespaces = strings .Split (strings .ToLower (* namespacesFlag ), "," )
65
+ ignoreResources = strings .Split (strings .ToLower (* ignoreResourcesFlag ), "," )
66
+ ignoreNamespaces = strings .Split (strings .ToLower (* ignoreNamespacesFlag ), "," )
63
67
)
64
68
65
69
kubeConfig , err := buildConfigFromFlags (* kubeContext , * kubeConfigPath )
@@ -92,19 +96,7 @@ func main() {
92
96
}
93
97
94
98
for _ , res := range resources .APIResources {
95
- if ! slices .Contains (res .Verbs , "get" ) {
96
- // we can't get the resource, so let's skip it
97
- continue
98
- }
99
-
100
- if strings .Contains (res .Name , "/" ) {
101
- // skip subresources
102
- // TODO: probably there is a better way to not get them in the first place
103
- continue
104
- }
105
-
106
- // check if we got the specified resources (if any resources were specified)
107
- if * resourcesFlag != "" && ! slices .Contains (wantResources , res .Name ) {
99
+ if skipResource (res , wantResources , ignoreResources ) {
108
100
continue
109
101
}
110
102
@@ -125,14 +117,7 @@ func main() {
125
117
}
126
118
127
119
for _ , listItem := range unstrList .Items {
128
- // filter according to flags
129
- if listItem .GetNamespace () != "" && ! * namespacedFlag {
130
- continue
131
- }
132
- if listItem .GetNamespace () == "" && ! * clusterscopedFlag {
133
- continue
134
- }
135
- if * namespacesFlag != "" && ! slices .Contains (wantNamespaces , listItem .GetNamespace ()) {
120
+ if skipItem (listItem , * namespacedFlag , * clusterscopedFlag , wantNamespaces , ignoreNamespaces ) {
136
121
continue
137
122
}
138
123
@@ -162,6 +147,52 @@ func main() {
162
147
fmt .Printf ("loaded %d manifests in %v\n " , written , time .Since (start ).Round (1 * time .Millisecond ))
163
148
}
164
149
150
+ func skipResource (res metav1.APIResource , wantResources , ignoreResources []string ) bool {
151
+ // check if we can even 'get' the resource
152
+ if ! slices .Contains (res .Verbs , "get" ) {
153
+ return true
154
+ }
155
+
156
+ // skip subresources
157
+ // TODO: maybe there is a better way to not get them in the first place
158
+ if strings .Contains (res .Name , "/" ) {
159
+ return true
160
+ }
161
+
162
+ // check if we got the specified resources (if any resources were specified)
163
+ if len (wantResources ) > 0 && wantResources [0 ] != "" && ! slices .Contains (wantResources , res .Name ) {
164
+ return true
165
+ }
166
+
167
+ // check if we got a resource to ignore (if any resources were specified)
168
+ if len (ignoreResources ) > 0 && ignoreResources [0 ] != "" && slices .Contains (ignoreResources , res .Name ) {
169
+ return true
170
+ }
171
+
172
+ return false
173
+ }
174
+
175
+ func skipItem (item unstructured.Unstructured , namespaced , clusterscoped bool , wantNamespaces , ignoreNamespaces []string ) bool {
176
+ // item with namespace but we skip namespaced items
177
+ if item .GetNamespace () != "" && ! namespaced {
178
+ return true
179
+ }
180
+ // item clusterscoped but we skip them
181
+ if item .GetNamespace () == "" && ! clusterscoped {
182
+ return true
183
+ }
184
+ // specific namespaces specied but doesn't match
185
+ if len (wantNamespaces ) > 0 && wantNamespaces [0 ] != "" && ! slices .Contains (wantNamespaces , item .GetNamespace ()) {
186
+ return true
187
+ }
188
+ // ignore specific namespaces and it matches
189
+ if len (ignoreNamespaces ) > 0 && ignoreNamespaces [0 ] != "" && slices .Contains (ignoreNamespaces , item .GetNamespace ()) {
190
+ return true
191
+ }
192
+
193
+ return false
194
+ }
195
+
165
196
func writeYAML (outDir , resourceAndGroup string , item unstructured.Unstructured , stateless bool ) error {
166
197
if stateless {
167
198
cleanState (item )
0 commit comments