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
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"`))
})
})
})
86 changes: 86 additions & 0 deletions integrationtests/gitjob/controller/userid_logging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package controller

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

"github.com/rancher/fleet/integrationtests/utils"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("GitRepo UserID logging", func() {
var (
gitrepo *fleet.GitRepo
namespace string
)

createGitRepo := func(name string, labels map[string]string) {
gitrepo = &fleet.GitRepo{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Spec: fleet.GitRepoSpec{
Repo: "https://github.com/rancher/fleet-test-data/single-path",
},
}
Expect(k8sClient.Create(ctx, gitrepo)).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, gitrepo)).ToNot(HaveOccurred())
Expect(k8sClient.Delete(ctx, ns)).ToNot(HaveOccurred())
})
})

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

BeforeEach(func() {
createGitRepo("test-gitrepo-with-userid", map[string]string{
fleet.CreatedByUserIDLabel: userID,
})
})

It("includes userID in log output", func() {
Eventually(func() string {
return logsBuffer.String()
}, timeout).Should(Or(
ContainSubstring(`"userID":"`+userID+`"`),
ContainSubstring(`"userID": "`+userID+`"`),
))

logs := logsBuffer.String()
Expect(logs).To(ContainSubstring("gitjob"))
Expect(logs).To(ContainSubstring(gitrepo.Name))
})
})

When("GitRepo does not have user ID label", func() {
BeforeEach(func() {
createGitRepo("test-gitrepo-without-userid", nil)
})

It("does not include userID in log output", func() {
Eventually(func() string {
return logsBuffer.String()
}, timeout).Should(ContainSubstring(gitrepo.Name))

logs := logsBuffer.String()
gitrepoLogs := utils.ExtractResourceLogs(logs, gitrepo.Name)
Expect(gitrepoLogs).NotTo(ContainSubstring(`"userID"`))
})
})
})
12 changes: 12 additions & 0 deletions integrationtests/utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"context"
"strings"

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

Expand Down Expand Up @@ -51,3 +52,14 @@ func CreateCluster(ctx context.Context, k8sClient client.Client, name, controlle
err = k8sClient.Status().Update(ctx, cluster)
return cluster, err
}

// ExtractResourceLogs extracts log lines related to a specific resource name
func ExtractResourceLogs(allLogs, resourceName string) string {
var resourceLogs []string
for _, line := range strings.Split(allLogs, "\n") {
if strings.Contains(line, resourceName) {
resourceLogs = append(resourceLogs, line)
}
}
return strings.Join(resourceLogs, "\n")
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ func (r *GitJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}

logger = logger.WithValues("generation", gitrepo.Generation, "commit", gitrepo.Status.Commit).WithValues("conditions", gitrepo.Status.Conditions)

if userID := gitrepo.Labels[v1alpha1.CreatedByUserIDLabel]; userID != "" {
logger = logger.WithValues("userID", userID)
}

ctx = log.IntoContext(ctx, logger)

logger.V(1).Info("Reconciling GitRepo")
Expand Down
4 changes: 4 additions & 0 deletions internal/cmd/controller/reconciler/bundle_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ func (r *BundleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
)
}

if userID := bundle.Labels[fleet.CreatedByUserIDLabel]; userID != "" {
logger = logger.WithValues("userID", userID)
}

if !bundle.DeletionTimestamp.IsZero() {
return r.handleDelete(ctx, logger, req, bundle)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/gitrepo_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
RepoLabel = "fleet.cattle.io/repo-name"
BundleLabel = "fleet.cattle.io/bundle-name"
BundleNamespaceLabel = "fleet.cattle.io/bundle-namespace"
CreatedByUserIDLabel = "fleet.cattle.io/created-by-user-id"
)

const (
Expand Down