Skip to content

Commit

Permalink
fix(feature): ensures all new resources are created in one reconcile (o…
Browse files Browse the repository at this point in the history
…pendatahub-io#1159)

Manifests used by Feature to create cluster resources can be either
defined as a single resource per file or grouped together and separated
using the `---` delimiter. Even though the former approach is currently
preferred across the codebase, the latter is used to create Envoy filters
for the KServe setup.

When applying resources not yet present in the cluster, the original
implementation returns after creating the first resource, leaving the rest
unprocessed. They are applied in the next reconcile loop, though again
on a one-per-reconcile event basis. This is not a bug per se, as it will
eventually result in creating all resources; however, it is suboptimal.

This change ensures all resources defined in a single YAML manifest,
separated using `---`, are processed in a single go.
  • Loading branch information
bartoszmajsak authored Aug 8, 2024
1 parent 286caf9 commit 959643d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
5 changes: 3 additions & 2 deletions pkg/feature/resource/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ func Apply(ctx context.Context, cli client.Client, objects []*unstructured.Unstr
return fmt.Errorf("failed to get resource %s/%s: %w", namespace, name, errGet)
}

justCreated := false
if k8serr.IsNotFound(errGet) {
if errCreate := cli.Create(ctx, target); client.IgnoreAlreadyExists(errCreate) != nil {
return fmt.Errorf("failed to create source %s/%s: %w", namespace, name, errCreate)
}

return nil
justCreated = true
}

if shouldReconcile(source) {
if !justCreated && shouldReconcile(source) {
if errUpdate := patchUsingApplyStrategy(ctx, cli, source, target); errUpdate != nil {
return fmt.Errorf("failed to reconcile resource %s/%s: %w", namespace, name, errUpdate)
}
Expand Down
4 changes: 0 additions & 4 deletions tests/integration/features/fixtures/templates/namespace.yaml

This file was deleted.

10 changes: 10 additions & 0 deletions tests/integration/features/fixtures/templates/namespaces.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: Namespace
metadata:
name: embedded-test-ns-1
---
apiVersion: v1
kind: Namespace
metadata:
name: embedded-test-ns-2

17 changes: 11 additions & 6 deletions tests/integration/features/manifests_int_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ var _ = Describe("Applying resources", func() {
It("should be able to process an embedded YAML file", func(ctx context.Context) {
// given
featuresHandler := feature.ClusterFeaturesHandler(dsci, func(registry feature.FeaturesRegistry) error {
errNsCreate := registry.Add(feature.Define("create-namespace").
errNsCreate := registry.Add(feature.Define("create-namespaces").
UsingConfig(envTest.Config).
Manifests(
manifest.Location(fixtures.TestEmbeddedFiles).
Include(path.Join(fixtures.BaseDir, "namespace.yaml")),
Include(path.Join(fixtures.BaseDir, "namespaces.yaml")),
),
)

Expand All @@ -63,10 +63,15 @@ var _ = Describe("Applying resources", func() {
Expect(featuresHandler.Apply(ctx)).To(Succeed())

// then
embeddedNs, err := fixtures.GetNamespace(ctx, envTestClient, "embedded-test-ns")
defer objectCleaner.DeleteAll(ctx, embeddedNs)
Expect(err).ToNot(HaveOccurred())
Expect(embeddedNs.Name).To(Equal("embedded-test-ns"))
embeddedNs1, errNS1 := fixtures.GetNamespace(ctx, envTestClient, "embedded-test-ns-1")
embeddedNs2, errNS2 := fixtures.GetNamespace(ctx, envTestClient, "embedded-test-ns-2")
defer objectCleaner.DeleteAll(ctx, embeddedNs1, embeddedNs2)

Expect(errNS1).ToNot(HaveOccurred())
Expect(errNS2).ToNot(HaveOccurred())

Expect(embeddedNs1.Name).To(Equal("embedded-test-ns-1"))
Expect(embeddedNs2.Name).To(Equal("embedded-test-ns-2"))
})

It("should be able to process an embedded template file", func(ctx context.Context) {
Expand Down

0 comments on commit 959643d

Please sign in to comment.