|
|
@ -31,16 +31,15 @@ import (
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"sigs.k8s.io/kustomize/api/konfig"
|
|
|
|
"sigs.k8s.io/kustomize/api/konfig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/fluxcd/flux2/pkg/manifestgen/kustomization"
|
|
|
|
runclient "github.com/fluxcd/pkg/runtime/client"
|
|
|
|
runclient "github.com/fluxcd/pkg/runtime/client"
|
|
|
|
"github.com/fluxcd/pkg/ssa"
|
|
|
|
"github.com/fluxcd/pkg/ssa"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/fluxcd/flux2/pkg/manifestgen/kustomization"
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Apply is the equivalent of 'kubectl apply --server-side -f'.
|
|
|
|
// Apply is the equivalent of 'kubectl apply --server-side -f'.
|
|
|
|
// If the given manifest is a kustomization.yaml, then apply performs the equivalent of 'kubectl apply --server-side -k'.
|
|
|
|
// If the given manifest is a kustomization.yaml, then apply performs the equivalent of 'kubectl apply --server-side -k'.
|
|
|
|
func Apply(ctx context.Context, rcg genericclioptions.RESTClientGetter, opts *runclient.Options, manifestPath string) (string, error) {
|
|
|
|
func Apply(ctx context.Context, rcg genericclioptions.RESTClientGetter, opts *runclient.Options, root, manifestPath string) (string, error) {
|
|
|
|
objs, err := readObjects(manifestPath)
|
|
|
|
objs, err := readObjects(root, manifestPath)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -92,13 +91,17 @@ func Apply(ctx context.Context, rcg genericclioptions.RESTClientGetter, opts *ru
|
|
|
|
return changeSet.String(), nil
|
|
|
|
return changeSet.String(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func readObjects(manifestPath string) ([]*unstructured.Unstructured, error) {
|
|
|
|
func readObjects(root, manifestPath string) ([]*unstructured.Unstructured, error) {
|
|
|
|
if _, err := os.Stat(manifestPath); err != nil {
|
|
|
|
fi, err := os.Lstat(manifestPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() || !fi.Mode().IsRegular() {
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("expected %q to be a file", manifestPath)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if filepath.Base(manifestPath) == konfig.DefaultKustomizationFileName() {
|
|
|
|
if isRecognizedKustomizationFile(manifestPath) {
|
|
|
|
resources, err := kustomization.Build(filepath.Dir(manifestPath))
|
|
|
|
resources, err := kustomization.BuildWithRoot(root, filepath.Dir(manifestPath))
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -152,3 +155,13 @@ func waitForSet(rcg genericclioptions.RESTClientGetter, opts *runclient.Options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return man.WaitForSet(changeSet.ToObjMetadataSet(), ssa.WaitOptions{Interval: 2 * time.Second, Timeout: time.Minute})
|
|
|
|
return man.WaitForSet(changeSet.ToObjMetadataSet(), ssa.WaitOptions{Interval: 2 * time.Second, Timeout: time.Minute})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func isRecognizedKustomizationFile(path string) bool {
|
|
|
|
|
|
|
|
base := filepath.Base(path)
|
|
|
|
|
|
|
|
for _, v := range konfig.RecognizedKustomizationFileNames() {
|
|
|
|
|
|
|
|
if base == v {
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|