Skip to content

Commit 21f693f

Browse files
committed
fix(tracker): revert watching of all cluster kustomizations
1 parent 07ab3ff commit 21f693f

6 files changed

Lines changed: 62 additions & 202 deletions

File tree

cmd/main.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ import (
2020
"github.com/lmittmann/tint"
2121
"github.com/mattn/go-isatty"
2222
otelruntime "go.opentelemetry.io/contrib/instrumentation/runtime"
23+
"k8s.io/apimachinery/pkg/fields"
2324
"k8s.io/apimachinery/pkg/runtime"
2425
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2526
"k8s.io/client-go/rest"
2627
ctrl "sigs.k8s.io/controller-runtime"
28+
"sigs.k8s.io/controller-runtime/pkg/cache"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
2730
"sigs.k8s.io/controller-runtime/pkg/client/config"
2831
"sigs.k8s.io/controller-runtime/pkg/healthz"
2932
"sigs.k8s.io/controller-runtime/pkg/manager"
3033
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
3134
)
3235

36+
const (
37+
// FallbackNamespace is the namespace to look for a Kustomization in if the
38+
// runtime namespace is not available.
39+
FallbackNamespace = "default"
40+
)
41+
3342
// k8s controller
3443
var scheme = runtime.NewScheme()
3544

@@ -137,12 +146,7 @@ func (cli CLI) run(handler slog.Handler) error {
137146
finalLogrLogger.Error(err, "Failed to start Go runtime metrics")
138147
}
139148

140-
mgr, err := ctrl.NewManager(cfg, manager.Options{
141-
Scheme: scheme,
142-
Metrics: server.Options{BindAddress: cli.Config.MetricsAddr},
143-
HealthProbeBindAddress: cli.Config.HealthAddr,
144-
Logger: finalLogrLogger,
145-
})
149+
mgr, err := ctrl.NewManager(cfg, setupManagerOptions(&cli.Config, finalLogrLogger))
146150
if err != nil {
147151
return fmt.Errorf("unable to start manager: %w", err)
148152
}
@@ -202,3 +206,31 @@ func getKubeConfig(kubeContext string) (*rest.Config, error) {
202206

203207
return cfg, nil
204208
}
209+
210+
func setupManagerOptions(config *ControllerConfig, logger logr.Logger) manager.Options {
211+
namespace := os.Getenv("RUNTIME_NAMESPACE")
212+
if namespace == "" {
213+
logger.Info("unable to determine runtime namespace, watching fallback namespace", "namespace", FallbackNamespace)
214+
namespace = FallbackNamespace
215+
}
216+
217+
kustomizationName := fmt.Sprintf("kube-manifests-oci-%s", namespace)
218+
ctrl.Log.Info("watching single namespace", "namespace", namespace, "name", kustomizationName)
219+
220+
return manager.Options{
221+
Scheme: scheme,
222+
Metrics: server.Options{BindAddress: config.MetricsAddr},
223+
HealthProbeBindAddress: config.HealthAddr,
224+
Cache: cache.Options{
225+
ByObject: map[client.Object]cache.ByObject{
226+
&kustomizev1.Kustomization{}: {
227+
Field: fields.OneTermEqualSelector("metadata.name", kustomizationName),
228+
Namespaces: map[string]cache.Config{
229+
namespace: {},
230+
},
231+
},
232+
},
233+
},
234+
Logger: logger,
235+
}
236+
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ require (
1010
github.com/go-slog/otelslog v0.3.0
1111
github.com/google/go-containerregistry v0.21.5
1212
github.com/google/uuid v1.6.0
13-
github.com/hashicorp/golang-lru/v2 v2.0.7
1413
github.com/lmittmann/tint v1.1.3
1514
github.com/mattn/go-isatty v0.0.22
1615
github.com/samber/slog-multi v1.8.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
7878
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7979
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 h1:HWRh5R2+9EifMyIHV7ZV+MIZqgz+PMpZ14Jynv3O2Zs=
8080
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0/go.mod h1:JfhWUomR1baixubs02l85lZYYOm7LV6om4ceouMv45c=
81-
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
82-
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
8381
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
8482
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
8583
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=

internal/tracker/cache.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

internal/tracker/tracker.go

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
kustomizev1beta2 "github.com/fluxcd/kustomize-controller/api/v1beta2"
1111
sourcev1 "github.com/fluxcd/source-controller/api/v1"
1212
"github.com/grafana/flux-commit-tracker/internal/oci"
13-
lru "github.com/hashicorp/golang-lru/v2/expirable"
1413
otel "go.opentelemetry.io/otel"
1514
"go.opentelemetry.io/otel/attribute"
1615
"go.opentelemetry.io/otel/codes"
@@ -33,10 +32,6 @@ const (
3332
MetricE2EExportTime = Prefix + ".e2e.export-time"
3433

3534
InstrumentationScope = "tracker"
36-
37-
ociRepositoryKind = "OCIRepository"
38-
kubeManifestsOCIRepositoryName = "kube-manifests-oci"
39-
o11yAppsPlatformOCIRepositoryName = "kube-manifests-oci-o11y-apps-platform"
4035
)
4136

4237
var (
@@ -74,10 +69,9 @@ func init() {
7469
// taken from deployment-tools commits to flux apply.
7570
type KustomizationReconciler struct {
7671
client.Client
77-
Scheme *runtime.Scheme
78-
Log *slog.Logger
79-
OCI oci.Resolver
80-
exporterInfoCache *lru.LRU[exporterInfoCacheKey, oci.ExporterInfo]
72+
Scheme *runtime.Scheme
73+
Log *slog.Logger
74+
OCI oci.Resolver
8175
}
8276

8377
type reconciledState struct {
@@ -88,19 +82,6 @@ type reconciledState struct {
8882
TimeApplied time.Time
8983
}
9084

91-
func isTrackedKustomization(k *kustomizev1.Kustomization) bool {
92-
if k.Spec.SourceRef.Kind != ociRepositoryKind {
93-
return false
94-
}
95-
96-
switch k.Spec.SourceRef.Name {
97-
case kubeManifestsOCIRepositoryName, o11yAppsPlatformOCIRepositoryName:
98-
return true
99-
default:
100-
return false
101-
}
102-
}
103-
10485
// getKustomization fetches the Kustomization object from the cluster. It
10586
// returns `nil, nil` if the object is not found.
10687
func (r *KustomizationReconciler) getKustomization(ctx context.Context, req ctrl.Request) (*kustomizev1.Kustomization, error) {
@@ -217,15 +198,6 @@ func (r *KustomizationReconciler) fetchExporterInfoFromOCI(ctx context.Context,
217198
return oci.ExporterInfo{}, fmt.Errorf("failed to resolve OCIRepository URL: %w", err)
218199
}
219200

220-
cacheKey := exporterInfoCacheKey{
221-
repositoryURL: repositoryURL,
222-
revision: appliedRevision,
223-
}
224-
if info, ok := r.exporterInfoCache.Get(cacheKey); ok {
225-
log.DebugContext(ctx, "reusing cached exporter-info", "oci.repository_url", repositoryURL, "kustomization.revision", appliedRevision)
226-
return info, nil
227-
}
228-
229201
timeoutCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
230202
defer cancel()
231203

@@ -234,7 +206,6 @@ func (r *KustomizationReconciler) fetchExporterInfoFromOCI(ctx context.Context,
234206
return oci.ExporterInfo{}, fmt.Errorf("failed to fetch exporter info from OCI layer: %w", err)
235207
}
236208

237-
r.exporterInfoCache.Add(cacheKey, info)
238209
return info, nil
239210
}
240211

@@ -281,12 +252,6 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
281252
}
282253
span.SetAttributes(attribute.String("k8s.resource.uid", string(kustomization.UID)))
283254

284-
if !isTrackedKustomization(kustomization) {
285-
span.SetStatus(codes.Ok, "Kustomization is outside tracker scope")
286-
log.DebugContext(ctx, "ignoring unsupported kustomization")
287-
return ctrl.Result{}, nil
288-
}
289-
290255
// 2. Extract Data
291256
state, err := extractReconciledState(ctx, log, kustomization)
292257
if err != nil {
@@ -340,7 +305,6 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
340305
// SetupWithManager sets up the controller with the Manager
341306
func (r *KustomizationReconciler) SetupWithManager(mgr ctrl.Manager) error {
342307
r.Scheme = mgr.GetScheme()
343-
r.exporterInfoCache = newExporterInfoCache()
344308
return ctrl.NewControllerManagedBy(mgr).
345309
For(&kustomizev1.Kustomization{}).
346310
WithEventFilter(kustomizationPredicate{}).
@@ -367,10 +331,6 @@ func (p kustomizationPredicate) Update(e event.UpdateEvent) bool {
367331
return false
368332
}
369333

370-
if !isTrackedKustomization(newKustomization) {
371-
return false
372-
}
373-
374334
// Reconcile only if LastAppliedRevision changes and the new revision is not empty
375335
newRevision := newKustomization.Status.LastAppliedRevision
376336
if oldKustomization.Status.LastAppliedRevision != newRevision && newRevision != "" {

0 commit comments

Comments
 (0)