Skip to content

Commit 071b3db

Browse files
Merge pull request GoogleCloudPlatform#2899 from acpana/acpana/export-everyone
feat: implement missing exports [cloudbuild]
2 parents f33fcd0 + 3a34b9e commit 071b3db

File tree

6 files changed

+98
-6
lines changed

6 files changed

+98
-6
lines changed

pkg/controller/direct/cloudbuild/workerpool_controller.go

+47-1
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,29 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u
143143
}
144144

145145
func (m *model) AdapterForURL(ctx context.Context, url string) (directbase.Adapter, error) {
146+
// Format: //cloudbuild.googleapis.com/projects/<project>/lcoations/<location>/workerPools/<id>
147+
if !strings.HasPrefix(url, "//cloudbuild.googleapis.com/") {
148+
return nil, nil
149+
}
150+
151+
tokens := strings.Split(strings.TrimPrefix(url, "//cloudbuild.googleapis.com/"), "/")
152+
if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "locations" && tokens[4] == "workerPools" {
153+
// Get CloudBuild GCP client
154+
gcpClient, err := m.client(ctx)
155+
if err != nil {
156+
return nil, err
157+
}
158+
159+
return &Adapter{
160+
id: &CloudBuildWorkerPoolIdentity{
161+
project: tokens[1],
162+
location: tokens[3],
163+
workerpool: tokens[5],
164+
},
165+
gcpClient: gcpClient,
166+
}, nil
167+
}
168+
146169
return nil, nil
147170
}
148171

@@ -250,7 +273,30 @@ func (a *Adapter) Update(ctx context.Context, updateOp *directbase.UpdateOperati
250273
}
251274

252275
func (a *Adapter) Export(ctx context.Context) (*unstructured.Unstructured, error) {
253-
return nil, nil
276+
if a.actual == nil {
277+
return nil, fmt.Errorf("Find() not called")
278+
}
279+
u := &unstructured.Unstructured{}
280+
281+
obj := &krm.CloudBuildWorkerPool{}
282+
obj.SetGroupVersionKind(krm.GroupVersionKind)
283+
obj.SetName(a.actual.Name)
284+
285+
mapCtx := &direct.MapContext{}
286+
obj.Spec = direct.ValueOf(CloudBuildWorkerPoolSpec_FromProto(mapCtx, a.actual))
287+
if mapCtx.Err() != nil {
288+
return nil, mapCtx.Err()
289+
}
290+
291+
obj.Spec.ProjectRef = &refs.ProjectRef{External: *a.id.AsExternalRef()}
292+
obj.Spec.Location = a.id.location
293+
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
294+
if err != nil {
295+
return nil, err
296+
}
297+
298+
u.Object = uObj
299+
return u, nil
254300
}
255301

256302
// Delete implements the Adapter interface.

pkg/controller/direct/cloudbuild/workerpool_mappings.go

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ func CloudBuildWorkerPoolSpec_ToProto(mapCtx *direct.MapContext, in *krm.CloudBu
5454
return out
5555
}
5656

57+
func CloudBuildWorkerPoolSpec_FromProto(mapCtx *direct.MapContext, in *pb.WorkerPool) *krm.CloudBuildWorkerPoolSpec {
58+
if in == nil {
59+
return nil
60+
}
61+
62+
out := &krm.CloudBuildWorkerPoolSpec{}
63+
out.DisplayName = in.DisplayName
64+
out.PrivatePoolConfig = PrivatePoolV1Config_FromProto(mapCtx, in.GetPrivatePoolV1Config())
65+
66+
return out
67+
}
68+
5769
func PrivatePoolV1Config_NetworkConfigStatus_FromProto(mapCtx *direct.MapContext, in *pb.PrivatePoolV1Config_NetworkConfig) *krm.PrivatePoolV1Config_NetworkConfigStatus {
5870
if in == nil {
5971
return nil

pkg/controller/direct/registry/registry.go

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ func SupportsIAM(groupKind schema.GroupKind) (bool, error) {
139139
return false, nil
140140
case schema.GroupKind{Group: "sql.cnrm.cloud.google.com", Kind: "SQLInstance"}:
141141
return false, nil
142+
case schema.GroupKind{Group: "cloudbuild.cnrm.cloud.google.com", Kind: "CloudBuildWorkerPool"}:
143+
return false, nil
142144
case schema.GroupKind{Group: "securesourcemanager.cnrm.cloud.google.com", Kind: "SecureSourceManagerInstance"}:
143145
return false, nil
144146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: cloudbuild.cnrm.cloud.google.com/v1beta1
2+
kind: CloudBuildWorkerPool
3+
metadata:
4+
name: projects-${projectId}-locations-us-central1-workerpools-cloudbuildworkerpool-${uniqueId}
5+
spec:
6+
displayName: New CloudBuild WorkerPool
7+
location: us-central1
8+
privatePoolV1Config:
9+
networkConfig:
10+
egressOption: NO_PUBLIC_EGRESS
11+
peeredNetworkIPRange: /29
12+
peeredNetworkRef:
13+
external: projects/${projectId}/global/networks/computenetwork-${uniqueId}
14+
workerConfig:
15+
diskSizeGb: 100
16+
machineType: e2-medium
17+
projectRef:
18+
external: //cloudbuild.googleapis.com/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}

tests/e2e/export.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package e2e
1616

1717
import (
18+
"fmt"
1819
"path/filepath"
1920
"strings"
2021

@@ -26,7 +27,11 @@ import (
2627
"sigs.k8s.io/yaml"
2728
)
2829

29-
func exportResource(h *create.Harness, obj *unstructured.Unstructured) string {
30+
type Expectations struct {
31+
Location bool // location or region
32+
}
33+
34+
func exportResource(h *create.Harness, obj *unstructured.Unstructured, expectations *Expectations) string {
3035
exportURI := ""
3136

3237
projectID := resolveProjectID(h, obj)
@@ -35,8 +40,13 @@ func exportResource(h *create.Harness, obj *unstructured.Unstructured) string {
3540
if resourceID == "" {
3641
resourceID = obj.GetName()
3742
}
38-
// location, _, _ := unstructured.NestedString(obj.Object, "spec", "location")
39-
43+
location, found, err := unstructured.NestedString(obj.Object, "spec", "location")
44+
if err != nil {
45+
h.T.Error(fmt.Errorf("retrieving location from obj: %w", err))
46+
}
47+
if !found && expectations.Location {
48+
h.T.Error("expected to find location or region in obj but did not find it")
49+
}
4050
// This list should match https://cloud.google.com/asset-inventory/docs/resource-name-format
4151
gvk := obj.GroupVersionKind()
4252
switch gvk.GroupKind() {
@@ -51,6 +61,10 @@ func exportResource(h *create.Harness, obj *unstructured.Unstructured) string {
5161

5262
case schema.GroupKind{Group: "monitoring.cnrm.cloud.google.com", Kind: "MonitoringDashboard"}:
5363
exportURI = "//monitoring.googleapis.com/projects/" + projectID + "/dashboards/" + resourceID
64+
65+
case schema.GroupKind{Group: "cloudbuild.cnrm.cloud.google.com", Kind: "CloudBuildWorkerPool"}:
66+
exportURI = "//cloudbuild.googleapis.com/projects/" + projectID + "/locations/" + location + "/workerPools/" + resourceID
67+
5468
}
5569

5670
if exportURI == "" {
@@ -74,7 +88,7 @@ func exportResource(h *create.Harness, obj *unstructured.Unstructured) string {
7488
}
7589

7690
func exportResourceAsUnstructured(h *create.Harness, obj *unstructured.Unstructured) *unstructured.Unstructured {
77-
s := exportResource(h, obj)
91+
s := exportResource(h, obj, &Expectations{})
7892
if s == "" {
7993
return nil
8094
}

tests/e2e/unified_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ func runScenario(ctx context.Context, t *testing.T, testPause bool, fixture reso
317317
t.Errorf("failed to get test name")
318318
}
319319
// Golden test exported GCP object
320-
exportedYAML := exportResource(h, obj)
320+
exportedYAML := exportResource(h, obj, &Expectations{})
321321
if exportedYAML != "" {
322322
exportedObj := &unstructured.Unstructured{}
323323
if err := yaml.Unmarshal([]byte(exportedYAML), exportedObj); err != nil {

0 commit comments

Comments
 (0)