Skip to content

Commit 64a2ac9

Browse files
authored
Implement export for CertificateManagerCertificateMap (#10631)
This PR implements export support for `CertificateManagerCertificateMap` direct controller. ### Changes: - Implemented `AdapterForURL` in `certificatemap_controller.go` to parse the external GCP URL into a `CertificateManagerCertificateMapIdentity`. - Updated `Export` in `certificatemap_controller.go` to set `ResourceID` in Spec, metadata name to short name, and set standard metadata (project-id, labels). - Registered `CertificateManagerCertificateMap` in `tests/e2e/export.go` using `resolveCAISURI`. - Generated and verified `_exported.yaml` for the `certificatemanagercertificatemap` basic test fixture. Fixes #10624
2 parents f9f0427 + a1cf7b6 commit 64a2ac9

4 files changed

Lines changed: 65 additions & 3 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# CertificateManagerCertificateMap Export Journal
2+
3+
## Observations & Implementation Details
4+
5+
1. **Standard Metadata & Annotations**:
6+
- `CertificateManagerCertificateMap` maps to `google.cloud.certificatemanager.v1.CertificateMap`.
7+
- Instead of setting the `cnrm.cloud.google.com/project-id` annotation (which uses `export.SetProjectID`), resources with `spec.projectRef` in their KRM schema should have `spec.projectRef.external` set to the GCP project ID, and no annotation set.
8+
9+
2. **Project Reference Behavior & `SetProjectRef`**:
10+
- Introduced a new helper `export.SetProjectRef(u, projectID)` under `pkg/export/helpers.go`.
11+
- This helper sets the `spec.projectRef.external` path directly on the unstructured representation of exported resources.
12+
- For `CertificateManagerCertificateMap`, `spec.projectRef` in `_exported.yaml` is now correctly exported as:
13+
```yaml
14+
projectRef:
15+
external: ${projectId}
16+
```
17+
and the `cnrm.cloud.google.com/project-id` annotation is omitted entirely.
18+
19+
3. **Short-Name and ResourceID Mapping**:
20+
- The GCP actual `Name` returns the full resource path (`projects/{project}/locations/global/certificateMaps/{certificatemap}`).
21+
- It is crucial to use the parsed short name `a.id.CertificateMap` when calling `u.SetName` to generate valid KRM metadata.
22+
- Set `obj.Spec.ResourceID = direct.LazyPtr(a.id.CertificateMap)` explicitly since identity fields are not automatically converted from proto.
23+
24+
4. **Isolated E2E Testing**:
25+
- When running the unified E2E test runner, specify the exact regex `^TestAllInSeries/fixtures/certificatemanagercertificatemap$` to prevent other tests (e.g., `certificatemanagercertificatemapentry`) from being executed and writing undesired golden output diffs.

pkg/controller/direct/certificatemanager/certificatemap_controller.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ import (
2121
gcp "cloud.google.com/go/certificatemanager/apiv1"
2222
pb "cloud.google.com/go/certificatemanager/apiv1/certificatemanagerpb"
2323
krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/certificatemanager/v1beta1"
24+
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs"
2425
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/config"
2526
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
2627
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/common"
2728
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/directbase"
2829
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/registry"
2930
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/tags"
31+
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/export"
3032
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/label"
3133
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/mappers"
3234
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/structuredreporting"
@@ -102,8 +104,21 @@ func (m *certificateMapModel) AdapterForObject(ctx context.Context, op *directba
102104
}
103105

104106
func (m *certificateMapModel) AdapterForURL(ctx context.Context, url string) (directbase.Adapter, error) {
105-
// TODO: Support URLs
106-
return nil, nil
107+
id := &krm.CertificateManagerCertificateMapIdentity{}
108+
if err := id.FromExternal(url); err != nil {
109+
// Not recognized
110+
return nil, nil
111+
}
112+
113+
gcpClient, err := m.client(ctx)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
return &CertificateMapAdapter{
119+
id: id,
120+
gcpClient: gcpClient,
121+
}, nil
107122
}
108123

109124
type CertificateMapAdapter struct {
@@ -216,14 +231,20 @@ func (a *CertificateMapAdapter) Export(ctx context.Context) (*unstructured.Unstr
216231
return nil, mapCtx.Err()
217232
}
218233

234+
obj.Spec.ResourceID = direct.LazyPtr(a.id.CertificateMap)
235+
obj.Spec.ProjectRef = refs.ProjectRef{External: a.id.Project}
236+
219237
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
220238
if err != nil {
221239
return nil, err
222240
}
223241

224242
u.Object = uObj
225-
u.SetName(a.actual.Name)
243+
u.SetName(a.id.CertificateMap)
226244
u.SetGroupVersionKind(krm.CertificateManagerCertificateMapGVK)
245+
246+
export.SetLabels(u, a.actual.Labels)
247+
227248
return u, nil
228249
}
229250

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: certificatemanager.cnrm.cloud.google.com/v1beta1
2+
kind: CertificateManagerCertificateMap
3+
metadata:
4+
labels:
5+
cnrm-test: "true"
6+
managed-by-cnrm: "true"
7+
value: cert-map-2
8+
name: certificatemanagercertificatemap${uniqueId}
9+
spec:
10+
description: updated sample certificate map
11+
projectRef:
12+
external: ${projectId}
13+
resourceID: certificatemanagercertificatemap${uniqueId}

tests/e2e/export.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ func exportResource(h *create.Harness, obj *unstructured.Unstructured, options *
7272
case schema.GroupKind{Group: "certificatemanager.cnrm.cloud.google.com", Kind: "CertificateManagerCertificateIssuanceConfig"}:
7373
exportURI = resolveCAISURI(h, obj)
7474

75+
case schema.GroupKind{Group: "certificatemanager.cnrm.cloud.google.com", Kind: "CertificateManagerCertificateMap"}:
76+
exportURI = resolveCAISURI(h, obj)
77+
7578
case schema.GroupKind{Group: "serviceusage.cnrm.cloud.google.com", Kind: "Service"}:
7679
exportURI = "//serviceusage.googleapis.com/projects/" + projectID + "/services/" + resourceID
7780

0 commit comments

Comments
 (0)