Skip to content

Commit 074a218

Browse files
committed
kustomize no-op with go build tags
1 parent 18dbaf5 commit 074a218

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build without_kustomize
2+
// +build without_kustomize
3+
4+
package kustomize
5+
6+
import (
7+
"context"
8+
9+
"sigs.k8s.io/controller-runtime/pkg/log"
10+
"sigs.k8s.io/kustomize/kyaml/filesys"
11+
)
12+
13+
func init() {
14+
Kustomize = &SkipKustomize{}
15+
}
16+
17+
var _ Kustomizer = &SkipKustomize{}
18+
19+
// NoopKustomize ignore the `kustomization.yaml` file and won't run `kustomize build`.
20+
type SkipKustomize struct{}
21+
22+
// Run is a no-op
23+
func (k *SkipKustomize) Run(ctx context.Context, _ filesys.FileSystem, _ string) ([]byte, error) {
24+
log := log.FromContext(ctx)
25+
log.WithValues("kustomizer", "SkipKustomize").Info("skip running kustomize")
26+
return nil, nil
27+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package kustomize
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"sigs.k8s.io/controller-runtime/pkg/log"
8+
"sigs.k8s.io/kustomize/api/krusty"
9+
"sigs.k8s.io/kustomize/kyaml/filesys"
10+
)
11+
12+
func init() {
13+
Kustomize = &EnabledKustomize{}
14+
}
15+
16+
var _ Kustomizer = &EnabledKustomize{}
17+
18+
// EnabledKustomize runs kustomize build. It requires kustomize/api v0.12.1 and above
19+
type EnabledKustomize struct{}
20+
21+
// Run calls the kustomize/api library to run `kustomize build`.
22+
func (k *EnabledKustomize) Run(ctx context.Context, fs filesys.FileSystem, manifestPath string) ([]byte, error) {
23+
log := log.FromContext(ctx)
24+
log.WithValues("kustomizer", "EnabledKustomize").Info("running kustomize")
25+
// run kustomize to create final manifest
26+
opts := krusty.MakeDefaultOptions()
27+
kustomizer := krusty.MakeKustomizer(opts)
28+
m, err := kustomizer.Run(fs, manifestPath)
29+
if err != nil {
30+
return nil, fmt.Errorf("error running kustomize: %v", err)
31+
}
32+
33+
manifestYaml, err := m.AsYaml()
34+
if err != nil {
35+
return nil, fmt.Errorf("error converting kustomize output to yaml: %v", err)
36+
}
37+
return manifestYaml, nil
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package kustomize
2+
3+
import (
4+
"context"
5+
6+
"sigs.k8s.io/kustomize/kyaml/filesys"
7+
)
8+
9+
// Kustomize will be initialized differently according to the go build constraints.
10+
var Kustomize Kustomizer
11+
12+
// Kustomizer interface defines a `Run` method to differentiate the `kustomize` behavior under
13+
// different go build constraints
14+
type Kustomizer interface {
15+
Run(context.Context, filesys.FileSystem, string) ([]byte, error)
16+
}

pkg/patterns/declarative/reconciler.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"k8s.io/apimachinery/pkg/api/meta"
27+
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/kustomize"
2728

2829
apierrors "k8s.io/apimachinery/pkg/api/errors"
2930

@@ -39,7 +40,6 @@ import (
3940
"sigs.k8s.io/controller-runtime/pkg/log"
4041
"sigs.k8s.io/controller-runtime/pkg/manager"
4142
"sigs.k8s.io/controller-runtime/pkg/reconcile"
42-
"sigs.k8s.io/kustomize/api/krusty"
4343
"sigs.k8s.io/kustomize/kyaml/filesys"
4444

4545
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative/pkg/applier"
@@ -390,20 +390,12 @@ func (r *Reconciler) BuildDeploymentObjectsWithFs(ctx context.Context, name type
390390
// Here, the manifest is built using Kustomize and then replaces the Object items with the created manifest
391391
if r.IsKustomizeOptionUsed() {
392392
// run kustomize to create final manifest
393-
opts := krusty.MakeDefaultOptions()
394-
k := krusty.MakeKustomizer(opts)
395-
m, err := k.Run(fs, manifestObjects.Path)
393+
manifestYaml, err := kustomize.Kustomize.Run(ctx, fs, manifestObjects.Path)
396394
if err != nil {
397-
log.Error(err, "running kustomize to create final manifest")
395+
log.Error(err, "run kustomize build")
398396
return nil, fmt.Errorf("error running kustomize: %v", err)
399397
}
400398

401-
manifestYaml, err := m.AsYaml()
402-
if err != nil {
403-
log.Error(err, "creating final manifest yaml")
404-
return nil, fmt.Errorf("error converting kustomize output to yaml: %v", err)
405-
}
406-
407399
objects, err := r.parseManifest(ctx, instance, string(manifestYaml))
408400
if err != nil {
409401
log.Error(err, "creating final manifest yaml")

0 commit comments

Comments
 (0)