Skip to content

Commit 5d53822

Browse files
committed
test: review
Signed-off-by: Francesco Canovai <[email protected]>
1 parent e63ffb3 commit 5d53822

File tree

4 files changed

+55
-103
lines changed

4 files changed

+55
-103
lines changed

test/e2e/e2e_suite_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ var _ = SynchronizedBeforeSuite(func(ctx SpecContext) []byte {
9494
Fail(fmt.Sprintf("failed to add cert-manager.io/v1 to scheme: %v", err))
9595
}
9696

97-
if err := kustomize.ApplyKustomization(ctx,
98-
cl,
99-
scheme,
100-
barmanCloudKustomization,
101-
kustomize.WithIgnoreExistingResources(false)); err != nil {
97+
if err := kustomize.ApplyKustomization(ctx, cl, barmanCloudKustomization); err != nil {
10298
Fail(fmt.Sprintf("failed to apply kustomization: %v", err))
10399
}
104100
const defaultTimeout = 1 * time.Minute

test/e2e/internal/certmanager/certmanager.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ import (
2121
"fmt"
2222
"time"
2323

24-
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
25-
appsv1 "k8s.io/api/apps/v1"
26-
corev1 "k8s.io/api/core/v1"
27-
rbacv1 "k8s.io/api/rbac/v1"
28-
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
29-
"k8s.io/apimachinery/pkg/runtime"
3024
types2 "k8s.io/apimachinery/pkg/types"
3125
"sigs.k8s.io/controller-runtime/pkg/client"
3226
"sigs.k8s.io/kustomize/api/types"
@@ -84,13 +78,7 @@ func Install(ctx context.Context, cl client.Client, opts ...InstallOption) error
8478
}
8579

8680
// Add all the resources defined in the cert-manager manifests
87-
scheme, err := setupScheme()
88-
if err != nil {
89-
return err
90-
}
91-
92-
if err := kustomize.ApplyKustomization(ctx, cl, scheme, kustomization,
93-
kustomize.WithIgnoreExistingResources(options.IgnoreExistResources)); err != nil {
81+
if err := kustomize.ApplyKustomization(ctx, cl, kustomization); err != nil {
9482
return fmt.Errorf("failed to apply kustomization: %w", err)
9583
}
9684

@@ -116,24 +104,3 @@ func Install(ctx context.Context, cl client.Client, opts ...InstallOption) error
116104

117105
return nil
118106
}
119-
120-
func setupScheme() (*runtime.Scheme, error) {
121-
scheme := runtime.NewScheme()
122-
if err := corev1.AddToScheme(scheme); err != nil {
123-
return nil, fmt.Errorf("failed to add core/v1 to scheme: %w", err)
124-
}
125-
if err := apiextensionsv1.AddToScheme(scheme); err != nil {
126-
return nil, fmt.Errorf("failed to add apiextensions/v1 to scheme: %w", err)
127-
}
128-
if err := admissionregistrationv1.AddToScheme(scheme); err != nil {
129-
return nil, fmt.Errorf("failed to add admissionregistration/v1 to scheme: %w", err)
130-
}
131-
if err := rbacv1.AddToScheme(scheme); err != nil {
132-
return nil, fmt.Errorf("failed to add rbac/v1 to scheme: %w", err)
133-
}
134-
if err := appsv1.AddToScheme(scheme); err != nil {
135-
return nil, fmt.Errorf("failed to add apps/v1 to scheme: %w", err)
136-
}
137-
138-
return scheme, nil
139-
}

test/e2e/internal/cloudnativepg/cloudnativepg.go

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ import (
2121
"fmt"
2222
"time"
2323

24-
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
25-
appsv1 "k8s.io/api/apps/v1"
26-
corev1 "k8s.io/api/core/v1"
27-
rbacv1 "k8s.io/api/rbac/v1"
28-
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
29-
"k8s.io/apimachinery/pkg/runtime"
3024
types2 "k8s.io/apimachinery/pkg/types"
3125
"sigs.k8s.io/controller-runtime/pkg/client"
3226
"sigs.k8s.io/kustomize/api/types"
@@ -135,13 +129,7 @@ func Install(ctx context.Context, cl client.Client, opts ...InstallOption) error
135129
},
136130
}
137131

138-
scheme, err := setupScheme()
139-
if err != nil {
140-
return err
141-
}
142-
143-
if err := kustomize.ApplyKustomization(ctx, cl, scheme, kustomization,
144-
kustomize.WithIgnoreExistingResources(options.IgnoreExistResources)); err != nil {
132+
if err := kustomize.ApplyKustomization(ctx, cl, kustomization); err != nil {
145133
return fmt.Errorf("failed to apply kustomization: %w", err)
146134
}
147135

@@ -163,23 +151,3 @@ func Install(ctx context.Context, cl client.Client, opts ...InstallOption) error
163151

164152
return nil
165153
}
166-
167-
func setupScheme() (*runtime.Scheme, error) {
168-
scheme := runtime.NewScheme()
169-
if err := corev1.AddToScheme(scheme); err != nil {
170-
return nil, fmt.Errorf("failed to add core/v1 to scheme: %w", err)
171-
}
172-
if err := apiextensionsv1.AddToScheme(scheme); err != nil {
173-
return nil, fmt.Errorf("failed to add apiextensions to scheme: %w", err)
174-
}
175-
if err := appsv1.AddToScheme(scheme); err != nil {
176-
return nil, fmt.Errorf("failed to add apps/v1 to scheme: %w", err)
177-
}
178-
if err := rbacv1.AddToScheme(scheme); err != nil {
179-
return nil, fmt.Errorf("failed to add rbac/v1 to scheme: %w", err)
180-
}
181-
if err := admissionregistrationv1.AddToScheme(scheme); err != nil {
182-
return nil, fmt.Errorf("failed to add admissionregistration/v1 to scheme: %w", err)
183-
}
184-
return scheme, nil
185-
}

test/e2e/internal/kustomize/kustomize.go

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ limitations under the License.
1717
package kustomize
1818

1919
import (
20+
"bytes"
2021
"context"
22+
"errors"
2123
"fmt"
24+
"io"
25+
"log"
2226

2327
"gopkg.in/yaml.v3"
24-
"k8s.io/apimachinery/pkg/api/errors"
28+
apimachineryerrors "k8s.io/apimachinery/pkg/api/errors"
2529
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26-
"k8s.io/apimachinery/pkg/runtime"
27-
"k8s.io/apimachinery/pkg/runtime/serializer"
2830
"sigs.k8s.io/controller-runtime/pkg/client"
2931
"sigs.k8s.io/kustomize/api/krusty"
32+
"sigs.k8s.io/kustomize/api/resmap"
3033
"sigs.k8s.io/kustomize/api/types"
3134
"sigs.k8s.io/kustomize/kyaml/filesys"
3235
)
@@ -39,18 +42,10 @@ type ApplyKustomizationOptions struct {
3942
// ApplyKustomizationOption is a functional option for ApplyKustomization
4043
type ApplyKustomizationOption func(*ApplyKustomizationOptions)
4144

42-
// WithIgnoreExistingResources sets the ignore existing option
43-
func WithIgnoreExistingResources(ignore bool) ApplyKustomizationOption {
44-
return func(opts *ApplyKustomizationOptions) {
45-
opts.IgnoreExistingResources = ignore
46-
}
47-
}
48-
4945
// ApplyKustomization builds the kustomization and creates the resources
5046
func ApplyKustomization(
5147
ctx context.Context,
5248
cl client.Client,
53-
scheme *runtime.Scheme,
5449
kustomization *types.Kustomization,
5550
options ...ApplyKustomizationOption,
5651
) error {
@@ -85,34 +80,60 @@ func ApplyKustomization(
8580
return fmt.Errorf("failed to run kustomize: %w", err)
8681
}
8782

88-
codecs := serializer.NewCodecFactory(scheme)
89-
deserializer := codecs.UniversalDeserializer()
83+
return applyResourceMap(ctx, cl, resourceMap)
84+
}
9085

91-
// Apply the resources
92-
for _, res := range resourceMap.Resources() {
93-
resJSON, err := res.MarshalJSON()
94-
if err != nil {
95-
return fmt.Errorf("failed to convert resource map to yaml: %w", err)
86+
func applyResourceMap(ctx context.Context, cl client.Client, resourceMap resmap.ResMap) error {
87+
yamlBytes, err := resourceMap.AsYaml()
88+
if err != nil {
89+
return fmt.Errorf("failed to convert resources to YAML: %w", err)
90+
}
91+
r := bytes.NewReader(yamlBytes)
92+
dec := yaml.NewDecoder(r)
93+
for {
94+
// parse the YAML doc
95+
obj := &unstructured.Unstructured{Object: map[string]interface{}{}}
96+
err := dec.Decode(obj.Object)
97+
if errors.Is(err, io.EOF) {
98+
break
9699
}
97-
98-
obj, _, err := deserializer.Decode(resJSON, nil, nil)
99100
if err != nil {
100-
return fmt.Errorf("failed to decode resource: %w", err)
101+
return fmt.Errorf("could not decode object: %w", err)
101102
}
102-
// TODO: review
103-
unstructuredObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
104-
if err != nil {
105-
return fmt.Errorf("failed to convert object to unstructured: %w", err)
103+
if obj.Object == nil {
104+
continue
106105
}
107-
u := &unstructured.Unstructured{Object: unstructuredObj}
106+
if err := applyResource(ctx, cl, obj); err != nil {
107+
return err
108+
}
109+
}
110+
111+
return nil
112+
}
108113

109-
if err := cl.Create(ctx, u); err != nil {
110-
if errors.IsAlreadyExists(err) && opts.IgnoreExistingResources {
111-
continue
114+
func applyResource(ctx context.Context, cl client.Client, obj *unstructured.Unstructured) error {
115+
if err := cl.Create(ctx, obj); err != nil {
116+
if apimachineryerrors.IsAlreadyExists(err) {
117+
// If the resource already exists, retrieve the existing resource
118+
existing := &unstructured.Unstructured{}
119+
existing.SetGroupVersionKind(obj.GroupVersionKind())
120+
key := client.ObjectKey{
121+
Namespace: obj.GetNamespace(),
122+
Name: obj.GetName(),
112123
}
113-
return fmt.Errorf("failed to apply resource: %w", err)
124+
if err := cl.Get(ctx, key, existing); err != nil {
125+
log.Fatalf("Error getting existing resource: %v", err)
126+
}
127+
128+
// Update the existing resource with the new data
129+
obj.SetResourceVersion(existing.GetResourceVersion())
130+
err = cl.Update(ctx, obj)
131+
if err != nil {
132+
return fmt.Errorf("error updating resource: %v", err)
133+
}
134+
} else {
135+
return fmt.Errorf("error creating resource: %v", err)
114136
}
115137
}
116-
117138
return nil
118139
}

0 commit comments

Comments
 (0)