Skip to content

Commit 475e7bd

Browse files
committed
kustomize generator: add a builtin function kubernetesVersion
this builtin returns the deployment target cluster's server version as a k8s version.Info struct
1 parent 6447771 commit 475e7bd

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

pkg/component/reconcile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
// TODO: allow to override namespace auto-creation on a per-object level
4444
// TODO: allow some timeout feature, such that component will go into error state if not ready within the given timeout
4545
// (e.g. through a TimeoutConfiguration interface that components could optionally implement)
46+
// TODO: from time to time, enforce dependent updates even if digest is matching
4647
// TODO: run admission webhooks (if present) in reconcile (e.g. as post-read hook)
4748
// TODO: improve overall log output
4849

pkg/component/reference.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525

2626
// TODO: should below reference actions be thread safe?
2727
// If not, it should be stated somewhere explicitly that they are not.
28+
// TODO: allow to skip loading of references upon deletion in general (e.g. by a tagLoadPolicy)
29+
// (currently it is only possible to ignore not-found errors upon deletion)
2830

2931
const (
3032
tagNotFoundPolicy = "notFoundPolicy"

pkg/manifests/kustomize/generator.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ import (
2020

2121
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2222
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
23+
"k8s.io/apimachinery/pkg/version"
2324
"sigs.k8s.io/controller-runtime/pkg/client"
2425
"sigs.k8s.io/kustomize/api/konfig"
2526
"sigs.k8s.io/kustomize/api/krusty"
2627
kustypes "sigs.k8s.io/kustomize/api/types"
2728
kustfsys "sigs.k8s.io/kustomize/kyaml/filesys"
2829
kyaml "sigs.k8s.io/yaml"
2930

30-
"github.com/sap/component-operator-runtime/internal/cluster"
3131
"github.com/sap/component-operator-runtime/internal/fileutils"
3232
"github.com/sap/component-operator-runtime/internal/templatex"
3333
"github.com/sap/component-operator-runtime/pkg/component"
@@ -165,6 +165,11 @@ func (g *KustomizeGenerator) Generate(ctx context.Context, namespace string, nam
165165
return nil, err
166166
}
167167

168+
serverInfo, err := clnt.DiscoveryClient().ServerVersion()
169+
if err != nil {
170+
return nil, err
171+
}
172+
168173
data := parameters.ToUnstructured()
169174
fsys := kustfsys.MakeFsInMemory()
170175

@@ -183,7 +188,7 @@ func (g *KustomizeGenerator) Generate(ctx context.Context, namespace string, nam
183188
}
184189
t0.Option("missingkey=zero").
185190
Funcs(templatex.FuncMapForClient(clnt)).
186-
Funcs(funcMapForGenerateContext(clnt, component, namespace, name))
191+
Funcs(funcMapForGenerateContext(serverInfo, component, namespace, name))
187192
}
188193
var buf bytes.Buffer
189194
if err := t0.ExecuteTemplate(&buf, t.Name(), data); err != nil {
@@ -239,14 +244,14 @@ func (g *KustomizeGenerator) Generate(ctx context.Context, namespace string, nam
239244
return objects, nil
240245
}
241246

242-
func funcMapForGenerateContext(clnt cluster.Client, component component.Component, namespace string, name string) template.FuncMap {
243-
// TODO: add accessors for Kubernetes version etc.
247+
func funcMapForGenerateContext(serverInfo *version.Info, component component.Component, namespace string, name string) template.FuncMap {
244248
return template.FuncMap{
245249
// TODO: maybe it would it be better to convert component to unstructured;
246250
// then calling methods would no longer be possible, and attributes would be in lowercase
247-
"component": makeFuncData(component),
248-
"namespace": func() string { return namespace },
249-
"name": func() string { return name },
251+
"component": makeFuncData(component),
252+
"namespace": func() string { return namespace },
253+
"name": func() string { return name },
254+
"kubernetesVersion": func() *version.Info { return serverInfo },
250255
}
251256
}
252257

website/content/en/docs/generators/kustomize.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ This generator allows to generate the manifests of the component's resources fro
1111
As a special case, one or more simple Kubernetes manifests (without a `kustomization.yaml`) are supported as well.
1212
In addition, all (or selected; see below) files in the kustomization directory can be templatized in a helm'ish way.
1313
That means, they will be considered as a common template group (where all templates are associated with each other),
14-
and the same template function set that is available on helm can be used; so, all the [sprig](http://masterminds.github.io/sprig) functions, and custom functions such as `include`, `tpl`, `lookup` can be used. In addition, parameterless functions `namespace` and `name` are defined, which return the corresponding arguments passed to `Generate()`.
14+
and the same template function set that is available on helm can be used; so, all the [sprig](http://masterminds.github.io/sprig) functions, and custom functions such as `include`, `tpl`, `lookup` can be used. In addition:
15+
- parameterless functions `namespace` and `name` are defined, which return the corresponding arguments passed to `Generate()`
16+
- a function `kubernetesVersion` is available, which returns the version information of the target cluster, as a [version.Info](https://pkg.go.dev/k8s.io/apimachinery/pkg/version#Info) structure.
1517

1618
In the generation step, first, all the go templates will be rendered, and the result of this pre-step will be passed to kustomize.
1719

0 commit comments

Comments
 (0)