Skip to content

K8SPG-1083 improve platform detection to reduce unknown telemetry values - #1655

Merged
hors merged 12 commits into
mainfrom
detect-more-platforms
Jul 24, 2026
Merged

K8SPG-1083 improve platform detection to reduce unknown telemetry values#1655
hors merged 12 commits into
mainfrom
detect-more-platforms

Conversation

@hors

@hors hors commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Replace CRD-based detection (which relied on optional addons being installed) with discovery API group checks and API server URL patterns.

Add detection for AKS, DOKS, OKE, ACK, NKP, Platform9, Tanzu, and Rancher alongside the existing GKE, EKS, and OpenShift detections.

CHANGE DESCRIPTION

Problem:
Short explanation of the problem.

Cause:
Short explanation of the root cause of the issue if applicable.

Solution:
Short explanation of the solution we are providing with this PR.

CHECKLIST

Jira

  • Is the Jira ticket created and referenced properly?
  • Does the Jira ticket have the proper statuses for documentation (Needs Doc) and QA (Needs QA)?
  • Does the Jira ticket link to the proper milestone (Fix Version field)?

Tests

  • Is an E2E test/test case added for the new feature/change?
  • Are unit tests added where appropriate?

Config/Logging/Testability

  • Are all needed new/changed options added to default YAML files?
  • Are all needed new/changed options added to the Helm Chart?
  • Did we add proper logging messages for operator actions?
  • Did we ensure compatibility with the previous version or cluster upgrade process?
  • Does the change support oldest and newest supported PG version?
  • Does the change support oldest and newest supported Kubernetes version?

Replace CRD-based detection (which relied on optional addons being
installed) with discovery API group checks and API server URL patterns.

Add detection for AKS, DOKS, OKE, ACK, NKP, Platform9, Tanzu, and
Rancher alongside the existing GKE, EKS, and OpenShift detections.
Copilot AI review requested due to automatic review settings June 26, 2026 11:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the operator’s platform/managed-Kubernetes detection used for telemetry, moving away from CRD-based heuristics toward API discovery group checks and API server host pattern checks to reduce “unknown” platform values.

Changes:

  • Added a hasAPIGroup helper that detects platforms by checking for the presence of API groups via the discovery API.
  • Updated existing platform detection (notably GKE/EKS) to use API groups and API server host patterns rather than optional addon CRDs.
  • Added detection and telemetry labels for AKS, DOKS, OKE, ACK, NKP, Platform9, Tanzu, and Rancher.

Comment on lines +361 to +370
// hasAPIGroup returns true if the cluster exposes the given API group name.
// Uses the discovery API which is available to all authenticated users with no extra RBAC.
func hasAPIGroup(ctx context.Context, cfg *rest.Config, groupName string) bool {
client, err := discovery.NewDiscoveryClientForConfig(cfg)
assertNoError(err)

if err != nil {
return false
}
groups, err := client.ServerGroups()
if err != nil {
assertNoError(err)
return false
Comment thread cmd/postgres-operator/main.go Outdated
Comment on lines +389 to +396
func isEKS(ctx context.Context, cfg *rest.Config) bool {
log := logging.FromContext(ctx)
// EKS API server hostnames always end with .eks.amazonaws.com.
if strings.Contains(cfg.Host, ".eks.amazonaws.com") {
logging.FromContext(ctx).Info("detected EKS environment")
return true
}
return false
}
Comment thread cmd/postgres-operator/main.go Outdated
Comment on lines +470 to +493
case isTanzu(ctx, cfg):
return "tanzu"
case isRancher(ctx, cfg):
return "rancher"
Copilot AI review requested due to automatic review settings June 26, 2026 13:53
@hors
hors force-pushed the detect-more-platforms branch from 44406c3 to 6cbdb1e Compare June 26, 2026 13:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.

Comment thread cmd/postgres-operator/main.go Outdated
Comment thread cmd/postgres-operator/main.go Outdated
Comment on lines +365 to +369
client, err := discovery.NewDiscoveryClientForConfig(cfg)
assertNoError(err)

if err != nil {
log.V(1).Info("platform detection: could not create discovery client", "error", err.Error())
return false
}
Comment thread cmd/postgres-operator/main.go
Comment thread cmd/postgres-operator/main.go Outdated
Comment thread cmd/postgres-operator/main.go Outdated
@hors
hors force-pushed the detect-more-platforms branch from 6cbdb1e to ef39113 Compare June 29, 2026 10:14
@hors hors changed the title Improve platform detection to reduce unknown telemetry values K8SPG-1083 improve platform detection to reduce unknown telemetry values Jul 8, 2026
@hors
hors marked this pull request as ready for review July 9, 2026 15:52
@hors
hors requested review from Copilot and gkech July 9, 2026 15:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 4 comments.

Comment thread cmd/postgres-operator/main.go Outdated
Comment on lines 373 to 377
groups, err := client.ServerGroups()
if err != nil {
assertNoError(err)
log.V(1).Info("platform detection: could not list API groups", "error", err.Error())
return false
}
Comment thread cmd/postgres-operator/main.go Outdated
if err != nil {
assertNoError(err)
if hasAPIGroup(ctx, cfg, "security.openshift.io") {
logging.FromContext(ctx).Info("detected Openshift environment")
Comment on lines +456 to +461
for _, probe := range platformProbes {
if probe.detect(ctx, cfg) {
logging.FromContext(ctx).Info("detected " + probe.label + " environment")
return probe.name
}
}
Comment thread cmd/postgres-operator/main.go Outdated
Comment on lines +433 to +435
host := strings.TrimPrefix(cfg.Host, "https://")
host = strings.TrimPrefix(host, "http://")
netConn, err := (&tls.Dialer{Config: tlsCfg}).DialContext(ctx, "tcp", host)
Comment thread cmd/postgres-operator/main.go Outdated
@hors
hors requested a review from egegunes July 18, 2026 11:00
egegunes
egegunes previously approved these changes Jul 20, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (4)

cmd/postgres-operator/main.go:422

  • Problem: platformProbes logs "Openshift", but the proper product name and existing codebase spelling is "OpenShift".
    Why it matters: Keeps logs/telemetry consistent and avoids introducing a new spelling variant.
    Fix: Update the label capitalization.
var platformProbes = []platformProbe{
	{name: "openshift", label: "Openshift", apiGroups: []string{"security.openshift.io"}},
	{name: "gke", label: "GKE", apiGroups: []string{"networking.gke.io"}},

cmd/postgres-operator/main.go:466

  • Problem: New platform detection logic is not covered by unit tests, while this package already has tests (e.g., main_test.go).
    Why it matters: Detection regressions will silently change telemetry and OpenShift-specific behavior.
    Fix: Add table-driven unit tests that validate probe ordering/host-based matches and that error paths fall back to "unknown" without panicking.
func detectPlatform(ctx context.Context, cfg *rest.Config) string {
	for _, probe := range platformProbes {
		if probe.detect(ctx, cfg) {
			logging.FromContext(ctx).Info("detected " + probe.label + " environment")
			return probe.name

cmd/postgres-operator/main.go:377

  • Problem: hasAPIGroup creates a new discovery client and calls ServerGroups() on every invocation; detectPlatform can trigger many discovery round-trips during startup.
    Why it matters: Excessive discovery calls can slow startup and may fail on clusters where discovery is rate-limited.
    Fix: Cache the discovery results for the duration of a single detectPlatform call (e.g., build a map[string]struct{} from one ServerGroups() call and check membership for all probes/groups).
// hasAPIGroup returns true if the cluster exposes the given API group name.
// Uses the discovery API; note that hardened clusters may restrict discovery access.
func hasAPIGroup(ctx context.Context, cfg *rest.Config, groupName string) bool {
	log := logging.FromContext(ctx)
	client, err := discovery.NewDiscoveryClientForConfig(cfg)

cmd/postgres-operator/main.go:447

  • Problem: detectAKS dials the API server with the process context (no timeout) and assumes cfg.Host already includes a port.
    Why it matters: A stalled TCP/TLS dial can block operator startup; and a host without an explicit port will make DialContext fail.
    Fix: Add a short timeout and normalize the dial address to include a default port when missing.
	host := strings.TrimPrefix(cfg.Host, "https://")
	host = strings.TrimPrefix(host, "http://")
	netConn, err := (&tls.Dialer{Config: tlsCfg}).DialContext(ctx, "tcp", host)
	if err != nil {
		logging.FromContext(ctx).V(1).Info("platform detection: could not dial API server", "error", err.Error())

Comment thread cmd/postgres-operator/main.go
Comment thread cmd/postgres-operator/main.go
func isGKE(ctx context.Context, cfg *rest.Config) bool {
// hasAPIGroup returns true if the cluster exposes the given API group name.
// Uses the discovery API; note that hardened clusters may restrict discovery access.
func hasAPIGroup(ctx context.Context, cfg *rest.Config, groupName string) bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a helper utility GroupVersionKindExists here -#1655

I'd suggest we move this to a GroupVersionExists in the same package for consistency

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{name: "openshift", label: "Openshift", apiGroups: []string{"security.openshift.io"}},
{name: "gke", label: "GKE", apiGroups: []string{"networking.gke.io"}},
// crd.k8s.amazonaws.com (VPC CNI) and metrics.eks.amazonaws.com are independent EKS signals.
{name: "eks", label: "EKS", apiGroups: []string{"crd.k8s.amazonaws.com", "metrics.eks.amazonaws.com"}},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if rancher run on AWS, what's the api group returned?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a more eks specific signal?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The most reliable truly-EKS signal without extra RBAC requires both groups

  return hasAPIGroup(ctx, cfg, "crd.k8s.amazonaws.com") &&
          hasAPIGroup(ctx, cfg, "metrics.eks.amazonaws.com")
}},

The problem is that clusters without the metrics addon enabled would fall back to unknown :(

Comment thread cmd/postgres-operator/main.go Outdated
}
host := strings.TrimPrefix(cfg.Host, "https://")
host = strings.TrimPrefix(host, "http://")
netConn, err := (&tls.Dialer{Config: tlsCfg}).DialContext(ctx, "tcp", host)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a timeout here? Raw TLS connection potentially can hang which would block the operator startup

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return false
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[gofmt] reported by reviewdog 🐶

Suggested change

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (2)

cmd/postgres-operator/main.go:149

  • The upgrade-check scheduler is now always told openshift=false, which changes the telemetry/header generation behavior compared to the previous OpenShift detection when upgrade checking is enabled.
		assertNoError(upgradecheck.ManagedScheduler(mgr,
			false, os.Getenv("CHECK_FOR_UPGRADES_URL"), versionString, nil))

cmd/postgres-operator/main.go:441

  • tls.Dialer.DialContext requires an address in host:port form; kubeconfigs commonly set cfg.Host without an explicit port (e.g. https://xxx.azmk8s.io). In that case this will fail with "missing port in address" and AKS detection will always return false.
	host := strings.TrimPrefix(cfg.Host, "https://")
	host = strings.TrimPrefix(host, "http://")
	dialCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
	defer cancel()
	netConn, err := (&tls.Dialer{Config: tlsCfg}).DialContext(dialCtx, "tcp", host)

client, err := discovery.NewDiscoveryClientForConfig(cfg)
assertNoError(err)
var platformProbes = []platformProbe{
{name: "openshift", label: "Openshift", apiGroups: []string{"security.openshift.io"}},
@hors
hors merged commit d5cb681 into main Jul 24, 2026
17 of 18 checks passed
@hors
hors deleted the detect-more-platforms branch July 24, 2026 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants