From 704346fc89551cd3290b988e380a78d360486b44 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Wed, 30 Oct 2024 13:25:52 +0100 Subject: [PATCH] api: add NetworkFenceClass CRD adding NetworkFenceClass which is going to be used to get the client details to be fenced. The controller will watch for the NetworkFenceClass CR and sends a periodic request to all the sidecars registered with the matching driver name that registered the GET_CLIENTS_TO_FENCE caps to get the client details, once the controller gets the client details its going to update the CSIAddonsNode object status with the client information that can be used to do Network fencing and unfencing. Signed-off-by: Madhu Rajanna --- PROJECT | 8 ++ .../v1alpha1/networkfenceclass_types.go | 67 +++++++++++++ .../v1alpha1/zz_generated.deepcopy.go | 96 +++++++++++++++++++ cmd/manager/main.go | 8 ++ ...dons.openshift.io_networkfenceclasses.yaml | 74 ++++++++++++++ config/crd/kustomization.yaml | 2 + .../bases/clusterserviceversion.yaml.in | 5 + ...iaddons_networkfenceclass_editor_role.yaml | 27 ++++++ ...iaddons_networkfenceclass_viewer_role.yaml | 23 +++++ config/rbac/kustomization.yaml | 2 + config/rbac/role.yaml | 3 + .../csiaddons_v1alpha1_networkfenceclass.yaml | 9 ++ config/samples/kustomization.yaml | 1 + deploy/controller/crds.yaml | 74 ++++++++++++++ deploy/controller/rbac.yaml | 53 ++++++++++ .../csiaddons/networkfenceclass_controller.go | 60 ++++++++++++ .../networkfenceclass_controller_test.go | 84 ++++++++++++++++ 17 files changed, 596 insertions(+) create mode 100644 api/csiaddons/v1alpha1/networkfenceclass_types.go create mode 100644 config/crd/bases/csiaddons.openshift.io_networkfenceclasses.yaml create mode 100644 config/rbac/csiaddons_networkfenceclass_editor_role.yaml create mode 100644 config/rbac/csiaddons_networkfenceclass_viewer_role.yaml create mode 100644 config/samples/csiaddons_v1alpha1_networkfenceclass.yaml create mode 100644 internal/controller/csiaddons/networkfenceclass_controller.go create mode 100644 internal/controller/csiaddons/networkfenceclass_controller_test.go diff --git a/PROJECT b/PROJECT index ae473ee78..bd7da76a3 100644 --- a/PROJECT +++ b/PROJECT @@ -105,4 +105,12 @@ resources: kind: EncryptionKeyRotationCronJob path: github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1 version: v1alpha1 +- api: + crdVersion: v1 + controller: true + domain: openshift.io + group: csiaddons + kind: NetworkFenceClass + path: github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1 + version: v1alpha1 version: "3" diff --git a/api/csiaddons/v1alpha1/networkfenceclass_types.go b/api/csiaddons/v1alpha1/networkfenceclass_types.go new file mode 100644 index 000000000..6db4e4fa9 --- /dev/null +++ b/api/csiaddons/v1alpha1/networkfenceclass_types.go @@ -0,0 +1,67 @@ +/* +Copyright 2022 The Kubernetes-CSI-Addons Authors. + +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 ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// NetworkFenceClassSpec specifies parameters that an underlying storage system uses +// to get client for network fencing. Upon creating a NetworkFenceClass object, a RPC will be set +// to the storage system that matches the provisioner to get the client for network fencing. +// +kubebuilder:validation:XValidation:rule="has(self.parameters) == has(oldSelf.parameters)",message="parameters are immutable" +type NetworkFenceClassSpec struct { + // Provisioner is the name of storage provisioner + // +kubebuilder:validation:Required + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="provisioner is immutable" + Provisioner string `json:"provisioner"` + // Parameters is a key-value map with storage provisioner specific configurations for + // creating volume replicas + // +kubebuilder:validation:Optional + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="parameters are immutable" + Parameters map[string]string `json:"parameters,omitempty"` +} + +// NetworkFenceClassStatus defines the observed state of NetworkFenceClass +type NetworkFenceClassStatus struct { +} + +//+kubebuilder:object:root=true +//+kubebuilder:subresource:status +//+kubebuilder:resource:scope=Cluster + +// NetworkFenceClass is the Schema for the networkfenceclasses API +type NetworkFenceClass struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec NetworkFenceClassSpec `json:"spec,omitempty"` + Status NetworkFenceClassStatus `json:"status,omitempty"` +} + +//+kubebuilder:object:root=true + +// NetworkFenceClassList contains a list of NetworkFenceClass +type NetworkFenceClassList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []NetworkFenceClass `json:"items"` +} + +func init() { + SchemeBuilder.Register(&NetworkFenceClass{}, &NetworkFenceClassList{}) +} diff --git a/api/csiaddons/v1alpha1/zz_generated.deepcopy.go b/api/csiaddons/v1alpha1/zz_generated.deepcopy.go index afd8974f9..7368cc8d2 100644 --- a/api/csiaddons/v1alpha1/zz_generated.deepcopy.go +++ b/api/csiaddons/v1alpha1/zz_generated.deepcopy.go @@ -413,6 +413,102 @@ func (in *NetworkFence) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NetworkFenceClass) DeepCopyInto(out *NetworkFenceClass) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + out.Status = in.Status +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NetworkFenceClass. +func (in *NetworkFenceClass) DeepCopy() *NetworkFenceClass { + if in == nil { + return nil + } + out := new(NetworkFenceClass) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *NetworkFenceClass) 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 *NetworkFenceClassList) DeepCopyInto(out *NetworkFenceClassList) { + *out = *in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]NetworkFenceClass, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NetworkFenceClassList. +func (in *NetworkFenceClassList) DeepCopy() *NetworkFenceClassList { + if in == nil { + return nil + } + out := new(NetworkFenceClassList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *NetworkFenceClassList) 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 *NetworkFenceClassSpec) DeepCopyInto(out *NetworkFenceClassSpec) { + *out = *in + if in.Parameters != nil { + in, out := &in.Parameters, &out.Parameters + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NetworkFenceClassSpec. +func (in *NetworkFenceClassSpec) DeepCopy() *NetworkFenceClassSpec { + if in == nil { + return nil + } + out := new(NetworkFenceClassSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NetworkFenceClassStatus) DeepCopyInto(out *NetworkFenceClassStatus) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NetworkFenceClassStatus. +func (in *NetworkFenceClassStatus) DeepCopy() *NetworkFenceClassStatus { + if in == nil { + return nil + } + out := new(NetworkFenceClassStatus) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NetworkFenceList) DeepCopyInto(out *NetworkFenceList) { *out = *in diff --git a/cmd/manager/main.go b/cmd/manager/main.go index 7ddd98424..df218544b 100644 --- a/cmd/manager/main.go +++ b/cmd/manager/main.go @@ -234,6 +234,14 @@ func main() { setupLog.Error(err, "unable to create controller", "controller", "EncryptionKeyRotationCronJob") os.Exit(1) } + + if err = (&controllers.NetworkFenceClassReconciler{ + Client: mgr.GetClient(), + Scheme: mgr.GetScheme(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "NetworkFenceClass") + os.Exit(1) + } //+kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/config/crd/bases/csiaddons.openshift.io_networkfenceclasses.yaml b/config/crd/bases/csiaddons.openshift.io_networkfenceclasses.yaml new file mode 100644 index 000000000..e89f94a8a --- /dev/null +++ b/config/crd/bases/csiaddons.openshift.io_networkfenceclasses.yaml @@ -0,0 +1,74 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.16.5 + name: networkfenceclasses.csiaddons.openshift.io +spec: + group: csiaddons.openshift.io + names: + kind: NetworkFenceClass + listKind: NetworkFenceClassList + plural: networkfenceclasses + singular: networkfenceclass + scope: Cluster + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: NetworkFenceClass is the Schema for the networkfenceclasses 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: |- + NetworkFenceClassSpec specifies parameters that an underlying storage system uses + to get client for network fencing. Upon creating a NetworkFenceClass object, a RPC will be set + to the storage system that matches the provisioner to get the client for network fencing. + properties: + parameters: + additionalProperties: + type: string + description: |- + Parameters is a key-value map with storage provisioner specific configurations for + creating volume replicas + type: object + x-kubernetes-validations: + - message: parameters are immutable + rule: self == oldSelf + provisioner: + description: Provisioner is the name of storage provisioner + type: string + x-kubernetes-validations: + - message: provisioner is immutable + rule: self == oldSelf + required: + - provisioner + type: object + x-kubernetes-validations: + - message: parameters are immutable + rule: has(self.parameters) == has(oldSelf.parameters) + status: + description: NetworkFenceClassStatus defines the observed state of NetworkFenceClass + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/config/crd/kustomization.yaml b/config/crd/kustomization.yaml index c566456c8..6559e81ca 100644 --- a/config/crd/kustomization.yaml +++ b/config/crd/kustomization.yaml @@ -17,6 +17,7 @@ resources: # yamllint disable-line rule:comments - bases/csiaddons.openshift.io_encryptionkeyrotationjobs.yaml - bases/csiaddons.openshift.io_encryptionkeyrotationcronjobs.yaml +- bases/csiaddons.openshift.io_networkfenceclasses.yaml #+kubebuilder:scaffold:crdkustomizeresource patchesStrategicMerge: @@ -42,6 +43,7 @@ patchesStrategicMerge: #- patches/cainjection_in_volumegroupreplicationcontents.yaml #- path: patches/cainjection_in_csiaddons_encryptionkeyrotationjobs.yaml #- path: patches/cainjection_in_csiaddons_encryptionkeyrotationcronjobs.yaml +#- path: patches/cainjection_in_csiaddons_networkfenceclasses.yaml #+kubebuilder:scaffold:crdkustomizecainjectionpatch # the following config is for teaching kustomize how to do kustomization for CRDs. diff --git a/config/manifests/bases/clusterserviceversion.yaml.in b/config/manifests/bases/clusterserviceversion.yaml.in index 07584ed69..1db682066 100644 --- a/config/manifests/bases/clusterserviceversion.yaml.in +++ b/config/manifests/bases/clusterserviceversion.yaml.in @@ -66,6 +66,11 @@ spec: kind: VolumeGroupReplicationContent name: volumegroupreplicationcontents.replication.storage.openshift.io version: v1alpha1 + - description: NetworkFenceClass is the Schema for the networkfenceclasses API + displayName: Network Fence Class + kind: NetworkFenceClass + name: networkfenceclasses.csiaddons.openshift.io + version: v1alpha1 description: CSI Addons provides the CSI Addons Controller that enables advanced storage operations for CSI-drivers. displayName: CSI Addons diff --git a/config/rbac/csiaddons_networkfenceclass_editor_role.yaml b/config/rbac/csiaddons_networkfenceclass_editor_role.yaml new file mode 100644 index 000000000..963831e31 --- /dev/null +++ b/config/rbac/csiaddons_networkfenceclass_editor_role.yaml @@ -0,0 +1,27 @@ +# permissions for end users to edit networkfenceclasses. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: kubernetes-csi-addons + app.kubernetes.io/managed-by: kustomize + name: csiaddons-networkfenceclass-editor-role +rules: +- apiGroups: + - csiaddons.openshift.io + resources: + - networkfenceclasses + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - csiaddons.openshift.io + resources: + - networkfenceclasses/status + verbs: + - get diff --git a/config/rbac/csiaddons_networkfenceclass_viewer_role.yaml b/config/rbac/csiaddons_networkfenceclass_viewer_role.yaml new file mode 100644 index 000000000..505ce430a --- /dev/null +++ b/config/rbac/csiaddons_networkfenceclass_viewer_role.yaml @@ -0,0 +1,23 @@ +# permissions for end users to view networkfenceclasses. +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/name: kubernetes-csi-addons + app.kubernetes.io/managed-by: kustomize + name: csiaddons-networkfenceclass-viewer-role +rules: +- apiGroups: + - csiaddons.openshift.io + resources: + - networkfenceclasses + verbs: + - get + - list + - watch +- apiGroups: + - csiaddons.openshift.io + resources: + - networkfenceclasses/status + verbs: + - get diff --git a/config/rbac/kustomization.yaml b/config/rbac/kustomization.yaml index f7626b084..9a9639b88 100644 --- a/config/rbac/kustomization.yaml +++ b/config/rbac/kustomization.yaml @@ -31,6 +31,8 @@ resources: # default, aiding admins in cluster management. Those roles are # not used by the Project itself. You can comment the following lines # if you do not want those helpers be installed with your Project. +- csiaddons_networkfenceclass_editor_role.yaml +- csiaddons_networkfenceclass_viewer_role.yaml # - csiaddons_encryptionkeyrotationcronjob_editor_role.yaml # - csiaddons_encryptionkeyrotationcronjob_viewer_role.yaml # - csiaddons_encryptionkeyrotationjob_editor_role.yaml diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index 232df6d5b..e311fe0fc 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -44,6 +44,7 @@ rules: - csiaddonsnodes - encryptionkeyrotationcronjobs - encryptionkeyrotationjobs + - networkfenceclasses - networkfences - reclaimspacecronjobs - reclaimspacejobs @@ -61,6 +62,7 @@ rules: - csiaddonsnodes/finalizers - encryptionkeyrotationcronjobs/finalizers - encryptionkeyrotationjobs/finalizers + - networkfenceclasses/finalizers - networkfences/finalizers - reclaimspacecronjobs/finalizers - reclaimspacejobs/finalizers @@ -72,6 +74,7 @@ rules: - csiaddonsnodes/status - encryptionkeyrotationcronjobs/status - encryptionkeyrotationjobs/status + - networkfenceclasses/status - networkfences/status - reclaimspacecronjobs/status - reclaimspacejobs/status diff --git a/config/samples/csiaddons_v1alpha1_networkfenceclass.yaml b/config/samples/csiaddons_v1alpha1_networkfenceclass.yaml new file mode 100644 index 000000000..ee40729f5 --- /dev/null +++ b/config/samples/csiaddons_v1alpha1_networkfenceclass.yaml @@ -0,0 +1,9 @@ +apiVersion: csiaddons.openshift.io/v1alpha1 +kind: NetworkFenceClass +metadata: + labels: + app.kubernetes.io/name: kubernetes-csi-addons + app.kubernetes.io/managed-by: kustomize + name: networkfenceclass-sample +spec: + # TODO(user): Add fields here diff --git a/config/samples/kustomization.yaml b/config/samples/kustomization.yaml index d1cbb258e..e1ae43a59 100644 --- a/config/samples/kustomization.yaml +++ b/config/samples/kustomization.yaml @@ -11,4 +11,5 @@ resources: - replication.storage_v1alpha1_volumereplication.yaml - csiaddons_v1alpha1_encryptionkeyrotationjob.yaml - csiaddons_v1alpha1_encryptionkeyrotationcronjob.yaml +- csiaddons_v1alpha1_networkfenceclass.yaml # +kubebuilder:scaffold:manifestskustomizesamples diff --git a/deploy/controller/crds.yaml b/deploy/controller/crds.yaml index 5999d0f19..7c14b7e10 100644 --- a/deploy/controller/crds.yaml +++ b/deploy/controller/crds.yaml @@ -552,6 +552,80 @@ spec: --- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.16.5 + name: networkfenceclasses.csiaddons.openshift.io +spec: + group: csiaddons.openshift.io + names: + kind: NetworkFenceClass + listKind: NetworkFenceClassList + plural: networkfenceclasses + singular: networkfenceclass + scope: Cluster + versions: + - name: v1alpha1 + schema: + openAPIV3Schema: + description: NetworkFenceClass is the Schema for the networkfenceclasses 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: |- + NetworkFenceClassSpec specifies parameters that an underlying storage system uses + to get client for network fencing. Upon creating a NetworkFenceClass object, a RPC will be set + to the storage system that matches the provisioner to get the client for network fencing. + properties: + parameters: + additionalProperties: + type: string + description: |- + Parameters is a key-value map with storage provisioner specific configurations for + creating volume replicas + type: object + x-kubernetes-validations: + - message: parameters are immutable + rule: self == oldSelf + provisioner: + description: Provisioner is the name of storage provisioner + type: string + x-kubernetes-validations: + - message: provisioner is immutable + rule: self == oldSelf + required: + - provisioner + type: object + x-kubernetes-validations: + - message: parameters are immutable + rule: has(self.parameters) == has(oldSelf.parameters) + status: + description: NetworkFenceClassStatus defines the observed state of NetworkFenceClass + type: object + type: object + served: true + storage: true + subresources: + status: {} +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition metadata: annotations: controller-gen.kubebuilder.io/version: v0.16.5 diff --git a/deploy/controller/rbac.yaml b/deploy/controller/rbac.yaml index 539d6b928..8dbaf23c3 100644 --- a/deploy/controller/rbac.yaml +++ b/deploy/controller/rbac.yaml @@ -44,6 +44,56 @@ rules: --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole +metadata: + labels: + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/name: kubernetes-csi-addons + name: csi-addons-csiaddons-networkfenceclass-editor-role +rules: +- apiGroups: + - csiaddons.openshift.io + resources: + - networkfenceclasses + verbs: + - create + - delete + - get + - list + - patch + - update + - watch +- apiGroups: + - csiaddons.openshift.io + resources: + - networkfenceclasses/status + verbs: + - get +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + labels: + app.kubernetes.io/managed-by: kustomize + app.kubernetes.io/name: kubernetes-csi-addons + name: csi-addons-csiaddons-networkfenceclass-viewer-role +rules: +- apiGroups: + - csiaddons.openshift.io + resources: + - networkfenceclasses + verbs: + - get + - list + - watch +- apiGroups: + - csiaddons.openshift.io + resources: + - networkfenceclasses/status + verbs: + - get +--- +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole metadata: name: csi-addons-manager-role rules: @@ -87,6 +137,7 @@ rules: - csiaddonsnodes - encryptionkeyrotationcronjobs - encryptionkeyrotationjobs + - networkfenceclasses - networkfences - reclaimspacecronjobs - reclaimspacejobs @@ -104,6 +155,7 @@ rules: - csiaddonsnodes/finalizers - encryptionkeyrotationcronjobs/finalizers - encryptionkeyrotationjobs/finalizers + - networkfenceclasses/finalizers - networkfences/finalizers - reclaimspacecronjobs/finalizers - reclaimspacejobs/finalizers @@ -115,6 +167,7 @@ rules: - csiaddonsnodes/status - encryptionkeyrotationcronjobs/status - encryptionkeyrotationjobs/status + - networkfenceclasses/status - networkfences/status - reclaimspacecronjobs/status - reclaimspacejobs/status diff --git a/internal/controller/csiaddons/networkfenceclass_controller.go b/internal/controller/csiaddons/networkfenceclass_controller.go new file mode 100644 index 000000000..e37f05853 --- /dev/null +++ b/internal/controller/csiaddons/networkfenceclass_controller.go @@ -0,0 +1,60 @@ +/* +Copyright 2024 The Kubernetes-CSI-Addons Authors. + +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 controller + +import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + + csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1" +) + +// NetworkFenceClassReconciler reconciles a NetworkFenceClass object +type NetworkFenceClassReconciler struct { + client.Client + Scheme *runtime.Scheme +} + +//+kubebuilder:rbac:groups=csiaddons.openshift.io,resources=networkfenceclasses,verbs=get;list;watch;create;update;patch;delete +//+kubebuilder:rbac:groups=csiaddons.openshift.io,resources=networkfenceclasses/status,verbs=get;update;patch +//+kubebuilder:rbac:groups=csiaddons.openshift.io,resources=networkfenceclasses/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 NetworkFenceClass 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.17.3/pkg/reconcile +func (r *NetworkFenceClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + _ = log.FromContext(ctx) + + return ctrl.Result{}, nil +} + +// SetupWithManager sets up the controller with the Manager. +func (r *NetworkFenceClassReconciler) SetupWithManager(mgr ctrl.Manager) error { + return ctrl.NewControllerManagedBy(mgr). + For(&csiaddonsv1alpha1.NetworkFenceClass{}). + Complete(r) +} diff --git a/internal/controller/csiaddons/networkfenceclass_controller_test.go b/internal/controller/csiaddons/networkfenceclass_controller_test.go new file mode 100644 index 000000000..5560577a9 --- /dev/null +++ b/internal/controller/csiaddons/networkfenceclass_controller_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2024 The Kubernetes-CSI-Addons Authors. + +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 controller + +import ( + "context" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + csiaddonsv1alpha1 "github.com/csi-addons/kubernetes-csi-addons/api/csiaddons/v1alpha1" +) + +var _ = Describe("NetworkFenceClass Controller", func() { + Context("When reconciling a resource", func() { + const resourceName = "test-resource" + + ctx := context.Background() + + typeNamespacedName := types.NamespacedName{ + Name: resourceName, + Namespace: "default", // TODO(user):Modify as needed + } + networkfenceclass := &csiaddonsv1alpha1.NetworkFenceClass{} + + BeforeEach(func() { + By("creating the custom resource for the Kind NetworkFenceClass") + err := k8sClient.Get(ctx, typeNamespacedName, networkfenceclass) + if err != nil && errors.IsNotFound(err) { + resource := &csiaddonsv1alpha1.NetworkFenceClass{ + ObjectMeta: metav1.ObjectMeta{ + Name: resourceName, + Namespace: "default", + }, + // TODO(user): Specify other spec details if needed. + } + Expect(k8sClient.Create(ctx, resource)).To(Succeed()) + } + }) + + AfterEach(func() { + // TODO(user): Cleanup logic after each test, like removing the resource instance. + resource := &csiaddonsv1alpha1.NetworkFenceClass{} + err := k8sClient.Get(ctx, typeNamespacedName, resource) + Expect(err).NotTo(HaveOccurred()) + + By("Cleanup the specific resource instance NetworkFenceClass") + Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + }) + It("should successfully reconcile the resource", func() { + By("Reconciling the created resource") + controllerReconciler := &NetworkFenceClassReconciler{ + Client: k8sClient, + Scheme: k8sClient.Scheme(), + } + + _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).NotTo(HaveOccurred()) + // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. + // Example: If you expect a certain status condition after reconciliation, verify it here. + }) + }) +})