Skip to content

feat(composer): use cloud monitoring metrics for historical GKE cluster discovery in composer - #879

Merged
kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-nlzourvymlsw
Aug 12, 2026
Merged

kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-nlzourvymlsw

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 12, 2026

Copy link
Copy Markdown
Member

Problem Statement

Previously, ComposerEnvironmentClusterFinder invoked the live GKE API (ContainerClusterManagerClient.ListClusters) and searched for clusters whose goog-composer-environment resource label matched the environment name. Because this live listing API only returns currently active GKE clusters, KHI could not discover or autocomplete deleted or historical GKE clusters associated with a Cloud Composer environment.

Solution & Key Changes

  1. Switch to Cloud Monitoring Metric Queries (pkg/task/inspection/googlecloudclustercomposer/contract/environmentclusterfinder.go):
    • Updated the ComposerEnvironmentClusterFinder interface signature to:
      type ComposerEnvironmentClusterFinder interface {
          GetGKEClusterNames(ctx context.Context, projectID, location, environment string, startTime, endTime time.Time) ([]string, error)
      }
    • Replaced live ListClusters API calls with Cloud Monitoring resource label queries (metric.type="kubernetes.io/container/uptime" AND resource.type="k8s_container").
    • Added pattern matching logic (filterAndMatchComposerGKEClusterNames) that identifies historical GKE clusters by checking against the Cloud Composer naming convention: <location>-<environment>-<hash (8 chars)>-gke.
    • By stripping the -gke suffix and 8-character hash and verifying that the remaining prefix matches <location>-<environment>, KHI discovers all matching past and present clusters.
  2. Update Autocomplete Task & UX (pkg/task/inspection/googlecloudclustercomposer/impl/autocompletecomposerclusternames_task.go):
    • Added dependencies on InputStartTimeTaskID and InputEndTimeTaskID so that the cache digest automatically updates when the inspection time window changes.
    • Added a validation check that returns a helpful hint ("Cluster names are suggested after the location is provided.") when location is empty.
    • Updated the execution function to iterate over all discovered cluster names and include multiple cluster identities in AutocompleteResult.Values.
  3. Dead Code Cleanup (pkg/api/googlecloud/clientfactory.go):
    • Removed ContainerClusterManagerClient (GKE ClusterManager API client) method, cache maps, configuration fields, and the cloud.google.com/go/container/apiv1 import from ClientFactory, as it is no longer referenced anywhere in the repository.
  4. Unit Tests:
    • Added comprehensive table-driven tests in pkg/task/inspection/googlecloudclustercomposer/contract/environmentclusterfinder_test.go verifying cluster name extraction, hash validation, sorting, and deduplication.
    • Updated unit tests in pkg/task/inspection/googlecloudclustercomposer/impl/autocompletecomposerclusternames_task_test.go to test the new interface signature, empty location hint, and multiple cluster autocompletion.

Verification

  • Backend Build: make build-go completed cleanly with zero errors.
  • Unit & Integration Tests: go test ./pkg/task/inspection/googlecloudclustercomposer/... and full make test-go passed cleanly.
  • Formatting & Linting: make pre-commit passed with 0 linting or formatting errors across the project.
…er discovery in composer

Replace live ListClusters API calls in ComposerEnvironmentClusterFinder with Cloud Monitoring metric queries to discover both current and deleted/past GKE clusters for Cloud Composer environments.

- Update ComposerEnvironmentClusterFinder interface method to GetGKEClusterNames supporting time range and location parameters.
- Query kubernetes.io/container/uptime metrics on k8s_container to extract cluster names matching <location>-<environment>-<hash>-gke naming pattern.
- Update AutocompleteComposerClusterNamesTask to include start/end time dependencies, return empty location hint, and return all matching past/present GKE cluster identities.
- Remove unused ContainerClusterManagerClient and associated cache fields from ClientFactory.
@kyasbal kyasbal added the type:feature New feature or request label Aug 12, 2026

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the Cloud Composer GKE cluster finding logic to query cluster names from monitoring metrics instead of listing GKE clusters directly, allowing it to return multiple matching clusters. It also updates the autocomplete task to incorporate start and end times. The review feedback highlights a bug in filterAndMatchComposerGKEClusterNames when the location parameter is empty, suggests adding a corresponding test case, and recommends replacing time.Now() with a fixed time in tests to prevent non-determinism.

…er discovery in composer

Replace live ListClusters API calls in ComposerEnvironmentClusterFinder with Cloud Monitoring metric queries to discover both current and deleted/past GKE clusters for Cloud Composer environments.

- Update ComposerEnvironmentClusterFinder interface method to GetGKEClusterNames supporting time range and location parameters.
- Query kubernetes.io/container/uptime metrics on k8s_container to extract cluster names matching <location>-<candidate>-<hash>-gke pattern.
- Extract extractEnvironmentPrefixCandidate helper function using strings.LastIndex to extract candidate environment name prefixes from GKE cluster names.
- Return ErrEnvironmentClusterNotFound when no cluster candidates are found in GetGKEClusterNames.
- Update AutocompleteComposerClusterNamesTask to include start/end time dependencies, return empty location hint, handle ErrEnvironmentClusterNotFound, and return all matching past/present GKE cluster identities.
- Remove unused ContainerClusterManagerClient and associated cache fields from ClientFactory.
@kyasbal
kyasbal marked this pull request as ready for review August 12, 2026 02:19
@kyasbal
kyasbal merged commit 06ca96d into GoogleCloudPlatform:main Aug 12, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-nlzourvymlsw branch August 12, 2026 02:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:feature New feature or request

1 participant