Skip to content

Commit

Permalink
WIP: Implement attached clusters direct controller
Browse files Browse the repository at this point in the history
  • Loading branch information
hankfreund committed Nov 6, 2024
1 parent 8d50ea3 commit 93de603
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 0 deletions.
73 changes: 73 additions & 0 deletions apis/containerattached/v1beta1/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
// limitations under the License.

package v1beta1
import (
"context"
"fmt"
"strings"

refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type FleetProjectRef struct {
/* The project of the fleet. Allowed value: The Google Cloud resource name of a `Project` resource (format: `projects/{{name}}`).*/
Expand All @@ -22,3 +31,67 @@ type FleetProjectRef struct {
/* Namespace of the project resource. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/ */
Namespace string `json:"namespace,omitempty"`
}

type ContainerAttachedClusterRef struct {
// A reference to an externally managed ContainerAttachedCluster resource.
// Should be in the format `projects/<projectID>/locations/<location>/attachedClusters/<attachedClusterId>`.
External string `json:"external,omitempty"`
// The `name` of a `ContainerAttachedCluster` resource.
Name string `json:"name,omitempty"`
// The `namespace` of a `ContainerAttachedCluster` resource.
Namespace string `json:"namespace,omitempty"`
// The location where this cluster is registered.
Location string `json:"location,omitempty"`
// The parent location where this ContainerAttachedCluster resource lives.
// Should be in the format `projects/<projectID>/locations/<location>`.
parent string
}

// NewContainerAttachedClusterRef builds a ContainerAttachedClusterRef from the ConfigConnector ContainerAttachedCluster object.
func NewContainerAttachedClusterRef(ctx context.Context, reader client.Reader, obj *ContainerAttachedCluster) (*ContainerAttachedClusterRef, error) {
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj, obj.Spec.ProjectRef)
if err != nil {
return nil, err
}
projectID := projectRef.ProjectID
if projectID == "" {
return nil, fmt.Errorf("cannot resolve project")
}

location := obj.Spec.Location
if location == "" {
return nil, fmt.Errorf("cannot resolve location")
}

resourceID := direct.ValueOf(obj.Spec.ResourceID)
if resourceID == "" {
resourceID = obj.GetName()
}
if resourceID == "" {
return nil, fmt.Errorf("cannot resolve resource ID")
}

parent := "projects/" + projectID + "/locations/" + location
return &ContainerAttachedClusterRef{
External: parent + "/attachedClusters/" + resourceID,
Name: resourceID,
Location: location,
parent: parent,
}, nil
}

func (r *ContainerAttachedClusterRef) Parent() (string, error) {
if r.parent != "" {
return r.parent, nil
}
if r.External != "" {
r.External = strings.TrimPrefix(r.External, "/")
tokens := strings.Split(r.External, "/")
if len(tokens) != 6 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "attachedClusters" {
return "", fmt.Errorf("format of ContainerAttachedCluster external=%q was not known (use projects/<projectId>/locations/<location>/attachedClusters/<clusterId>)", r.External)
}
r.parent = "projects/" + tokens[1] + "/locations/" + tokens[3]
return r.parent, nil
}
return "", fmt.Errorf("ContainerAttachedClusterRef not normalized to External form or not created from `New()`")
}
Loading

0 comments on commit 93de603

Please sign in to comment.