Skip to content

Commit c58a777

Browse files
authored
Merge pull request #299 from yuwenma/without-kustomize
Opt-out the kustomize feature via go build tags
2 parents 18dbaf5 + 195304f commit c58a777

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build without_kustomize
2+
// +build without_kustomize
3+
4+
package kustomize
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"sigs.k8s.io/kustomize/kyaml/filesys"
11+
)
12+
13+
// Run ignore the `kustomization.yaml` file and won't run `kustomize build`.
14+
func Run(_ context.Context, _ filesys.FileSystem, _ string) ([]byte, error) {
15+
return nil, fmt.Errorf("kustomize support is not compiled in (built with tag `without_kustomize`)")
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build !without_kustomize
2+
// +build !without_kustomize
3+
4+
package kustomize
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"sigs.k8s.io/controller-runtime/pkg/log"
11+
"sigs.k8s.io/kustomize/api/krusty"
12+
"sigs.k8s.io/kustomize/kyaml/filesys"
13+
)
14+
15+
// Run calls the kustomize/api library to run `kustomize build`. This method is differentiated by go build
16+
// tag `without-kustomize`
17+
func Run(ctx context.Context, fs filesys.FileSystem, manifestPath string) ([]byte, error) {
18+
log := log.FromContext(ctx)
19+
log.WithValues("manifestPath", manifestPath).Info("running kustomize")
20+
// run kustomize to create final manifest
21+
opts := krusty.MakeDefaultOptions()
22+
kustomizer := krusty.MakeKustomizer(opts)
23+
m, err := kustomizer.Run(fs, manifestPath)
24+
if err != nil {
25+
return nil, fmt.Errorf("error running kustomize: %v", err)
26+
}
27+
28+
manifestYaml, err := m.AsYaml()
29+
if err != nil {
30+
return nil, fmt.Errorf("error converting kustomize output to yaml: %v", err)
31+
}
32+
return manifestYaml, nil
33+
}

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.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)