Skip to content

Commit 80e5eb3

Browse files
committed
FIXUP: add a runner -- log success
1 parent f9b7af8 commit 80e5eb3

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

cmd/postgres-operator/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func main() {
148148

149149
k8s, err := kubernetes.NewDiscoveryRunner(cfg)
150150
assertNoError(err)
151-
assertNoError(k8s.Read())
151+
assertNoError(k8s.Read(ctx))
152152

153153
log.Info("Connected to Kubernetes", "api", k8s.Version().String(), "openshift", k8s.IsOpenShift())
154154

internal/kubernetes/discovery.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type Version = version.Info
2424
// DiscoveryRunner implements [APIs] by reading from a Kubernetes API client.
2525
// Its methods are safe to call concurrently.
2626
type DiscoveryRunner struct {
27+
// NOTE(tracing): The methods of [discovery.DiscoveryClient] do not take
28+
// a Context so their API calls won't have a parent span.
2729
Client interface {
2830
ServerGroups() (*metav1.APIGroupList, error)
2931
ServerResourcesForGroupVersion(string) (*metav1.APIResourceList, error)
@@ -100,11 +102,11 @@ func (r *DiscoveryRunner) IsOpenShift() bool {
100102
func (r *DiscoveryRunner) NeedLeaderElection() bool { return false }
101103

102104
// Read fetches available APIs from Kubernetes.
103-
func (r *DiscoveryRunner) Read() error {
104-
return errors.Join(r.readAPIs(), r.readVersion())
105+
func (r *DiscoveryRunner) Read(ctx context.Context) error {
106+
return errors.Join(r.readAPIs(ctx), r.readVersion())
105107
}
106108

107-
func (r *DiscoveryRunner) readAPIs() error {
109+
func (r *DiscoveryRunner) readAPIs(ctx context.Context) error {
108110
// Build an index of the APIs we want to know about.
109111
wantAPIs := make(map[string]map[string]sets.Set[string])
110112
for _, want := range r.relevant {
@@ -160,6 +162,10 @@ func (r *DiscoveryRunner) readAPIs() error {
160162
r.have.APISet = haveAPIs
161163
r.have.Unlock()
162164

165+
r.have.RLock()
166+
defer r.have.RUnlock()
167+
logging.FromContext(ctx).V(1).Info("Found APIs", "index_size", r.have.APISet.Len())
168+
163169
return nil
164170
}
165171

@@ -181,11 +187,12 @@ func (r *DiscoveryRunner) Start(ctx context.Context) error {
181187
defer ticker.Stop()
182188

183189
log := logging.FromContext(ctx).WithValues("controller", "kubernetes")
190+
ctx = logging.NewContext(ctx, log)
184191

185192
for {
186193
select {
187194
case <-ticker.C:
188-
if err := r.Read(); err != nil {
195+
if err := r.Read(ctx); err != nil {
189196
log.Error(err, "Unable to detect Kubernetes APIs")
190197
}
191198
case <-ctx.Done():

internal/kubernetes/discovery_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestDiscoveryRunnerInterfaces(t *testing.T) {
2424
}
2525

2626
func TestDiscoveryRunnerAPIs(t *testing.T) {
27+
ctx := context.Background()
2728
cfg, _ := require.Kubernetes2(t)
2829
require.ParallelCapacity(t, 0)
2930

@@ -32,7 +33,7 @@ func TestDiscoveryRunnerAPIs(t *testing.T) {
3233

3334
// Search for an API that should always exist.
3435
runner.relevant = append(runner.relevant, API{Kind: "Pod"})
35-
assert.NilError(t, runner.readAPIs())
36+
assert.NilError(t, runner.readAPIs(ctx))
3637

3738
assert.Assert(t, runner.Has(API{Kind: "Pod"}))
3839
assert.Assert(t, runner.HasAll(API{Kind: "Pod"}, API{Kind: "Secret"}))

0 commit comments

Comments
 (0)