Skip to content

Commit

Permalink
Merge pull request #3412 from jasonvigil/workstationconfig-promote
Browse files Browse the repository at this point in the history
feat: Promote WorkstationConfig to v1beta1
  • Loading branch information
google-oss-prow[bot] authored Dec 19, 2024
2 parents 80e33b5 + 05e927e commit 0249649
Show file tree
Hide file tree
Showing 30 changed files with 3,423 additions and 19 deletions.
2 changes: 1 addition & 1 deletion apis/workstations/v1alpha1/config_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func NewWorkstationConfigIdentity(ctx context.Context, reader client.Reader, obj
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)
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],
Expand Down
2 changes: 1 addition & 1 deletion apis/workstations/v1alpha1/workstation_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func NewWorkstationIdentity(ctx context.Context, reader client.Reader, obj *Work
func ParseWorkstationExternal(external string) (parent *WorkstationParent, resourceID string, err error) {
tokens := strings.Split(external, "/")
if len(tokens) != 10 || tokens[0] != "projects" || tokens[2] != "locations" || tokens[4] != "workstationClusters" || tokens[6] != "workstationConfigs" || tokens[8] != "workstations" {
return nil, "", fmt.Errorf("format of Workstation external=%q was not known (use projects/<projectID>/locations/<location>/workstationClusters/<workstationclusterID>/workstationConfigs/<workstationconfigID>/workstations/<workstationID>)", external)
return nil, "", fmt.Errorf("format of Workstation external=%q was not known (use projects/{{projectID}}/locations/{{location}}/workstationClusters/{{workstationclusterID}}/workstationConfigs/{{workstationconfigID}}/workstations/{{workstationID}})", external)
}
parent = &WorkstationParent{
ProjectID: tokens[1],
Expand Down
2 changes: 1 addition & 1 deletion apis/workstations/v1beta1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ type WorkstationClusterObservedState struct {
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:resource:categories=gcp
// +kubebuilder:subresource:status
// +kubebuilder:storageversion
// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/system=true"
// +kubebuilder:printcolumn:name="Age",JSONPath=".metadata.creationTimestamp",type="date"
// +kubebuilder:printcolumn:name="Ready",JSONPath=".status.conditions[?(@.type=='Ready')].status",type="string",description="When 'True', the most recent reconcile of the resource succeeded"
Expand All @@ -164,6 +163,7 @@ type WorkstationClusterObservedState struct {

// WorkstationCluster is the Schema for the WorkstationCluster API
// +k8s:openapi-gen=true
// +kubebuilder:storageversion
type WorkstationCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand Down
132 changes: 132 additions & 0 deletions apis/workstations/v1beta1/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 v1beta1

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
}
83 changes: 83 additions & 0 deletions apis/workstations/v1beta1/config_reference.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// 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 v1beta1

import (
"context"
"fmt"

refsv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ refsv1beta1.ExternalNormalizer = &WorkstationConfigRef{}

// WorkstationConfigRef defines the resource reference to WorkstationConfig, which "External" field
// holds the GCP identifier for the KRM object.
type WorkstationConfigRef struct {
// A reference to an externally managed WorkstationConfig resource.
// Should be in the format "projects/{{projectID}}/locations/{{location}}/workstationClusters/{{workstationclusterID}}/workstationConfigs/{{workstationconfigID}}".
External string `json:"external,omitempty"`

// The name of a WorkstationConfig resource.
Name string `json:"name,omitempty"`

// The namespace of a WorkstationConfig resource.
Namespace string `json:"namespace,omitempty"`
}

// NormalizedExternal provision the "External" value for other resource that depends on WorkstationConfig.
// If the "External" is given in the other resource's spec.WorkstationConfigRef, the given value will be used.
// Otherwise, the "Name" and "Namespace" will be used to query the actual WorkstationConfig object from the cluster.
func (r *WorkstationConfigRef) NormalizedExternal(ctx context.Context, reader client.Reader, otherNamespace string) (string, error) {
if r.External != "" && r.Name != "" {
return "", fmt.Errorf("cannot specify both name and external on %s reference", WorkstationConfigGVK.Kind)
}
// From given External
if r.External != "" {
if _, _, err := ParseWorkstationConfigExternal(r.External); err != nil {
return "", err
}
return r.External, nil
}

// From the Config Connector object
if r.Namespace == "" {
r.Namespace = otherNamespace
}
key := types.NamespacedName{Name: r.Name, Namespace: r.Namespace}
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(WorkstationConfigGVK)
if err := reader.Get(ctx, key, u); err != nil {
if apierrors.IsNotFound(err) {
return "", k8s.NewReferenceNotFoundError(u.GroupVersionKind(), key)
}
return "", fmt.Errorf("reading referenced %s %s: %w", WorkstationConfigGVK, key, err)
}
// Get external from status.externalRef. This is the most trustworthy place.
actualExternalRef, _, err := unstructured.NestedString(u.Object, "status", "externalRef")
if err != nil {
return "", fmt.Errorf("reading status.externalRef: %w", err)
}
if actualExternalRef == "" {
return "", k8s.NewReferenceNotReadyError(u.GroupVersionKind(), key)
}
r.External = actualExternalRef
return r.External, nil
}
Loading

0 comments on commit 0249649

Please sign in to comment.