mirror of
https://github.com/cloudnative-pg/plugin-barman-cloud.git
synced 2026-01-12 05:33:11 +01:00
test: review
Signed-off-by: Francesco Canovai <francesco.canovai@enterprisedb.com>
This commit is contained in:
parent
e63ffb34b2
commit
5d53822098
@ -94,11 +94,7 @@ var _ = SynchronizedBeforeSuite(func(ctx SpecContext) []byte {
|
|||||||
Fail(fmt.Sprintf("failed to add cert-manager.io/v1 to scheme: %v", err))
|
Fail(fmt.Sprintf("failed to add cert-manager.io/v1 to scheme: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := kustomize.ApplyKustomization(ctx,
|
if err := kustomize.ApplyKustomization(ctx, cl, barmanCloudKustomization); err != nil {
|
||||||
cl,
|
|
||||||
scheme,
|
|
||||||
barmanCloudKustomization,
|
|
||||||
kustomize.WithIgnoreExistingResources(false)); err != nil {
|
|
||||||
Fail(fmt.Sprintf("failed to apply kustomization: %v", err))
|
Fail(fmt.Sprintf("failed to apply kustomization: %v", err))
|
||||||
}
|
}
|
||||||
const defaultTimeout = 1 * time.Minute
|
const defaultTimeout = 1 * time.Minute
|
||||||
|
|||||||
@ -21,12 +21,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
|
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
|
||||||
rbacv1 "k8s.io/api/rbac/v1"
|
|
||||||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
|
||||||
types2 "k8s.io/apimachinery/pkg/types"
|
types2 "k8s.io/apimachinery/pkg/types"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
@ -84,13 +78,7 @@ func Install(ctx context.Context, cl client.Client, opts ...InstallOption) error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add all the resources defined in the cert-manager manifests
|
// Add all the resources defined in the cert-manager manifests
|
||||||
scheme, err := setupScheme()
|
if err := kustomize.ApplyKustomization(ctx, cl, kustomization); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := kustomize.ApplyKustomization(ctx, cl, scheme, kustomization,
|
|
||||||
kustomize.WithIgnoreExistingResources(options.IgnoreExistResources)); err != nil {
|
|
||||||
return fmt.Errorf("failed to apply kustomization: %w", err)
|
return fmt.Errorf("failed to apply kustomization: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -116,24 +104,3 @@ func Install(ctx context.Context, cl client.Client, opts ...InstallOption) error
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupScheme() (*runtime.Scheme, error) {
|
|
||||||
scheme := runtime.NewScheme()
|
|
||||||
if err := corev1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add core/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
if err := apiextensionsv1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add apiextensions/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
if err := admissionregistrationv1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add admissionregistration/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
if err := rbacv1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add rbac/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
if err := appsv1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add apps/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return scheme, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@ -21,12 +21,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
|
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
|
||||||
rbacv1 "k8s.io/api/rbac/v1"
|
|
||||||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
|
||||||
types2 "k8s.io/apimachinery/pkg/types"
|
types2 "k8s.io/apimachinery/pkg/types"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
@ -135,13 +129,7 @@ func Install(ctx context.Context, cl client.Client, opts ...InstallOption) error
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
scheme, err := setupScheme()
|
if err := kustomize.ApplyKustomization(ctx, cl, kustomization); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := kustomize.ApplyKustomization(ctx, cl, scheme, kustomization,
|
|
||||||
kustomize.WithIgnoreExistingResources(options.IgnoreExistResources)); err != nil {
|
|
||||||
return fmt.Errorf("failed to apply kustomization: %w", err)
|
return fmt.Errorf("failed to apply kustomization: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,23 +151,3 @@ func Install(ctx context.Context, cl client.Client, opts ...InstallOption) error
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupScheme() (*runtime.Scheme, error) {
|
|
||||||
scheme := runtime.NewScheme()
|
|
||||||
if err := corev1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add core/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
if err := apiextensionsv1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add apiextensions to scheme: %w", err)
|
|
||||||
}
|
|
||||||
if err := appsv1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add apps/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
if err := rbacv1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add rbac/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
if err := admissionregistrationv1.AddToScheme(scheme); err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to add admissionregistration/v1 to scheme: %w", err)
|
|
||||||
}
|
|
||||||
return scheme, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@ -17,16 +17,19 @@ limitations under the License.
|
|||||||
package kustomize
|
package kustomize
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
apimachineryerrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
"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/serializer"
|
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
"sigs.k8s.io/kustomize/api/krusty"
|
"sigs.k8s.io/kustomize/api/krusty"
|
||||||
|
"sigs.k8s.io/kustomize/api/resmap"
|
||||||
"sigs.k8s.io/kustomize/api/types"
|
"sigs.k8s.io/kustomize/api/types"
|
||||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||||
)
|
)
|
||||||
@ -39,18 +42,10 @@ type ApplyKustomizationOptions struct {
|
|||||||
// ApplyKustomizationOption is a functional option for ApplyKustomization
|
// ApplyKustomizationOption is a functional option for ApplyKustomization
|
||||||
type ApplyKustomizationOption func(*ApplyKustomizationOptions)
|
type ApplyKustomizationOption func(*ApplyKustomizationOptions)
|
||||||
|
|
||||||
// WithIgnoreExistingResources sets the ignore existing option
|
|
||||||
func WithIgnoreExistingResources(ignore bool) ApplyKustomizationOption {
|
|
||||||
return func(opts *ApplyKustomizationOptions) {
|
|
||||||
opts.IgnoreExistingResources = ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyKustomization builds the kustomization and creates the resources
|
// ApplyKustomization builds the kustomization and creates the resources
|
||||||
func ApplyKustomization(
|
func ApplyKustomization(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
cl client.Client,
|
cl client.Client,
|
||||||
scheme *runtime.Scheme,
|
|
||||||
kustomization *types.Kustomization,
|
kustomization *types.Kustomization,
|
||||||
options ...ApplyKustomizationOption,
|
options ...ApplyKustomizationOption,
|
||||||
) error {
|
) error {
|
||||||
@ -85,34 +80,60 @@ func ApplyKustomization(
|
|||||||
return fmt.Errorf("failed to run kustomize: %w", err)
|
return fmt.Errorf("failed to run kustomize: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
codecs := serializer.NewCodecFactory(scheme)
|
return applyResourceMap(ctx, cl, resourceMap)
|
||||||
deserializer := codecs.UniversalDeserializer()
|
}
|
||||||
|
|
||||||
// Apply the resources
|
func applyResourceMap(ctx context.Context, cl client.Client, resourceMap resmap.ResMap) error {
|
||||||
for _, res := range resourceMap.Resources() {
|
yamlBytes, err := resourceMap.AsYaml()
|
||||||
resJSON, err := res.MarshalJSON()
|
if err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("failed to convert resources to YAML: %w", err)
|
||||||
return fmt.Errorf("failed to convert resource map to yaml: %w", err)
|
}
|
||||||
|
r := bytes.NewReader(yamlBytes)
|
||||||
|
dec := yaml.NewDecoder(r)
|
||||||
|
for {
|
||||||
|
// parse the YAML doc
|
||||||
|
obj := &unstructured.Unstructured{Object: map[string]interface{}{}}
|
||||||
|
err := dec.Decode(obj.Object)
|
||||||
|
if errors.Is(err, io.EOF) {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
obj, _, err := deserializer.Decode(resJSON, nil, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to decode resource: %w", err)
|
return fmt.Errorf("could not decode object: %w", err)
|
||||||
}
|
}
|
||||||
// TODO: review
|
if obj.Object == nil {
|
||||||
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
|
continue
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to convert object to unstructured: %w", err)
|
|
||||||
}
|
}
|
||||||
u := &unstructured.Unstructured{Object: unstructuredObj}
|
if err := applyResource(ctx, cl, obj); err != nil {
|
||||||
|
return err
|
||||||
if err := cl.Create(ctx, u); err != nil {
|
|
||||||
if errors.IsAlreadyExists(err) && opts.IgnoreExistingResources {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return fmt.Errorf("failed to apply resource: %w", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func applyResource(ctx context.Context, cl client.Client, obj *unstructured.Unstructured) error {
|
||||||
|
if err := cl.Create(ctx, obj); err != nil {
|
||||||
|
if apimachineryerrors.IsAlreadyExists(err) {
|
||||||
|
// If the resource already exists, retrieve the existing resource
|
||||||
|
existing := &unstructured.Unstructured{}
|
||||||
|
existing.SetGroupVersionKind(obj.GroupVersionKind())
|
||||||
|
key := client.ObjectKey{
|
||||||
|
Namespace: obj.GetNamespace(),
|
||||||
|
Name: obj.GetName(),
|
||||||
|
}
|
||||||
|
if err := cl.Get(ctx, key, existing); err != nil {
|
||||||
|
log.Fatalf("Error getting existing resource: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the existing resource with the new data
|
||||||
|
obj.SetResourceVersion(existing.GetResourceVersion())
|
||||||
|
err = cl.Update(ctx, obj)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("error updating resource: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("error creating resource: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user