diff --git a/dev/tools/controllerbuilder/template/controller.go b/dev/tools/controllerbuilder/template/controller.go index bfc6c77a9d..7d86ce3ba2 100644 --- a/dev/tools/controllerbuilder/template/controller.go +++ b/dev/tools/controllerbuilder/template/controller.go @@ -305,3 +305,21 @@ func (a *Adapter) Delete(ctx context.Context) (bool, error) { } return true, nil } + +func SetStatus(u *unstructured.Unstructured, typedStatus any) error { + status, err := runtime.DefaultUnstructuredConverter.ToUnstructured(typedStatus) + if err != nil { + return fmt.Errorf("error converting status to unstructured: %w", err) + } + + old, _, _ := unstructured.NestedMap(u.Object, "status") + if old != nil { + status["conditions"] = old["conditions"] + status["observedGeneration"] = old["observedGeneration"] + status["externalRef"] = old["externalRef"] + } + + u.Object["status"] = status + + return nil +} diff --git a/dev/tools/controllerbuilder/template/externalresource.go b/dev/tools/controllerbuilder/template/externalresource.go index fc141fee06..aae46b4db0 100644 --- a/dev/tools/controllerbuilder/template/externalresource.go +++ b/dev/tools/controllerbuilder/template/externalresource.go @@ -24,7 +24,7 @@ import ( const ( // TODO(user): Add service domain - serviceDomain = "//{{.KindToLower}}.googleapis.com" + serviceDomain = "//{{.Service}}.googleapis.com" ) // TODO(user): Define resource identity @@ -53,9 +53,30 @@ func (c *{{.Kind}}Identity) ExternalRef() *string { // BuildIDFromExternal builds a {{.Kind}}Identity from a external reference func BuildIDFromExternal(external string) (*{{.Kind}}Identity, error) { // TODO(user): Build resource identity from external reference + if !strings.HasPrefix(externalRef, serviceDomain) { + return nil, fmt.Errorf("externalRef should have prefix %s, got %s", serviceDomain, externalRef) + } + path := strings.TrimPrefix(externalRef, serviceDomain+"/") + tokens := strings.Split(path, "/") + + // TODO(user): Confirm the format of your resources, and verify it like the example below + if len(tokens) != 6 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "{{.KindToLower}}s" { + return nil, fmt.Errorf("externalRef should be %s/projects//locations//{{.KindToLower}}s/<{{.KindToLower}}>, got %s", + serviceDomain, externalRef) + } + return &{{.Kind}}Identity{ + project: tokens[1], + location: tokens[3], + {{.KindToLower}}: tokens[5], + }, nil } -// BuildID builds a {{.Kind}}Identity from resource components +// BuildID builds a unique identifier {{.Kind}}Identity from resource components func BuildID(project, location string) *{{.Kind}}Identity { // TODO(user): Build resource identity from resource components, i.e. project, location, resource id + return &{{.Kind}}Identity{ + project: project, + location: location, + {{.KindToLower}}: {{.KindToLower}}, + } } diff --git a/pkg/controller/direct/cloudbuild/workerpool_controller.go b/pkg/controller/direct/cloudbuild/workerpool_controller.go index 310a6ccbde..7d295ee378 100644 --- a/pkg/controller/direct/cloudbuild/workerpool_controller.go +++ b/pkg/controller/direct/cloudbuild/workerpool_controller.go @@ -207,7 +207,7 @@ func (a *Adapter) Create(ctx context.Context, u *unstructured.Unstructured) erro return mapCtx.Err() } status.ExternalRef = a.id.ExternalRef() - return direct.SetStatus(u, status) + return setStatus(u, status) } func (a *Adapter) Update(ctx context.Context, u *unstructured.Unstructured) error { @@ -291,7 +291,7 @@ func (a *Adapter) Update(ctx context.Context, u *unstructured.Unstructured) erro if mapCtx.Err() != nil { return fmt.Errorf("update workerpool status %w", mapCtx.Err()) } - return direct.SetStatus(u, status) + return setStatus(u, status) } func (a *Adapter) Export(ctx context.Context) (*unstructured.Unstructured, error) { @@ -318,3 +318,21 @@ func (a *Adapter) Delete(ctx context.Context) (bool, error) { } return true, nil } + +func setStatus(u *unstructured.Unstructured, typedStatus any) error { + status, err := runtime.DefaultUnstructuredConverter.ToUnstructured(typedStatus) + if err != nil { + return fmt.Errorf("error converting status to unstructured: %w", err) + } + + old, _, _ := unstructured.NestedMap(u.Object, "status") + if old != nil { + status["conditions"] = old["conditions"] + status["observedGeneration"] = old["observedGeneration"] + status["externalRef"] = old["externalRef"] + } + + u.Object["status"] = status + + return nil +} diff --git a/pkg/controller/direct/statusutils.go b/pkg/controller/direct/statusutils.go deleted file mode 100644 index 7dcc2b965b..0000000000 --- a/pkg/controller/direct/statusutils.go +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2024 Google LLC -// -// 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 direct - -import ( - "fmt" - - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime" -) - -func SetStatus(u *unstructured.Unstructured, typedStatus any) error { - status, err := runtime.DefaultUnstructuredConverter.ToUnstructured(typedStatus) - if err != nil { - return fmt.Errorf("error converting status to unstructured: %w", err) - } - - old, _, _ := unstructured.NestedMap(u.Object, "status") - if old != nil { - status["conditions"] = old["conditions"] - status["observedGeneration"] = old["observedGeneration"] - status["externalRef"] = old["externalRef"] - } - - u.Object["status"] = status - - return nil -}