plugin-barman-cloud/test/e2e/e2e_suite_test.go
Armando Ruocco 8971a397f4
fix(rbac): reconcile Role when ObjectStore spec changes (#823)
When an ObjectStore's credentials change (e.g., secret rename), the
RBAC Role granting the Cluster's ServiceAccount access to those
secrets was not updated because nothing triggered a Cluster
reconciliation.

Implement the ObjectStore controller's Reconcile to detect affected
Roles and update their rules directly, without needing access to
Cluster objects. The controller lists Roles by a label set by the
Pre hook, inspects their rules to find which ObjectStores they
reference, fetches those ObjectStores, and patches the rules to
match the current specs.

Replace the custom setOwnerReference helper with
controllerutil.SetControllerReference. Add dynamic CNPG scheme
registration (internal/scheme) to the operator, instance, and
restore managers.

Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Signed-off-by: Gabriele Quaresima <gabriele.quaresima@enterprisedb.com>
Co-authored-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
Co-authored-by: Gabriele Quaresima <gabriele.quaresima@enterprisedb.com>
2026-04-13 15:48:35 +02:00

139 lines
4.4 KiB
Go

/*
Copyright © contributors to CloudNativePG, established as
CloudNativePG a Series of LF Projects, LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/
package e2e
import (
"context"
"fmt"
"testing"
"time"
"github.com/cloudnative-pg/machinery/pkg/log"
apimachineryTypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
kustomizeTypes "sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/resid"
internalClient "github.com/cloudnative-pg/plugin-barman-cloud/test/e2e/internal/client"
"github.com/cloudnative-pg/plugin-barman-cloud/test/e2e/internal/deployment"
"github.com/cloudnative-pg/plugin-barman-cloud/test/e2e/internal/e2etestenv"
"github.com/cloudnative-pg/plugin-barman-cloud/test/e2e/internal/kustomize"
_ "github.com/cloudnative-pg/plugin-barman-cloud/test/e2e/internal/tests/backup"
_ "github.com/cloudnative-pg/plugin-barman-cloud/test/e2e/internal/tests/credentialrotation"
_ "github.com/cloudnative-pg/plugin-barman-cloud/test/e2e/internal/tests/replicacluster"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// We don't want multiple ginkgo nodes to run the setup concurrently, we use a single cluster for all tests.
var _ = SynchronizedBeforeSuite(func(ctx SpecContext) []byte {
cl, _, err := internalClient.NewClient()
if err != nil {
Fail(fmt.Sprintf("failed to create Kubernetes client: %v", err))
}
if err = e2etestenv.Setup(ctx, cl); err != nil {
Fail(fmt.Sprintf("failed to setup environment: %v", err))
}
const barmanCloudKustomizationPath = "./kustomize/kubernetes/"
barmanCloudKustomization := &kustomizeTypes.Kustomization{
Resources: []string{barmanCloudKustomizationPath},
// Override the image from the base kustomization (kubernetes/kustomization.yaml)
// with the locally-built one. The Name must match the newName in the base.
Images: []kustomizeTypes.Image{
{
Name: "ghcr.io/cloudnative-pg/plugin-barman-cloud-testing",
NewName: "registry.barman-cloud-plugin:5000/plugin-barman-cloud",
NewTag: "testing",
},
},
SecretGenerator: []kustomizeTypes.SecretArgs{
{
GeneratorArgs: kustomizeTypes.GeneratorArgs{
Name: "plugin-barman-cloud",
Behavior: "replace",
KvPairSources: kustomizeTypes.KvPairSources{
LiteralSources: []string{"SIDECAR_IMAGE=registry.barman-cloud-plugin:5000/sidecar-barman-cloud:testing"},
},
},
},
},
Patches: []kustomizeTypes.Patch{
{
Patch: `[{"op": "replace", "path": "/spec/template/spec/containers/0/imagePullPolicy", "value": "Always"}]`,
Target: &kustomizeTypes.Selector{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "apps",
Version: "v1",
Kind: "Deployment",
},
Name: "barman-cloud",
Namespace: "cnpg-system",
},
},
Options: nil,
},
},
}
if err := kustomize.ApplyKustomization(ctx, cl, barmanCloudKustomization); err != nil {
Fail(fmt.Sprintf("failed to apply kustomization: %v", err))
}
const defaultTimeout = 1 * time.Minute
ctxDeploy, cancel := context.WithTimeout(ctx, defaultTimeout)
defer cancel()
deploy := apimachineryTypes.NamespacedName{
Namespace: "cnpg-system",
Name: "barman-cloud",
}
err = wait.PollUntilContextCancel(ctxDeploy, 5*time.Second, false,
func(ctx context.Context) (bool, error) {
ready, err := deployment.IsReady(ctx, cl, deploy)
if err != nil {
return false, fmt.Errorf("failed to check if %s is ready: %w", deploy, err)
}
if ready {
return true, nil
}
return false, nil
})
if err != nil {
Fail(fmt.Sprintf("failed to wait for deployment to be ready: %v", err))
}
return []byte{}
}, func(_ []byte) {
logFlags := &log.Flags{}
logFlags.ConfigureLogging()
})
// Run e2e tests using the Ginkgo runner.
func TestE2E(t *testing.T) {
RegisterFailHandler(Fail)
_, _ = fmt.Fprintf(GinkgoWriter, "Starting plugin-barman-cloud suite\n")
RunSpecs(t, "e2e suite")
}