diff --git a/cmd/flux/build_kustomization.go b/cmd/flux/build_kustomization.go index 507fb94c..ee29edc7 100644 --- a/cmd/flux/build_kustomization.go +++ b/cmd/flux/build_kustomization.go @@ -21,10 +21,12 @@ import ( "os" "os/signal" + "github.com/fluxcd/pkg/ssa" "github.com/spf13/cobra" - "github.com/fluxcd/flux2/v2/internal/build" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" + + "github.com/fluxcd/flux2/v2/internal/build" ) var buildKsCmd = &cobra.Command{ @@ -114,12 +116,17 @@ func buildKsCmdRun(cmd *cobra.Command, args []string) (err error) { errChan := make(chan error) go func() { - manifests, err := builder.Build() + objects, err := builder.Build() + if err != nil { + errChan <- err + } + + manifests, err := ssa.ObjectsToYAML(objects) if err != nil { errChan <- err } - cmd.Print(string(manifests)) + cmd.Print(manifests) errChan <- nil }() diff --git a/cmd/flux/testdata/build-kustomization/podinfo-result.yaml b/cmd/flux/testdata/build-kustomization/podinfo-result.yaml index 009347d8..bf162251 100644 --- a/cmd/flux/testdata/build-kustomization/podinfo-result.yaml +++ b/cmd/flux/testdata/build-kustomization/podinfo-result.yaml @@ -171,3 +171,4 @@ metadata: name: db-user-pass-bkbd782d2c namespace: default type: Opaque +--- diff --git a/cmd/flux/testdata/build-kustomization/podinfo-with-var-substitution-result.yaml b/cmd/flux/testdata/build-kustomization/podinfo-with-var-substitution-result.yaml index 0c32c936..01686566 100644 --- a/cmd/flux/testdata/build-kustomization/podinfo-with-var-substitution-result.yaml +++ b/cmd/flux/testdata/build-kustomization/podinfo-with-var-substitution-result.yaml @@ -214,3 +214,4 @@ metadata: kustomize.toolkit.fluxcd.io/substitute: disabled name: flux-grafana-dashboards-kt8md725kf namespace: default +--- diff --git a/cmd/flux/testdata/build-kustomization/podinfo-without-service-result.yaml b/cmd/flux/testdata/build-kustomization/podinfo-without-service-result.yaml index e1e1885f..647ce020 100644 --- a/cmd/flux/testdata/build-kustomization/podinfo-without-service-result.yaml +++ b/cmd/flux/testdata/build-kustomization/podinfo-without-service-result.yaml @@ -99,3 +99,4 @@ spec: apiVersion: apps/v1 kind: Deployment name: podinfo +--- diff --git a/internal/build/build.go b/internal/build/build.go index 083a7ca4..0f2f1ca4 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -28,6 +28,7 @@ import ( "sync" "time" + "github.com/fluxcd/pkg/ssa" "github.com/theckman/yacspin" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -212,7 +213,7 @@ func (b *Builder) getKustomization(ctx context.Context) (*kustomizev1.Kustomizat // and overlays the manifests with the resources specified in the resourcesPath // It expects a kustomization.yaml file in the resourcesPath, and it will // generate a kustomization.yaml file if it doesn't exist -func (b *Builder) Build() ([]byte, error) { +func (b *Builder) Build() ([]*unstructured.Unstructured, error) { m, err := b.build() if err != nil { return nil, err @@ -223,7 +224,16 @@ func (b *Builder) Build() ([]byte, error) { return nil, fmt.Errorf("kustomize build failed: %w", err) } - return resources, nil + objects, err := ssa.ReadObjects(bytes.NewReader(resources)) + if err != nil { + return nil, fmt.Errorf("kustomize build failed: %w", err) + } + + if m := b.kustomization.Spec.CommonMetadata; m != nil { + ssa.SetCommonMetadata(objects, m.Labels, m.Annotations) + } + + return objects, nil } func (b *Builder) build() (m resmap.ResMap, err error) { diff --git a/internal/build/diff.go b/internal/build/diff.go index b510b129..e693e1d9 100644 --- a/internal/build/diff.go +++ b/internal/build/diff.go @@ -39,10 +39,10 @@ import ( "sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/yaml" + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" "github.com/fluxcd/pkg/ssa" "github.com/fluxcd/flux2/v2/pkg/printers" - kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" ) func (b *Builder) Manager() (*ssa.ResourceManager, error) { @@ -58,12 +58,12 @@ func (b *Builder) Manager() (*ssa.ResourceManager, error) { func (b *Builder) Diff() (string, bool, error) { output := strings.Builder{} createdOrDrifted := false - res, err := b.Build() + objects, err := b.Build() if err != nil { return "", createdOrDrifted, err } - // convert the build result into Kubernetes unstructured objects - objects, err := ssa.ReadObjects(bytes.NewReader(res)) + + err = ssa.SetNativeKindsDefaults(objects) if err != nil { return "", createdOrDrifted, err } @@ -76,10 +76,6 @@ func (b *Builder) Diff() (string, bool, error) { ctx, cancel := context.WithTimeout(context.Background(), b.timeout) defer cancel() - if err := ssa.SetNativeKindsDefaults(objects); err != nil { - return "", createdOrDrifted, err - } - if b.spinner != nil { err = b.spinner.Start() if err != nil {