From 556dfd1e50ab59c7f3d59a871c165ae943260f49 Mon Sep 17 00:00:00 2001 From: Jason Vigil Date: Mon, 30 Sep 2024 17:59:33 +0000 Subject: [PATCH] fix: Make WorkstationCluster network/subnetwork required --- .../v1alpha1/workstationcluster_types.go | 6 ++- .../v1alpha1/zz_generated.deepcopy.go | 12 +---- ...rs.workstations.cnrm.cloud.google.com.yaml | 2 + .../v1alpha1/workstationcluster_types.go | 6 +-- .../v1alpha1/zz_generated.deepcopy.go | 12 +---- .../workstationcluster_mappings.go | 50 ++++--------------- .../workstationcluster_normalize.go | 25 +++++----- 7 files changed, 36 insertions(+), 77 deletions(-) diff --git a/apis/workstations/v1alpha1/workstationcluster_types.go b/apis/workstations/v1alpha1/workstationcluster_types.go index c93e10a8e9..f6687bab7b 100644 --- a/apis/workstations/v1alpha1/workstationcluster_types.go +++ b/apis/workstations/v1alpha1/workstationcluster_types.go @@ -56,12 +56,14 @@ type WorkstationClusterSpec struct { // Immutable. Reference to the Compute Engine network in which instances associated // with this workstation cluster will be created. - NetworkRef *refs.ComputeNetworkRef `json:"networkRef,omitempty"` + // +required + NetworkRef refs.ComputeNetworkRef `json:"networkRef"` // Immutable. Reference to the Compute Engine subnetwork in which instances // associated with this workstation cluster will be created. Must be part of // the subnetwork specified for this workstation cluster. - SubnetworkRef *refs.ComputeSubnetworkRef `json:"subnetworkRef,omitempty"` + // +required + SubnetworkRef refs.ComputeSubnetworkRef `json:"subnetworkRef"` // Optional. Configuration for private workstation cluster. PrivateClusterConfig *WorkstationCluster_PrivateClusterConfig `json:"privateClusterConfig,omitempty"` diff --git a/apis/workstations/v1alpha1/zz_generated.deepcopy.go b/apis/workstations/v1alpha1/zz_generated.deepcopy.go index e10d5482d0..4af5f5102b 100644 --- a/apis/workstations/v1alpha1/zz_generated.deepcopy.go +++ b/apis/workstations/v1alpha1/zz_generated.deepcopy.go @@ -220,16 +220,8 @@ func (in *WorkstationClusterSpec) DeepCopyInto(out *WorkstationClusterSpec) { *out = make([]WorkstationClusterLabel, len(*in)) copy(*out, *in) } - if in.NetworkRef != nil { - in, out := &in.NetworkRef, &out.NetworkRef - *out = new(v1beta1.ComputeNetworkRef) - **out = **in - } - if in.SubnetworkRef != nil { - in, out := &in.SubnetworkRef, &out.SubnetworkRef - *out = new(v1beta1.ComputeSubnetworkRef) - **out = **in - } + out.NetworkRef = in.NetworkRef + out.SubnetworkRef = in.SubnetworkRef if in.PrivateClusterConfig != nil { in, out := &in.PrivateClusterConfig, &out.PrivateClusterConfig *out = new(WorkstationCluster_PrivateClusterConfig) diff --git a/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_workstationclusters.workstations.cnrm.cloud.google.com.yaml b/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_workstationclusters.workstations.cnrm.cloud.google.com.yaml index f27c4145c8..46603dfdac 100644 --- a/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_workstationclusters.workstations.cnrm.cloud.google.com.yaml +++ b/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_workstationclusters.workstations.cnrm.cloud.google.com.yaml @@ -240,7 +240,9 @@ spec: type: string type: object required: + - networkRef - projectRef + - subnetworkRef type: object status: description: WorkstationClusterStatus defines the config connector machine diff --git a/pkg/clients/generated/apis/workstations/v1alpha1/workstationcluster_types.go b/pkg/clients/generated/apis/workstations/v1alpha1/workstationcluster_types.go index 115f795863..f597b69fa1 100644 --- a/pkg/clients/generated/apis/workstations/v1alpha1/workstationcluster_types.go +++ b/pkg/clients/generated/apis/workstations/v1alpha1/workstationcluster_types.go @@ -101,8 +101,7 @@ type WorkstationClusterSpec struct { Location *string `json:"location,omitempty"` /* Immutable. Reference to the Compute Engine network in which instances associated with this workstation cluster will be created. */ - // +optional - NetworkRef *v1alpha1.ResourceRef `json:"networkRef,omitempty"` + NetworkRef v1alpha1.ResourceRef `json:"networkRef"` /* Optional. Configuration for private workstation cluster. */ // +optional @@ -116,8 +115,7 @@ type WorkstationClusterSpec struct { ResourceID *string `json:"resourceID,omitempty"` /* Immutable. Reference to the Compute Engine subnetwork in which instances associated with this workstation cluster will be created. Must be part of the subnetwork specified for this workstation cluster. */ - // +optional - SubnetworkRef *v1alpha1.ResourceRef `json:"subnetworkRef,omitempty"` + SubnetworkRef v1alpha1.ResourceRef `json:"subnetworkRef"` } type WorkstationclusterGcpConditionsStatus struct { diff --git a/pkg/clients/generated/apis/workstations/v1alpha1/zz_generated.deepcopy.go b/pkg/clients/generated/apis/workstations/v1alpha1/zz_generated.deepcopy.go index a81c82c514..c07eabab08 100644 --- a/pkg/clients/generated/apis/workstations/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/clients/generated/apis/workstations/v1alpha1/zz_generated.deepcopy.go @@ -117,11 +117,7 @@ func (in *WorkstationClusterSpec) DeepCopyInto(out *WorkstationClusterSpec) { *out = new(string) **out = **in } - if in.NetworkRef != nil { - in, out := &in.NetworkRef, &out.NetworkRef - *out = new(k8sv1alpha1.ResourceRef) - **out = **in - } + out.NetworkRef = in.NetworkRef if in.PrivateClusterConfig != nil { in, out := &in.PrivateClusterConfig, &out.PrivateClusterConfig *out = new(WorkstationclusterPrivateClusterConfig) @@ -133,11 +129,7 @@ func (in *WorkstationClusterSpec) DeepCopyInto(out *WorkstationClusterSpec) { *out = new(string) **out = **in } - if in.SubnetworkRef != nil { - in, out := &in.SubnetworkRef, &out.SubnetworkRef - *out = new(k8sv1alpha1.ResourceRef) - **out = **in - } + out.SubnetworkRef = in.SubnetworkRef return } diff --git a/pkg/controller/direct/workstations/workstationcluster_mappings.go b/pkg/controller/direct/workstations/workstationcluster_mappings.go index 1163105b26..fc3e4e051f 100644 --- a/pkg/controller/direct/workstations/workstationcluster_mappings.go +++ b/pkg/controller/direct/workstations/workstationcluster_mappings.go @@ -31,8 +31,8 @@ func WorkstationClusterSpec_ToProto(mapCtx *direct.MapContext, in *krm.Workstati DisplayName: direct.ValueOf(in.DisplayName), Annotations: WorkstationClusterAnnotations_ToProto(mapCtx, in.Annotations), Labels: WorkstationClusterLabels_ToProto(mapCtx, in.Labels), - Network: WorkstationClusterNetworkRef_ToProto(mapCtx, in.NetworkRef), - Subnetwork: WorkstationClusterSubnetworkRef_ToProto(mapCtx, in.SubnetworkRef), + Network: in.NetworkRef.External, + Subnetwork: in.SubnetworkRef.External, PrivateClusterConfig: WorkstationCluster_PrivateClusterConfig_ToProto(mapCtx, in.PrivateClusterConfig), } return out @@ -60,20 +60,6 @@ func WorkstationClusterLabels_ToProto(mapCtx *direct.MapContext, in []krm.Workst return out } -func WorkstationClusterNetworkRef_ToProto(mapCtx *direct.MapContext, in *refs.ComputeNetworkRef) string { - if in == nil { - return "" - } - return in.External -} - -func WorkstationClusterSubnetworkRef_ToProto(mapCtx *direct.MapContext, in *refs.ComputeSubnetworkRef) string { - if in == nil { - return "" - } - return in.External -} - func WorkstationCluster_PrivateClusterConfig_ToProto(mapCtx *direct.MapContext, in *krm.WorkstationCluster_PrivateClusterConfig) *pb.WorkstationCluster_PrivateClusterConfig { if in == nil { return nil @@ -101,11 +87,15 @@ func WorkstationClusterSpec_FromProto(mapCtx *direct.MapContext, in *pb.Workstat return nil } out := &krm.WorkstationClusterSpec{ - DisplayName: direct.LazyPtr(in.GetDisplayName()), - Annotations: WorkstationClusterAnnotations_FromProto(mapCtx, in.GetAnnotations()), - Labels: WorkstationClusterLabels_FromProto(mapCtx, in.GetLabels()), - NetworkRef: WorkstationClusterNetworkRef_FromProto(mapCtx, in.GetNetwork()), - SubnetworkRef: WorkstationClusterSubnetworkRef_FromProto(mapCtx, in.GetSubnetwork()), + DisplayName: direct.LazyPtr(in.GetDisplayName()), + Annotations: WorkstationClusterAnnotations_FromProto(mapCtx, in.GetAnnotations()), + Labels: WorkstationClusterLabels_FromProto(mapCtx, in.GetLabels()), + NetworkRef: refs.ComputeNetworkRef{ + External: in.GetNetwork(), + }, + SubnetworkRef: refs.ComputeSubnetworkRef{ + External: in.GetSubnetwork(), + }, PrivateClusterConfig: WorkstationCluster_PrivateClusterConfig_FromProto(mapCtx, in.GetPrivateClusterConfig()), } return out @@ -139,24 +129,6 @@ func WorkstationClusterLabels_FromProto(mapCtx *direct.MapContext, in map[string return out } -func WorkstationClusterNetworkRef_FromProto(mapCtx *direct.MapContext, in string) *refs.ComputeNetworkRef { - if in == "" { - return nil - } - return &refs.ComputeNetworkRef{ - External: in, - } -} - -func WorkstationClusterSubnetworkRef_FromProto(mapCtx *direct.MapContext, in string) *refs.ComputeSubnetworkRef { - if in == "" { - return nil - } - return &refs.ComputeSubnetworkRef{ - External: in, - } -} - func WorkstationCluster_PrivateClusterConfig_FromProto(mapCtx *direct.MapContext, in *pb.WorkstationCluster_PrivateClusterConfig) *krm.WorkstationCluster_PrivateClusterConfig { if in == nil { return nil diff --git a/pkg/controller/direct/workstations/workstationcluster_normalize.go b/pkg/controller/direct/workstations/workstationcluster_normalize.go index 7e66cbc939..f256363d2f 100644 --- a/pkg/controller/direct/workstations/workstationcluster_normalize.go +++ b/pkg/controller/direct/workstations/workstationcluster_normalize.go @@ -24,20 +24,21 @@ import ( ) func NormalizeWorkstationCluster(ctx context.Context, kube client.Reader, obj *krm.WorkstationCluster) error { - if obj.Spec.NetworkRef != nil { - network, err := refs.ResolveComputeNetwork(ctx, kube, obj, obj.Spec.NetworkRef) - if err != nil { - return err - } - obj.Spec.NetworkRef.External = network.String() + // Resolve network. + network, err := refs.ResolveComputeNetwork(ctx, kube, obj, &obj.Spec.NetworkRef) + if err != nil { + return err } - if obj.Spec.SubnetworkRef != nil { - subnet, err := refs.ResolveComputeSubnetwork(ctx, kube, obj, obj.Spec.SubnetworkRef) - if err != nil { - return err - } - obj.Spec.SubnetworkRef.External = subnet.External + obj.Spec.NetworkRef.External = network.String() + + // Resolve subnetwork. + subnet, err := refs.ResolveComputeSubnetwork(ctx, kube, obj, &obj.Spec.SubnetworkRef) + if err != nil { + return err } + obj.Spec.SubnetworkRef.External = subnet.External + + // Resolve projects (in private cluster config). if obj.Spec.PrivateClusterConfig != nil && obj.Spec.PrivateClusterConfig.AllowedProjects != nil { var resolvedProjects []refs.ProjectRef for _, projectRef := range obj.Spec.PrivateClusterConfig.AllowedProjects {