Skip to content

Commit

Permalink
Merge pull request GoogleCloudPlatform#3265 from jasonvigil/workstati…
Browse files Browse the repository at this point in the history
…onconfig

feat: Implement WorkstationConfig direct controller and record GCP
  • Loading branch information
google-oss-prow[bot] authored Dec 5, 2024
2 parents adcb218 + b9b9fe4 commit 46c118d
Show file tree
Hide file tree
Showing 20 changed files with 4,852 additions and 220 deletions.
132 changes: 132 additions & 0 deletions apis/workstations/v1alpha1/config_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// 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 v1alpha1

import (
"context"
"fmt"
"strings"

"github.com/GoogleCloudPlatform/k8s-config-connector/apis/common"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// WorkstationConfigIdentity defines the resource reference to WorkstationConfig, which "External" field
// holds the GCP identifier for the KRM object.
type WorkstationConfigIdentity struct {
parent *WorkstationConfigParent
id string
}

func (i *WorkstationConfigIdentity) String() string {
return i.parent.String() + "/workstationConfigs/" + i.id
}

func (i *WorkstationConfigIdentity) ID() string {
return i.id
}

func (i *WorkstationConfigIdentity) Parent() *WorkstationConfigParent {
return i.parent
}

type WorkstationConfigParent struct {
ProjectID string
Location string
Cluster string
}

func (p *WorkstationConfigParent) String() string {
return "projects/" + p.ProjectID + "/locations/" + p.Location + "/workstationClusters/" + p.Cluster
}

// New builds a ConfigIdentity from the Config Connector WorkstationConfig object.
func NewWorkstationConfigIdentity(ctx context.Context, reader client.Reader, obj *WorkstationConfig) (*WorkstationConfigIdentity, error) {
// Get Parent
clusterRef := obj.Spec.Parent
if clusterRef == nil {
return nil, fmt.Errorf("no parent cluster")
}
clusterExternal, err := clusterRef.NormalizedExternal(ctx, reader, obj.Namespace)
if err != nil {
return nil, fmt.Errorf("cannot resolve cluster: %w", err)
}
clusterParent, cluster, err := parseWorkstationClusterExternal(clusterExternal)
if err != nil {
return nil, fmt.Errorf("cannot parse external cluster: %w", err)
}
projectID := clusterParent.ProjectID
if projectID == "" {
return nil, fmt.Errorf("cannot resolve project")
}
location := clusterParent.Location
if location == "" {
return nil, fmt.Errorf("cannot resolve location")
}

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

// Use approved External
externalRef := common.ValueOf(obj.Status.ExternalRef)
if externalRef != "" {
// Validate desired with actual
actualParent, actualResourceID, err := ParseWorkstationConfigExternal(externalRef)
if err != nil {
return nil, err
}
if actualParent.ProjectID != projectID {
return nil, fmt.Errorf("spec.projectRef changed, expect %s, got %s", actualParent.ProjectID, projectID)
}
if actualParent.Location != location {
return nil, fmt.Errorf("spec.location changed, expect %s, got %s", actualParent.Location, location)
}
if actualParent.Cluster != cluster {
return nil, fmt.Errorf("spec.cluster changed, expect %s, got %s", actualParent.Cluster, cluster)
}
if actualResourceID != resourceID {
return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s",
resourceID, actualResourceID)
}
}
return &WorkstationConfigIdentity{
parent: &WorkstationConfigParent{
ProjectID: projectID,
Location: location,
Cluster: cluster,
},
id: resourceID,
}, nil
}

func ParseWorkstationConfigExternal(external string) (parent *WorkstationConfigParent, resourceID string, err error) {
tokens := strings.Split(external, "/")
if len(tokens) != 8 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "workstationClusters" || tokens[6] != "workstationConfigs" {
return nil, "", fmt.Errorf("format of Workstation external=%q was not known (use projects/<projectID>/locations/<location>/workstationClusters/<workstationclusterID>/workstationConfigs/<workstationconfigID>)", external)
}
parent = &WorkstationConfigParent{
ProjectID: tokens[1],
Location: tokens[3],
Cluster: tokens[5],
}
resourceID = tokens[7]
return parent, resourceID, nil
}
112 changes: 1 addition & 111 deletions apis/workstations/v1alpha1/config_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package v1alpha1
import (
"context"
"fmt"
"strings"

refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
Expand Down Expand Up @@ -52,7 +51,7 @@ func (r *WorkstationConfigRef) NormalizedExternal(ctx context.Context, reader cl
}
// From given External
if r.External != "" {
if _, _, err := parseWorkstationConfigExternal(r.External); err != nil {
if _, _, err := ParseWorkstationConfigExternal(r.External); err != nil {
return "", err
}
return r.External, nil
Expand Down Expand Up @@ -82,112 +81,3 @@ func (r *WorkstationConfigRef) NormalizedExternal(ctx context.Context, reader cl
r.External = actualExternalRef
return r.External, nil
}

// New builds a WorkstationConfigRef from the Config Connector WorkstationConfig object.
func NewWorkstationConfigRef(ctx context.Context, reader client.Reader, obj *WorkstationConfig) (*WorkstationConfigRef, error) {
id := &WorkstationConfigRef{}

// Get Parent
projectRef, err := refsv1beta1.ResolveProject(ctx, reader, obj.GetNamespace(), 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")
}
clusterRef := obj.Spec.Parent
if clusterRef == nil {
return nil, fmt.Errorf("no parent cluster")
}
clusterExternal, err := clusterRef.NormalizedExternal(ctx, reader, obj.Namespace)
if err != nil {
return nil, fmt.Errorf("cannot resolve cluster: %w", err)
}
_, clusterID, err := parseWorkstationClusterExternal(clusterExternal)
if err != nil {
return nil, fmt.Errorf("cannot parse external cluster: %w", err)
}

// Get desired ID
resourceID := valueOf(obj.Spec.ResourceID)
if resourceID == "" {
resourceID = obj.GetName()
}
if resourceID == "" {
return nil, fmt.Errorf("cannot resolve resource ID")
}

// Use approved External
externalRef := valueOf(obj.Status.ExternalRef)
if externalRef == "" {
parent := &WorkstationConfigParent{ProjectID: projectID, Location: location, Cluster: clusterID}
id.External = asWorkstationConfigExternal(parent, resourceID)
return id, nil
}

// Validate desired with actual
actualParent, actualResourceID, err := parseWorkstationConfigExternal(externalRef)
if err != nil {
return nil, err
}
if actualParent.ProjectID != projectID {
return nil, fmt.Errorf("spec.projectRef changed, expect %s, got %s", actualParent.ProjectID, projectID)
}
if actualParent.Location != location {
return nil, fmt.Errorf("spec.location changed, expect %s, got %s", actualParent.Location, location)
}
if actualParent.Cluster != clusterID {
return nil, fmt.Errorf("spec.parentRef changed, expect %s, got %s", actualParent.Cluster, clusterID)
}
if actualResourceID != resourceID {
return nil, fmt.Errorf("cannot reset `metadata.name` or `spec.resourceID` to %s, since it has already assigned to %s",
resourceID, actualResourceID)
}
id.External = externalRef
return id, nil
}

func (r *WorkstationConfigRef) Parent() (*WorkstationConfigParent, error) {
if r.External != "" {
parent, _, err := parseWorkstationConfigExternal(r.External)
if err != nil {
return nil, err
}
return parent, nil
}
return nil, fmt.Errorf("WorkstationConfigRef not initialized from `NewWorkstationConfigRef` or `NormalizedExternal`")
}

type WorkstationConfigParent struct {
ProjectID string
Location string
Cluster string
}

func (p *WorkstationConfigParent) String() string {
return "projects/" + p.ProjectID + "/locations/" + p.Location + "/workstationClusters/" + p.Cluster
}

func asWorkstationConfigExternal(parent *WorkstationConfigParent, resourceID string) (external string) {
return parent.String() + "/workstationConfigs/" + resourceID
}

func parseWorkstationConfigExternal(external string) (parent *WorkstationConfigParent, resourceID string, err error) {
external = strings.TrimPrefix(external, "/")
tokens := strings.Split(external, "/")
if len(tokens) != 8 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "workstationClusters" || tokens[6] != "workstationConfigs" {
return nil, "", fmt.Errorf("format of WorkstationConfig external=%q was not known (use projects/<projectID>/locations/<location>/workstationClusters/<workstationclusterID>/workstationConfigs/<workstationconfigID>)", external)
}
parent = &WorkstationConfigParent{
ProjectID: tokens[1],
Location: tokens[3],
Cluster: tokens[5],
}
resourceID = tokens[7]
return parent, resourceID, nil
}
32 changes: 19 additions & 13 deletions apis/workstations/v1alpha1/config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (

var WorkstationConfigGVK = GroupVersion.WithKind("WorkstationConfig")

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// +kcc:proto=google.cloud.workstations.v1.WorkstationConfig.Host
type WorkstationConfig_Host struct {
// Specifies a Compute Engine instance as the host.
Expand Down Expand Up @@ -274,13 +271,6 @@ type WorkstationConfig_ReadinessCheck struct {
// WorkstationConfigSpec defines the desired state of WorkstationConfig
// +kcc:proto=google.cloud.workstations.v1.WorkstationConfig
type WorkstationConfigSpec struct {
// Immutable. The Project that this resource belongs to.
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="ResourceID field is immutable"
ProjectRef *refs.ProjectRef `json:"projectRef"`

// The location of the WorkstationConfig.
Location string `json:"location,omitempty"`

// Parent is a reference to the parent WorkstationCluster for this WorkstationConfig.
Parent *WorkstationClusterRef `json:"parentRef"`

Expand Down Expand Up @@ -398,6 +388,7 @@ type WorkstationConfigStatus struct {
}

// WorkstationConfigObservedState is the state of the WorkstationConfig resource as most recently observed in GCP.
// +kcc:proto=google.cloud.workstations.v1.WorkstationConfig
type WorkstationConfigObservedState struct {
// Output only. A system-assigned unique identifier for this workstation
// configuration.
Expand All @@ -413,11 +404,15 @@ type WorkstationConfigObservedState struct {
// Output only. Time when this workstation configuration was soft-deleted.
DeleteTime *string `json:"deleteTime,omitempty"`

// Optional. Checksum computed by the server. May be sent on update and delete
// requests to make sure that the client has an up-to-date value before
// proceeding.
// Output only. Checksum computed by the server. May be sent on update and
// delete requests to make sure that the client has an up-to-date value
// before proceeding.
Etag *string `json:"etag,omitempty"`

// Output only. Observed state of the runtime host for the workstation
// configuration.
Host *WorkstationConfig_HostObservedState `json:"host,omitempty"`

// Output only. Whether this resource is degraded, in which case it may
// require user action to restore full functionality. See also the
// [conditions][google.cloud.workstations.v1.WorkstationConfig.conditions]
Expand All @@ -426,7 +421,18 @@ type WorkstationConfigObservedState struct {

// Output only. Status conditions describing the current resource state.
GCPConditions []WorkstationServiceGCPCondition `json:"gcpConditions,omitempty"`
}

// WorkstationConfigObservedState is the state of the WorkstationConfig_Host resource as most recently observed in GCP.
// +kcc:proto=google.cloud.workstations.v1.WorkstationConfig.Host
type WorkstationConfig_HostObservedState struct {
// Output only. Observed state of the Compute Engine runtime host for the workstation configuration.
GceInstance *WorkstationConfig_Host_GceInstanceObservedState `json:"gceInstance,omitempty"`
}

// WorkstationConfigObservedState is the state of the WorkstationConfig_Host_GceInstanceObservedState resource as most recently observed in GCP.
// +kcc:proto=google.cloud.workstations.v1.WorkstationConfig.Host.GceInstance
type WorkstationConfig_Host_GceInstanceObservedState struct {
// Output only. Number of instances currently available in the pool for
// faster workstation startup.
PooledInstances *int32 `json:"pooledInstances,omitempty"`
Expand Down
Loading

0 comments on commit 46c118d

Please sign in to comment.