-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ExternalReference (selfLink, resourceRef) with GCP update/d…
…eletion uniqueness guardrail
- Loading branch information
Showing
13 changed files
with
288 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
4 changes: 4 additions & 0 deletions
4
pkg/clients/generated/apis/cloudbuild/v1alpha1/cloudbuildworkerpool_types.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
pkg/clients/generated/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,51 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cloudbuild | ||
|
||
import ( | ||
"strings" | ||
|
||
cloudbuildpb "cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb" | ||
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/externalresource" | ||
) | ||
|
||
func NewResourceRef(gcpObj *cloudbuildpb.WorkerPool) *ResourceRef { | ||
return &ResourceRef{ | ||
externalresource.New("https://cloudbuild.googleapis.com/v1/", gcpObj), | ||
} | ||
} | ||
|
||
type ResourceRef struct { | ||
*externalresource.ExternalResourceReference | ||
} | ||
|
||
func (e *ResourceRef) GetResourceID() string { | ||
segments := strings.Split(*e.Get(), "/workerPools/") | ||
return segments[1] | ||
} | ||
|
||
func (e *ResourceRef) GetLocation() string { | ||
segments := strings.Split(*e.Get(), "/locations/") | ||
segment := strings.Split(segments[1], "/workerPools/") | ||
return segment[0] | ||
} | ||
|
||
func (e *ResourceRef) GetProject() string { | ||
segments := strings.Split(*e.Get(), "/projects/") | ||
segment := strings.Split(segments[1], "/locations/") | ||
return segment[0] | ||
} |
This file contains 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 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 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,64 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package externalresource | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
type ExternalResourceReference struct { | ||
hasSelfLink bool | ||
externalRef string | ||
} | ||
|
||
type GCPResource interface { | ||
GetName() string | ||
} | ||
|
||
// New builds the External Reference for CloudBuildWorkerPool when the object already exists on the GCP server. | ||
// Ideally, it expects to use the GCP object's selfLink value as the external reference. However, if | ||
// the resource does not have `selfLink` (like CloudBuildWorkerPool), it builds the URL from the GCP object. | ||
func New(host string, gcpObj GCPResource) *ExternalResourceReference { | ||
baseExternalRef := &ExternalResourceReference{ | ||
hasSelfLink: false, | ||
externalRef: host + gcpObj.GetName(), | ||
} | ||
// Ignore the actual GCP proto, get the `selfLink` in general. | ||
s := reflect.ValueOf(gcpObj).Elem() | ||
if s.Kind() != reflect.Struct { | ||
return baseExternalRef | ||
} | ||
|
||
selfLink := s.FieldByName("SelfLink") | ||
|
||
if !selfLink.IsValid() || selfLink.IsZero() { | ||
return baseExternalRef | ||
} | ||
switch selfLink.Kind() { | ||
case reflect.String: | ||
baseExternalRef.hasSelfLink = true | ||
baseExternalRef.externalRef = selfLink.String() | ||
case reflect.Pointer: | ||
baseExternalRef.hasSelfLink = true | ||
baseExternalRef.externalRef = selfLink.Elem().String() | ||
} | ||
return baseExternalRef | ||
} | ||
|
||
func (e *ExternalResourceReference) Get() *string { | ||
return &e.externalRef | ||
} |
This file contains 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,61 @@ | ||
/* | ||
Copyright 2024. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package externalresource | ||
|
||
import ( | ||
"testing" | ||
|
||
"cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb" | ||
"cloud.google.com/go/compute/apiv1/computepb" | ||
) | ||
|
||
var ( | ||
networkSelfLink = "https://www.googleapis.com/mockservice/v1/projects/yuwenma-gke-dev/global/networks/computenetwork-52ldcmpbgxfmizhrk74q" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
gcpResource GCPResource | ||
expectedHasSelfLink bool | ||
expectedExternalRef string | ||
}{ | ||
{ | ||
name: "cloudbuild workerpool, no selfLink", | ||
gcpResource: &cloudbuildpb.WorkerPool{Name: "projects/mock-project/locations/us-central1/workerPools/mock-pool"}, | ||
expectedHasSelfLink: false, | ||
expectedExternalRef: "https://www.googleapis.com/mockservice/v1/projects/mock-project/locations/us-central1/workerPools/mock-pool", | ||
}, | ||
{ | ||
name: "compute network, has seflLink as string pointer", | ||
gcpResource: &computepb.Network{SelfLink: &networkSelfLink}, | ||
expectedHasSelfLink: true, | ||
expectedExternalRef: networkSelfLink, | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
gcpObj := test.gcpResource | ||
actualRef := New("https://www.googleapis.com/mockservice/v1/", gcpObj) | ||
if actualRef.hasSelfLink != test.expectedHasSelfLink { | ||
t.Errorf("expected %v, got %v", test.expectedHasSelfLink, actualRef.hasSelfLink) | ||
} | ||
if actualRef.externalRef != test.expectedExternalRef { | ||
t.Errorf("expected %v, got %v", test.expectedExternalRef, actualRef.externalRef) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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.