Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Aug 14, 2024
1 parent c947888 commit fa015b8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 44 deletions.
18 changes: 18 additions & 0 deletions dev/tools/controllerbuilder/template/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
25 changes: 23 additions & 2 deletions dev/tools/controllerbuilder/template/externalresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

const (
// TODO(user): Add service domain
serviceDomain = "//{{.KindToLower}}.googleapis.com"
serviceDomain = "//{{.Service}}.googleapis.com"
)

// TODO(user): Define resource identity
Expand Down Expand Up @@ -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/<project>/locations/<location>/{{.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}},
}
}
22 changes: 20 additions & 2 deletions pkg/controller/direct/cloudbuild/workerpool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
}
40 changes: 0 additions & 40 deletions pkg/controller/direct/statusutils.go

This file was deleted.

0 comments on commit fa015b8

Please sign in to comment.