Add commonMetadata to flux build/diff

Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
pull/3758/head
Stefan Prodan 2 years ago
parent 9293781702
commit cecffc0588
No known key found for this signature in database
GPG Key ID: 3299AEB0E4085BAF

@ -21,10 +21,12 @@ import (
"os" "os"
"os/signal" "os/signal"
"github.com/fluxcd/pkg/ssa"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/fluxcd/flux2/v2/internal/build"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
"github.com/fluxcd/flux2/v2/internal/build"
) )
var buildKsCmd = &cobra.Command{ var buildKsCmd = &cobra.Command{
@ -114,12 +116,17 @@ func buildKsCmdRun(cmd *cobra.Command, args []string) (err error) {
errChan := make(chan error) errChan := make(chan error)
go func() { go func() {
manifests, err := builder.Build() objects, err := builder.Build()
if err != nil {
errChan <- err
}
manifests, err := ssa.ObjectsToYAML(objects)
if err != nil { if err != nil {
errChan <- err errChan <- err
} }
cmd.Print(string(manifests)) cmd.Print(manifests)
errChan <- nil errChan <- nil
}() }()

@ -171,3 +171,4 @@ metadata:
name: db-user-pass-bkbd782d2c name: db-user-pass-bkbd782d2c
namespace: default namespace: default
type: Opaque type: Opaque
---

@ -214,3 +214,4 @@ metadata:
kustomize.toolkit.fluxcd.io/substitute: disabled kustomize.toolkit.fluxcd.io/substitute: disabled
name: flux-grafana-dashboards-kt8md725kf name: flux-grafana-dashboards-kt8md725kf
namespace: default namespace: default
---

@ -99,3 +99,4 @@ spec:
apiVersion: apps/v1 apiVersion: apps/v1
kind: Deployment kind: Deployment
name: podinfo name: podinfo
---

@ -28,6 +28,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/fluxcd/pkg/ssa"
"github.com/theckman/yacspin" "github.com/theckman/yacspin"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "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 // and overlays the manifests with the resources specified in the resourcesPath
// It expects a kustomization.yaml file in the resourcesPath, and it will // It expects a kustomization.yaml file in the resourcesPath, and it will
// generate a kustomization.yaml file if it doesn't exist // 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() m, err := b.build()
if err != nil { if err != nil {
return nil, err return nil, err
@ -223,7 +224,16 @@ func (b *Builder) Build() ([]byte, error) {
return nil, fmt.Errorf("kustomize build failed: %w", err) 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) { func (b *Builder) build() (m resmap.ResMap, err error) {

@ -39,10 +39,10 @@ import (
"sigs.k8s.io/cli-utils/pkg/object" "sigs.k8s.io/cli-utils/pkg/object"
"sigs.k8s.io/yaml" "sigs.k8s.io/yaml"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
"github.com/fluxcd/pkg/ssa" "github.com/fluxcd/pkg/ssa"
"github.com/fluxcd/flux2/v2/pkg/printers" "github.com/fluxcd/flux2/v2/pkg/printers"
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
) )
func (b *Builder) Manager() (*ssa.ResourceManager, error) { 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) { func (b *Builder) Diff() (string, bool, error) {
output := strings.Builder{} output := strings.Builder{}
createdOrDrifted := false createdOrDrifted := false
res, err := b.Build() objects, err := b.Build()
if err != nil { if err != nil {
return "", createdOrDrifted, err 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 { if err != nil {
return "", createdOrDrifted, err return "", createdOrDrifted, err
} }
@ -76,10 +76,6 @@ func (b *Builder) Diff() (string, bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), b.timeout) ctx, cancel := context.WithTimeout(context.Background(), b.timeout)
defer cancel() defer cancel()
if err := ssa.SetNativeKindsDefaults(objects); err != nil {
return "", createdOrDrifted, err
}
if b.spinner != nil { if b.spinner != nil {
err = b.spinner.Start() err = b.spinner.Start()
if err != nil { if err != nil {

Loading…
Cancel
Save