From 077289117e9fd05a9b111506616f495825ce274a Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Thu, 21 Nov 2024 17:00:52 -0600 Subject: [PATCH] FIXUP: add a runner -- log success --- cmd/postgres-operator/main.go | 2 +- internal/kubernetes/discovery.go | 15 +++++++++++---- internal/kubernetes/discovery_test.go | 3 ++- internal/upgradecheck/header_test.go | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/postgres-operator/main.go b/cmd/postgres-operator/main.go index 24d022087..1f503962a 100644 --- a/cmd/postgres-operator/main.go +++ b/cmd/postgres-operator/main.go @@ -148,7 +148,7 @@ func main() { k8s, err := kubernetes.NewDiscoveryRunner(cfg) assertNoError(err) - assertNoError(k8s.Read()) + assertNoError(k8s.Read(ctx)) log.Info("Connected to Kubernetes", "api", k8s.Version().String(), "openshift", k8s.IsOpenShift()) diff --git a/internal/kubernetes/discovery.go b/internal/kubernetes/discovery.go index e47f62849..ab188c5f6 100644 --- a/internal/kubernetes/discovery.go +++ b/internal/kubernetes/discovery.go @@ -24,6 +24,8 @@ type Version = version.Info // DiscoveryRunner implements [APIs] by reading from a Kubernetes API client. // Its methods are safe to call concurrently. type DiscoveryRunner struct { + // NOTE(tracing): The methods of [discovery.DiscoveryClient] do not take + // a Context so their API calls won't have a parent span. Client interface { ServerGroups() (*metav1.APIGroupList, error) ServerResourcesForGroupVersion(string) (*metav1.APIResourceList, error) @@ -100,11 +102,11 @@ func (r *DiscoveryRunner) IsOpenShift() bool { func (r *DiscoveryRunner) NeedLeaderElection() bool { return false } // Read fetches available APIs from Kubernetes. -func (r *DiscoveryRunner) Read() error { - return errors.Join(r.readAPIs(), r.readVersion()) +func (r *DiscoveryRunner) Read(ctx context.Context) error { + return errors.Join(r.readAPIs(ctx), r.readVersion()) } -func (r *DiscoveryRunner) readAPIs() error { +func (r *DiscoveryRunner) readAPIs(ctx context.Context) error { // Build an index of the APIs we want to know about. wantAPIs := make(map[string]map[string]sets.Set[string]) for _, want := range r.relevant { @@ -160,6 +162,10 @@ func (r *DiscoveryRunner) readAPIs() error { r.have.APISet = haveAPIs r.have.Unlock() + r.have.RLock() + defer r.have.RUnlock() + logging.FromContext(ctx).V(1).Info("Found APIs", "index_size", r.have.APISet.Len()) + return nil } @@ -181,11 +187,12 @@ func (r *DiscoveryRunner) Start(ctx context.Context) error { defer ticker.Stop() log := logging.FromContext(ctx).WithValues("controller", "kubernetes") + ctx = logging.NewContext(ctx, log) for { select { case <-ticker.C: - if err := r.Read(); err != nil { + if err := r.Read(ctx); err != nil { log.Error(err, "Unable to detect Kubernetes APIs") } case <-ctx.Done(): diff --git a/internal/kubernetes/discovery_test.go b/internal/kubernetes/discovery_test.go index ea59639e5..a6f5a26df 100644 --- a/internal/kubernetes/discovery_test.go +++ b/internal/kubernetes/discovery_test.go @@ -24,6 +24,7 @@ func TestDiscoveryRunnerInterfaces(t *testing.T) { } func TestDiscoveryRunnerAPIs(t *testing.T) { + ctx := context.Background() cfg, _ := require.Kubernetes2(t) require.ParallelCapacity(t, 0) @@ -32,7 +33,7 @@ func TestDiscoveryRunnerAPIs(t *testing.T) { // Search for an API that should always exist. runner.relevant = append(runner.relevant, API{Kind: "Pod"}) - assert.NilError(t, runner.readAPIs()) + assert.NilError(t, runner.readAPIs(ctx)) assert.Assert(t, runner.Has(API{Kind: "Pod"})) assert.Assert(t, runner.HasAll(API{Kind: "Pod"}, API{Kind: "Secret"})) diff --git a/internal/upgradecheck/header_test.go b/internal/upgradecheck/header_test.go index 7bc0125b5..39d3a9abd 100644 --- a/internal/upgradecheck/header_test.go +++ b/internal/upgradecheck/header_test.go @@ -33,7 +33,7 @@ func TestGenerateHeader(t *testing.T) { discovery, err := kubernetes.NewDiscoveryRunner(cfg) assert.NilError(t, err) - assert.NilError(t, discovery.Read()) + assert.NilError(t, discovery.Read(ctx)) ctx = kubernetes.NewAPIContext(ctx, discovery) t.Setenv("PGO_INSTALLER", "test")