|
|
@ -22,6 +22,7 @@ import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
@ -30,6 +31,7 @@ import (
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
"k8s.io/apimachinery/pkg/types"
|
|
|
|
|
|
|
|
k8syaml "k8s.io/apimachinery/pkg/util/yaml"
|
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/kustomize/api/resmap"
|
|
|
|
"sigs.k8s.io/kustomize/api/resmap"
|
|
|
@ -60,11 +62,12 @@ var defaultTimeout = 80 * time.Second
|
|
|
|
// It retrieves the kustomization object from the k8s cluster
|
|
|
|
// It retrieves the kustomization object from the k8s cluster
|
|
|
|
// and overlays the manifests with the resources specified in the resourcesPath
|
|
|
|
// and overlays the manifests with the resources specified in the resourcesPath
|
|
|
|
type Builder struct {
|
|
|
|
type Builder struct {
|
|
|
|
client client.WithWatch
|
|
|
|
client client.WithWatch
|
|
|
|
restMapper meta.RESTMapper
|
|
|
|
restMapper meta.RESTMapper
|
|
|
|
name string
|
|
|
|
name string
|
|
|
|
namespace string
|
|
|
|
namespace string
|
|
|
|
resourcesPath string
|
|
|
|
resourcesPath string
|
|
|
|
|
|
|
|
kustomizationFile string
|
|
|
|
// mu is used to synchronize access to the kustomization file
|
|
|
|
// mu is used to synchronize access to the kustomization file
|
|
|
|
mu sync.Mutex
|
|
|
|
mu sync.Mutex
|
|
|
|
action kustomize.Action
|
|
|
|
action kustomize.Action
|
|
|
@ -75,6 +78,13 @@ type Builder struct {
|
|
|
|
|
|
|
|
|
|
|
|
type BuilderOptionFunc func(b *Builder) error
|
|
|
|
type BuilderOptionFunc func(b *Builder) error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func WithKustomizationFile(file string) BuilderOptionFunc {
|
|
|
|
|
|
|
|
return func(b *Builder) error {
|
|
|
|
|
|
|
|
b.kustomizationFile = file
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func WithTimeout(timeout time.Duration) BuilderOptionFunc {
|
|
|
|
func WithTimeout(timeout time.Duration) BuilderOptionFunc {
|
|
|
|
return func(b *Builder) error {
|
|
|
|
return func(b *Builder) error {
|
|
|
|
b.timeout = timeout
|
|
|
|
b.timeout = timeout
|
|
|
@ -176,9 +186,18 @@ func (b *Builder) build() (m resmap.ResMap, err error) {
|
|
|
|
defer cancel()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
|
|
// Get the kustomization object
|
|
|
|
// Get the kustomization object
|
|
|
|
k, err := b.getKustomization(ctx)
|
|
|
|
k := &kustomizev1.Kustomization{}
|
|
|
|
if err != nil {
|
|
|
|
if b.kustomizationFile != "" {
|
|
|
|
return
|
|
|
|
k, err = b.unMarshallKustomization()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
k, err = b.getKustomization(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
err = fmt.Errorf("failed to get kustomization object: %w", err)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// store the kustomization object
|
|
|
|
// store the kustomization object
|
|
|
@ -225,6 +244,21 @@ func (b *Builder) build() (m resmap.ResMap, err error) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (b *Builder) unMarshallKustomization() (*kustomizev1.Kustomization, error) {
|
|
|
|
|
|
|
|
data, err := os.ReadFile(b.kustomizationFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to read kustomization file %s: %w", b.kustomizationFile, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
k := &kustomizev1.Kustomization{}
|
|
|
|
|
|
|
|
decoder := k8syaml.NewYAMLOrJSONDecoder(bytes.NewBuffer(data), len(data))
|
|
|
|
|
|
|
|
err = decoder.Decode(k)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to unmarshall kustomization file %s: %w", b.kustomizationFile, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return k, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (b *Builder) generate(kustomization kustomizev1.Kustomization, dirPath string) (kustomize.Action, error) {
|
|
|
|
func (b *Builder) generate(kustomization kustomizev1.Kustomization, dirPath string) (kustomize.Action, error) {
|
|
|
|
data, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&kustomization)
|
|
|
|
data, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&kustomization)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|