@@ -18,7 +18,9 @@ import (
1818 "text/template"
1919
2020 "github.com/Masterminds/sprig/v3"
21+ "github.com/sap/go-generics/slices"
2122
23+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2224 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2325 utilyaml "k8s.io/apimachinery/pkg/util/yaml"
2426 "k8s.io/apimachinery/pkg/version"
@@ -140,7 +142,7 @@ func NewKustomizeGenerator(fsys fs.FS, kustomizationPath string, _ client.Client
140142 Funcs (templatex .FuncMapForTemplate (nil )).
141143 Funcs (templatex .FuncMapForLocalClient (nil )).
142144 Funcs (templatex .FuncMapForClient (nil )).
143- Funcs (funcMapForGenerateContext (nil , nil , "" , "" ))
145+ Funcs (funcMapForGenerateContext (nil , nil , nil , "" , "" ))
144146 } else {
145147 t = t .New (name )
146148 }
@@ -201,10 +203,15 @@ func (g *KustomizeGenerator) Generate(ctx context.Context, namespace string, nam
201203 if err != nil {
202204 return nil , err
203205 }
204- serverInfo , err := clnt .DiscoveryClient ().ServerVersion ()
206+ serverVersion , err := clnt .DiscoveryClient ().ServerVersion ()
205207 if err != nil {
206208 return nil , err
207209 }
210+ _ , serverGroupsWithResources , err := clnt .DiscoveryClient ().ServerGroupsAndResources ()
211+ if err != nil {
212+ return nil , err
213+ }
214+ serverGroupsWithResources = normalizeServerGroupsWithResources (serverGroupsWithResources )
208215
209216 data := parameters .ToUnstructured ()
210217 fsys := kustfsys .MakeFsInMemory ()
@@ -226,11 +233,11 @@ func (g *KustomizeGenerator) Generate(ctx context.Context, namespace string, nam
226233 Funcs (templatex .FuncMapForTemplate (t0 )).
227234 Funcs (templatex .FuncMapForLocalClient (localClient )).
228235 Funcs (templatex .FuncMapForClient (clnt )).
229- Funcs (funcMapForGenerateContext (serverInfo , component , namespace , name ))
236+ Funcs (funcMapForGenerateContext (serverVersion , serverGroupsWithResources , component , namespace , name ))
230237 }
231238 var buf bytes.Buffer
232239 // TODO: templates (accidentally or intentionally) could modify data, or even some of the objects supplied through builtin functions;
233- // such as serverInfo or component; this should be hardened, e.g. by deep-copying things upfront, or serializing them; see the comment in
240+ // such as serverVersion or component; this should be hardened, e.g. by deep-copying things upfront, or serializing them; see the comment in
234241 // funcMapForGenerateContext()
235242 if err := t0 .ExecuteTemplate (& buf , t .Name (), data ); err != nil {
236243 return nil , err
@@ -285,14 +292,15 @@ func (g *KustomizeGenerator) Generate(ctx context.Context, namespace string, nam
285292 return objects , nil
286293}
287294
288- func funcMapForGenerateContext (serverInfo * version.Info , component component.Component , namespace string , name string ) template.FuncMap {
295+ func funcMapForGenerateContext (serverInfo * version.Info , serverGroupsWithResources [] * metav1. APIResourceList , component component.Component , namespace string , name string ) template.FuncMap {
289296 return template.FuncMap {
290297 // TODO: maybe it would it be better to convert component to unstructured;
291298 // then calling methods would no longer be possible, and attributes would be in lowercase
292299 "component" : makeFuncData (component ),
293300 "namespace" : func () string { return namespace },
294301 "name" : func () string { return name },
295302 "kubernetesVersion" : func () * version.Info { return serverInfo },
303+ "apiResources" : func () []* metav1.APIResourceList { return serverGroupsWithResources },
296304 }
297305}
298306
@@ -355,3 +363,21 @@ func readOptions(fsys fs.FS, path string, options *KustomizeGeneratorOptions) er
355363
356364 return nil
357365}
366+
367+ func normalizeServerGroupsWithResources (serverGroupsWithResources []* metav1.APIResourceList ) []* metav1.APIResourceList {
368+ serverGroupsWithResources = slices .SortBy (serverGroupsWithResources , func (x , y * metav1.APIResourceList ) bool { return x .GroupVersion > y .GroupVersion })
369+ for _ , serverGroupWithResources := range serverGroupsWithResources {
370+ serverGroupWithResources .APIResources = normalizeApiResources (serverGroupWithResources .APIResources )
371+ }
372+ return serverGroupsWithResources
373+ }
374+
375+ func normalizeApiResources (apiResources []metav1.APIResource ) []metav1.APIResource {
376+ apiResources = slices .SortBy (apiResources , func (x , y metav1.APIResource ) bool { return x .Name > y .Name })
377+ for i := 0 ; i < len (apiResources ); i ++ {
378+ apiResources [i ].Verbs = slices .Sort (apiResources [i ].Verbs )
379+ apiResources [i ].ShortNames = slices .Sort (apiResources [i ].ShortNames )
380+ apiResources [i ].Categories = slices .Sort (apiResources [i ].Categories )
381+ }
382+ return apiResources
383+ }
0 commit comments