@@ -20,9 +20,11 @@ import (
20
20
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21
21
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
22
22
"k8s.io/apimachinery/pkg/runtime"
23
+ "k8s.io/apimachinery/pkg/runtime/schema"
23
24
"k8s.io/apimachinery/pkg/util/errors"
24
25
"k8s.io/apimachinery/pkg/util/sets"
25
26
"k8s.io/apimachinery/pkg/util/wait"
27
+ "k8s.io/client-go/discovery"
26
28
"k8s.io/client-go/dynamic"
27
29
restclient "k8s.io/client-go/rest"
28
30
"k8s.io/kubernetes/pkg/api/legacyscheme"
@@ -154,6 +156,50 @@ type NewAppOptions struct {
154
156
* ObjectGeneratorOptions
155
157
}
156
158
159
+ func nonEmpty (str string , def string ) string {
160
+ if str == "" {
161
+ return def
162
+ }
163
+ return str
164
+ }
165
+
166
+ func checkResources (discoveryClient discovery.DiscoveryInterface , items []runtime.Object ) error {
167
+ resources , err := discoveryClient .ServerResources ()
168
+ if err != nil {
169
+ return fmt .Errorf ("Unable to to get list of available resources: %v" , err )
170
+ }
171
+
172
+ resourceMap := make (map [schema.GroupVersionKind ]bool )
173
+ for _ , resourceList := range resources {
174
+ for _ , resource := range resourceList .APIResources {
175
+ listGv , _ := schema .ParseGroupVersion (resourceList .GroupVersion )
176
+ gvk := schema.GroupVersionKind {
177
+ Group : nonEmpty (resource .Group , listGv .Group ),
178
+ Version : nonEmpty (resource .Version , listGv .Version ),
179
+ Kind : resource .Kind ,
180
+ }
181
+ resourceMap [gvk ] = true
182
+ }
183
+ }
184
+
185
+ declaredResources := make (map [schema.GroupVersionKind ]bool )
186
+ for _ , item := range items {
187
+ versioned := kcmdutil .AsDefaultVersionedOrOriginal (item , nil )
188
+ declaredResources [versioned .GetObjectKind ().GroupVersionKind ()] = true
189
+ }
190
+ missingResources := make ([]string , 0 )
191
+ for r := range declaredResources {
192
+ if present := resourceMap [r ]; ! present {
193
+ missingResources = append (missingResources , r .String ())
194
+ }
195
+ }
196
+
197
+ if len (missingResources ) > 0 {
198
+ return fmt .Errorf ("Missing declared resources: %v" , strings .Join (missingResources , `, ` ))
199
+ }
200
+ return nil
201
+ }
202
+
157
203
//Complete sets all common default options for commands (new-app and new-build)
158
204
func (o * ObjectGeneratorOptions ) Complete (baseName , commandName string , f kcmdutil.Factory , c * cobra.Command , args []string , in io.Reader , out , errout io.Writer ) error {
159
205
cmdutil .WarnAboutCommaSeparation (errout , o .Config .Environment , "--env" )
@@ -184,6 +230,12 @@ func (o *ObjectGeneratorOptions) Complete(baseName, commandName string, f kcmdut
184
230
return err
185
231
}
186
232
233
+ discoveryClient , err := discovery .NewDiscoveryClientForConfig (clientConfig )
234
+ if err != nil {
235
+ return fmt .Errorf ("Unable to validate if required resources are available: %v" , err )
236
+ }
237
+ o .Config .DiscoveryClient = discoveryClient
238
+
187
239
o .Action .Out , o .Action .ErrOut = out , o .ErrOut
188
240
o .Action .Bulk .Scheme = legacyscheme .Scheme
189
241
o .Action .Bulk .Op = bulk.Creator {Client : dynamicClient , RESTMapper : mapper }.Create
@@ -307,6 +359,9 @@ func (o *NewAppOptions) RunNewApp() error {
307
359
if err := handleError (err , o .BaseName , o .CommandName , o .CommandPath , config , transformRunError ); err != nil {
308
360
return err
309
361
}
362
+ if err := checkResources (config .DiscoveryClient , result .List .Items ); err != nil {
363
+ return err
364
+ }
310
365
311
366
// set labels explicitly supplied by the user on the command line
312
367
if err := setLabels (config .Labels , result ); err != nil {
0 commit comments