diff --git a/apis/cloudbuild/v1alpha1/groupversion_info.go b/apis/cloudbuild/v1alpha1/groupversion_info.go new file mode 100644 index 0000000000..c55dd0b164 --- /dev/null +++ b/apis/cloudbuild/v1alpha1/groupversion_info.go @@ -0,0 +1,44 @@ +/* +Copyright 2024. + +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 contains API Schema definitions for the cloudbuild v1alpha1 API group +// +kubebuilder:object:generate=true +// +groupName=cloudbuild.cnrm.cloud.google.com +package v1alpha1 + +import ( + "k8s.io/apimachinery/pkg/runtime/schema" + "sigs.k8s.io/controller-runtime/pkg/scheme" +) + +var ( + // GroupVersion is group version used to register these objects + GroupVersion = schema.GroupVersion{Group: "cloudbuild.cnrm.cloud.google.com", Version: "v1alpha1"} + + // SchemeBuilder is used to add go types to the GroupVersionKind scheme + SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion} + + // AddToScheme adds the types in this group-version to the given scheme. + AddToScheme = SchemeBuilder.AddToScheme + + CloudBuildWorkerPoolKind = "CloudBuildWorkerPool" + + GroupVersionKind = schema.GroupVersionKind{ + Group: GroupVersion.Group, + Version: GroupVersion.Version, + Kind: CloudBuildWorkerPoolKind, + } +) diff --git a/apis/cloudbuild/v1alpha1/workerpool_types.go b/apis/cloudbuild/v1alpha1/workerpool_types.go new file mode 100644 index 0000000000..f2661f3293 --- /dev/null +++ b/apis/cloudbuild/v1alpha1/workerpool_types.go @@ -0,0 +1,122 @@ +/* +Copyright 2024. + +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 ( + refv1beta1 "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// CloudBuildWorkerPoolSpec defines the desired state of Instance +type CloudBuildWorkerPoolSpec struct { + Name string `json:"name,omitempty"` + DisplayName string `json:"displayName,omitempty"` + ResourceID *string `json:"resourceID,omitempty"` + // +required + ProjectRef *refv1beta1.ProjectRef `json:"projectRef"` + // +required + Location string `json:"location"` + // +required + PrivatePoolConfig *PrivatePoolV1Config `json:"privatePoolV1Config,omitempty"` +} + +type PrivatePoolV1Config struct { + // +required + WorkerConfig *WorkerConfig `json:"workerConfig,omitempty"` + // +optional + NetworkConfig *NetworkConfig `json:"networkConfig,omitempty"` +} + +type WorkerConfig struct { + // +optional + MachineType string `json:"machineType,omitempty"` + // +optional + DiskSizeGb int64 `json:"diskSizeGb,omitempty"` +} + +type NetworkConfig struct { + // +required + PeeredNetworkRef refv1beta1.ComputeNetworkRef `json:"peeredNetworkRef,omitempty"` + // +optional + EgressOption string `json:"egressOption,omitempty"` + // +optional + PeeredNetworkIPRange string `json:"peeredNetworkIPRange,omitempty"` +} + +// CloudBuildWorkerPoolStatus defines the observed state of Instance +type CloudBuildWorkerPoolStatus struct { + /* Conditions represent the latest available observations of the + object's current state. */ + Conditions []v1alpha1.Condition `json:"conditions,omitempty"` + + /* ObservedGeneration is the generation of the resource that was most recently observed by the Config Connector controller. If this is equal to metadata.generation, then that means that the current reported status reflects the most recent desired state of the resource. */ + // +optional + ObservedGeneration *int64 `json:"observedGeneration,omitempty"` + + /* ObservedState is the state of the resource as most recently observed in GCP. */ + // +optional + ObservedState *CloudBuildWorkerPoolObservedState `json:"observedState,omitempty"` +} + +type CloudBuildWorkerPoolObservedState struct { + /* The creation timestamp of the workerpool.*/ + // +optional + // +kubebuilder:validation:Format=date-time + CreateTime *string `json:"createTime,omitempty"` + + /* The last update timestamp of the workerpool.*/ + // +optional + // +kubebuilder:validation:Format=date-time + UpdateTime *string `json:"updateTime,omitempty"` + // +optional + WorkerConfig *WorkerConfig `json:"workerConfig,omitempty"` + NetworkConfig *NetworkConfigState `json:"networkConfig,omitempty"` +} + +type NetworkConfigState struct { + // +optional + PeeredNetwork string `json:"peeredNetwork,omitempty"` + // +optional + EgressOption string `json:"egressOption,omitempty"` + // +optional + PeeredNetworkIPRange string `json:"peeredNetworkIPRange,omitempty"` +} + +// +kubebuilder:metadata:labels="cnrm.cloud.google.com/managed-by-kcc=true";"cnrm.cloud.google.com/stability-level=beta" +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +// CloudBuildWorkerPool is the Schema for the CloudBuild WorkerPool API +// +kubebuilder:subresource:status +type CloudBuildWorkerPool struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec CloudBuildWorkerPoolSpec `json:"spec,omitempty"` + Status CloudBuildWorkerPoolStatus `json:"status,omitempty"` +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object +// CloudBuildWorkerPoolList contains a list of WorkerPool +type CloudBuildWorkerPoolList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []CloudBuildWorkerPool `json:"items"` +} + +func init() { + SchemeBuilder.Register(&CloudBuildWorkerPool{}, &CloudBuildWorkerPoolList{}) +} diff --git a/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go b/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go new file mode 100644 index 0000000000..2e9aed9c88 --- /dev/null +++ b/apis/cloudbuild/v1alpha1/zz_generated.deepcopy.go @@ -0,0 +1,236 @@ +//go:build !ignore_autogenerated + +// Code generated by controller-gen. DO NOT EDIT. + +package v1alpha1 + +import ( + "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1" + k8sv1alpha1 "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/clients/generated/apis/k8s/v1alpha1" + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CloudBuildWorkerPool) DeepCopyInto(out *CloudBuildWorkerPool) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CloudBuildWorkerPool. +func (in *CloudBuildWorkerPool) DeepCopy() *CloudBuildWorkerPool { + if in == nil { + return nil + } + out := new(CloudBuildWorkerPool) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CloudBuildWorkerPool) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CloudBuildWorkerPoolList) DeepCopyInto(out *CloudBuildWorkerPoolList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]CloudBuildWorkerPool, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CloudBuildWorkerPoolList. +func (in *CloudBuildWorkerPoolList) DeepCopy() *CloudBuildWorkerPoolList { + if in == nil { + return nil + } + out := new(CloudBuildWorkerPoolList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CloudBuildWorkerPoolList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CloudBuildWorkerPoolObservedState) DeepCopyInto(out *CloudBuildWorkerPoolObservedState) { + *out = *in + if in.CreateTime != nil { + in, out := &in.CreateTime, &out.CreateTime + *out = new(string) + **out = **in + } + if in.UpdateTime != nil { + in, out := &in.UpdateTime, &out.UpdateTime + *out = new(string) + **out = **in + } + if in.WorkerConfig != nil { + in, out := &in.WorkerConfig, &out.WorkerConfig + *out = new(WorkerConfig) + **out = **in + } + if in.NetworkConfig != nil { + in, out := &in.NetworkConfig, &out.NetworkConfig + *out = new(NetworkConfigState) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CloudBuildWorkerPoolObservedState. +func (in *CloudBuildWorkerPoolObservedState) DeepCopy() *CloudBuildWorkerPoolObservedState { + if in == nil { + return nil + } + out := new(CloudBuildWorkerPoolObservedState) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CloudBuildWorkerPoolSpec) DeepCopyInto(out *CloudBuildWorkerPoolSpec) { + *out = *in + if in.ResourceID != nil { + in, out := &in.ResourceID, &out.ResourceID + *out = new(string) + **out = **in + } + if in.ProjectRef != nil { + in, out := &in.ProjectRef, &out.ProjectRef + *out = new(v1beta1.ProjectRef) + **out = **in + } + if in.PrivatePoolConfig != nil { + in, out := &in.PrivatePoolConfig, &out.PrivatePoolConfig + *out = new(PrivatePoolV1Config) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CloudBuildWorkerPoolSpec. +func (in *CloudBuildWorkerPoolSpec) DeepCopy() *CloudBuildWorkerPoolSpec { + if in == nil { + return nil + } + out := new(CloudBuildWorkerPoolSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CloudBuildWorkerPoolStatus) DeepCopyInto(out *CloudBuildWorkerPoolStatus) { + *out = *in + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]k8sv1alpha1.Condition, len(*in)) + copy(*out, *in) + } + if in.ObservedGeneration != nil { + in, out := &in.ObservedGeneration, &out.ObservedGeneration + *out = new(int64) + **out = **in + } + if in.ObservedState != nil { + in, out := &in.ObservedState, &out.ObservedState + *out = new(CloudBuildWorkerPoolObservedState) + (*in).DeepCopyInto(*out) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CloudBuildWorkerPoolStatus. +func (in *CloudBuildWorkerPoolStatus) DeepCopy() *CloudBuildWorkerPoolStatus { + if in == nil { + return nil + } + out := new(CloudBuildWorkerPoolStatus) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NetworkConfig) DeepCopyInto(out *NetworkConfig) { + *out = *in + out.PeeredNetworkRef = in.PeeredNetworkRef +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NetworkConfig. +func (in *NetworkConfig) DeepCopy() *NetworkConfig { + if in == nil { + return nil + } + out := new(NetworkConfig) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NetworkConfigState) DeepCopyInto(out *NetworkConfigState) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NetworkConfigState. +func (in *NetworkConfigState) DeepCopy() *NetworkConfigState { + if in == nil { + return nil + } + out := new(NetworkConfigState) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *PrivatePoolV1Config) DeepCopyInto(out *PrivatePoolV1Config) { + *out = *in + if in.WorkerConfig != nil { + in, out := &in.WorkerConfig, &out.WorkerConfig + *out = new(WorkerConfig) + **out = **in + } + if in.NetworkConfig != nil { + in, out := &in.NetworkConfig, &out.NetworkConfig + *out = new(NetworkConfig) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PrivatePoolV1Config. +func (in *PrivatePoolV1Config) DeepCopy() *PrivatePoolV1Config { + if in == nil { + return nil + } + out := new(PrivatePoolV1Config) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *WorkerConfig) DeepCopyInto(out *WorkerConfig) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkerConfig. +func (in *WorkerConfig) DeepCopy() *WorkerConfig { + if in == nil { + return nil + } + out := new(WorkerConfig) + in.DeepCopyInto(out) + return out +} diff --git a/apis/cloudbuild/v1beta1/workerpool_types.go b/apis/cloudbuild/v1beta1/workerpool_types.go index fe44dba761..0ed641b731 100644 --- a/apis/cloudbuild/v1beta1/workerpool_types.go +++ b/apis/cloudbuild/v1beta1/workerpool_types.go @@ -150,6 +150,7 @@ type CloudBuildWorkerPoolObservedState struct { // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // CloudBuildWorkerPool is the Schema for the CloudBuild WorkerPool API // +kubebuilder:subresource:status +// +kubebuilder:storageversion type CloudBuildWorkerPool struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` diff --git a/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml b/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml index 80ddfb41ff..07c58ce43f 100644 --- a/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml +++ b/config/crds/resources/apiextensions.k8s.io_v1_customresourcedefinition_cloudbuildworkerpools.cloudbuild.cnrm.cloud.google.com.yaml @@ -18,6 +18,196 @@ spec: preserveUnknownFields: false scope: Namespaced versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: CloudBuildWorkerPool is the Schema for the CloudBuild WorkerPool + API + properties: + apiVersion: + description: 'APIVersion defines the versioned schema of this representation + of an object. Servers should convert recognized schemas to the latest + internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources' + type: string + kind: + description: 'Kind is a string value representing the REST resource this + object represents. Servers may infer this from the endpoint the client + submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds' + type: string + metadata: + type: object + spec: + description: CloudBuildWorkerPoolSpec defines the desired state of Instance + properties: + displayName: + type: string + location: + type: string + name: + type: string + privatePoolV1Config: + properties: + networkConfig: + properties: + egressOption: + type: string + peeredNetworkIPRange: + type: string + peeredNetworkRef: + oneOf: + - not: + required: + - external + required: + - name + - not: + anyOf: + - required: + - name + - required: + - namespace + required: + - external + properties: + external: + description: The compute network selflink of form "projects//global/networks/", + when not managed by KCC. + type: string + name: + description: The `name` field of a `ComputeNetwork` resource. + type: string + namespace: + description: The `namespace` field of a `ComputeNetwork` + resource. + type: string + type: object + required: + - peeredNetworkRef + type: object + workerConfig: + properties: + diskSizeGb: + format: int64 + type: integer + machineType: + type: string + type: object + required: + - workerConfig + type: object + projectRef: + description: The Project that this resource belongs to. + oneOf: + - not: + required: + - external + required: + - name + - not: + anyOf: + - required: + - name + - required: + - namespace + required: + - external + properties: + external: + description: The `projectID` field of a project, when not managed + by KCC. + type: string + kind: + description: The kind of the Project resource; optional but must + be `Project` if provided. + type: string + name: + description: The `name` field of a `Project` resource. + type: string + namespace: + description: The `namespace` field of a `Project` resource. + type: string + type: object + resourceID: + type: string + required: + - location + - privatePoolV1Config + - projectRef + type: object + status: + description: CloudBuildWorkerPoolStatus defines the observed state of + Instance + properties: + conditions: + description: Conditions represent the latest available observations + of the object's current state. + items: + properties: + lastTransitionTime: + description: Last time the condition transitioned from one status + to another. + type: string + message: + description: Human-readable message indicating details about + last transition. + type: string + reason: + description: Unique, one-word, CamelCase reason for the condition's + last transition. + type: string + status: + description: Status is the status of the condition. Can be True, + False, Unknown. + type: string + type: + description: Type is the type of the condition. + type: string + type: object + type: array + observedGeneration: + description: ObservedGeneration is the generation of the resource + that was most recently observed by the Config Connector controller. + If this is equal to metadata.generation, then that means that the + current reported status reflects the most recent desired state of + the resource. + format: int64 + type: integer + observedState: + description: ObservedState is the state of the resource as most recently + observed in GCP. + properties: + createTime: + description: The creation timestamp of the workerpool. + format: date-time + type: string + networkConfig: + properties: + egressOption: + type: string + peeredNetwork: + type: string + peeredNetworkIPRange: + type: string + type: object + updateTime: + description: The last update timestamp of the workerpool. + format: date-time + type: string + workerConfig: + properties: + diskSizeGb: + format: int64 + type: integer + machineType: + type: string + type: object + type: object + type: object + type: object + served: true + storage: false + subresources: + status: {} - name: v1beta1 schema: openAPIV3Schema: diff --git a/pkg/k8s/crds.go b/pkg/k8s/crds.go index e9c546b958..0c45b177fe 100644 --- a/pkg/k8s/crds.go +++ b/pkg/k8s/crds.go @@ -20,22 +20,46 @@ import ( apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" ) +var scoreForVersion = map[string]int{ + "v1alpha1": 111, + "v1beta1": 121, + "v1": 131, +} + +func PreferredVersion(crd *apiextensions.CustomResourceDefinition) *apiextensions.CustomResourceDefinitionVersion { + bestScore := -1 + var preferredVersion *apiextensions.CustomResourceDefinitionVersion + for _, version := range crd.Spec.Versions { + score, found := scoreForVersion[version.Name] + if !found { + panic(fmt.Sprintf("version %q is not known in getLatestVersion", version.Name)) + } + + if score > bestScore { + preferredVersion = &version + bestScore = score + } + + } + return preferredVersion +} + func GetAPIVersionFromCRD(crd *apiextensions.CustomResourceDefinition) string { panicIfNoVersionPresent(crd) // Currently KCC CRDs only support one version. - return fmt.Sprintf("%v/%v", crd.Spec.Group, crd.Spec.Versions[0].Name) + return fmt.Sprintf("%v/%v", crd.Spec.Group, PreferredVersion(crd).Name) } func GetVersionFromCRD(crd *apiextensions.CustomResourceDefinition) string { panicIfNoVersionPresent(crd) // Currently KCC CRDs only support one version. - return crd.Spec.Versions[0].Name + return PreferredVersion(crd).Name } func GetOpenAPIV3SchemaFromCRD(crd *apiextensions.CustomResourceDefinition) *apiextensions.JSONSchemaProps { panicIfNoVersionPresent(crd) // Currently KCC CRDs only support one version. - return crd.Spec.Versions[0].Schema.OpenAPIV3Schema + return PreferredVersion(crd).Schema.OpenAPIV3Schema } func panicIfNoVersionPresent(crd *apiextensions.CustomResourceDefinition) { diff --git a/scripts/generate-crds/main.go b/scripts/generate-crds/main.go index a36c88b9fd..25f3bd88ff 100644 --- a/scripts/generate-crds/main.go +++ b/scripts/generate-crds/main.go @@ -245,7 +245,7 @@ func addOneOfRulesForMultiTypeResourceReferences(crd *apiextensions.CustomResour } jsonSchema = setOneOfRuleForField(jsonSchema, field, oneOfRule) } - outCRD.Spec.Versions[0].Schema.OpenAPIV3Schema = jsonSchema + k8s.PreferredVersion(outCRD).Schema.OpenAPIV3Schema = jsonSchema return outCRD } diff --git a/scripts/generate-go-crd-clients/generate-types-file.go b/scripts/generate-go-crd-clients/generate-types-file.go index 166393d2b5..dfff89b2a9 100644 --- a/scripts/generate-go-crd-clients/generate-types-file.go +++ b/scripts/generate-go-crd-clients/generate-types-file.go @@ -281,8 +281,7 @@ func constructResourceDefinition(crdsPath, crdFile string) *resourceDefinition { r.Kind = strings.ToLower(crd.Spec.Names.Kind) // TODO: Should we handle multiple versions? - r.Version = &crd.Spec.Versions[0] - + r.Version = k8s.PreferredVersion(crd) return r }