From 021b6cc8458c38307d991657bb786d8197d0e07c Mon Sep 17 00:00:00 2001 From: Amir Balaie Date: Wed, 24 Jan 2024 11:20:32 -0800 Subject: [PATCH] Adding GcpNfsVolume CR --- components/kcp/PROJECT | 9 ++ .../v1beta1/gcpnfsvolume_types.go | 64 +++++++++ .../v1beta1/zz_generated.deepcopy.go | 96 ++++++++++++++ components/kcp/cmd/main.go | 7 +- ...sources.kyma-project.io_gcpnfsvolumes.yaml | 122 ++++++++++++++++++ components/kcp/config/crd/kustomization.yaml | 3 + ...ud-resources_gcpnfsvolume_editor_role.yaml | 31 +++++ ...ud-resources_gcpnfsvolume_viewer_role.yaml | 27 ++++ components/kcp/config/rbac/role.yaml | 26 ++++ .../cloud-resources_v1beta1_gcpnfsvolume.yaml | 12 ++ .../kcp/config/samples/kustomization.yaml | 1 + .../gcpnfsvolume_controller.go | 75 +++++++++++ 12 files changed, 472 insertions(+), 1 deletion(-) create mode 100644 components/kcp/api/cloud-resources/v1beta1/gcpnfsvolume_types.go create mode 100644 components/kcp/config/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml create mode 100644 components/kcp/config/rbac/cloud-resources_gcpnfsvolume_editor_role.yaml create mode 100644 components/kcp/config/rbac/cloud-resources_gcpnfsvolume_viewer_role.yaml create mode 100644 components/kcp/config/samples/cloud-resources_v1beta1_gcpnfsvolume.yaml create mode 100644 components/kcp/internal/controller/cloud-resources/gcpnfsvolume_controller.go diff --git a/components/kcp/PROJECT b/components/kcp/PROJECT index 2ee1b1940..a4b9dddad 100644 --- a/components/kcp/PROJECT +++ b/components/kcp/PROJECT @@ -71,4 +71,13 @@ resources: kind: IpRange path: github.com/kyma-project/cloud-manager/components/kcp/api/cloud-resources/v1beta1 version: v1beta1 +- api: + crdVersion: v1 + namespaced: true + controller: true + domain: kyma-project.io + group: cloud-resources + kind: GcpNfsVolume + path: github.com/kyma-project/cloud-manager/components/kcp/api/cloud-resources/v1beta1 + version: v1beta1 version: "3" diff --git a/components/kcp/api/cloud-resources/v1beta1/gcpnfsvolume_types.go b/components/kcp/api/cloud-resources/v1beta1/gcpnfsvolume_types.go new file mode 100644 index 000000000..e90561b7c --- /dev/null +++ b/components/kcp/api/cloud-resources/v1beta1/gcpnfsvolume_types.go @@ -0,0 +1,64 @@ +/* +Copyright 2023. + +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 ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// GcpNfsVolumeSpec defines the desired state of GcpNfsVolume +type GcpNfsVolumeSpec struct { + // INSERT ADDITIONAL SPEC FIELDS - desired state of cluster + // Important: Run "make" to regenerate code after modifying this file + + // Foo is an example field of GcpNfsVolume. Edit gcpnfsvolume_types.go to remove/update + Foo string `json:"foo,omitempty"` +} + +// GcpNfsVolumeStatus defines the observed state of GcpNfsVolume +type GcpNfsVolumeStatus struct { + // List of status conditions + // +optional + // +listType=map + // +listMapKey=type + Conditions []metav1.Condition `json:"conditions,omitempty"` +} + +//+kubebuilder:object:root=true +//+kubebuilder:subresource:status + +// GcpNfsVolume is the Schema for the gcpnfsvolumes API +type GcpNfsVolume struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec GcpNfsVolumeSpec `json:"spec,omitempty"` + Status GcpNfsVolumeStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// GcpNfsVolumeList contains a list of GcpNfsVolume +type GcpNfsVolumeList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []GcpNfsVolume `json:"items"` +} + +func init() { + SchemeBuilder.Register(&GcpNfsVolume{}, &GcpNfsVolumeList{}) +} diff --git a/components/kcp/api/cloud-resources/v1beta1/zz_generated.deepcopy.go b/components/kcp/api/cloud-resources/v1beta1/zz_generated.deepcopy.go index 60d313f0b..74312fddc 100644 --- a/components/kcp/api/cloud-resources/v1beta1/zz_generated.deepcopy.go +++ b/components/kcp/api/cloud-resources/v1beta1/zz_generated.deepcopy.go @@ -217,6 +217,102 @@ func (in *CloudResourcesStatus) DeepCopy() *CloudResourcesStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GcpNfsVolume) DeepCopyInto(out *GcpNfsVolume) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Spec = in.Spec + in.Status.DeepCopyInto(&out.Status) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GcpNfsVolume. +func (in *GcpNfsVolume) DeepCopy() *GcpNfsVolume { + if in == nil { + return nil + } + out := new(GcpNfsVolume) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *GcpNfsVolume) 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 *GcpNfsVolumeList) DeepCopyInto(out *GcpNfsVolumeList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]GcpNfsVolume, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GcpNfsVolumeList. +func (in *GcpNfsVolumeList) DeepCopy() *GcpNfsVolumeList { + if in == nil { + return nil + } + out := new(GcpNfsVolumeList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *GcpNfsVolumeList) 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 *GcpNfsVolumeSpec) DeepCopyInto(out *GcpNfsVolumeSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GcpNfsVolumeSpec. +func (in *GcpNfsVolumeSpec) DeepCopy() *GcpNfsVolumeSpec { + if in == nil { + return nil + } + out := new(GcpNfsVolumeSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *GcpNfsVolumeStatus) DeepCopyInto(out *GcpNfsVolumeStatus) { + *out = *in + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]v1.Condition, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GcpNfsVolumeStatus. +func (in *GcpNfsVolumeStatus) DeepCopy() *GcpNfsVolumeStatus { + if in == nil { + return nil + } + out := new(GcpNfsVolumeStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *IpRange) DeepCopyInto(out *IpRange) { *out = *in diff --git a/components/kcp/cmd/main.go b/components/kcp/cmd/main.go index fd8963201..1da42d4c6 100644 --- a/components/kcp/cmd/main.go +++ b/components/kcp/cmd/main.go @@ -19,9 +19,10 @@ package main import ( "flag" "fmt" + "os" + kcpscope "github.com/kyma-project/cloud-manager/components/kcp/pkg/kcp/scope" scopeclient "github.com/kyma-project/cloud-manager/components/kcp/pkg/kcp/scope/client" - "os" "github.com/kyma-project/cloud-manager/components/kcp/pkg/common/abstractions" "github.com/kyma-project/cloud-manager/components/kcp/pkg/common/actions/focal" @@ -138,6 +139,10 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "AwsNfsVolume") os.Exit(1) } + if err = cloudresourcescontroller.SetupGcpNfsVolumeReconciler(skrRegistry); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "GcpNfsVolume") + os.Exit(1) + } // KCP Controllers if err = (&cloudcontrolcontroller.ScopeReconciler{ diff --git a/components/kcp/config/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml b/components/kcp/config/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml new file mode 100644 index 000000000..a954bcbd2 --- /dev/null +++ b/components/kcp/config/crd/bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml @@ -0,0 +1,122 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.13.0 + name: gcpnfsvolumes.cloud-resources.kyma-project.io +spec: + group: cloud-resources.kyma-project.io + names: + kind: GcpNfsVolume + listKind: GcpNfsVolumeList + plural: gcpnfsvolumes + singular: gcpnfsvolume + scope: Namespaced + versions: + - name: v1beta1 + schema: + openAPIV3Schema: + description: GcpNfsVolume is the Schema for the gcpnfsvolumes 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: GcpNfsVolumeSpec defines the desired state of GcpNfsVolume + properties: + foo: + description: Foo is an example field of GcpNfsVolume. Edit gcpnfsvolume_types.go + to remove/update + type: string + type: object + status: + description: GcpNfsVolumeStatus defines the observed state of GcpNfsVolume + properties: + conditions: + description: List of status conditions + items: + description: "Condition contains details for one aspect of the current + state of this API Resource. --- This struct is intended for direct + use as an array at the field path .status.conditions. For example, + \n type FooStatus struct{ // Represents the observations of a + foo's current state. // Known .status.conditions.type are: \"Available\", + \"Progressing\", and \"Degraded\" // +patchMergeKey=type // +patchStrategy=merge + // +listType=map // +listMapKey=type Conditions []metav1.Condition + `json:\"conditions,omitempty\" patchStrategy:\"merge\" patchMergeKey:\"type\" + protobuf:\"bytes,1,rep,name=conditions\"` \n // other fields }" + properties: + lastTransitionTime: + description: lastTransitionTime is the last time the condition + transitioned from one status to another. This should be when + the underlying condition changed. If that is not known, then + using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: message is a human readable message indicating + details about the transition. This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: observedGeneration represents the .metadata.generation + that the condition was set based upon. For instance, if .metadata.generation + is currently 12, but the .status.conditions[x].observedGeneration + is 9, the condition is out of date with respect to the current + state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: reason contains a programmatic identifier indicating + the reason for the condition's last transition. Producers + of specific condition types may define expected values and + meanings for this field, and whether the values are considered + a guaranteed API. The value should be a CamelCase string. + This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. + --- Many .condition.type values are consistent across resources + like Available, but because arbitrary conditions can be useful + (see .node.status.conditions), the ability to deconflict is + important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt) + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + x-kubernetes-list-map-keys: + - type + x-kubernetes-list-type: map + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/components/kcp/config/crd/kustomization.yaml b/components/kcp/config/crd/kustomization.yaml index aedb0d1da..edfaf8d8d 100644 --- a/components/kcp/config/crd/kustomization.yaml +++ b/components/kcp/config/crd/kustomization.yaml @@ -9,6 +9,7 @@ resources: - bases/cloud-resources.kyma-project.io_cloudresources.yaml - bases/cloud-resources.kyma-project.io_awsnfsvolumes.yaml - bases/cloud-resources.kyma-project.io_ipranges.yaml +- bases/cloud-resources.kyma-project.io_gcpnfsvolumes.yaml #+kubebuilder:scaffold:crdkustomizeresource patches: @@ -21,6 +22,7 @@ patches: #- path: patches/webhook_in_cloud-resources_cloudresources.yaml #- path: patches/webhook_in_cloud-resources_awsnfsvolumes.yaml #- path: patches/webhook_in_cloud-resources_ipranges.yaml +#- path: patches/webhook_in_gcpnfsvolumes.yaml #+kubebuilder:scaffold:crdkustomizewebhookpatch # [CERTMANAGER] To enable cert-manager, uncomment all the sections with [CERTMANAGER] prefix. @@ -32,6 +34,7 @@ patches: #- path: patches/cainjection_in_cloud-resources_cloudresources.yaml #- path: patches/cainjection_in_cloud-resources_awsnfsvolumes.yaml #- path: patches/cainjection_in_cloud-resources_ipranges.yaml +#- path: patches/cainjection_in_gcpnfsvolumes.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # [WEBHOOK] To enable webhook, uncomment the following section diff --git a/components/kcp/config/rbac/cloud-resources_gcpnfsvolume_editor_role.yaml b/components/kcp/config/rbac/cloud-resources_gcpnfsvolume_editor_role.yaml new file mode 100644 index 000000000..2e8250996 --- /dev/null +++ b/components/kcp/config/rbac/cloud-resources_gcpnfsvolume_editor_role.yaml @@ -0,0 +1,31 @@ +# permissions for end users to edit gcpnfsvolumes. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: gcpnfsvolume-editor-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: cloud-manager + app.kubernetes.io/part-of: cloud-manager + app.kubernetes.io/managed-by: kustomize + name: gcpnfsvolume-editor-role +rules: +- apiGroups: + - cloud-resources.kyma-project.io + resources: + - gcpnfsvolumes + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - cloud-resources.kyma-project.io + resources: + - gcpnfsvolumes/status + verbs: + - get diff --git a/components/kcp/config/rbac/cloud-resources_gcpnfsvolume_viewer_role.yaml b/components/kcp/config/rbac/cloud-resources_gcpnfsvolume_viewer_role.yaml new file mode 100644 index 000000000..e2855cf2f --- /dev/null +++ b/components/kcp/config/rbac/cloud-resources_gcpnfsvolume_viewer_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to view gcpnfsvolumes. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: clusterrole + app.kubernetes.io/instance: gcpnfsvolume-viewer-role + app.kubernetes.io/component: rbac + app.kubernetes.io/created-by: cloud-manager + app.kubernetes.io/part-of: cloud-manager + app.kubernetes.io/managed-by: kustomize + name: gcpnfsvolume-viewer-role +rules: +- apiGroups: + - cloud-resources.kyma-project.io + resources: + - gcpnfsvolumes + verbs: + - get + - list + - watch +- apiGroups: + - cloud-resources.kyma-project.io + resources: + - gcpnfsvolumes/status + verbs: + - get diff --git a/components/kcp/config/rbac/role.yaml b/components/kcp/config/rbac/role.yaml index a8f743a6d..33feab1d9 100644 --- a/components/kcp/config/rbac/role.yaml +++ b/components/kcp/config/rbac/role.yaml @@ -160,6 +160,32 @@ rules: - get - patch - update +- apiGroups: + - cloud-resources.kyma-project.io + resources: + - gcpnfsvolumes + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - cloud-resources.kyma-project.io + resources: + - gcpnfsvolumes/finalizers + verbs: + - update +- apiGroups: + - cloud-resources.kyma-project.io + resources: + - gcpnfsvolumes/status + verbs: + - get + - patch + - update - apiGroups: - cloud-resources.kyma-project.io resources: diff --git a/components/kcp/config/samples/cloud-resources_v1beta1_gcpnfsvolume.yaml b/components/kcp/config/samples/cloud-resources_v1beta1_gcpnfsvolume.yaml new file mode 100644 index 000000000..dc1820fda --- /dev/null +++ b/components/kcp/config/samples/cloud-resources_v1beta1_gcpnfsvolume.yaml @@ -0,0 +1,12 @@ +apiVersion: cloud-resources.kyma-project.io/v1beta1 +kind: GcpNfsVolume +metadata: + labels: + app.kubernetes.io/name: gcpnfsvolume + app.kubernetes.io/instance: gcpnfsvolume-sample + app.kubernetes.io/part-of: cloud-manager + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/created-by: cloud-manager + name: gcpnfsvolume-sample +spec: + # TODO(user): Add fields here diff --git a/components/kcp/config/samples/kustomization.yaml b/components/kcp/config/samples/kustomization.yaml index 98003c427..32236cc5d 100644 --- a/components/kcp/config/samples/kustomization.yaml +++ b/components/kcp/config/samples/kustomization.yaml @@ -7,4 +7,5 @@ resources: - cloud-resources_v1beta1_cloudresources.yaml - cloud-resources_v1beta1_awsnfsvolume.yaml - cloud-resources_v1beta1_iprange.yaml +- cloud-resources_v1beta1_gcpnfsvolume.yaml #+kubebuilder:scaffold:manifestskustomizesamples diff --git a/components/kcp/internal/controller/cloud-resources/gcpnfsvolume_controller.go b/components/kcp/internal/controller/cloud-resources/gcpnfsvolume_controller.go new file mode 100644 index 000000000..540651d24 --- /dev/null +++ b/components/kcp/internal/controller/cloud-resources/gcpnfsvolume_controller.go @@ -0,0 +1,75 @@ +/* +Copyright 2023. + +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 cloudresources + +import ( + "context" + skrruntime "github.com/kyma-project/cloud-manager/components/kcp/pkg/skr/runtime" + "k8s.io/klog/v2" + "sigs.k8s.io/controller-runtime/pkg/cluster" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/log" + + cloudresourcesv1beta1 "github.com/kyma-project/cloud-manager/components/kcp/api/cloud-resources/v1beta1" +) + +type GcpNfsVolumeReconcilerFactory struct{} + +func (f *GcpNfsVolumeReconcilerFactory) New(kymaRef klog.ObjectRef, kcpCluster cluster.Cluster, skrCluster cluster.Cluster) reconcile.Reconciler { + return &AwsNfsVolumeReconciler{ + kymaRef: kymaRef, + kcpCluster: kcpCluster, + skrCluster: skrCluster, + } +} + +// GcpNfsVolumeReconciler reconciles a GcpNfsVolume object +type GcpNfsVolumeReconciler struct { + kymaRef klog.ObjectRef + kcpCluster cluster.Cluster + skrCluster cluster.Cluster +} + +//+kubebuilder:rbac:groups=cloud-resources.kyma-project.io,resources=gcpnfsvolumes,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=cloud-resources.kyma-project.io,resources=gcpnfsvolumes/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=cloud-resources.kyma-project.io,resources=gcpnfsvolumes/finalizers,verbs=update + +// Reconcile is part of the main kubernetes reconciliation loop which aims to +// move the current state of the cluster closer to the desired state. +// TODO(user): Modify the Reconcile function to compare the state specified by +// the GcpNfsVolume object against the actual cluster state, and then +// perform operations to make the cluster state reflect the state specified by +// the user. +// +// For more details, check Reconcile and its Result here: +// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.15.0/pkg/reconcile +func (r *GcpNfsVolumeReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = log.FromContext(ctx) + + // TODO(user): your logic here + + return ctrl.Result{}, nil +} + +func SetupGcpNfsVolumeReconciler(reg skrruntime.SkrRegistry) error { + return reg.Register(). + WithFactory(&GcpNfsVolumeReconcilerFactory{}). + For(&cloudresourcesv1beta1.AwsNfsVolume{}). + Complete() +}