Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions integrationtests/controller/bundle/label_migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package bundle

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/rancher/fleet/integrationtests/utils"
"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("Bundle label migration", func() {
BeforeEach(func() {
var err error
namespace, err = utils.NewNamespaceName()
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: namespace},
})).ToNot(HaveOccurred())

_, err = utils.CreateCluster(ctx, k8sClient, "cluster", namespace, nil, namespace)
Expect(err).NotTo(HaveOccurred())

DeferCleanup(func() {
Expect(k8sClient.Delete(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}})).ToNot(HaveOccurred())
})
})

createBundle := func(name string, labels map[string]string) {
bundle := &v1alpha1.Bundle{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: v1alpha1.BundleSpec{
BundleDeploymentOptions: v1alpha1.BundleDeploymentOptions{
DefaultNamespace: "default",
},
Targets: []v1alpha1.BundleTarget{
{
BundleDeploymentOptions: v1alpha1.BundleDeploymentOptions{
TargetNamespace: "targetNs",
},
Name: "cluster",
ClusterName: "cluster",
},
},
},
}
Expect(k8sClient.Create(ctx, bundle)).ToNot(HaveOccurred())
}

DescribeTable("should remove deprecated label after migration",
func(bundleName string, initialLabels map[string]string) {
const deprecatedLabel = "fleet.cattle.io/created-by-display-name"

createBundle(bundleName, initialLabels)
DeferCleanup(func() {
Expect(k8sClient.Delete(ctx, &v1alpha1.Bundle{
ObjectMeta: metav1.ObjectMeta{Name: bundleName, Namespace: namespace},
})).ToNot(HaveOccurred())
})

Eventually(func(g Gomega) {
bundle := &v1alpha1.Bundle{}
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: bundleName}, bundle)).To(Succeed())
g.Expect(bundle.Status.ObservedGeneration).To(BeNumerically(">", 0))
}).Should(Succeed())

bundle := &v1alpha1.Bundle{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: bundleName}, bundle)).To(Succeed())

Expect(bundle.Labels).ToNot(HaveKey(deprecatedLabel))
Expect(bundle.Labels).To(HaveKey(v1alpha1.CreatedByUserIDLabel))
},
Entry("with label present initially",
"bundle-with-label",
map[string]string{
"fleet.cattle.io/created-by-display-name": "admin",
v1alpha1.CreatedByUserIDLabel: "user-12345",
},
),
Entry("without label present initially",
"bundle-without-label",
map[string]string{
v1alpha1.CreatedByUserIDLabel: "user-12345",
},
),
)
})
18 changes: 13 additions & 5 deletions integrationtests/controller/bundle/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bundle

import (
"bytes"
"context"
"testing"
"time"
Expand All @@ -19,16 +20,19 @@ import (

"k8s.io/client-go/rest"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

var (
cancel context.CancelFunc
cfg *rest.Config
ctx context.Context
k8sClient client.Client
testenv *envtest.Environment
cancel context.CancelFunc
cfg *rest.Config
ctx context.Context
k8sClient client.Client
testenv *envtest.Environment
logsBuffer bytes.Buffer

namespace string
)
Expand All @@ -49,6 +53,10 @@ var _ = BeforeSuite(func() {
cfg, err = utils.StartTestEnv(testenv)
Expect(err).NotTo(HaveOccurred())

// Set up log capture
GinkgoWriter.TeeTo(&logsBuffer)
ctrl.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

k8sClient, err = utils.NewClient(cfg)
Expect(err).NotTo(HaveOccurred())

Expand Down
124 changes: 124 additions & 0 deletions integrationtests/controller/bundle/userid_logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package bundle

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/rancher/fleet/integrationtests/utils"
"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("Bundle UserID logging", func() {
var (
bundle *v1alpha1.Bundle
cluster *v1alpha1.Cluster
namespace string
)

createBundle := func(name, clusterName string, labels map[string]string) {
bundle = &v1alpha1.Bundle{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: v1alpha1.BundleSpec{
BundleDeploymentOptions: v1alpha1.BundleDeploymentOptions{
DefaultNamespace: "default",
},
Targets: []v1alpha1.BundleTarget{
{
Name: clusterName,
ClusterName: clusterName,
},
},
Resources: []v1alpha1.BundleResource{
{
Name: "test-configmap.yaml",
Content: "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: test-cm\ndata:\n key: value\n",
},
},
},
}
Expect(k8sClient.Create(ctx, bundle)).ToNot(HaveOccurred())
}

BeforeEach(func() {
var err error
namespace, err = utils.NewNamespaceName()
Expect(err).ToNot(HaveOccurred())
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}}
Expect(k8sClient.Create(ctx, ns)).ToNot(HaveOccurred())

logsBuffer.Reset()

DeferCleanup(func() {
Expect(k8sClient.Delete(ctx, bundle)).ToNot(HaveOccurred())
Expect(k8sClient.Delete(ctx, cluster)).ToNot(HaveOccurred())
Expect(k8sClient.Delete(ctx, ns)).ToNot(HaveOccurred())
})
})

waitForReconciliation := func() {
Eventually(func() int64 {
err := k8sClient.Get(ctx, client.ObjectKey{Namespace: bundle.Namespace, Name: bundle.Name}, bundle)
if err != nil {
return 0
}
return bundle.Status.ObservedGeneration
}).Should(BeNumerically(">", 0))

Eventually(func() string {
return logsBuffer.String()
}).Should(ContainSubstring(bundle.Name))
}

When("Bundle has user ID label", func() {
const userID = "user-12345"

BeforeEach(func() {
var err error
cluster, err = utils.CreateCluster(ctx, k8sClient, "test-cluster", namespace, nil, namespace)
Expect(err).NotTo(HaveOccurred())

createBundle("test-bundle-with-userid", "test-cluster", map[string]string{
v1alpha1.CreatedByUserIDLabel: userID,
})
})

It("includes userID in log output", func() {
waitForReconciliation()

logs := logsBuffer.String()
Expect(logs).To(Or(
ContainSubstring(`"userID":"`+userID+`"`),
ContainSubstring(`"userID": "`+userID+`"`),
))

Expect(logs).To(ContainSubstring("bundle"))
Expect(logs).To(ContainSubstring(bundle.Name))
})
})

When("Bundle does not have user ID label", func() {
BeforeEach(func() {
var err error
cluster, err = utils.CreateCluster(ctx, k8sClient, "test-cluster-2", namespace, nil, namespace)
Expect(err).NotTo(HaveOccurred())

createBundle("test-bundle-without-userid", "test-cluster-2", nil)
})

It("does not include userID in log output", func() {
waitForReconciliation()

logs := logsBuffer.String()
bundleLogs := utils.ExtractResourceLogs(logs, bundle.Name)
Expect(bundleLogs).NotTo(ContainSubstring(`"userID"`))
})
})
})
82 changes: 82 additions & 0 deletions integrationtests/gitjob/controller/label_migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package controller

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/rancher/fleet/integrationtests/utils"
"github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("GitRepo label migration", func() {
var namespace string

BeforeEach(func() {
var err error
namespace, err = utils.NewNamespaceName()
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient.Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: namespace},
})).ToNot(HaveOccurred())

DeferCleanup(func() {
Expect(k8sClient.Delete(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}})).ToNot(HaveOccurred())
})
})

createGitRepo := func(name string, labels map[string]string) {
gitrepo := &v1alpha1.GitRepo{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: v1alpha1.GitRepoSpec{
Repo: "https://github.com/rancher/fleet-test-data/not-found",
},
}
Expect(k8sClient.Create(ctx, gitrepo)).ToNot(HaveOccurred())
}

DescribeTable("should remove deprecated label after migration",
func(gitRepoName string, initialLabels map[string]string) {
const deprecatedLabel = "fleet.cattle.io/created-by-display-name"

createGitRepo(gitRepoName, initialLabels)
DeferCleanup(func() {
Expect(k8sClient.Delete(ctx, &v1alpha1.GitRepo{
ObjectMeta: metav1.ObjectMeta{Name: gitRepoName, Namespace: namespace},
})).ToNot(HaveOccurred())
})

Eventually(func(g Gomega) {
gitrepo := &v1alpha1.GitRepo{}
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: gitRepoName}, gitrepo)).To(Succeed())
g.Expect(gitrepo.Status.ObservedGeneration).To(BeNumerically(">", 0))
}).Should(Succeed())

gitrepo := &v1alpha1.GitRepo{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Namespace: namespace, Name: gitRepoName}, gitrepo)).To(Succeed())

Expect(gitrepo.Labels).ToNot(HaveKey(deprecatedLabel))
Expect(gitrepo.Labels).To(HaveKey(v1alpha1.CreatedByUserIDLabel))
},
Entry("with label present initially",
"gitrepo-with-label",
map[string]string{
"fleet.cattle.io/created-by-display-name": "admin",
v1alpha1.CreatedByUserIDLabel: "user-12345",
},
),
Entry("without label present initially",
"gitrepo-without-label",
map[string]string{
v1alpha1.CreatedByUserIDLabel: "user-12345",
},
),
)
})
Loading
Loading