Skip to content

Commit

Permalink
FIXUP: add a runner -- log success
Browse files Browse the repository at this point in the history
  • Loading branch information
cbandy committed Nov 21, 2024
1 parent f9b7af8 commit 0772891
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/postgres-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
15 changes: 11 additions & 4 deletions internal/kubernetes/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand All @@ -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():
Expand Down
3 changes: 2 additions & 1 deletion internal/kubernetes/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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"}))
Expand Down
2 changes: 1 addition & 1 deletion internal/upgradecheck/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 0772891

Please sign in to comment.