Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#2899 from acpana/acpana/export…
Browse files Browse the repository at this point in the history
…-everyone

feat: implement missing exports [cloudbuild]
  • Loading branch information
google-oss-prow[bot] authored Oct 24, 2024
2 parents f33fcd0 + 3a34b9e commit 071b3db
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 6 deletions.
48 changes: 47 additions & 1 deletion pkg/controller/direct/cloudbuild/workerpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ func (m *model) AdapterForObject(ctx context.Context, reader client.Reader, u *u
}

func (m *model) AdapterForURL(ctx context.Context, url string) (directbase.Adapter, error) {
// Format: //cloudbuild.googleapis.com/projects/<project>/lcoations/<location>/workerPools/<id>
if !strings.HasPrefix(url, "//cloudbuild.googleapis.com/") {
return nil, nil
}

tokens := strings.Split(strings.TrimPrefix(url, "//cloudbuild.googleapis.com/"), "/")
if len(tokens) == 6 && tokens[0] == "projects" && tokens[2] == "locations" && tokens[4] == "workerPools" {
// Get CloudBuild GCP client
gcpClient, err := m.client(ctx)
if err != nil {
return nil, err
}

return &Adapter{
id: &CloudBuildWorkerPoolIdentity{
project: tokens[1],
location: tokens[3],
workerpool: tokens[5],
},
gcpClient: gcpClient,
}, nil
}

return nil, nil
}

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

func (a *Adapter) Export(ctx context.Context) (*unstructured.Unstructured, error) {
return nil, nil
if a.actual == nil {
return nil, fmt.Errorf("Find() not called")
}
u := &unstructured.Unstructured{}

obj := &krm.CloudBuildWorkerPool{}
obj.SetGroupVersionKind(krm.GroupVersionKind)
obj.SetName(a.actual.Name)

mapCtx := &direct.MapContext{}
obj.Spec = direct.ValueOf(CloudBuildWorkerPoolSpec_FromProto(mapCtx, a.actual))
if mapCtx.Err() != nil {
return nil, mapCtx.Err()
}

obj.Spec.ProjectRef = &refs.ProjectRef{External: *a.id.AsExternalRef()}
obj.Spec.Location = a.id.location
uObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}

u.Object = uObj
return u, nil
}

// Delete implements the Adapter interface.
Expand Down
12 changes: 12 additions & 0 deletions pkg/controller/direct/cloudbuild/workerpool_mappings.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ func CloudBuildWorkerPoolSpec_ToProto(mapCtx *direct.MapContext, in *krm.CloudBu
return out
}

func CloudBuildWorkerPoolSpec_FromProto(mapCtx *direct.MapContext, in *pb.WorkerPool) *krm.CloudBuildWorkerPoolSpec {
if in == nil {
return nil
}

out := &krm.CloudBuildWorkerPoolSpec{}
out.DisplayName = in.DisplayName
out.PrivatePoolConfig = PrivatePoolV1Config_FromProto(mapCtx, in.GetPrivatePoolV1Config())

return out
}

func PrivatePoolV1Config_NetworkConfigStatus_FromProto(mapCtx *direct.MapContext, in *pb.PrivatePoolV1Config_NetworkConfig) *krm.PrivatePoolV1Config_NetworkConfigStatus {
if in == nil {
return nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/direct/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func SupportsIAM(groupKind schema.GroupKind) (bool, error) {
return false, nil
case schema.GroupKind{Group: "sql.cnrm.cloud.google.com", Kind: "SQLInstance"}:
return false, nil
case schema.GroupKind{Group: "cloudbuild.cnrm.cloud.google.com", Kind: "CloudBuildWorkerPool"}:
return false, nil
case schema.GroupKind{Group: "securesourcemanager.cnrm.cloud.google.com", Kind: "SecureSourceManagerInstance"}:
return false, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: cloudbuild.cnrm.cloud.google.com/v1beta1
kind: CloudBuildWorkerPool
metadata:
name: projects-${projectId}-locations-us-central1-workerpools-cloudbuildworkerpool-${uniqueId}
spec:
displayName: New CloudBuild WorkerPool
location: us-central1
privatePoolV1Config:
networkConfig:
egressOption: NO_PUBLIC_EGRESS
peeredNetworkIPRange: /29
peeredNetworkRef:
external: projects/${projectId}/global/networks/computenetwork-${uniqueId}
workerConfig:
diskSizeGb: 100
machineType: e2-medium
projectRef:
external: //cloudbuild.googleapis.com/projects/${projectId}/locations/us-central1/workerPools/cloudbuildworkerpool-${uniqueId}
22 changes: 18 additions & 4 deletions tests/e2e/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package e2e

import (
"fmt"
"path/filepath"
"strings"

Expand All @@ -26,7 +27,11 @@ import (
"sigs.k8s.io/yaml"
)

func exportResource(h *create.Harness, obj *unstructured.Unstructured) string {
type Expectations struct {
Location bool // location or region
}

func exportResource(h *create.Harness, obj *unstructured.Unstructured, expectations *Expectations) string {
exportURI := ""

projectID := resolveProjectID(h, obj)
Expand All @@ -35,8 +40,13 @@ func exportResource(h *create.Harness, obj *unstructured.Unstructured) string {
if resourceID == "" {
resourceID = obj.GetName()
}
// location, _, _ := unstructured.NestedString(obj.Object, "spec", "location")

location, found, err := unstructured.NestedString(obj.Object, "spec", "location")
if err != nil {
h.T.Error(fmt.Errorf("retrieving location from obj: %w", err))
}
if !found && expectations.Location {
h.T.Error("expected to find location or region in obj but did not find it")
}
// This list should match https://cloud.google.com/asset-inventory/docs/resource-name-format
gvk := obj.GroupVersionKind()
switch gvk.GroupKind() {
Expand All @@ -51,6 +61,10 @@ func exportResource(h *create.Harness, obj *unstructured.Unstructured) string {

case schema.GroupKind{Group: "monitoring.cnrm.cloud.google.com", Kind: "MonitoringDashboard"}:
exportURI = "//monitoring.googleapis.com/projects/" + projectID + "/dashboards/" + resourceID

case schema.GroupKind{Group: "cloudbuild.cnrm.cloud.google.com", Kind: "CloudBuildWorkerPool"}:
exportURI = "//cloudbuild.googleapis.com/projects/" + projectID + "/locations/" + location + "/workerPools/" + resourceID

}

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

func exportResourceAsUnstructured(h *create.Harness, obj *unstructured.Unstructured) *unstructured.Unstructured {
s := exportResource(h, obj)
s := exportResource(h, obj, &Expectations{})
if s == "" {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/unified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func runScenario(ctx context.Context, t *testing.T, testPause bool, fixture reso
t.Errorf("failed to get test name")
}
// Golden test exported GCP object
exportedYAML := exportResource(h, obj)
exportedYAML := exportResource(h, obj, &Expectations{})
if exportedYAML != "" {
exportedObj := &unstructured.Unstructured{}
if err := yaml.Unmarshal([]byte(exportedYAML), exportedObj); err != nil {
Expand Down

0 comments on commit 071b3db

Please sign in to comment.