diff --git a/.mockery.yaml b/.mockery.yaml index 2baeb8c8d..efe5405eb 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -4,15 +4,15 @@ dir: "{{.InterfaceDir}}/mocks" mockname: "{{.Mock}}{{.InterfaceName}}" outpkg: "{{.PackageName}}" packages: - github.com/openshift/lvm-operator/pkg/lsblk: - interfaces: - LSBLK: - github.com/openshift/lvm-operator/pkg/lvm: - interfaces: - LVM: - github.com/openshift/lvm-operator/pkg/wipefs: - interfaces: - Wipefs: - github.com/openshift/lvm-operator/pkg/dmsetup: + github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk: + interfaces: + LSBLK: + github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm: + interfaces: + LVM: + github.com/openshift/lvm-operator/internal/controllers/vgmanager/wipefs: + interfaces: + Wipefs: + github.com/openshift/lvm-operator/internal/controllers/vgmanager/dmsetup: interfaces: Dmsetup: diff --git a/Dockerfile b/Dockerfile index b526e37e0..babcc797f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,12 +17,12 @@ RUN go mod verify # Copy the go source COPY api/ api/ -COPY controllers/ controllers/ COPY cmd/ cmd/ -COPY pkg/ pkg/ +COPY internal/ internal/ # Build -RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -mod=vendor --ldflags "-s -w" -a -o lvms cmd/main.go +RUN --mount=type=cache,target=/root/.cache/go-build \ + GOOS=$TARGETOS GOARCH=$TARGETARCH go build -mod=vendor --ldflags "-s -w" -a -o lvms cmd/main.go # vgmanager needs 'nsenter' and other basic linux utils to correctly function FROM --platform=$TARGETPLATFORM registry.access.redhat.com/ubi9/ubi-minimal:9.2 diff --git a/api/v1alpha1/lvmcluster_test.go b/api/v1alpha1/lvmcluster_test.go index 7e7315176..2bed8261d 100644 --- a/api/v1alpha1/lvmcluster_test.go +++ b/api/v1alpha1/lvmcluster_test.go @@ -7,7 +7,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/openshift/lvm-operator/pkg/cluster" + "github.com/openshift/lvm-operator/internal/cluster" v1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" diff --git a/api/v1alpha1/lvmcluster_webhook.go b/api/v1alpha1/lvmcluster_webhook.go index d7f57654a..83988c108 100644 --- a/api/v1alpha1/lvmcluster_webhook.go +++ b/api/v1alpha1/lvmcluster_webhook.go @@ -22,7 +22,7 @@ import ( "fmt" "strings" - "github.com/openshift/lvm-operator/pkg/cluster" + "github.com/openshift/lvm-operator/internal/cluster" "k8s.io/apimachinery/pkg/runtime" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" diff --git a/cmd/operator/operator.go b/cmd/operator/operator.go index f97937991..905f95963 100644 --- a/cmd/operator/operator.go +++ b/cmd/operator/operator.go @@ -21,7 +21,10 @@ import ( "fmt" "github.com/go-logr/logr" - "github.com/openshift/lvm-operator/controllers/node" + "github.com/openshift/lvm-operator/internal/controllers/lvmcluster" + "github.com/openshift/lvm-operator/internal/controllers/node" + "github.com/openshift/lvm-operator/internal/controllers/persistent-volume" + "github.com/openshift/lvm-operator/internal/controllers/persistent-volume-claim" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/runtime" @@ -42,10 +45,7 @@ import ( "github.com/openshift/library-go/pkg/config/leaderelection" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/controllers" - persistent_volume "github.com/openshift/lvm-operator/controllers/persistent-volume" - persistent_volume_claim "github.com/openshift/lvm-operator/controllers/persistent-volume-claim" - "github.com/openshift/lvm-operator/pkg/cluster" + "github.com/openshift/lvm-operator/internal/cluster" //+kubebuilder:scaffold:imports ) @@ -158,9 +158,8 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error { } // register controllers - if err = (&controllers.LVMClusterReconciler{ + if err = (&lvmcluster.LVMClusterReconciler{ Client: mgr.GetClient(), - Scheme: mgr.GetScheme(), EventRecorder: mgr.GetEventRecorderFor("LVMClusterReconciler"), ClusterType: clusterType, Namespace: operatorNamespace, diff --git a/cmd/vgmanager/vgmanager.go b/cmd/vgmanager/vgmanager.go index 198dd2f36..679c5a576 100644 --- a/cmd/vgmanager/vgmanager.go +++ b/cmd/vgmanager/vgmanager.go @@ -21,13 +21,13 @@ import ( "os" "github.com/go-logr/logr" - "github.com/openshift/lvm-operator/pkg/dmsetup" - "github.com/openshift/lvm-operator/pkg/filter" - "github.com/openshift/lvm-operator/pkg/lsblk" - "github.com/openshift/lvm-operator/pkg/lvm" - "github.com/openshift/lvm-operator/pkg/lvmd" - "github.com/openshift/lvm-operator/pkg/vgmanager" - "github.com/openshift/lvm-operator/pkg/wipefs" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/dmsetup" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/filter" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvmd" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/wipefs" "github.com/spf13/cobra" "sigs.k8s.io/controller-runtime/pkg/cache" metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" diff --git a/docs/design/lvm-operator-manager.md b/docs/design/lvm-operator-manager.md index 15deb5263..e2c55cac4 100644 --- a/docs/design/lvm-operator-manager.md +++ b/docs/design/lvm-operator-manager.md @@ -61,7 +61,7 @@ The Operator requires elevated permissions to interact with the host's LVM comma ## Implementation Notes -Each unit of reconciliation should implement the `reconcileUnit` interface. This is run by the controller. Errors and success messages are propagated as Operator status and events. This interface is defined in [lvmcluster_controller.go](../../controllers/lvmcluster_controller.go) +Each unit of reconciliation should implement the `reconcileUnit` interface. This is run by the controller. Errors and success messages are propagated as Operator status and events. This interface is defined in [lvmcluster_controller.go](../../internal/controllers/lvmcluster/lvmcluster_controller.go) ```go type resourceManager interface { diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 631503fa3..0157ffe51 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -63,7 +63,7 @@ If you encounter a failure message such as `no available devices found` while in $ lsblk --paths --json -o NAME,ROTA,TYPE,SIZE,MODEL,VENDOR,RO,STATE,KNAME,SERIAL,PARTLABEL,FSTYPE ``` -This prints information about the disks on the host. Review this information to see why a device is not considered available for LVMS utilization. For example, if a device has partlabel `bios` or `reserved`, or if they are suspended or read-only, or if they have children disks or `fstype` set, LVMS considers them unavailable. Check [filter.go](../pkg/vgmanager/filter.go) for the complete list of filters LVMS makes use of. +This prints information about the disks on the host. Review this information to see why a device is not considered available for LVMS utilization. For example, if a device has partlabel `bios` or `reserved`, or if they are suspended or read-only, or if they have children disks or `fstype` set, LVMS considers them unavailable. Check [filter.go](../internal/controllers/vgmanager/filter/filter.go) for the complete list of filters LVMS makes use of. > If you set a device path in the `LVMCluster` CR under `spec.storage.deviceClasses.deviceSelector.paths`, make sure the paths match with `kname` of the device from the `lsblk` output. diff --git a/pkg/cluster/leaderelection.go b/internal/cluster/leaderelection.go similarity index 100% rename from pkg/cluster/leaderelection.go rename to internal/cluster/leaderelection.go diff --git a/pkg/cluster/leaderelection_test.go b/internal/cluster/leaderelection_test.go similarity index 100% rename from pkg/cluster/leaderelection_test.go rename to internal/cluster/leaderelection_test.go diff --git a/pkg/cluster/namespace.go b/internal/cluster/namespace.go similarity index 100% rename from pkg/cluster/namespace.go rename to internal/cluster/namespace.go diff --git a/pkg/cluster/sno.go b/internal/cluster/sno.go similarity index 100% rename from pkg/cluster/sno.go rename to internal/cluster/sno.go diff --git a/pkg/cluster/type.go b/internal/cluster/type.go similarity index 100% rename from pkg/cluster/type.go rename to internal/cluster/type.go diff --git a/controllers/constants.go b/internal/controllers/constants/constants.go similarity index 89% rename from controllers/constants.go rename to internal/controllers/constants/constants.go index 1d6331865..880e1f58d 100644 --- a/controllers/constants.go +++ b/internal/controllers/constants/constants.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package constants const ( TopolvmCSIDriverName = "topolvm.io" @@ -25,7 +25,6 @@ const ( // CSI Controller container and deployment names TopolvmControllerDeploymentName = "topolvm-controller" - TopolvmControllerContainerName = "topolvm-controller" CsiResizerContainerName = "csi-resizer" CsiProvisionerContainerName = "csi-provisioner" CsiSnapshotterContainerName = "csi-snapshotter" @@ -68,10 +67,6 @@ const ( FileCheckerMemRequest = "10Mi" FileCheckerCPURequest = "1m" - // CSI Provisioner requires below environment values to make use of CSIStorageCapacity - PodNameEnv = "POD_NAME" - NameSpaceEnv = "NAMESPACE" - // topoLVM Node TopolvmNodeServiceAccount = "topolvm-node" TopolvmNodeDaemonsetName = "topolvm-node" @@ -89,9 +84,9 @@ const ( // annotations // WorkloadPartitioningManagement contains the management workload annotation - workloadPartitioningManagementAnnotation = "target.workload.openshift.io/management" + WorkloadPartitioningManagementAnnotation = "target.workload.openshift.io/management" - managementAnnotationVal = `{"effect": "PreferredDuringScheduling"}` + ManagementAnnotationVal = `{"effect": "PreferredDuringScheduling"}` // labels and values @@ -112,6 +107,6 @@ const ( CsiDriverNameVal = "topolvm-csi-driver" StorageClassPrefix = "lvms-" - volumeSnapshotClassPrefix = "lvms-" - sccPrefix = "lvms-" + VolumeSnapshotClassPrefix = "lvms-" + SCCPrefix = "lvms-" ) diff --git a/pkg/labels/managed.go b/internal/controllers/labels/managed.go similarity index 100% rename from pkg/labels/managed.go rename to internal/controllers/labels/managed.go diff --git a/controllers/lvmcluster_controller.go b/internal/controllers/lvmcluster/lvmcluster_controller.go similarity index 87% rename from controllers/lvmcluster_controller.go rename to internal/controllers/lvmcluster/lvmcluster_controller.go index a19a6681d..9a0864497 100644 --- a/controllers/lvmcluster_controller.go +++ b/internal/controllers/lvmcluster/lvmcluster_controller.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package lvmcluster import ( "context" @@ -25,13 +25,14 @@ import ( configv1 "github.com/openshift/api/config/v1" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/cluster" + "github.com/openshift/lvm-operator/internal/cluster" + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/lvmcluster/resource" topolvmv1 "github.com/topolvm/topolvm/api/v1" corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" corev1helper "k8s.io/component-helpers/scheduling/corev1" @@ -54,24 +55,10 @@ const ( lvmClusterFinalizer = "lvmcluster.topolvm.io" ) -// NOTE: when updating this, please also update docs/design/lvm-operator-manager.md -type resourceManager interface { - - // getName should return a camelCase name of this unit of reconciliation - getName() string - - // ensureCreated should check the resources managed by this unit - ensureCreated(*LVMClusterReconciler, context.Context, *lvmv1alpha1.LVMCluster) error - - // ensureDeleted should wait for the resources to be cleaned up - ensureDeleted(*LVMClusterReconciler, context.Context, *lvmv1alpha1.LVMCluster) error -} - // LVMClusterReconciler reconciles a LVMCluster object type LVMClusterReconciler struct { client.Client record.EventRecorder - Scheme *runtime.Scheme ClusterType cluster.Type EnableSnapshotting bool Namespace string @@ -82,6 +69,26 @@ type LVMClusterReconciler struct { TopoLVMLeaderElectionPassthrough configv1.LeaderElection } +func (r *LVMClusterReconciler) GetNamespace() string { + return r.Namespace +} + +func (r *LVMClusterReconciler) GetImageName() string { + return r.ImageName +} + +func (r *LVMClusterReconciler) GetClient() client.Client { + return r.Client +} + +func (r *LVMClusterReconciler) SnapshotsEnabled() bool { + return r.EnableSnapshotting +} + +func (r *LVMClusterReconciler) GetTopoLVMLeaderElectionPassthrough() configv1.LeaderElection { + return r.TopoLVMLeaderElectionPassthrough +} + //+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmclusters,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch;create;update;patch;delete //+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmclusters/status,verbs=get;update;patch @@ -176,27 +183,27 @@ func (r *LVMClusterReconciler) reconcile(ctx context.Context, instance *lvmv1alp logger.Info("successfully added finalizer") } - resources := []resourceManager{ - &csiDriver{}, - &topolvmController{}, - &topolvmNode{}, - &vgManager{}, - &lvmVG{}, - &topolvmStorageClass{}, + resources := []resource.Manager{ + resource.CSIDriver(), + resource.TopoLVMController(), + resource.TopoLVMNode(), + resource.VGManager(), + resource.LVMVGs(), + resource.TopoLVMStorageClass(), } if r.ClusterType == cluster.TypeOCP { - resources = append(resources, openshiftSccs{}) + resources = append(resources, resource.OpenShiftSCCs()) } - if r.EnableSnapshotting { - resources = append(resources, &topolvmVolumeSnapshotClass{}) + if r.SnapshotsEnabled() { + resources = append(resources, resource.TopoLVMVolumeSnapshotClass()) } resourceSyncStart := time.Now() results := make(chan error, len(resources)) create := func(i int) { - results <- resources[i].ensureCreated(r, ctx, instance) + results <- resources[i].EnsureCreated(r, ctx, instance) } for i := range resources { @@ -343,24 +350,24 @@ func (r *LVMClusterReconciler) setRunningPodImage(ctx context.Context) error { if r.ImageName == "" { // 'POD_NAME' and 'POD_NAMESPACE' are set in env of lvm-operator when running as a container - podName := os.Getenv(PodNameEnv) + podName := os.Getenv(resource.PodNameEnv) if podName == "" { - return fmt.Errorf("failed to get pod name env variable, %s env variable is not set", PodNameEnv) + return fmt.Errorf("failed to get pod name env variable, %s env variable is not set", resource.PodNameEnv) } pod := &corev1.Pod{} - if err := r.Get(ctx, types.NamespacedName{Name: podName, Namespace: r.Namespace}, pod); err != nil { + if err := r.Get(ctx, types.NamespacedName{Name: podName, Namespace: r.GetNamespace()}, pod); err != nil { return fmt.Errorf("failed to get pod %s: %w", podName, err) } for _, c := range pod.Spec.Containers { - if c.Name == LVMOperatorContainerName { + if c.Name == constants.LVMOperatorContainerName { r.ImageName = c.Image return nil } } - return fmt.Errorf("failed to get container image for %s in pod %s", LVMOperatorContainerName, podName) + return fmt.Errorf("failed to get container image for %s in pod %s", constants.LVMOperatorContainerName, podName) } return nil @@ -379,26 +386,26 @@ func (r *LVMClusterReconciler) logicalVolumesExist(ctx context.Context) (bool, e func (r *LVMClusterReconciler) processDelete(ctx context.Context, instance *lvmv1alpha1.LVMCluster) error { if controllerutil.ContainsFinalizer(instance, lvmClusterFinalizer) { - resourceDeletionList := []resourceManager{ - &topolvmStorageClass{}, - &lvmVG{}, - &topolvmController{}, - &csiDriver{}, - &topolvmNode{}, - &vgManager{}, + resourceDeletionList := []resource.Manager{ + resource.TopoLVMStorageClass(), + resource.LVMVGs(), + resource.TopoLVMController(), + resource.CSIDriver(), + resource.TopoLVMNode(), + resource.VGManager(), } if r.ClusterType == cluster.TypeOCP { - resourceDeletionList = append(resourceDeletionList, openshiftSccs{}) + resourceDeletionList = append(resourceDeletionList, resource.OpenShiftSCCs()) } - if r.EnableSnapshotting { - resourceDeletionList = append(resourceDeletionList, &topolvmVolumeSnapshotClass{}) + if r.SnapshotsEnabled() { + resourceDeletionList = append(resourceDeletionList, resource.TopoLVMVolumeSnapshotClass()) } for _, unit := range resourceDeletionList { - if err := unit.ensureDeleted(r, ctx, instance); err != nil { - err := fmt.Errorf("failed cleaning up %s: %w", unit.getName(), err) + if err := unit.EnsureDeleted(r, ctx, instance); err != nil { + err := fmt.Errorf("failed cleaning up %s: %w", unit.GetName(), err) r.WarningEvent(ctx, instance, EventReasonErrorDeletionPending, err) return err } diff --git a/controllers/lvmcluster_controller_integration_test.go b/internal/controllers/lvmcluster/lvmcluster_controller_integration_test.go similarity index 94% rename from controllers/lvmcluster_controller_integration_test.go rename to internal/controllers/lvmcluster/lvmcluster_controller_integration_test.go index bf4982e22..444c7026d 100644 --- a/controllers/lvmcluster_controller_integration_test.go +++ b/internal/controllers/lvmcluster/lvmcluster_controller_integration_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package lvmcluster import ( "context" @@ -23,6 +23,8 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/lvmcluster/resource" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" @@ -92,19 +94,19 @@ var _ = Describe("LVMCluster controller", func() { } // CSI Driver Resource - csiDriverName := types.NamespacedName{Name: TopolvmCSIDriverName} + csiDriverName := types.NamespacedName{Name: constants.TopolvmCSIDriverName} csiDriverOut := &storagev1.CSIDriver{} // VgManager Resource vgManagerDaemonset := &appsv1.DaemonSet{} - vgManagerNamespacedName := types.NamespacedName{Name: VGManagerUnit, Namespace: testLvmClusterNamespace} + vgManagerNamespacedName := types.NamespacedName{Name: resource.VGManagerUnit, Namespace: testLvmClusterNamespace} // Topolvm Controller Deployment - controllerName := types.NamespacedName{Name: TopolvmControllerDeploymentName, Namespace: testLvmClusterNamespace} + controllerName := types.NamespacedName{Name: constants.TopolvmControllerDeploymentName, Namespace: testLvmClusterNamespace} controllerOut := &appsv1.Deployment{} // CSI Node resource - csiNodeName := types.NamespacedName{Namespace: testLvmClusterNamespace, Name: TopolvmNodeDaemonsetName} + csiNodeName := types.NamespacedName{Namespace: testLvmClusterNamespace, Name: constants.TopolvmNodeDaemonsetName} csiNodeOut := &appsv1.DaemonSet{} lvmVolumeGroupName := types.NamespacedName{Namespace: testLvmClusterNamespace, Name: testDeviceClassName} @@ -114,7 +116,7 @@ var _ = Describe("LVMCluster controller", func() { var scNames []types.NamespacedName for _, deviceClass := range lvmClusterIn.Spec.Storage.DeviceClasses { scNames = append(scNames, types.NamespacedName{ - Name: getStorageClassName(deviceClass.Name), + Name: resource.GetStorageClassName(deviceClass.Name), }) } scOut := &storagev1.StorageClass{} diff --git a/controllers/lvmcluster_controller_watches.go b/internal/controllers/lvmcluster/lvmcluster_controller_watches.go similarity index 89% rename from controllers/lvmcluster_controller_watches.go rename to internal/controllers/lvmcluster/lvmcluster_controller_watches.go index de6a09d3e..5ab18cb25 100644 --- a/controllers/lvmcluster_controller_watches.go +++ b/internal/controllers/lvmcluster/lvmcluster_controller_watches.go @@ -14,12 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package lvmcluster import ( "context" snapapiv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" + "github.com/openshift/lvm-operator/internal/controllers/labels" + internalselector "github.com/openshift/lvm-operator/internal/controllers/lvmcluster/selector" v1 "k8s.io/api/core/v1" storagev1 "k8s.io/api/storage/v1" corev1helper "k8s.io/component-helpers/scheduling/corev1" @@ -27,9 +29,7 @@ import ( secv1 "github.com/openshift/api/security/v1" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/cluster" - "github.com/openshift/lvm-operator/pkg/labels" - + "github.com/openshift/lvm-operator/internal/cluster" appsv1 "k8s.io/api/apps/v1" "k8s.io/apimachinery/pkg/types" ctrl "sigs.k8s.io/controller-runtime" @@ -61,7 +61,7 @@ func (r *LVMClusterReconciler) SetupWithManager(mgr ctrl.Manager) error { handler.EnqueueRequestsFromMapFunc(r.getManagedLabelObjsForReconcile), ) } - if r.EnableSnapshotting { + if r.SnapshotsEnabled() { builder = builder.Watches( &snapapiv1.VolumeSnapshotClass{}, handler.EnqueueRequestsFromMapFunc(r.getManagedLabelObjsForReconcile), @@ -82,14 +82,14 @@ func (r *LVMClusterReconciler) getManagedLabelObjsForReconcile(ctx context.Conte Namespace: obj.GetNamespace(), } - if err := r.Client.List(ctx, foundLVMClusterList, listOps); err != nil { + if err := r.List(ctx, foundLVMClusterList, listOps); err != nil { log.FromContext(ctx).Error(err, "getManagedLabelObjsForReconcile: Failed to get LVMCluster objs") return []reconcile.Request{} } var requests []reconcile.Request for _, lvmCluster := range foundLVMClusterList.Items { - if !labels.MatchesManagedLabels(r.Scheme, obj, &lvmCluster) { + if !labels.MatchesManagedLabels(r.Scheme(), obj, &lvmCluster) { continue } requests = append(requests, reconcile.Request{ @@ -112,20 +112,20 @@ func (r *LVMClusterReconciler) getLVMClusterObjsByNameFittingNodeSelector(ctx co Namespace: obj.GetNamespace(), } - if err := r.Client.List(ctx, foundLVMClusterList, listOps); err != nil { + if err := r.List(ctx, foundLVMClusterList, listOps); err != nil { log.FromContext(ctx).Error(err, "getLVMClusterObjsByNameFittingNodeSelector: Failed to get LVMCluster objs") return []reconcile.Request{} } node := &v1.Node{} - if err := r.Client.Get(ctx, client.ObjectKeyFromObject(obj), node); err != nil { + if err := r.Get(ctx, client.ObjectKeyFromObject(obj), node); err != nil { log.FromContext(ctx).Error(err, "getLVMClusterObjsByNameFittingNodeSelector: Failed to get Node") return []reconcile.Request{} } var requests []reconcile.Request for _, lvmCluster := range foundLVMClusterList.Items { - selector, _ := extractNodeSelectorAndTolerations(&lvmCluster) + selector, _ := internalselector.ExtractNodeSelectorAndTolerations(&lvmCluster) // if the selector is nil then the default behavior is to match all nodes if selector == nil { requests = append(requests, reconcile.Request{ diff --git a/controllers/defaults.go b/internal/controllers/lvmcluster/resource/env.go similarity index 79% rename from controllers/defaults.go rename to internal/controllers/lvmcluster/resource/env.go index 4875787db..57101e311 100644 --- a/controllers/defaults.go +++ b/internal/controllers/lvmcluster/resource/env.go @@ -14,12 +14,38 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "os" + + corev1 "k8s.io/api/core/v1" +) + +const ( + PodNameEnv = "POD_NAME" + NameSpaceEnv = "NAMESPACE" ) +var defaultEnvVars = []corev1.EnvVar{ + { + Name: PodNameEnv, + ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{ + FieldPath: "metadata.name", + }, + }, + }, + { + Name: NameSpaceEnv, + ValueFrom: &corev1.EnvVarSource{ + FieldRef: &corev1.ObjectFieldSelector{ + FieldPath: "metadata.namespace", + }, + }, + }, +} + var ( defaultValMap = map[string]string{ "OPERATOR_NAMESPACE": "openshift-storage", @@ -34,7 +60,7 @@ var ( OperatorNamespace = GetEnvOrDefault("OPERATOR_NAMESPACE") - //CSI + // CSI TopolvmCsiImage = GetEnvOrDefault("TOPOLVM_CSI_IMAGE") RbacProxyImage = GetEnvOrDefault("RBAC_PROXY_IMAGE") CsiRegistrarImage = GetEnvOrDefault("CSI_REGISTRAR_IMAGE") diff --git a/controllers/lvm_volumegroup.go b/internal/controllers/lvmcluster/resource/lvm_volumegroup.go similarity index 83% rename from controllers/lvm_volumegroup.go rename to internal/controllers/lvmcluster/resource/lvm_volumegroup.go index dc900701a..c497eacb1 100644 --- a/controllers/lvm_volumegroup.go +++ b/internal/controllers/lvmcluster/resource/lvm_volumegroup.go @@ -14,14 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "context" "fmt" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -38,16 +37,20 @@ const ( legacyVGFinalizer = "lvm.openshift.io/lvmvolumegroup" ) +func LVMVGs() Manager { + return lvmVG{} +} + type lvmVG struct{} -func (c lvmVG) getName() string { +func (c lvmVG) GetName() string { return lvmVGName } -func (c lvmVG) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("topolvmNode", c.getName()) +func (c lvmVG) EnsureCreated(r Reconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("topolvmNode", c.GetName()) - lvmVolumeGroups := lvmVolumeGroups(r.Namespace, lvmCluster.Spec.Storage.DeviceClasses) + lvmVolumeGroups := lvmVolumeGroups(r.GetNamespace(), lvmCluster.Spec.Storage.DeviceClasses) for _, volumeGroup := range lvmVolumeGroups { existingVolumeGroup := &lvmv1alpha1.LVMVolumeGroup{ @@ -57,11 +60,11 @@ func (c lvmVG) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCl }, } - if err := cutil.SetControllerReference(lvmCluster, existingVolumeGroup, r.Scheme); err != nil { + if err := cutil.SetControllerReference(lvmCluster, existingVolumeGroup, r.Scheme()); err != nil { return fmt.Errorf("failed to set controller reference to LVMVolumeGroup: %w", err) } - result, err := cutil.CreateOrUpdate(ctx, r.Client, existingVolumeGroup, func() error { + result, err := cutil.CreateOrUpdate(ctx, r, existingVolumeGroup, func() error { // removes the old finalizer that was maintained up until 4.15, now we have vgmanager owned finalizers // per node starting with 4.15 // This code path makes sure to remove the old finalizer if it is encountered from a previous installation @@ -73,7 +76,7 @@ func (c lvmVG) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCl }) if err != nil { - return fmt.Errorf("%s failed to reconcile: %w", c.getName(), err) + return fmt.Errorf("%s failed to reconcile: %w", c.GetName(), err) } logger.Info("LVMVolumeGroup applied to cluster", "operation", result, "name", volumeGroup.Name) @@ -81,9 +84,9 @@ func (c lvmVG) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCl return nil } -func (c lvmVG) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", c.getName()) - vgcrs := lvmVolumeGroups(r.Namespace, lvmCluster.Spec.Storage.DeviceClasses) +func (c lvmVG) EnsureDeleted(r Reconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", c.GetName()) + vgcrs := lvmVolumeGroups(r.GetNamespace(), lvmCluster.Spec.Storage.DeviceClasses) var volumeGroupsPendingDelete []string @@ -91,7 +94,7 @@ func (c lvmVG) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCl vgName := client.ObjectKeyFromObject(volumeGroup) logger := logger.WithValues("LVMVolumeGroup", volumeGroup.GetName()) - if err := r.Client.Get(ctx, vgName, volumeGroup); err != nil { + if err := r.Get(ctx, vgName, volumeGroup); err != nil { if errors.IsNotFound(err) { continue } @@ -102,7 +105,7 @@ func (c lvmVG) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCl // The controller reference will usually propagate all deletion timestamps so this will // only occur if the propagation from the API server was delayed. if volumeGroup.GetDeletionTimestamp().IsZero() { - if err := r.Client.Delete(ctx, volumeGroup); err != nil { + if err := r.Delete(ctx, volumeGroup); err != nil { return fmt.Errorf("failed to delete LVMVolumeGroup %s: %w", volumeGroup.GetName(), err) } logger.Info("initiated LVMVolumeGroup deletion", "volumeGroup", client.ObjectKeyFromObject(volumeGroup)) diff --git a/internal/controllers/lvmcluster/resource/manager.go b/internal/controllers/lvmcluster/resource/manager.go new file mode 100644 index 000000000..8101081a5 --- /dev/null +++ b/internal/controllers/lvmcluster/resource/manager.go @@ -0,0 +1,34 @@ +package resource + +import ( + "context" + + configv1 "github.com/openshift/api/config/v1" + lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +type Reconciler interface { + client.Client + + GetNamespace() string + GetImageName() string + SnapshotsEnabled() bool + + // GetTopoLVMLeaderElectionPassthrough uses the given leaderElection when initializing TopoLVM to synchronize + // leader election configuration + GetTopoLVMLeaderElectionPassthrough() configv1.LeaderElection +} + +// Manager NOTE: when updating this, please also update docs/design/lvm-operator-manager.md +type Manager interface { + + // GetName should return a camelCase name of this unit of reconciliation + GetName() string + + // EnsureCreated should check the resources managed by this unit + EnsureCreated(Reconciler, context.Context, *lvmv1alpha1.LVMCluster) error + + // EnsureDeleted should wait for the resources to be cleaned up + EnsureDeleted(Reconciler, context.Context, *lvmv1alpha1.LVMCluster) error +} diff --git a/controllers/scc.go b/internal/controllers/lvmcluster/resource/scc.go similarity index 81% rename from controllers/scc.go rename to internal/controllers/lvmcluster/resource/scc.go index 67aa889d4..d04969de0 100644 --- a/controllers/scc.go +++ b/internal/controllers/lvmcluster/resource/scc.go @@ -14,17 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "context" "fmt" secv1 "github.com/openshift/api/security/v1" - lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/labels" - + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/labels" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -37,18 +36,22 @@ const ( sccName = "topolvm-scc" ) +func OpenShiftSCCs() Manager { + return openshiftSccs{} +} + type openshiftSccs struct{} // openshiftSccs unit satisfies resourceManager interface -var _ resourceManager = openshiftSccs{} +var _ Manager = openshiftSccs{} -func (c openshiftSccs) getName() string { +func (c openshiftSccs) GetName() string { return sccName } -func (c openshiftSccs) ensureCreated(r *LVMClusterReconciler, ctx context.Context, cluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", c.getName()) - sccs := getAllSCCs(r.Namespace) +func (c openshiftSccs) EnsureCreated(r Reconciler, ctx context.Context, cluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", c.GetName()) + sccs := getAllSCCs(r.GetNamespace()) for _, scc := range sccs { err := r.Get(ctx, client.ObjectKeyFromObject(scc), &secv1.SecurityContextConstraints{}) if err == nil { @@ -61,9 +64,9 @@ func (c openshiftSccs) ensureCreated(r *LVMClusterReconciler, ctx context.Contex return fmt.Errorf("something went wrong when checking for SecurityContextConstraint: %w", err) } - labels.SetManagedLabels(r.Scheme, scc, cluster) + labels.SetManagedLabels(r.Scheme(), scc, cluster) if err := r.Create(ctx, scc); err != nil { - return fmt.Errorf("%s failed to reconcile: %w", c.getName(), err) + return fmt.Errorf("%s failed to reconcile: %w", c.GetName(), err) } logger.Info("SecurityContextConstraint created", "name", scc.Name) @@ -72,13 +75,13 @@ func (c openshiftSccs) ensureCreated(r *LVMClusterReconciler, ctx context.Contex return nil } -func (c openshiftSccs) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, _ *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", c.getName()) - sccs := getAllSCCs(r.Namespace) +func (c openshiftSccs) EnsureDeleted(r Reconciler, ctx context.Context, _ *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", c.GetName()) + sccs := getAllSCCs(r.GetNamespace()) for _, scc := range sccs { name := types.NamespacedName{Name: scName} logger := logger.WithValues("SecurityContextConstraint", scName) - if err := r.Client.Get(ctx, name, scc); err != nil { + if err := r.Get(ctx, name, scc); err != nil { if errors.IsNotFound(err) { continue } @@ -111,7 +114,7 @@ func newTopolvmNodeScc(namespace string) *secv1.SecurityContextConstraints { Kind: "SecurityContextConstraints", }, } - scc.Name = sccPrefix + "topolvm-node" + scc.Name = constants.SCCPrefix + "topolvm-node" scc.AllowPrivilegedContainer = true scc.AllowHostNetwork = false scc.AllowHostDirVolumePlugin = true @@ -139,7 +142,7 @@ func newTopolvmNodeScc(namespace string) *secv1.SecurityContextConstraints { secv1.FSTypeSecret, } scc.Users = []string{ - fmt.Sprintf("system:serviceaccount:%s:%s", namespace, TopolvmNodeServiceAccount), + fmt.Sprintf("system:serviceaccount:%s:%s", namespace, constants.TopolvmNodeServiceAccount), } return scc @@ -152,7 +155,7 @@ func newVGManagerScc(namespace string) *secv1.SecurityContextConstraints { Kind: "SecurityContextConstraints", }, } - scc.Name = sccPrefix + "vgmanager" + scc.Name = constants.SCCPrefix + "vgmanager" scc.AllowPrivilegedContainer = true scc.AllowHostNetwork = false scc.AllowHostDirVolumePlugin = true @@ -180,7 +183,7 @@ func newVGManagerScc(namespace string) *secv1.SecurityContextConstraints { secv1.FSTypeSecret, } scc.Users = []string{ - fmt.Sprintf("system:serviceaccount:%s:%s", namespace, VGManagerServiceAccount), + fmt.Sprintf("system:serviceaccount:%s:%s", namespace, constants.VGManagerServiceAccount), } return scc diff --git a/controllers/topolvm_controller.go b/internal/controllers/lvmcluster/resource/topolvm_controller.go similarity index 66% rename from controllers/topolvm_controller.go rename to internal/controllers/lvmcluster/resource/topolvm_controller.go index cb94cbb0d..aa32b4d45 100644 --- a/controllers/topolvm_controller.go +++ b/internal/controllers/lvmcluster/resource/topolvm_controller.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "context" @@ -23,7 +23,7 @@ import ( v1 "github.com/openshift/api/config/v1" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - + "github.com/openshift/lvm-operator/internal/controllers/constants" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -33,27 +33,37 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" ) -const ( - controllerName = "topolvm-controller" -) +var volumeMountsWithCSISocket = []corev1.VolumeMount{ + {Name: "socket-dir", MountPath: filepath.Dir(constants.DefaultCSISocket)}, +} -type topolvmController struct { +var controllerLabels = map[string]string{ + constants.AppKubernetesNameLabel: constants.CsiDriverNameVal, + constants.AppKubernetesManagedByLabel: constants.ManagedByLabelVal, + constants.AppKubernetesPartOfLabel: constants.PartOfLabelVal, + constants.AppKubernetesComponentLabel: constants.TopolvmControllerLabelVal, } +func TopoLVMController() Manager { + return topolvmController{} +} + +type topolvmController struct{} + // topolvmController unit satisfies resourceManager interface -var _ resourceManager = topolvmController{} +var _ Manager = topolvmController{} -func (c topolvmController) getName() string { - return controllerName +func (c topolvmController) GetName() string { + return constants.TopolvmControllerDeploymentName } //+kubebuilder:rbac:groups=apps,resources=deployments,verbs=create;update;delete;get;list;watch -func (c topolvmController) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", c.getName()) +func (c topolvmController) EnsureCreated(r Reconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", c.GetName()) // get the desired state of topolvm controller deployment - desiredDeployment := getControllerDeployment(r.Namespace, r.TopoLVMLeaderElectionPassthrough) + desiredDeployment := getControllerDeployment(r.GetNamespace(), r.SnapshotsEnabled(), r.GetTopoLVMLeaderElectionPassthrough()) existingDeployment := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: desiredDeployment.Name, @@ -61,8 +71,8 @@ func (c topolvmController) ensureCreated(r *LVMClusterReconciler, ctx context.Co }, } - result, err := cutil.CreateOrUpdate(ctx, r.Client, existingDeployment, func() error { - if err := cutil.SetControllerReference(lvmCluster, existingDeployment, r.Scheme); err != nil { + result, err := cutil.CreateOrUpdate(ctx, r, existingDeployment, func() error { + if err := cutil.SetControllerReference(lvmCluster, existingDeployment, r.Scheme()); err != nil { return fmt.Errorf("failed to set controller reference for csi controller: %w", err) } // at creation, deep copy desired deployment into existing @@ -92,11 +102,11 @@ func (c topolvmController) ensureCreated(r *LVMClusterReconciler, ctx context.Co } // ensureDeleted is a noop. Deletion will be handled by ownerref -func (c topolvmController) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, _ *lvmv1alpha1.LVMCluster) error { +func (c topolvmController) EnsureDeleted(_ Reconciler, _ context.Context, _ *lvmv1alpha1.LVMCluster) error { return nil } -func getControllerDeployment(namespace string, topoLVMLeaderElectionPassthrough v1.LeaderElection) *appsv1.Deployment { +func getControllerDeployment(namespace string, enableSnapshots bool, topoLVMLeaderElectionPassthrough v1.LeaderElection) *appsv1.Deployment { // Topolvm CSI Controller Deployment var replicas int32 = 1 volumes := []corev1.Volume{ @@ -109,41 +119,37 @@ func getControllerDeployment(namespace string, topoLVMLeaderElectionPassthrough csiProvisionerContainer(topoLVMLeaderElectionPassthrough), csiResizerContainer(topoLVMLeaderElectionPassthrough), livenessProbeContainer(), - csiSnapshotterContainer(topoLVMLeaderElectionPassthrough), } - annotations := map[string]string{ - workloadPartitioningManagementAnnotation: managementAnnotationVal, + if enableSnapshots { + containers = append(containers, csiSnapshotterContainer(topoLVMLeaderElectionPassthrough)) } - labels := map[string]string{ - AppKubernetesNameLabel: CsiDriverNameVal, - AppKubernetesManagedByLabel: ManagedByLabelVal, - AppKubernetesPartOfLabel: PartOfLabelVal, - AppKubernetesComponentLabel: TopolvmControllerLabelVal, + annotations := map[string]string{ + constants.WorkloadPartitioningManagementAnnotation: constants.ManagementAnnotationVal, } return &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ - Name: TopolvmControllerDeploymentName, + Name: constants.TopolvmControllerDeploymentName, Namespace: namespace, Annotations: annotations, - Labels: labels, + Labels: controllerLabels, }, Spec: appsv1.DeploymentSpec{ Replicas: &replicas, Selector: &metav1.LabelSelector{ - MatchLabels: labels, + MatchLabels: controllerLabels, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Name: TopolvmControllerDeploymentName, + Name: constants.TopolvmControllerDeploymentName, Namespace: namespace, - Labels: labels, + Labels: controllerLabels, }, Spec: corev1.PodSpec{ Containers: containers, - ServiceAccountName: TopolvmControllerServiceAccount, + ServiceAccountName: constants.TopolvmControllerServiceAccount, Volumes: volumes, }, }, @@ -165,23 +171,19 @@ func controllerContainer(topoLVMLeaderElectionPassthrough v1.LeaderElection) cor resourceRequirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(TopolvmControllerCPURequest), - corev1.ResourceMemory: resource.MustParse(TopolvmControllerMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.TopolvmControllerCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.TopolvmControllerMemRequest), }, } - volumeMounts := []corev1.VolumeMount{ - {Name: "socket-dir", MountPath: filepath.Dir(DefaultCSISocket)}, - } - return corev1.Container{ - Name: TopolvmControllerContainerName, + Name: constants.TopolvmControllerDeploymentName, Image: TopolvmCsiImage, Command: command, Ports: []corev1.ContainerPort{ { - Name: TopolvmControllerContainerHealthzName, - ContainerPort: TopolvmControllerContainerLivenessPort, + Name: constants.TopolvmControllerContainerHealthzName, + ContainerPort: constants.TopolvmControllerContainerLivenessPort, Protocol: corev1.ProtocolTCP, }, }, @@ -189,7 +191,7 @@ func controllerContainer(topoLVMLeaderElectionPassthrough v1.LeaderElection) cor ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{ Path: "/healthz", - Port: intstr.FromString(TopolvmControllerContainerHealthzName), + Port: intstr.FromString(constants.TopolvmControllerContainerHealthzName), }, }, FailureThreshold: 3, @@ -202,14 +204,14 @@ func controllerContainer(topoLVMLeaderElectionPassthrough v1.LeaderElection) cor HTTPGet: &corev1.HTTPGetAction{ Path: "/metrics", Port: intstr.IntOrString{ - IntVal: TopolvmControllerContainerReadinessPort, + IntVal: constants.TopolvmControllerContainerReadinessPort, }, Scheme: corev1.URISchemeHTTP, }, }, }, Resources: resourceRequirements, - VolumeMounts: volumeMounts, + VolumeMounts: volumeMountsWithCSISocket, } } @@ -217,7 +219,7 @@ func csiProvisionerContainer(topoLVMLeaderElectionPassthrough v1.LeaderElection) // csi provisioner container args := []string{ - fmt.Sprintf("--csi-address=%s", DefaultCSISocket), + fmt.Sprintf("--csi-address=%s", constants.DefaultCSISocket), "--enable-capacity", "--capacity-ownerref-level=2", "--capacity-poll-interval=30s", @@ -230,41 +232,19 @@ func csiProvisionerContainer(topoLVMLeaderElectionPassthrough v1.LeaderElection) resourceRequirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(TopolvmCsiProvisionerCPURequest), - corev1.ResourceMemory: resource.MustParse(TopolvmCsiProvisionerMemRequest), - }, - } - - volumeMounts := []corev1.VolumeMount{ - {Name: "socket-dir", MountPath: filepath.Dir(DefaultCSISocket)}, - } - - env := []corev1.EnvVar{ - { - Name: PodNameEnv, - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.name", - }, - }, - }, - { - Name: NameSpaceEnv, - ValueFrom: &corev1.EnvVarSource{ - FieldRef: &corev1.ObjectFieldSelector{ - FieldPath: "metadata.namespace", - }, - }, + corev1.ResourceCPU: resource.MustParse(constants.TopolvmCsiProvisionerCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.TopolvmCsiProvisionerMemRequest), }, } return corev1.Container{ - Name: CsiProvisionerContainerName, + Name: constants.CsiProvisionerContainerName, Image: CsiProvisionerImage, Args: args, Resources: resourceRequirements, - VolumeMounts: volumeMounts, - Env: env, + VolumeMounts: volumeMountsWithCSISocket, + // CSI Provisioner requires below environment values to make use of CSIStorageCapacity + Env: defaultEnvVars, } } @@ -272,59 +252,51 @@ func csiResizerContainer(topoLVMLeaderElectionPassthrough v1.LeaderElection) cor // csi resizer container args := []string{ - fmt.Sprintf("--csi-address=%s", DefaultCSISocket), + fmt.Sprintf("--csi-address=%s", constants.DefaultCSISocket), fmt.Sprintf("--leader-election-namespace=%s", topoLVMLeaderElectionPassthrough.Namespace), fmt.Sprintf("--leader-election-lease-duration=%s", topoLVMLeaderElectionPassthrough.LeaseDuration.Duration), fmt.Sprintf("--leader-election-renew-deadline=%s", topoLVMLeaderElectionPassthrough.RenewDeadline.Duration), fmt.Sprintf("--leader-election-retry-period=%s", topoLVMLeaderElectionPassthrough.RetryPeriod.Duration), } - volumeMounts := []corev1.VolumeMount{ - {Name: "socket-dir", MountPath: filepath.Dir(DefaultCSISocket)}, - } - resourceRequirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(TopolvmCsiResizerCPURequest), - corev1.ResourceMemory: resource.MustParse(TopolvmCsiResizerMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.TopolvmCsiResizerCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.TopolvmCsiResizerMemRequest), }, } return corev1.Container{ - Name: CsiResizerContainerName, + Name: constants.CsiResizerContainerName, Image: CsiResizerImage, Args: args, Resources: resourceRequirements, - VolumeMounts: volumeMounts, + VolumeMounts: volumeMountsWithCSISocket, } } func csiSnapshotterContainer(topoLVMLeaderElectionPassthrough v1.LeaderElection) corev1.Container { args := []string{ - fmt.Sprintf("--csi-address=%s", DefaultCSISocket), + fmt.Sprintf("--csi-address=%s", constants.DefaultCSISocket), fmt.Sprintf("--leader-election-namespace=%s", topoLVMLeaderElectionPassthrough.Namespace), fmt.Sprintf("--leader-election-lease-duration=%s", topoLVMLeaderElectionPassthrough.LeaseDuration.Duration), fmt.Sprintf("--leader-election-renew-deadline=%s", topoLVMLeaderElectionPassthrough.RenewDeadline.Duration), fmt.Sprintf("--leader-election-retry-period=%s", topoLVMLeaderElectionPassthrough.RetryPeriod.Duration), } - volumeMounts := []corev1.VolumeMount{ - {Name: "socket-dir", MountPath: filepath.Dir(DefaultCSISocket)}, - } - resourceRequirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(TopolvmCsiSnapshotterCPURequest), - corev1.ResourceMemory: resource.MustParse(TopolvmCsiSnapshotterMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.TopolvmCsiSnapshotterCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.TopolvmCsiSnapshotterMemRequest), }, } return corev1.Container{ - Name: CsiSnapshotterContainerName, + Name: constants.CsiSnapshotterContainerName, Image: CsiSnapshotterImage, Args: args, - VolumeMounts: volumeMounts, + VolumeMounts: volumeMountsWithCSISocket, Resources: resourceRequirements, } } @@ -332,25 +304,21 @@ func csiSnapshotterContainer(topoLVMLeaderElectionPassthrough v1.LeaderElection) func livenessProbeContainer() corev1.Container { resourceRequirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(LivenessProbeCPURequest), - corev1.ResourceMemory: resource.MustParse(LivenessProbeMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.LivenessProbeCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.LivenessProbeMemRequest), }, } // csi liveness probe container args := []string{ - fmt.Sprintf("--csi-address=%s", DefaultCSISocket), - } - - volumeMounts := []corev1.VolumeMount{ - {Name: "socket-dir", MountPath: filepath.Dir(DefaultCSISocket)}, + fmt.Sprintf("--csi-address=%s", constants.DefaultCSISocket), } return corev1.Container{ - Name: CsiLivenessProbeContainerName, + Name: constants.CsiLivenessProbeContainerName, Image: CsiLivenessProbeImage, Args: args, - VolumeMounts: volumeMounts, + VolumeMounts: volumeMountsWithCSISocket, Resources: resourceRequirements, } } diff --git a/controllers/topolvm_csi_driver.go b/internal/controllers/lvmcluster/resource/topolvm_csi_driver.go similarity index 71% rename from controllers/topolvm_csi_driver.go rename to internal/controllers/lvmcluster/resource/topolvm_csi_driver.go index 03b13568e..e137a16ee 100644 --- a/controllers/topolvm_csi_driver.go +++ b/internal/controllers/lvmcluster/resource/topolvm_csi_driver.go @@ -14,15 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "context" "fmt" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/labels" - + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/labels" storagev1 "k8s.io/api/storage/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" @@ -35,47 +35,51 @@ const ( driverName = "topolvm-csi-driver" ) +func CSIDriver() Manager { + return csiDriver{} +} + type csiDriver struct{} // csiDriver unit satisfies resourceManager interface -var _ resourceManager = csiDriver{} +var _ Manager = csiDriver{} -func (c csiDriver) getName() string { +func (c csiDriver) GetName() string { return driverName } //+kubebuilder:rbac:groups=storage.k8s.io,resources=csidrivers,verbs=get;create;delete;watch;list -func (c csiDriver) ensureCreated(r *LVMClusterReconciler, ctx context.Context, cluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", c.getName()) +func (c csiDriver) EnsureCreated(r Reconciler, ctx context.Context, cluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", c.GetName()) csiDriverResource := getCSIDriverResource() - result, err := cutil.CreateOrUpdate(ctx, r.Client, csiDriverResource, func() error { - labels.SetManagedLabels(r.Scheme, csiDriverResource, cluster) + result, err := cutil.CreateOrUpdate(ctx, r, csiDriverResource, func() error { + labels.SetManagedLabels(r.Scheme(), csiDriverResource, cluster) // no need to mutate any field return nil }) if err != nil { - return fmt.Errorf("%s failed to reconcile: %w", c.getName(), err) + return fmt.Errorf("%s failed to reconcile: %w", c.GetName(), err) } logger.Info("CSIDriver applied to cluster", "operation", result, "name", csiDriverResource.Name) return nil } -func (c csiDriver) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, _ *lvmv1alpha1.LVMCluster) error { - name := types.NamespacedName{Name: TopolvmCSIDriverName} - logger := log.FromContext(ctx).WithValues("resourceManager", c.getName(), "CSIDriver", TopolvmCSIDriverName) +func (c csiDriver) EnsureDeleted(r Reconciler, ctx context.Context, _ *lvmv1alpha1.LVMCluster) error { + name := types.NamespacedName{Name: constants.TopolvmCSIDriverName} + logger := log.FromContext(ctx).WithValues("resourceManager", c.GetName(), "CSIDriver", constants.TopolvmCSIDriverName) csiDriverResource := &storagev1.CSIDriver{} - if err := r.Client.Get(ctx, name, csiDriverResource); err != nil { + if err := r.Get(ctx, name, csiDriverResource); err != nil { return client.IgnoreNotFound(err) } if !csiDriverResource.GetDeletionTimestamp().IsZero() { - return fmt.Errorf("the CSIDriver %s is still present, waiting for deletion", TopolvmCSIDriverName) + return fmt.Errorf("the CSIDriver %s is still present, waiting for deletion", constants.TopolvmCSIDriverName) } - if err := r.Client.Delete(ctx, csiDriverResource); err != nil { + if err := r.Delete(ctx, csiDriverResource); err != nil { return fmt.Errorf("failed to delete topolvm csi driver %s: %w", csiDriverResource.GetName(), err) } logger.Info("initiated topolvm csi driver deletion", "TopolvmCSIDriverName", csiDriverResource.Name) @@ -92,7 +96,7 @@ func getCSIDriverResource() *storagev1.CSIDriver { storageCapacity := true csiDriver := &storagev1.CSIDriver{ ObjectMeta: metav1.ObjectMeta{ - Name: TopolvmCSIDriverName, + Name: constants.TopolvmCSIDriverName, }, Spec: storagev1.CSIDriverSpec{ AttachRequired: &attachRequired, diff --git a/controllers/topolvm_node.go b/internal/controllers/lvmcluster/resource/topolvm_node.go similarity index 75% rename from controllers/topolvm_node.go rename to internal/controllers/lvmcluster/resource/topolvm_node.go index 8900a7f33..4c6337eb9 100644 --- a/controllers/topolvm_node.go +++ b/internal/controllers/lvmcluster/resource/topolvm_node.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "context" @@ -23,7 +23,9 @@ import ( "strings" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/lvmd" + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/lvmcluster/selector" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvmd" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -39,19 +41,23 @@ const ( topolvmMetricsSecretName = "topolvm-metrics-cert" ) +func TopoLVMNode() Manager { + return topolvmNode{} +} + type topolvmNode struct{} -func (n topolvmNode) getName() string { +func (n topolvmNode) GetName() string { return topolvmNodeName } //+kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=create;update;delete;get;list;watch -func (n topolvmNode) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("topolvmNode", n.getName()) +func (n topolvmNode) EnsureCreated(r Reconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("topolvmNode", n.GetName()) // get desired daemonSet spec - dsTemplate := getNodeDaemonSet(lvmCluster, r.Namespace, r.ImageName) + dsTemplate := getNodeDaemonSet(lvmCluster, r.GetNamespace(), r.GetImageName()) // create desired daemonSet or update mutable fields on existing one ds := &appsv1.DaemonSet{ @@ -62,8 +68,8 @@ func (n topolvmNode) ensureCreated(r *LVMClusterReconciler, ctx context.Context, } logger.Info("running CreateOrUpdate") - result, err := cutil.CreateOrUpdate(ctx, r.Client, ds, func() error { - if err := cutil.SetControllerReference(lvmCluster, ds, r.Scheme); err != nil { + result, err := cutil.CreateOrUpdate(ctx, r, ds, func() error { + if err := cutil.SetControllerReference(lvmCluster, ds, r.Scheme()); err != nil { return fmt.Errorf("failed to set controller reference to topolvm node daemonset: %w", err) } // at creation, deep copy the whole daemonSet @@ -92,7 +98,7 @@ func (n topolvmNode) ensureCreated(r *LVMClusterReconciler, ctx context.Context, }) if err != nil { - return fmt.Errorf("%s failed to reconcile: %w", n.getName(), err) + return fmt.Errorf("%s failed to reconcile: %w", n.GetName(), err) } logger.Info("DaemonSet applied to cluster", "operation", result, "name", ds.Name) @@ -106,7 +112,7 @@ func (n topolvmNode) ensureCreated(r *LVMClusterReconciler, ctx context.Context, } // ensureDeleted is a noop. Deletion will be handled by ownerref -func (n topolvmNode) ensureDeleted(_ *LVMClusterReconciler, _ context.Context, _ *lvmv1alpha1.LVMCluster) error { +func (n topolvmNode) EnsureDeleted(_ Reconciler, _ context.Context, _ *lvmv1alpha1.LVMCluster) error { return nil } @@ -120,22 +126,22 @@ func getNodeDaemonSet(lvmCluster *lvmv1alpha1.LVMCluster, namespace string, init {Name: "registration-dir", VolumeSource: corev1.VolumeSource{ HostPath: &corev1.HostPathVolumeSource{ - Path: fmt.Sprintf("%splugins_registry/", getAbsoluteKubeletPath(CSIKubeletRootDir)), + Path: fmt.Sprintf("%splugins_registry/", getAbsoluteKubeletPath(constants.CSIKubeletRootDir)), Type: &hostPathDirectory}}}, {Name: "node-plugin-dir", VolumeSource: corev1.VolumeSource{ HostPath: &corev1.HostPathVolumeSource{ - Path: fmt.Sprintf("%splugins/topolvm.io/node", getAbsoluteKubeletPath(CSIKubeletRootDir)), + Path: fmt.Sprintf("%splugins/topolvm.io/node", getAbsoluteKubeletPath(constants.CSIKubeletRootDir)), Type: &hostPathDirectoryOrCreateType}}}, {Name: "csi-plugin-dir", VolumeSource: corev1.VolumeSource{ HostPath: &corev1.HostPathVolumeSource{ - Path: fmt.Sprintf("%splugins/kubernetes.io/csi", getAbsoluteKubeletPath(CSIKubeletRootDir)), + Path: fmt.Sprintf("%splugins/kubernetes.io/csi", getAbsoluteKubeletPath(constants.CSIKubeletRootDir)), Type: &hostPathDirectoryOrCreateType}}}, {Name: "pod-volumes-dir", VolumeSource: corev1.VolumeSource{ HostPath: &corev1.HostPathVolumeSource{ - Path: fmt.Sprintf("%spods/", getAbsoluteKubeletPath(CSIKubeletRootDir)), + Path: fmt.Sprintf("%spods/", getAbsoluteKubeletPath(constants.CSIKubeletRootDir)), Type: &hostPathDirectoryOrCreateType}}}, {Name: "lvmd-config-dir", VolumeSource: corev1.VolumeSource{ @@ -158,24 +164,24 @@ func getNodeDaemonSet(lvmCluster *lvmv1alpha1.LVMCluster, namespace string, init containers := []corev1.Container{*getLvmdContainer(), *getNodeContainer(), *getRBACProxyContainer(), *getCsiRegistrarContainer(), *getNodeLivenessProbeContainer()} // Affinity and tolerations - nodeSelector, tolerations := extractNodeSelectorAndTolerations(lvmCluster) + nodeSelector, tolerations := selector.ExtractNodeSelectorAndTolerations(lvmCluster) topolvmNodeTolerations := []corev1.Toleration{} if tolerations != nil { topolvmNodeTolerations = tolerations } annotations := map[string]string{ - workloadPartitioningManagementAnnotation: managementAnnotationVal, + constants.WorkloadPartitioningManagementAnnotation: constants.ManagementAnnotationVal, } labels := map[string]string{ - AppKubernetesNameLabel: CsiDriverNameVal, - AppKubernetesManagedByLabel: ManagedByLabelVal, - AppKubernetesPartOfLabel: PartOfLabelVal, - AppKubernetesComponentLabel: TopolvmNodeLabelVal, + constants.AppKubernetesNameLabel: constants.CsiDriverNameVal, + constants.AppKubernetesManagedByLabel: constants.ManagedByLabelVal, + constants.AppKubernetesPartOfLabel: constants.PartOfLabelVal, + constants.AppKubernetesComponentLabel: constants.TopolvmNodeLabelVal, } nodeDaemonSet := &appsv1.DaemonSet{ ObjectMeta: metav1.ObjectMeta{ - Name: TopolvmNodeDaemonsetName, + Name: constants.TopolvmNodeDaemonsetName, Namespace: namespace, Annotations: annotations, Labels: labels, @@ -191,7 +197,7 @@ func getNodeDaemonSet(lvmCluster *lvmv1alpha1.LVMCluster, namespace string, init Labels: labels, }, Spec: corev1.PodSpec{ - ServiceAccountName: TopolvmNodeServiceAccount, + ServiceAccountName: constants.TopolvmNodeServiceAccount, InitContainers: initContainers, Containers: containers, Volumes: volumes, @@ -226,8 +232,8 @@ func getNodeInitContainer(initImage string) *corev1.Container { VolumeMounts: volumeMounts, Resources: corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(FileCheckerCPURequest), - corev1.ResourceMemory: resource.MustParse(FileCheckerMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.FileCheckerCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.FileCheckerMemRequest), }, }, } @@ -244,13 +250,13 @@ func getLvmdContainer() *corev1.Container { resourceRequirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(TopolvmdCPURequest), - corev1.ResourceMemory: resource.MustParse(TopolvmdMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.TopolvmdCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.TopolvmdMemRequest), }, } volumeMounts := []corev1.VolumeMount{ - {Name: "lvmd-socket-dir", MountPath: filepath.Dir(DefaultLVMdSocket)}, + {Name: "lvmd-socket-dir", MountPath: filepath.Dir(constants.DefaultLVMdSocket)}, {Name: "lvmd-config-dir", MountPath: filepath.Dir(lvmd.DefaultFileConfigPath)}, } @@ -276,26 +282,26 @@ func getNodeContainer() *corev1.Container { command := []string{ "/topolvm-node", - fmt.Sprintf("--lvmd-socket=%s", DefaultLVMdSocket), + fmt.Sprintf("--lvmd-socket=%s", constants.DefaultLVMdSocket), } requirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(TopolvmNodeCPURequest), - corev1.ResourceMemory: resource.MustParse(TopolvmNodeMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.TopolvmNodeCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.TopolvmNodeMemRequest), }, } mountPropagationMode := corev1.MountPropagationBidirectional volumeMounts := []corev1.VolumeMount{ - {Name: "node-plugin-dir", MountPath: filepath.Dir(DefaultCSISocket)}, - {Name: "lvmd-socket-dir", MountPath: filepath.Dir(DefaultLVMdSocket)}, + {Name: "node-plugin-dir", MountPath: filepath.Dir(constants.DefaultCSISocket)}, + {Name: "lvmd-socket-dir", MountPath: filepath.Dir(constants.DefaultLVMdSocket)}, {Name: "pod-volumes-dir", - MountPath: fmt.Sprintf("%spods", getAbsoluteKubeletPath(CSIKubeletRootDir)), + MountPath: fmt.Sprintf("%spods", getAbsoluteKubeletPath(constants.CSIKubeletRootDir)), MountPropagation: &mountPropagationMode}, {Name: "csi-plugin-dir", - MountPath: fmt.Sprintf("%splugins/kubernetes.io/csi", getAbsoluteKubeletPath(CSIKubeletRootDir)), + MountPath: fmt.Sprintf("%splugins/kubernetes.io/csi", getAbsoluteKubeletPath(constants.CSIKubeletRootDir)), MountPropagation: &mountPropagationMode}, } @@ -306,20 +312,20 @@ func getNodeContainer() *corev1.Container { FieldPath: "spec.nodeName"}}}} node := &corev1.Container{ - Name: NodeContainerName, + Name: constants.NodeContainerName, Image: TopolvmCsiImage, Command: command, SecurityContext: &corev1.SecurityContext{ Privileged: &privileged, RunAsUser: &runAsUser, }, - Ports: []corev1.ContainerPort{{Name: TopolvmNodeContainerHealthzName, + Ports: []corev1.ContainerPort{{Name: constants.TopolvmNodeContainerHealthzName, ContainerPort: 9808, Protocol: corev1.ProtocolTCP}}, LivenessProbe: &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{Path: "/healthz", - Port: intstr.FromString(TopolvmNodeContainerHealthzName)}}, + Port: intstr.FromString(constants.TopolvmNodeContainerHealthzName)}}, FailureThreshold: 3, InitialDelaySeconds: 10, TimeoutSeconds: 3, @@ -342,8 +348,8 @@ func getRBACProxyContainer() *corev1.Container { requirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(TopolvmNodeCPURequest), - corev1.ResourceMemory: resource.MustParse(TopolvmNodeMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.TopolvmNodeCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.TopolvmNodeMemRequest), }, } @@ -370,12 +376,12 @@ func getRBACProxyContainer() *corev1.Container { func getCsiRegistrarContainer() *corev1.Container { args := []string{ - fmt.Sprintf("--csi-address=%s", DefaultCSISocket), - fmt.Sprintf("--kubelet-registration-path=%splugins/topolvm.io/node/csi-topolvm.sock", getAbsoluteKubeletPath(CSIKubeletRootDir)), + fmt.Sprintf("--csi-address=%s", constants.DefaultCSISocket), + fmt.Sprintf("--kubelet-registration-path=%splugins/topolvm.io/node/csi-topolvm.sock", getAbsoluteKubeletPath(constants.CSIKubeletRootDir)), } volumeMounts := []corev1.VolumeMount{ - {Name: "node-plugin-dir", MountPath: filepath.Dir(DefaultCSISocket)}, + {Name: "node-plugin-dir", MountPath: filepath.Dir(constants.DefaultCSISocket)}, {Name: "registration-dir", MountPath: "/registration"}, } @@ -387,8 +393,8 @@ func getCsiRegistrarContainer() *corev1.Container { requirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(CSIRegistrarCPURequest), - corev1.ResourceMemory: resource.MustParse(CSIRegistrarMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.CSIRegistrarCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.CSIRegistrarMemRequest), }, } @@ -406,17 +412,17 @@ func getCsiRegistrarContainer() *corev1.Container { func getNodeLivenessProbeContainer() *corev1.Container { resourceRequirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(LivenessProbeCPURequest), - corev1.ResourceMemory: resource.MustParse(LivenessProbeMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.LivenessProbeCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.LivenessProbeMemRequest), }, } args := []string{ - fmt.Sprintf("--csi-address=%s", DefaultCSISocket), + fmt.Sprintf("--csi-address=%s", constants.DefaultCSISocket), } volumeMounts := []corev1.VolumeMount{ - {Name: "node-plugin-dir", MountPath: filepath.Dir(DefaultCSISocket)}, + {Name: "node-plugin-dir", MountPath: filepath.Dir(constants.DefaultCSISocket)}, } liveness := &corev1.Container{ diff --git a/controllers/topolvm_snapshotclass.go b/internal/controllers/lvmcluster/resource/topolvm_snapshotclass.go similarity index 72% rename from controllers/topolvm_snapshotclass.go rename to internal/controllers/lvmcluster/resource/topolvm_snapshotclass.go index b85ab8aaa..9d90ba1c0 100644 --- a/controllers/topolvm_snapshotclass.go +++ b/internal/controllers/lvmcluster/resource/topolvm_snapshotclass.go @@ -14,18 +14,18 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "context" "fmt" snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/labels" "k8s.io/apimachinery/pkg/api/errors" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/labels" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" cutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -36,46 +36,50 @@ const ( vscName = "topolvm-volumeSnapshotClass" ) +func TopoLVMVolumeSnapshotClass() Manager { + return topolvmVolumeSnapshotClass{} +} + type topolvmVolumeSnapshotClass struct{} // topolvmVolumeSnapshotClass unit satisfies resourceManager interface -var _ resourceManager = topolvmVolumeSnapshotClass{} +var _ Manager = topolvmVolumeSnapshotClass{} -func (s topolvmVolumeSnapshotClass) getName() string { +func (s topolvmVolumeSnapshotClass) GetName() string { return vscName } //+kubebuilder:rbac:groups=snapshot.storage.k8s.io,resources=volumesnapshotclasses,verbs=get;create;delete;watch;list -func (s topolvmVolumeSnapshotClass) ensureCreated(r *LVMClusterReconciler, ctx context.Context, cluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", s.getName()) +func (s topolvmVolumeSnapshotClass) EnsureCreated(r Reconciler, ctx context.Context, cluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", s.GetName()) // one volume snapshot class for every deviceClass based on CR is created topolvmSnapshotClasses := getTopolvmSnapshotClasses(cluster) for _, vsc := range topolvmSnapshotClasses { // we anticipate no edits to volume snapshot class - result, err := cutil.CreateOrUpdate(ctx, r.Client, vsc, func() error { - labels.SetManagedLabels(r.Scheme, vsc, cluster) + result, err := cutil.CreateOrUpdate(ctx, r, vsc, func() error { + labels.SetManagedLabels(r.Scheme(), vsc, cluster) return nil }) if err != nil { - return fmt.Errorf("%s failed to reconcile: %w", s.getName(), err) + return fmt.Errorf("%s failed to reconcile: %w", s.GetName(), err) } logger.Info("VolumeSnapshotClass applied to cluster", "operation", result, "name", vsc.Name) } return nil } -func (s topolvmVolumeSnapshotClass) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", s.getName()) +func (s topolvmVolumeSnapshotClass) EnsureDeleted(r Reconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", s.GetName()) for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses { // construct name of volume snapshot class based on CR spec deviceClass field and // delete the corresponding volume snapshot class - vscName := getVolumeSnapshotClassName(deviceClass.Name) + vscName := GetVolumeSnapshotClassName(deviceClass.Name) logger := logger.WithValues("VolumeSnapshotClass", vscName) vsc := &snapapi.VolumeSnapshotClass{} - if err := r.Client.Get(ctx, types.NamespacedName{Name: vscName}, vsc); err != nil { + if err := r.Get(ctx, types.NamespacedName{Name: vscName}, vsc); err != nil { if errors.IsNotFound(err) { continue } @@ -86,7 +90,7 @@ func (s topolvmVolumeSnapshotClass) ensureDeleted(r *LVMClusterReconciler, ctx c return fmt.Errorf("the VolumeSnapshotClass %s is still present, waiting for deletion", vscName) } - if err := r.Client.Delete(ctx, vsc); err != nil { + if err := r.Delete(ctx, vsc); err != nil { return fmt.Errorf("failed to delete topolvm VolumeSnapshotClass %s: %w", vscName, err) } logger.Info("initiated VolumeSnapshotClass deletion") @@ -100,10 +104,10 @@ func getTopolvmSnapshotClasses(lvmCluster *lvmv1alpha1.LVMCluster) []*snapapi.Vo for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses { snapshotClass := &snapapi.VolumeSnapshotClass{ ObjectMeta: metav1.ObjectMeta{ - Name: getVolumeSnapshotClassName(deviceClass.Name), + Name: GetVolumeSnapshotClassName(deviceClass.Name), }, - Driver: TopolvmCSIDriverName, + Driver: constants.TopolvmCSIDriverName, DeletionPolicy: snapapi.VolumeSnapshotContentDelete, } vsc = append(vsc, snapshotClass) diff --git a/controllers/topolvm_storageclass.go b/internal/controllers/lvmcluster/resource/topolvm_storageclass.go similarity index 75% rename from controllers/topolvm_storageclass.go rename to internal/controllers/lvmcluster/resource/topolvm_storageclass.go index 67eac7711..2b6bbe58a 100644 --- a/controllers/topolvm_storageclass.go +++ b/internal/controllers/lvmcluster/resource/topolvm_storageclass.go @@ -14,15 +14,15 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "context" "fmt" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/labels" - + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/labels" storagev1 "k8s.io/api/storage/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -35,47 +35,51 @@ const ( scName = "topolvm-storageclass" ) +func TopoLVMStorageClass() Manager { + return &topolvmStorageClass{} +} + type topolvmStorageClass struct{} // topolvmStorageClass unit satisfies resourceManager interface -var _ resourceManager = topolvmStorageClass{} +var _ Manager = topolvmStorageClass{} -func (s topolvmStorageClass) getName() string { +func (s topolvmStorageClass) GetName() string { return scName } //+kubebuilder:rbac:groups=storage.k8s.io,resources=storageclasses,verbs=get;create;delete;watch;list -func (s topolvmStorageClass) ensureCreated(r *LVMClusterReconciler, ctx context.Context, cluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", s.getName()) +func (s topolvmStorageClass) EnsureCreated(r Reconciler, ctx context.Context, cluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", s.GetName()) // one storage class for every deviceClass based on CR is created topolvmStorageClasses := s.getTopolvmStorageClasses(r, ctx, cluster) for _, sc := range topolvmStorageClasses { // we anticipate no edits to storage class - result, err := cutil.CreateOrUpdate(ctx, r.Client, sc, func() error { - labels.SetManagedLabels(r.Scheme, sc, cluster) + result, err := cutil.CreateOrUpdate(ctx, r, sc, func() error { + labels.SetManagedLabels(r.Scheme(), sc, cluster) return nil }) if err != nil { - return fmt.Errorf("%s failed to reconcile: %w", s.getName(), err) + return fmt.Errorf("%s failed to reconcile: %w", s.GetName(), err) } logger.Info("StorageClass applied to cluster", "operation", result, "name", sc.Name) } return nil } -func (s topolvmStorageClass) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", s.getName()) +func (s topolvmStorageClass) EnsureDeleted(r Reconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", s.GetName()) // construct name of storage class based on CR spec deviceClass field and // delete the corresponding storage class for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses { - scName := getStorageClassName(deviceClass.Name) + scName := GetStorageClassName(deviceClass.Name) logger := logger.WithValues("StorageClass", scName) sc := &storagev1.StorageClass{} - if err := r.Client.Get(ctx, types.NamespacedName{Name: scName}, sc); err != nil { + if err := r.Get(ctx, types.NamespacedName{Name: scName}, sc); err != nil { if errors.IsNotFound(err) { continue } @@ -86,7 +90,7 @@ func (s topolvmStorageClass) ensureDeleted(r *LVMClusterReconciler, ctx context. return fmt.Errorf("the StorageClass %s is still present, waiting for deletion", scName) } - if err := r.Client.Delete(ctx, sc); err != nil { + if err := r.Delete(ctx, sc); err != nil { return fmt.Errorf("failed to delete StorageClass %s: %w", scName, err) } logger.Info("initiated StorageClass deletion") @@ -94,8 +98,8 @@ func (s topolvmStorageClass) ensureDeleted(r *LVMClusterReconciler, ctx context. return nil } -func (s topolvmStorageClass) getTopolvmStorageClasses(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) []*storagev1.StorageClass { - logger := log.FromContext(ctx).WithValues("resourceManager", s.getName()) +func (s topolvmStorageClass) getTopolvmStorageClasses(r Reconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) []*storagev1.StorageClass { + logger := log.FromContext(ctx).WithValues("resourceManager", s.GetName()) const defaultSCAnnotation string = "storageclass.kubernetes.io/is-default-class" allowVolumeExpansion := true @@ -105,7 +109,7 @@ func (s topolvmStorageClass) getTopolvmStorageClasses(r *LVMClusterReconciler, c // Mark the lvms storage class, associated with the default device class, as default if no other default storage class exists on the cluster scList := &storagev1.StorageClassList{} - err := r.Client.List(ctx, scList) + err := r.List(ctx, scList) if err != nil { logger.Error(err, "failed to list storage classes. Not setting any storageclass as the default") @@ -121,7 +125,7 @@ func (s topolvmStorageClass) getTopolvmStorageClasses(r *LVMClusterReconciler, c } var sc []*storagev1.StorageClass for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses { - scName := getStorageClassName(deviceClass.Name) + scName := GetStorageClassName(deviceClass.Name) storageClass := &storagev1.StorageClass{ ObjectMeta: metav1.ObjectMeta{ @@ -130,11 +134,11 @@ func (s topolvmStorageClass) getTopolvmStorageClasses(r *LVMClusterReconciler, c "description": "Provides RWO and RWOP Filesystem & Block volumes", }, }, - Provisioner: TopolvmCSIDriverName, + Provisioner: constants.TopolvmCSIDriverName, VolumeBindingMode: &volumeBindingMode, AllowVolumeExpansion: &allowVolumeExpansion, Parameters: map[string]string{ - DeviceClassKey: deviceClass.Name, + constants.DeviceClassKey: deviceClass.Name, "csi.storage.k8s.io/fstype": string(deviceClass.FilesystemType), }, } diff --git a/controllers/utils.go b/internal/controllers/lvmcluster/resource/utils.go similarity index 74% rename from controllers/utils.go rename to internal/controllers/lvmcluster/resource/utils.go index 4a57a4e81..666049254 100644 --- a/controllers/utils.go +++ b/internal/controllers/lvmcluster/resource/utils.go @@ -14,40 +14,17 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "fmt" - lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" + "github.com/openshift/lvm-operator/internal/controllers/constants" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/intstr" ) -// extractNodeSelectorAndTolerations combines and extracts scheduling parameters from the multiple deviceClass entries in an lvmCluster -func extractNodeSelectorAndTolerations(lvmCluster *lvmv1alpha1.LVMCluster) (*corev1.NodeSelector, []corev1.Toleration) { - var nodeSelector *corev1.NodeSelector - - tolerations := lvmCluster.Spec.Tolerations - - terms := make([]corev1.NodeSelectorTerm, 0) - matchAllNodes := false - for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses { - - if deviceClass.NodeSelector != nil { - terms = append(terms, deviceClass.NodeSelector.NodeSelectorTerms...) - } else { - matchAllNodes = true - } - } - // populate a nodeSelector unless one or more of the deviceClasses match all nodes with a nil nodeSelector - if !matchAllNodes { - nodeSelector = &corev1.NodeSelector{NodeSelectorTerms: terms} - } - return nodeSelector, tolerations -} - func setDaemonsetNodeSelector(nodeSelector *corev1.NodeSelector, ds *appsv1.DaemonSet) { if nodeSelector != nil { ds.Spec.Template.Spec.Affinity = &corev1.Affinity{ @@ -60,12 +37,12 @@ func setDaemonsetNodeSelector(nodeSelector *corev1.NodeSelector, ds *appsv1.Daem } } -func getStorageClassName(deviceName string) string { - return StorageClassPrefix + deviceName +func GetStorageClassName(deviceName string) string { + return constants.StorageClassPrefix + deviceName } -func getVolumeSnapshotClassName(deviceName string) string { - return volumeSnapshotClassPrefix + deviceName +func GetVolumeSnapshotClassName(deviceName string) string { + return constants.VolumeSnapshotClassPrefix + deviceName } func verifyDaemonSetReadiness(ds *appsv1.DaemonSet) error { diff --git a/controllers/vgmanager.go b/internal/controllers/lvmcluster/resource/vgmanager.go similarity index 83% rename from controllers/vgmanager.go rename to internal/controllers/lvmcluster/resource/vgmanager.go index 714b723e7..1fcc615b8 100644 --- a/controllers/vgmanager.go +++ b/internal/controllers/lvmcluster/resource/vgmanager.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( "context" @@ -27,23 +27,27 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" ) +func VGManager() Manager { + return vgManager{} +} + type vgManager struct{} -var _ resourceManager = vgManager{} +var _ Manager = vgManager{} const ( VGManagerUnit = "vg-manager" ) -func (v vgManager) getName() string { +func (v vgManager) GetName() string { return VGManagerUnit } -func (v vgManager) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { - logger := log.FromContext(ctx).WithValues("resourceManager", v.getName()) +func (v vgManager) EnsureCreated(r Reconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { + logger := log.FromContext(ctx).WithValues("resourceManager", v.GetName()) // get desired daemonset spec - dsTemplate := newVGManagerDaemonset(lvmCluster, r.Namespace, r.ImageName) + dsTemplate := newVGManagerDaemonset(lvmCluster, r.GetNamespace(), r.GetImageName()) // create desired daemonset or update mutable fields on existing one ds := &appsv1.DaemonSet{ @@ -55,8 +59,8 @@ func (v vgManager) ensureCreated(r *LVMClusterReconciler, ctx context.Context, l logger.Info("running CreateOrUpdate") // the anonymous mutate function modifies the daemonset object after fetching it. // if the daemonset does not already exist, it creates it, otherwise, it updates it - result, err := ctrl.CreateOrUpdate(ctx, r.Client, ds, func() error { - if err := ctrl.SetControllerReference(lvmCluster, ds, r.Scheme); err != nil { + result, err := ctrl.CreateOrUpdate(ctx, r, ds, func() error { + if err := ctrl.SetControllerReference(lvmCluster, ds, r.Scheme()); err != nil { return fmt.Errorf("failed to set controller reference on vgManager daemonset %q. %v", dsTemplate.Name, err) } // at creation, deep copy the whole daemonset @@ -85,7 +89,7 @@ func (v vgManager) ensureCreated(r *LVMClusterReconciler, ctx context.Context, l }) if err != nil { - return fmt.Errorf("%s failed to reconcile: %w", v.getName(), err) + return fmt.Errorf("%s failed to reconcile: %w", v.GetName(), err) } logger.Info("DaemonSet applied to cluster", "operation", result, "name", ds.Name) @@ -99,7 +103,7 @@ func (v vgManager) ensureCreated(r *LVMClusterReconciler, ctx context.Context, l } // ensureDeleted is a noop. Deletion will be handled by ownerref -func (v vgManager) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error { +func (v vgManager) EnsureDeleted(_ Reconciler, _ context.Context, _ *lvmv1alpha1.LVMCluster) error { return nil } diff --git a/controllers/vgmanager_daemonset.go b/internal/controllers/lvmcluster/resource/vgmanager_daemonset.go similarity index 88% rename from controllers/vgmanager_daemonset.go rename to internal/controllers/lvmcluster/resource/vgmanager_daemonset.go index e30fc2495..07ede64a8 100644 --- a/controllers/vgmanager_daemonset.go +++ b/internal/controllers/lvmcluster/resource/vgmanager_daemonset.go @@ -14,10 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package resource import ( lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/lvmcluster/selector" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -137,7 +139,7 @@ var ( // newVGManagerDaemonset returns the desired vgmanager daemonset for a given LVMCluster func newVGManagerDaemonset(lvmCluster *lvmv1alpha1.LVMCluster, namespace string, vgImage string) appsv1.DaemonSet { // aggregate nodeSelector and tolerations from all deviceClasses - nodeSelector, tolerations := extractNodeSelectorAndTolerations(lvmCluster) + nodeSelector, tolerations := selector.ExtractNodeSelectorAndTolerations(lvmCluster) volumes := []corev1.Volume{LVMDConfVol, DevHostDirVol, UDevHostDirVol, SysHostDirVol, MetricsCertsDirVol} volumeMounts := []corev1.VolumeMount{LVMDConfVolMount, DevHostDirVolMount, UDevHostDirVolMount, SysHostDirVolMount, MetricsCertsDirVolMount} privileged := true @@ -149,8 +151,8 @@ func newVGManagerDaemonset(lvmCluster *lvmv1alpha1.LVMCluster, namespace string, resourceRequirements := corev1.ResourceRequirements{ Requests: corev1.ResourceList{ - corev1.ResourceCPU: resource.MustParse(VgManagerCPURequest), - corev1.ResourceMemory: resource.MustParse(VgManagerMemRequest), + corev1.ResourceCPU: resource.MustParse(constants.VgManagerCPURequest), + corev1.ResourceMemory: resource.MustParse(constants.VgManagerMemRequest), }, } containers := []corev1.Container{ @@ -193,13 +195,13 @@ func newVGManagerDaemonset(lvmCluster *lvmv1alpha1.LVMCluster, namespace string, }, } annotations := map[string]string{ - workloadPartitioningManagementAnnotation: managementAnnotationVal, + constants.WorkloadPartitioningManagementAnnotation: constants.ManagementAnnotationVal, } labels := map[string]string{ - AppKubernetesNameLabel: VGManagerLabelVal, - AppKubernetesManagedByLabel: ManagedByLabelVal, - AppKubernetesPartOfLabel: PartOfLabelVal, - AppKubernetesComponentLabel: VGManagerLabelVal, + constants.AppKubernetesNameLabel: constants.VGManagerLabelVal, + constants.AppKubernetesManagedByLabel: constants.ManagedByLabelVal, + constants.AppKubernetesPartOfLabel: constants.PartOfLabelVal, + constants.AppKubernetesComponentLabel: constants.VGManagerLabelVal, } ds := appsv1.DaemonSet{ ObjectMeta: metav1.ObjectMeta{ @@ -221,7 +223,7 @@ func newVGManagerDaemonset(lvmCluster *lvmv1alpha1.LVMCluster, namespace string, // to read /proc/1/mountinfo HostPID: true, Tolerations: tolerations, - ServiceAccountName: VGManagerServiceAccount, + ServiceAccountName: constants.VGManagerServiceAccount, }, }, }, diff --git a/internal/controllers/lvmcluster/selector/selector.go b/internal/controllers/lvmcluster/selector/selector.go new file mode 100644 index 000000000..2d5071ebd --- /dev/null +++ b/internal/controllers/lvmcluster/selector/selector.go @@ -0,0 +1,29 @@ +package selector + +import ( + lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" + corev1 "k8s.io/api/core/v1" +) + +// ExtractNodeSelectorAndTolerations combines and extracts scheduling parameters from the multiple deviceClass entries in an lvmCluster +func ExtractNodeSelectorAndTolerations(lvmCluster *lvmv1alpha1.LVMCluster) (*corev1.NodeSelector, []corev1.Toleration) { + var nodeSelector *corev1.NodeSelector + + tolerations := lvmCluster.Spec.Tolerations + + terms := make([]corev1.NodeSelectorTerm, 0) + matchAllNodes := false + for _, deviceClass := range lvmCluster.Spec.Storage.DeviceClasses { + + if deviceClass.NodeSelector != nil { + terms = append(terms, deviceClass.NodeSelector.NodeSelectorTerms...) + } else { + matchAllNodes = true + } + } + // populate a nodeSelector unless one or more of the deviceClasses match all nodes with a nil nodeSelector + if !matchAllNodes { + nodeSelector = &corev1.NodeSelector{NodeSelectorTerms: terms} + } + return nodeSelector, tolerations +} diff --git a/controllers/suite_test.go b/internal/controllers/lvmcluster/suite_test.go similarity index 94% rename from controllers/suite_test.go rename to internal/controllers/lvmcluster/suite_test.go index 8517ef773..39f3e014c 100644 --- a/controllers/suite_test.go +++ b/internal/controllers/lvmcluster/suite_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package lvmcluster import ( "context" @@ -25,7 +25,8 @@ import ( . "github.com/onsi/gomega" configv1 "github.com/openshift/api/config/v1" secv1 "github.com/openshift/api/security/v1" - "github.com/openshift/lvm-operator/pkg/cluster" + "github.com/openshift/lvm-operator/internal/cluster" + "github.com/openshift/lvm-operator/internal/controllers/node" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/client-go/kubernetes/scheme" @@ -37,7 +38,6 @@ import ( snapapi "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/controllers/node" topolvmv1 "github.com/topolvm/topolvm/api/v1" //+kubebuilder:scaffold:imports ) @@ -74,8 +74,8 @@ var _ = BeforeSuite(func() { By("bootstrapping test environment") testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases"), - filepath.Join("..", "test", "e2e", "testdata")}, + CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases"), + filepath.Join("..", "..", "..", "test", "e2e", "testdata")}, ErrorIfCRDPathMissing: true, CRDInstallOptions: envtest.CRDInstallOptions{ CleanUpAfterUse: true, @@ -134,7 +134,6 @@ var _ = BeforeSuite(func() { Client: k8sManager.GetClient(), EventRecorder: k8sManager.GetEventRecorderFor("LVMClusterReconciler"), EnableSnapshotting: enableSnapshotting, - Scheme: k8sManager.GetScheme(), ClusterType: clusterType, Namespace: testLvmClusterNamespace, ImageName: testImageName, diff --git a/controllers/vgmanager_test.go b/internal/controllers/lvmcluster/vgmanager_test.go similarity index 86% rename from controllers/vgmanager_test.go rename to internal/controllers/lvmcluster/vgmanager_test.go index f51d57973..8ef8dd333 100644 --- a/controllers/vgmanager_test.go +++ b/internal/controllers/lvmcluster/vgmanager_test.go @@ -14,12 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -package controllers +package lvmcluster import ( "context" "testing" + "github.com/openshift/lvm-operator/internal/controllers/lvmcluster/resource" "gotest.tools/v3/assert" "github.com/go-logr/logr/testr" @@ -55,11 +56,8 @@ func newFakeLVMClusterReconciler(t *testing.T, objs ...client.Object) *LVMCluste err = snapapi.AddToScheme(scheme) assert.NilError(t, err, "adding snapshot api to scheme") - client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build() - return &LVMClusterReconciler{ - Client: client, - Scheme: scheme, + Client: fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build(), Namespace: "default", } } @@ -95,11 +93,11 @@ func TestVGManagerEnsureCreated(t *testing.T) { Spec: testCase.lvmclusterSpec, } r := newFakeLVMClusterReconciler(t, lvmcluster) - var unit resourceManager = vgManager{} - err := unit.ensureCreated(r, log.IntoContext(context.Background(), testr.New(t)), lvmcluster) - assert.NilError(t, err, "running ensureCreated") + var unit = resource.VGManager() + err := unit.EnsureCreated(r, log.IntoContext(context.Background(), testr.New(t)), lvmcluster) + assert.NilError(t, err, "running EnsureCreated") ds := &appsv1.DaemonSet{} - err = r.Client.Get(context.TODO(), types.NamespacedName{Name: VGManagerUnit, Namespace: testNamespace}, ds) + err = r.Get(context.TODO(), types.NamespacedName{Name: resource.VGManagerUnit, Namespace: testNamespace}, ds) assert.NilError(t, err, "fetching daemonset") if testCase.expectedAffinity == nil { assert.Equal(t, testCase.expectedAffinity, ds.Spec.Template.Spec.Affinity) diff --git a/controllers/node/removal_controller.go b/internal/controllers/node/removal_controller.go similarity index 100% rename from controllers/node/removal_controller.go rename to internal/controllers/node/removal_controller.go diff --git a/controllers/persistent-volume-claim/controller.go b/internal/controllers/persistent-volume-claim/controller.go similarity index 95% rename from controllers/persistent-volume-claim/controller.go rename to internal/controllers/persistent-volume-claim/controller.go index 2ff69ba96..1659f8774 100644 --- a/controllers/persistent-volume-claim/controller.go +++ b/internal/controllers/persistent-volume-claim/controller.go @@ -6,7 +6,7 @@ import ( "math" "strings" - "github.com/openshift/lvm-operator/controllers" + "github.com/openshift/lvm-operator/internal/controllers/constants" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" @@ -69,9 +69,9 @@ func (r *PersistentVolumeClaimReconciler) Reconcile(ctx context.Context, req ctr // Skip if StorageClassName does not contain the lvms prefix lvmsPrefix, deviceClass, exists := strings.Cut(*pvc.Spec.StorageClassName, "-") - if !exists || fmt.Sprintf("%s-", lvmsPrefix) != controllers.StorageClassPrefix { + if !exists || fmt.Sprintf("%s-", lvmsPrefix) != constants.StorageClassPrefix { logger.Info("skipping pvc as the storageClassName does not contain desired prefix", - "desired-prefix", controllers.StorageClassPrefix) + "desired-prefix", constants.StorageClassPrefix) return ctrl.Result{}, nil } diff --git a/controllers/persistent-volume-claim/controller_test.go b/internal/controllers/persistent-volume-claim/controller_test.go similarity index 93% rename from controllers/persistent-volume-claim/controller_test.go rename to internal/controllers/persistent-volume-claim/controller_test.go index 41f3b06d7..7e9773b80 100644 --- a/controllers/persistent-volume-claim/controller_test.go +++ b/internal/controllers/persistent-volume-claim/controller_test.go @@ -7,8 +7,8 @@ import ( "testing" "time" - "github.com/openshift/lvm-operator/controllers" - persistentvolumeclaim "github.com/openshift/lvm-operator/controllers/persistent-volume-claim" + "github.com/openshift/lvm-operator/internal/controllers/constants" + persistentvolumeclaim "github.com/openshift/lvm-operator/internal/controllers/persistent-volume-claim" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -81,7 +81,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-non-pending-PVC"}, Spec: v1.PersistentVolumeClaimSpec{ - StorageClassName: ptr.To(controllers.StorageClassPrefix + "bla"), + StorageClassName: ptr.To(constants.StorageClassPrefix + "bla"), }, Status: v1.PersistentVolumeClaimStatus{ Phase: v1.ClaimBound, @@ -99,7 +99,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"}, Spec: v1.PersistentVolumeClaimSpec{ - StorageClassName: ptr.To(controllers.StorageClassPrefix + "bla"), + StorageClassName: ptr.To(constants.StorageClassPrefix + "bla"), }, Status: v1.PersistentVolumeClaimStatus{ Phase: v1.ClaimPending, @@ -118,7 +118,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"}, Spec: v1.PersistentVolumeClaimSpec{ - StorageClassName: ptr.To(controllers.StorageClassPrefix + "bla"), + StorageClassName: ptr.To(constants.StorageClassPrefix + "bla"), Resources: v1.ResourceRequirements{ Requests: v1.ResourceList{ v1.ResourceStorage: *resource.NewQuantity(100, resource.DecimalSI), @@ -145,7 +145,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"}, Spec: v1.PersistentVolumeClaimSpec{ - StorageClassName: ptr.To(controllers.StorageClassPrefix + "bla"), + StorageClassName: ptr.To(constants.StorageClassPrefix + "bla"), Resources: v1.ResourceRequirements{ Requests: v1.ResourceList{ v1.ResourceStorage: *resource.NewQuantity(10, resource.DecimalSI), @@ -172,7 +172,7 @@ func TestPersistentVolumeClaimReconciler_Reconcile(t *testing.T) { &v1.PersistentVolumeClaim{ ObjectMeta: metav1.ObjectMeta{Namespace: defaultNamespace, Name: "test-pending-PVC"}, Spec: v1.PersistentVolumeClaimSpec{ - StorageClassName: ptr.To(controllers.StorageClassPrefix + "bla"), + StorageClassName: ptr.To(constants.StorageClassPrefix + "bla"), Resources: v1.ResourceRequirements{ Requests: v1.ResourceList{ v1.ResourceStorage: *resource.NewQuantity(10, resource.DecimalSI), diff --git a/controllers/persistent-volume/controller.go b/internal/controllers/persistent-volume/controller.go similarity index 95% rename from controllers/persistent-volume/controller.go rename to internal/controllers/persistent-volume/controller.go index a38033d14..16b8d4408 100644 --- a/controllers/persistent-volume/controller.go +++ b/internal/controllers/persistent-volume/controller.go @@ -4,8 +4,7 @@ import ( "context" "strings" - "github.com/openshift/lvm-operator/controllers" - + "github.com/openshift/lvm-operator/internal/controllers/constants" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/client-go/tools/record" @@ -50,7 +49,7 @@ func (r *PersistentVolumeReconciler) Reconcile(ctx context.Context, req ctrl.Req } // Skip if the PV is deleted or PV does not use the lvms storage class. - if pv.DeletionTimestamp != nil || !strings.HasPrefix(pv.Spec.StorageClassName, controllers.StorageClassPrefix) { + if pv.DeletionTimestamp != nil || !strings.HasPrefix(pv.Spec.StorageClassName, constants.StorageClassPrefix) { return ctrl.Result{}, nil } diff --git a/pkg/vgmanager/devices.go b/internal/controllers/vgmanager/devices.go similarity index 97% rename from pkg/vgmanager/devices.go rename to internal/controllers/vgmanager/devices.go index 54925a089..e11402447 100644 --- a/pkg/vgmanager/devices.go +++ b/internal/controllers/vgmanager/devices.go @@ -23,9 +23,9 @@ import ( "path/filepath" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/filter" - "github.com/openshift/lvm-operator/pkg/lsblk" - "github.com/openshift/lvm-operator/pkg/lvm" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/filter" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm" "sigs.k8s.io/controller-runtime/pkg/log" ) diff --git a/pkg/vgmanager/devices_test.go b/internal/controllers/vgmanager/devices_test.go similarity index 98% rename from pkg/vgmanager/devices_test.go rename to internal/controllers/vgmanager/devices_test.go index 7f7345aa9..b156ac8fc 100644 --- a/pkg/vgmanager/devices_test.go +++ b/internal/controllers/vgmanager/devices_test.go @@ -8,8 +8,8 @@ import ( "github.com/go-logr/logr/testr" "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/filter" - "github.com/openshift/lvm-operator/pkg/lsblk" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/filter" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" "github.com/stretchr/testify/assert" "sigs.k8s.io/controller-runtime/pkg/log" diff --git a/pkg/dmsetup/dmsetup.go b/internal/controllers/vgmanager/dmsetup/dmsetup.go similarity index 94% rename from pkg/dmsetup/dmsetup.go rename to internal/controllers/vgmanager/dmsetup/dmsetup.go index b78359e30..de2b3d612 100644 --- a/pkg/dmsetup/dmsetup.go +++ b/internal/controllers/vgmanager/dmsetup/dmsetup.go @@ -5,7 +5,7 @@ import ( "fmt" "strings" - "github.com/openshift/lvm-operator/pkg/internal/exec" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec" ) var ( diff --git a/pkg/dmsetup/dmsetup_test.go b/internal/controllers/vgmanager/dmsetup/dmsetup_test.go similarity index 92% rename from pkg/dmsetup/dmsetup_test.go rename to internal/controllers/vgmanager/dmsetup/dmsetup_test.go index f194f13ee..daa21271f 100644 --- a/pkg/dmsetup/dmsetup_test.go +++ b/internal/controllers/vgmanager/dmsetup/dmsetup_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - mockExec "github.com/openshift/lvm-operator/pkg/internal/exec/test" + mockExec "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec/test" "github.com/stretchr/testify/assert" ) diff --git a/pkg/dmsetup/mocks/mock_dmsetup.go b/internal/controllers/vgmanager/dmsetup/mocks/mock_dmsetup.go similarity index 100% rename from pkg/dmsetup/mocks/mock_dmsetup.go rename to internal/controllers/vgmanager/dmsetup/mocks/mock_dmsetup.go diff --git a/pkg/internal/exec/exec.go b/internal/controllers/vgmanager/exec/exec.go similarity index 100% rename from pkg/internal/exec/exec.go rename to internal/controllers/vgmanager/exec/exec.go diff --git a/pkg/internal/exec/test/mock_exec.go b/internal/controllers/vgmanager/exec/test/mock_exec.go similarity index 100% rename from pkg/internal/exec/test/mock_exec.go rename to internal/controllers/vgmanager/exec/test/mock_exec.go diff --git a/pkg/filter/filter.go b/internal/controllers/vgmanager/filter/filter.go similarity index 97% rename from pkg/filter/filter.go rename to internal/controllers/vgmanager/filter/filter.go index 4d222abe6..17385c9e9 100644 --- a/pkg/filter/filter.go +++ b/internal/controllers/vgmanager/filter/filter.go @@ -22,8 +22,8 @@ import ( "strings" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/lsblk" - "github.com/openshift/lvm-operator/pkg/lvm" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm" ) const ( diff --git a/pkg/filter/filter_test.go b/internal/controllers/vgmanager/filter/filter_test.go similarity index 97% rename from pkg/filter/filter_test.go rename to internal/controllers/vgmanager/filter/filter_test.go index 645a1cd91..cdb895073 100644 --- a/pkg/filter/filter_test.go +++ b/internal/controllers/vgmanager/filter/filter_test.go @@ -6,9 +6,9 @@ import ( "testing" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/lsblk" - "github.com/openshift/lvm-operator/pkg/lvm" - lvmmocks "github.com/openshift/lvm-operator/pkg/lvm/mocks" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm" + lvmmocks "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm/mocks" "github.com/stretchr/testify/assert" ) diff --git a/pkg/vgmanager/kname_test.go b/internal/controllers/vgmanager/kname_test.go similarity index 100% rename from pkg/vgmanager/kname_test.go rename to internal/controllers/vgmanager/kname_test.go diff --git a/pkg/lsblk/lsblk.go b/internal/controllers/vgmanager/lsblk/lsblk.go similarity index 98% rename from pkg/lsblk/lsblk.go rename to internal/controllers/vgmanager/lsblk/lsblk.go index 91ca12c1e..cd4b7318d 100644 --- a/pkg/lsblk/lsblk.go +++ b/internal/controllers/vgmanager/lsblk/lsblk.go @@ -7,7 +7,7 @@ import ( "path/filepath" "strings" - "github.com/openshift/lvm-operator/pkg/internal/exec" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec" ) var ( diff --git a/pkg/lsblk/mocks/mock_lsblk.go b/internal/controllers/vgmanager/lsblk/mocks/mock_lsblk.go similarity index 98% rename from pkg/lsblk/mocks/mock_lsblk.go rename to internal/controllers/vgmanager/lsblk/mocks/mock_lsblk.go index 75ac2c7b7..f117b7cba 100644 --- a/pkg/lsblk/mocks/mock_lsblk.go +++ b/internal/controllers/vgmanager/lsblk/mocks/mock_lsblk.go @@ -3,7 +3,7 @@ package lsblk import ( - lsblk "github.com/openshift/lvm-operator/pkg/lsblk" + lsblk "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" mock "github.com/stretchr/testify/mock" ) diff --git a/pkg/vgmanager/lv_attr.go b/internal/controllers/vgmanager/lv_attr.go similarity index 100% rename from pkg/vgmanager/lv_attr.go rename to internal/controllers/vgmanager/lv_attr.go diff --git a/pkg/vgmanager/lv_attr_test.go b/internal/controllers/vgmanager/lv_attr_test.go similarity index 100% rename from pkg/vgmanager/lv_attr_test.go rename to internal/controllers/vgmanager/lv_attr_test.go diff --git a/pkg/lvm/lvm.go b/internal/controllers/vgmanager/lvm/lvm.go similarity index 98% rename from pkg/lvm/lvm.go rename to internal/controllers/vgmanager/lvm/lvm.go index 29c6d830c..32fbc5f18 100644 --- a/pkg/lvm/lvm.go +++ b/internal/controllers/vgmanager/lvm/lvm.go @@ -23,7 +23,7 @@ import ( osexec "os/exec" "strings" - lvmexec "github.com/openshift/lvm-operator/pkg/internal/exec" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec" ) type lvmError string @@ -103,14 +103,14 @@ type LVM interface { } type HostLVM struct { - lvmexec.Executor + exec.Executor } func NewDefaultHostLVM() *HostLVM { - return NewHostLVM(&lvmexec.CommandExecutor{}) + return NewHostLVM(&exec.CommandExecutor{}) } -func NewHostLVM(executor lvmexec.Executor) *HostLVM { +func NewHostLVM(executor exec.Executor) *HostLVM { return &HostLVM{executor} } diff --git a/pkg/lvm/lvm_test.go b/internal/controllers/vgmanager/lvm/lvm_test.go similarity index 96% rename from pkg/lvm/lvm_test.go rename to internal/controllers/vgmanager/lvm/lvm_test.go index f46da6aca..c03b22465 100644 --- a/pkg/lvm/lvm_test.go +++ b/internal/controllers/vgmanager/lvm/lvm_test.go @@ -21,7 +21,7 @@ import ( "strings" "testing" - mockExec "github.com/openshift/lvm-operator/pkg/internal/exec/test" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec/test" "github.com/stretchr/testify/assert" ) @@ -74,7 +74,7 @@ func TestGetVolumeGroup(t *testing.T) { {"Valid volume group name", "vg1", 3, false}, {"Valid volume group name", "vg2", 2, false}, } - executor := &mockExec.MockExecutor{ + executor := &test.MockExecutor{ MockExecuteCommandWithOutputAsHost: func(command string, args ...string) (string, error) { if args[0] == "vgs" { return mockVgsOutput, nil @@ -112,7 +112,7 @@ func TestListVolumeGroup(t *testing.T) { }{ {"List all volume groups", false}, } - executor := &mockExec.MockExecutor{ + executor := &test.MockExecutor{ MockExecuteCommandWithOutputAsHost: func(command string, args ...string) (string, error) { if args[0] == "vgs" { return mockVgsOutput, nil @@ -159,7 +159,7 @@ func TestCreateVolumeGroup(t *testing.T) { {"Volume Group created successfully", VolumeGroup{Name: "vg1", PVs: []PhysicalVolume{{PvName: "/dev/sdb"}}}, false}, } - executor := &mockExec.MockExecutor{ + executor := &test.MockExecutor{ MockExecuteCommandWithOutputAsHost: func(command string, args ...string) (string, error) { return "", nil }, @@ -188,7 +188,7 @@ func TestExtendVolumeGroup(t *testing.T) { {"New PVs are available", VolumeGroup{Name: "vg1"}, []string{"/dev/sdb", "/dev/sdc"}, false}, } - executor := &mockExec.MockExecutor{ + executor := &test.MockExecutor{ MockExecuteCommandWithOutputAsHost: func(command string, args ...string) (string, error) { return "", nil }, diff --git a/pkg/lvm/mocks/mock_lvm.go b/internal/controllers/vgmanager/lvm/mocks/mock_lvm.go similarity index 99% rename from pkg/lvm/mocks/mock_lvm.go rename to internal/controllers/vgmanager/lvm/mocks/mock_lvm.go index 628fea83d..40d503574 100644 --- a/pkg/lvm/mocks/mock_lvm.go +++ b/internal/controllers/vgmanager/lvm/mocks/mock_lvm.go @@ -3,7 +3,7 @@ package lvm import ( - lvm "github.com/openshift/lvm-operator/pkg/lvm" + lvm "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm" mock "github.com/stretchr/testify/mock" ) diff --git a/pkg/lvmd/lvmd.go b/internal/controllers/vgmanager/lvmd/lvmd.go similarity index 100% rename from pkg/lvmd/lvmd.go rename to internal/controllers/vgmanager/lvmd/lvmd.go diff --git a/pkg/vgmanager/status.go b/internal/controllers/vgmanager/status.go similarity index 98% rename from pkg/vgmanager/status.go rename to internal/controllers/vgmanager/status.go index dbfdeeb3d..c1ddc2239 100644 --- a/pkg/vgmanager/status.go +++ b/internal/controllers/vgmanager/status.go @@ -22,7 +22,7 @@ import ( "sort" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/filter" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/filter" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/log" diff --git a/pkg/vgmanager/vgmanager_controller.go b/internal/controllers/vgmanager/vgmanager_controller.go similarity index 97% rename from pkg/vgmanager/vgmanager_controller.go rename to internal/controllers/vgmanager/vgmanager_controller.go index 895cf280f..5b18f54dc 100644 --- a/pkg/vgmanager/vgmanager_controller.go +++ b/internal/controllers/vgmanager/vgmanager_controller.go @@ -24,14 +24,13 @@ import ( "github.com/google/go-cmp/cmp" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/controllers" - "github.com/openshift/lvm-operator/pkg/dmsetup" - "github.com/openshift/lvm-operator/pkg/filter" - "github.com/openshift/lvm-operator/pkg/lsblk" - "github.com/openshift/lvm-operator/pkg/lvm" - "github.com/openshift/lvm-operator/pkg/lvmd" - "github.com/openshift/lvm-operator/pkg/wipefs" - + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/dmsetup" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/filter" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvmd" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/wipefs" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -166,7 +165,7 @@ func (r *VGReconciler) reconcile( if lvmdConfig == nil { lvmdConfigWasMissing = true lvmdConfig = &lvmd.Config{ - SocketName: controllers.DefaultLVMdSocket, + SocketName: constants.DefaultLVMdSocket, } } existingLvmdConfig := *lvmdConfig diff --git a/pkg/vgmanager/vgmanager_controller_test.go b/internal/controllers/vgmanager/vgmanager_controller_test.go similarity index 95% rename from pkg/vgmanager/vgmanager_controller_test.go rename to internal/controllers/vgmanager/vgmanager_controller_test.go index f454697b4..41f3f0a98 100644 --- a/pkg/vgmanager/vgmanager_controller_test.go +++ b/internal/controllers/vgmanager/vgmanager_controller_test.go @@ -15,13 +15,13 @@ import ( configv1 "github.com/openshift/api/config/v1" secv1 "github.com/openshift/api/security/v1" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/controllers" - "github.com/openshift/lvm-operator/pkg/filter" - "github.com/openshift/lvm-operator/pkg/lsblk" - lsblkmocks "github.com/openshift/lvm-operator/pkg/lsblk/mocks" - "github.com/openshift/lvm-operator/pkg/lvm" - lvmmocks "github.com/openshift/lvm-operator/pkg/lvm/mocks" - "github.com/openshift/lvm-operator/pkg/lvmd" + "github.com/openshift/lvm-operator/internal/controllers/constants" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/filter" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" + lsblkmocks "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk/mocks" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm" + lvmmocks "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm/mocks" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvmd" topolvmv1 "github.com/topolvm/topolvm/api/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme" @@ -269,7 +269,7 @@ func testMockedBlockDeviceOnHost(ctx context.Context) { lvmdConfig, err := instances.LVMD.Load() Expect(err).ToNot(HaveOccurred()) Expect(lvmdConfig).ToNot(BeNil()) - Expect(lvmdConfig.SocketName).To(Equal(controllers.DefaultLVMdSocket)) + Expect(lvmdConfig.SocketName).To(Equal(constants.DefaultLVMdSocket)) Expect(lvmdConfig.DeviceClasses).ToNot(BeNil()) Expect(lvmdConfig.DeviceClasses).To(HaveLen(1)) Expect(lvmdConfig.DeviceClasses).To(ContainElement(&lvmd.DeviceClass{ diff --git a/pkg/vgmanager/vgmanager_validatelvs_test.go b/internal/controllers/vgmanager/vgmanager_validatelvs_test.go similarity index 96% rename from pkg/vgmanager/vgmanager_validatelvs_test.go rename to internal/controllers/vgmanager/vgmanager_validatelvs_test.go index d5f7b548d..0c8bca9d9 100644 --- a/pkg/vgmanager/vgmanager_validatelvs_test.go +++ b/internal/controllers/vgmanager/vgmanager_validatelvs_test.go @@ -7,9 +7,9 @@ import ( "github.com/go-logr/logr/testr" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - lvmexec "github.com/openshift/lvm-operator/pkg/internal/exec" - mockExec "github.com/openshift/lvm-operator/pkg/internal/exec/test" - "github.com/openshift/lvm-operator/pkg/lvm" + lvmexec "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec" + mockExec "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec/test" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvm" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/strings/slices" diff --git a/pkg/vgmanager/wipe_devices.go b/internal/controllers/vgmanager/wipe_devices.go similarity index 95% rename from pkg/vgmanager/wipe_devices.go rename to internal/controllers/vgmanager/wipe_devices.go index f8a811eb3..32cb10db9 100644 --- a/pkg/vgmanager/wipe_devices.go +++ b/internal/controllers/vgmanager/wipe_devices.go @@ -6,8 +6,8 @@ import ( "fmt" lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1" - "github.com/openshift/lvm-operator/pkg/dmsetup" - "github.com/openshift/lvm-operator/pkg/lsblk" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/dmsetup" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" "sigs.k8s.io/controller-runtime/pkg/log" ) diff --git a/pkg/vgmanager/wipe_devices_test.go b/internal/controllers/vgmanager/wipe_devices_test.go similarity index 96% rename from pkg/vgmanager/wipe_devices_test.go rename to internal/controllers/vgmanager/wipe_devices_test.go index 277857322..53f66a1ac 100644 --- a/pkg/vgmanager/wipe_devices_test.go +++ b/internal/controllers/vgmanager/wipe_devices_test.go @@ -5,9 +5,9 @@ import ( "testing" "github.com/openshift/lvm-operator/api/v1alpha1" - dmsetupmocks "github.com/openshift/lvm-operator/pkg/dmsetup/mocks" - "github.com/openshift/lvm-operator/pkg/lsblk" - wipefsmocks "github.com/openshift/lvm-operator/pkg/wipefs/mocks" + dmsetupmocks "github.com/openshift/lvm-operator/internal/controllers/vgmanager/dmsetup/mocks" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lsblk" + wipefsmocks "github.com/openshift/lvm-operator/internal/controllers/vgmanager/wipefs/mocks" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" diff --git a/pkg/wipefs/mocks/mock_wipefs.go b/internal/controllers/vgmanager/wipefs/mocks/mock_wipefs.go similarity index 100% rename from pkg/wipefs/mocks/mock_wipefs.go rename to internal/controllers/vgmanager/wipefs/mocks/mock_wipefs.go diff --git a/pkg/wipefs/wipefs.go b/internal/controllers/vgmanager/wipefs/wipefs.go similarity index 92% rename from pkg/wipefs/wipefs.go rename to internal/controllers/vgmanager/wipefs/wipefs.go index 4e005a942..5c3168389 100644 --- a/pkg/wipefs/wipefs.go +++ b/internal/controllers/vgmanager/wipefs/wipefs.go @@ -3,7 +3,7 @@ package wipefs import ( "fmt" - "github.com/openshift/lvm-operator/pkg/internal/exec" + "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec" ) var ( diff --git a/pkg/wipefs/wipefs_test.go b/internal/controllers/vgmanager/wipefs/wipefs_test.go similarity index 92% rename from pkg/wipefs/wipefs_test.go rename to internal/controllers/vgmanager/wipefs/wipefs_test.go index 1e663bdcd..8bb9bf642 100644 --- a/pkg/wipefs/wipefs_test.go +++ b/internal/controllers/vgmanager/wipefs/wipefs_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - mockExec "github.com/openshift/lvm-operator/pkg/internal/exec/test" + mockExec "github.com/openshift/lvm-operator/internal/controllers/vgmanager/exec/test" "github.com/stretchr/testify/assert" )