-
Notifications
You must be signed in to change notification settings - Fork 567
GCP-410: feat(gcp): add HCCO credential propagation for GCP image registry #7896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 2 commits into
openshift:main
from
cblecker:feat/gcp-410-hcco-image-registry-creds
Apr 30, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
control-plane-operator/hostedclusterconfigoperator/controllers/resources/gcp/gcp.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package gcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1" | ||
| "github.com/openshift/hypershift/control-plane-operator/hostedclusterconfigoperator/controllers/resources/manifests" | ||
| "github.com/openshift/hypershift/support/capabilities" | ||
| "github.com/openshift/hypershift/support/gcputil" | ||
| "github.com/openshift/hypershift/support/upsert" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
|
|
||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| ) | ||
|
|
||
| // gcpCredentialConfig defines configuration for a single credential secret. | ||
| type gcpCredentialConfig struct { | ||
| manifestFunc func() *corev1.Secret | ||
| serviceAccountEmail string | ||
| capabilityChecker func(*hyperv1.Capabilities) bool | ||
| errorContext string | ||
| } | ||
|
|
||
| // SetupOperandCredentials ensures that the required GCP operand credential secrets are created or updated | ||
| // for the guest cluster's components based on the HostedControlPlane configuration. | ||
| func SetupOperandCredentials( | ||
| ctx context.Context, | ||
| c client.Client, | ||
| upsertProvider upsert.CreateOrUpdateProvider, | ||
| hcp *hyperv1.HostedControlPlane, | ||
| ) []error { | ||
| configs := []gcpCredentialConfig{ | ||
| { | ||
| manifestFunc: manifests.GCPImageRegistryCloudCredsSecret, | ||
| serviceAccountEmail: string(hcp.Spec.Platform.GCP.WorkloadIdentity.ServiceAccountsEmails.ImageRegistry), | ||
| capabilityChecker: capabilities.IsImageRegistryCapabilityEnabled, | ||
| errorContext: "guest cluster image-registry credential", | ||
| }, | ||
| } | ||
| return reconcileGCPCredentials(ctx, c, upsertProvider, hcp, configs) | ||
| } | ||
|
|
||
| func reconcileGCPCredentials( | ||
| ctx context.Context, | ||
| c client.Client, | ||
| upsertProvider upsert.CreateOrUpdateProvider, | ||
| hcp *hyperv1.HostedControlPlane, | ||
| configs []gcpCredentialConfig, | ||
| ) []error { | ||
| var errs []error | ||
|
|
||
| for _, cfg := range configs { | ||
| if cfg.capabilityChecker != nil && !cfg.capabilityChecker(hcp.Spec.Capabilities) { | ||
| continue | ||
| } | ||
|
|
||
| secret := cfg.manifestFunc() | ||
|
|
||
|
cblecker marked this conversation as resolved.
|
||
| ns := &corev1.Namespace{} | ||
| if err := c.Get(ctx, client.ObjectKey{Name: secret.Namespace}, ns); err != nil { | ||
| if apierrors.IsNotFound(err) { | ||
| ctrl.LoggerFrom(ctx).Info("WARNING: cannot sync cloud credential secret because namespace does not exist", | ||
| "secret", client.ObjectKeyFromObject(secret), | ||
| "context", cfg.errorContext) | ||
| continue | ||
| } | ||
| errs = append(errs, fmt.Errorf("failed to get namespace %s for %s: %w", secret.Namespace, cfg.errorContext, err)) | ||
| continue | ||
| } | ||
|
|
||
| credentialJSON, err := gcputil.BuildWorkloadIdentityCredentials(hcp.Spec.Platform.GCP.WorkloadIdentity, cfg.serviceAccountEmail) | ||
| if err != nil { | ||
| errs = append(errs, fmt.Errorf("failed to build %s: %w", cfg.errorContext, err)) | ||
| continue | ||
| } | ||
|
|
||
| if _, err := upsertProvider.CreateOrUpdate(ctx, c, secret, func() error { | ||
| secret.Data = map[string][]byte{ | ||
| "service_account.json": []byte(credentialJSON), | ||
| } | ||
| secret.Type = corev1.SecretTypeOpaque | ||
| return nil | ||
| }); err != nil { | ||
| errs = append(errs, fmt.Errorf("failed to reconcile %s: %w", cfg.errorContext, err)) | ||
| } | ||
| } | ||
|
|
||
| return errs | ||
| } | ||
121 changes: 121 additions & 0 deletions
121
control-plane-operator/hostedclusterconfigoperator/controllers/resources/gcp/gcp_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package gcp | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/gomega" | ||
|
|
||
| hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1" | ||
| "github.com/openshift/hypershift/support/api" | ||
| "github.com/openshift/hypershift/support/gcputil" | ||
| "github.com/openshift/hypershift/support/upsert" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| ) | ||
|
|
||
| const ( | ||
| testImageRegistryGSA = "image-registry@test-project.iam.gserviceaccount.com" | ||
| testProjectNumber = "123456789012" | ||
| testPoolID = "test-pool" | ||
| testProviderID = "test-provider" | ||
| ) | ||
|
|
||
| func makeHCP() *hyperv1.HostedControlPlane { | ||
| return &hyperv1.HostedControlPlane{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "hcp", | ||
| Namespace: "ns", | ||
| }, | ||
| Spec: hyperv1.HostedControlPlaneSpec{ | ||
| Platform: hyperv1.PlatformSpec{ | ||
| Type: hyperv1.GCPPlatform, | ||
| GCP: &hyperv1.GCPPlatformSpec{ | ||
| WorkloadIdentity: hyperv1.GCPWorkloadIdentityConfig{ | ||
| ProjectNumber: testProjectNumber, | ||
| PoolID: testPoolID, | ||
| ProviderID: testProviderID, | ||
| ServiceAccountsEmails: hyperv1.GCPServiceAccountsEmails{ | ||
| ImageRegistry: testImageRegistryGSA, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func TestSetupOperandCredentials(t *testing.T) { | ||
| t.Parallel() | ||
| tests := []struct { | ||
| name string | ||
| disableImageRegistry bool | ||
| createNamespace bool | ||
| expectImageRegistrySec bool | ||
| }{ | ||
| { | ||
| name: "When image registry capability is enabled it should create the credential secret", | ||
| createNamespace: true, | ||
| expectImageRegistrySec: true, | ||
| }, | ||
| { | ||
| name: "When image registry capability is disabled it should skip the credential secret", | ||
| disableImageRegistry: true, | ||
| createNamespace: true, | ||
| }, | ||
| { | ||
| name: "When target namespace does not exist it should skip without error", | ||
| createNamespace: false, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| g := NewWithT(t) | ||
| c := fake.NewClientBuilder().WithScheme(api.Scheme).Build() | ||
|
|
||
| if tc.createNamespace { | ||
| ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "openshift-image-registry"}} | ||
| g.Expect(c.Create(t.Context(), ns)).To(Succeed()) | ||
| } | ||
|
|
||
| hcp := makeHCP() | ||
| if tc.disableImageRegistry { | ||
| hcp.Spec.Capabilities = &hyperv1.Capabilities{ | ||
| Disabled: []hyperv1.OptionalCapability{hyperv1.ImageRegistryCapability}, | ||
| } | ||
| } | ||
|
|
||
| errs := SetupOperandCredentials(t.Context(), c, upsert.New(false), hcp) | ||
| g.Expect(errs).To(BeEmpty()) | ||
|
|
||
| key := client.ObjectKey{Namespace: "openshift-image-registry", Name: "installer-cloud-credentials"} | ||
| var sec corev1.Secret | ||
| err := c.Get(t.Context(), key, &sec) | ||
|
|
||
| if tc.expectImageRegistrySec { | ||
| g.Expect(err).ToNot(HaveOccurred()) | ||
| g.Expect(sec.Data).To(HaveKey("service_account.json")) | ||
| g.Expect(sec.Type).To(Equal(corev1.SecretTypeOpaque)) | ||
|
|
||
| // Validate the WIF credential JSON structure | ||
| var cred gcputil.ExternalAccountCredential | ||
| g.Expect(json.Unmarshal(sec.Data["service_account.json"], &cred)).To(Succeed()) | ||
| g.Expect(cred.Type).To(Equal("external_account")) | ||
| g.Expect(cred.Audience).To(ContainSubstring(testProjectNumber)) | ||
| g.Expect(cred.Audience).To(ContainSubstring(testPoolID)) | ||
| g.Expect(cred.Audience).To(ContainSubstring(testProviderID)) | ||
| g.Expect(cred.ServiceAccountImpersonationURL).To(ContainSubstring(testImageRegistryGSA)) | ||
| g.Expect(cred.CredentialSource.File).To(Equal("/var/run/secrets/openshift/serviceaccount/token")) | ||
| } else { | ||
| g.Expect(apierrors.IsNotFound(err)).To(BeTrue(), "expected image registry credentials secret to be absent") | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.