Skip to content

Commit d6a23dd

Browse files
weyfonkmanno
authored andcommitted
[v0.10] Increase visibility of gitjob controller logs and events (#2911)
* Add `fleet_` prefix to events This will help users find events produced by Fleet, whether through the Rancher UI or `kubectl` for instance. * Reduce verbosity of Gitjob controller logs This makes reconciliation, resource creation, deletion and status update errors for `GitRepo`s more visible, ensuring that they appear in logs even when debug logging is disabled. * Add gitrepo field to bundle reconciler logs This will help map logs on `GitRepo`s, produced by the `gitjob` reconciler, with those on corresponding bundles from their reconciler. * Add commit field to bundle logs This will help map logs referring to a bundle with those referring to its `GitRepo`, across the `fleet` and `gitjob` controllers. * Update gitops event recorder name This clarifies what events are about, as concisely as possible,.
1 parent 4b147db commit d6a23dd

File tree

9 files changed

+19
-11
lines changed

9 files changed

+19
-11
lines changed

internal/cmd/agent/deployer/deployer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (d *Deployer) helmdeploy(ctx context.Context, bd *fleet.BundleDeployment) (
151151
}
152152
}
153153

154-
manifest.Commit = bd.Labels["fleet.cattle.io/commit"]
154+
manifest.Commit = bd.Labels[fleet.CommitLabel]
155155
resource, err := d.helm.Deploy(ctx, bd.Name, manifest, bd.Spec.Options)
156156
if err != nil {
157157
return "", err

internal/cmd/cli/apply.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
command "github.com/rancher/fleet/internal/cmd"
1515
"github.com/rancher/fleet/internal/cmd/cli/apply"
1616
"github.com/rancher/fleet/internal/cmd/cli/writer"
17+
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
1718

1819
"k8s.io/apimachinery/pkg/util/yaml"
1920
)
@@ -75,7 +76,7 @@ func (a *Apply) Run(cmd *cobra.Command, args []string) error {
7576
if labels == nil {
7677
labels = map[string]string{}
7778
}
78-
labels["fleet.cattle.io/commit"] = a.Commit
79+
labels[fleet.CommitLabel] = a.Commit
7980
}
8081

8182
name := ""

internal/cmd/controller/gitops/operator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ func (g *GitOperator) Run(cmd *cobra.Command, args []string) error {
9292
return err
9393
}
9494

95-
var leaderElectionSuffix string
95+
var shardIDSuffix string
9696
if g.ShardID != "" {
97-
leaderElectionSuffix = fmt.Sprintf("-%s", g.ShardID)
97+
shardIDSuffix = fmt.Sprintf("-%s", g.ShardID)
9898
}
9999

100100
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
101101
Scheme: scheme,
102102
Metrics: g.setupMetrics(),
103103
LeaderElection: g.EnableLeaderElection,
104-
LeaderElectionID: fmt.Sprintf("fleet-gitops-leader-election-shard%s", leaderElectionSuffix),
104+
LeaderElectionID: fmt.Sprintf("fleet-gitops-leader-election-shard%s", shardIDSuffix),
105105
LeaderElectionNamespace: namespace,
106106
LeaseDuration: leaderOpts.LeaseDuration,
107107
RenewDeadline: leaderOpts.RenewDeadline,
@@ -133,7 +133,7 @@ func (g *GitOperator) Run(cmd *cobra.Command, args []string) error {
133133
JobNodeSelector: g.ShardNodeSelector,
134134
GitFetcher: &git.Fetch{},
135135
Clock: reconciler.RealClock{},
136-
Recorder: mgr.GetEventRecorderFor(fmt.Sprintf("gitjob-controller%s", g.ShardID)),
136+
Recorder: mgr.GetEventRecorderFor(fmt.Sprintf("fleet-gitops%s", shardIDSuffix)),
137137
}
138138

139139
group, ctx := errgroup.WithContext(ctx)

internal/cmd/controller/gitops/reconciler/gitjob_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (r *GitJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
223223

224224
err = grutil.UpdateStatus(ctx, r.Client, req.NamespacedName, gitrepo.Status)
225225
if err != nil {
226-
logger.V(1).Error(err, "Reconcile failed final update to git repo status", "status", gitrepo.Status)
226+
logger.Error(err, "Reconcile failed final update to git repo status", "status", gitrepo.Status)
227227

228228
return result(repoPolled, gitrepo), err
229229
}
@@ -297,7 +297,7 @@ func (r *GitJobReconciler) manageGitJob(ctx context.Context, logger logr.Logger,
297297
}
298298

299299
func (r *GitJobReconciler) cleanupGitRepo(ctx context.Context, logger logr.Logger, gitrepo *v1alpha1.GitRepo) error {
300-
logger.V(1).Info("Gitrepo deleted, deleting bundle, image scans")
300+
logger.Info("Gitrepo deleted, deleting bundle, image scans")
301301

302302
metrics.GitRepoCollector.Delete(gitrepo.Name, gitrepo.Namespace)
303303

internal/cmd/controller/reconciler/bundle_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ func (r *BundleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
131131
return ctrl.Result{}, client.IgnoreNotFound(err)
132132
}
133133

134+
if bundle.Labels[fleet.RepoLabel] != "" {
135+
logger = logger.WithValues(
136+
"gitrepo", bundle.Labels[fleet.RepoLabel],
137+
"commit", bundle.Labels[fleet.CommitLabel],
138+
)
139+
}
140+
134141
if !bundle.DeletionTimestamp.IsZero() {
135142
if controllerutil.ContainsFinalizer(bundle, bundleFinalizer) {
136143
metrics.BundleCollector.Delete(req.Name, req.Namespace)

internal/metrics/bundle_metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func collectBundleMetrics(obj any, metrics map[string]prometheus.Collector) {
112112
labels := prometheus.Labels{
113113
"name": bundle.Name,
114114
"namespace": bundle.Namespace,
115-
"commit": bundle.ObjectMeta.Labels[commitLabel],
115+
"commit": bundle.ObjectMeta.Labels[fleet.CommitLabel],
116116
"repo": bundle.ObjectMeta.Labels[repoNameLabel],
117117
"generation": fmt.Sprintf("%d", bundle.ObjectMeta.Generation),
118118
"state": string(currentState),

internal/metrics/bundledeployment_metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func collectBundleDeploymentMetrics(obj any, metrics map[string]prometheus.Colle
5555
"cluster_name": bundleDep.ObjectMeta.Labels["fleet.cattle.io/cluster"],
5656
"cluster_namespace": bundleDep.ObjectMeta.Labels["fleet.cattle.io/cluster-namespace"],
5757
"repo": bundleDep.ObjectMeta.Labels[repoNameLabel],
58-
"commit": bundleDep.ObjectMeta.Labels[commitLabel],
58+
"commit": bundleDep.ObjectMeta.Labels[fleet.CommitLabel],
5959
"bundle": bundleDep.ObjectMeta.Labels["fleet.cattle.io/bundle-name"],
6060
"bundle_namespace": bundleDep.ObjectMeta.Labels["fleet.cattle.io/bundle-namespace"],
6161
"generation": fmt.Sprintf("%d", bundleDep.ObjectMeta.Generation),

internal/metrics/metrics.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
const (
1515
metricPrefix = "fleet"
16-
commitLabel = "fleet.cattle.io/commit"
1716
repoNameLabel = "fleet.cattle.io/repo-name"
1817
)
1918

pkg/apis/fleet.cattle.io/v1alpha1/gitrepo_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func init() {
1010
}
1111

1212
var (
13+
CommitLabel = "fleet.cattle.io/commit"
1314
RepoLabel = "fleet.cattle.io/repo-name"
1415
BundleLabel = "fleet.cattle.io/bundle-name"
1516
BundleNamespaceLabel = "fleet.cattle.io/bundle-namespace"

0 commit comments

Comments
 (0)