Skip to content

Commit

Permalink
Merge pull request #556 from suleymanakbas91/release-4.15
Browse files Browse the repository at this point in the history
  • Loading branch information
suleymanakbas91 authored Jan 25, 2024
2 parents a312ffe + 3614a74 commit 2497a69
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 246 deletions.
20 changes: 8 additions & 12 deletions bundle/manifests/lvms-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,14 @@ spec:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- pods
verbs:
- delete
- list
- watch
- apiGroups:
- lvm.topolvm.io
resources:
Expand Down Expand Up @@ -872,18 +880,6 @@ spec:
- create
- patch
- update
- apiGroups:
- ""
resources:
- configmaps
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
serviceAccountName: vg-manager
strategy: deployment
installModes:
Expand Down
10 changes: 9 additions & 1 deletion cmd/vgmanager/vgmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

corev1 "k8s.io/api/core/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -122,10 +123,17 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error {
return fmt.Errorf("unable to start manager: %w", err)
}

if err := mgr.GetFieldIndexer().IndexField(cmd.Context(), &corev1.Pod{}, "spec.nodeName", func(rawObj client.Object) []string {
pod := rawObj.(*corev1.Pod)
return []string{pod.Spec.NodeName}
}); err != nil {
return err
}

if err = (&vgmanager.Reconciler{
Client: mgr.GetClient(),
EventRecorder: mgr.GetEventRecorderFor(vgmanager.ControllerName),
LVMD: lvmd.NewFileConfigurator(mgr.GetClient(), operatorNamespace),
LVMD: lvmd.DefaultConfigurator(),
Scheme: mgr.GetScheme(),
LSBLK: lsblk.NewDefaultHostLSBLK(),
Wipefs: wipefs.NewDefaultHostWipefs(),
Expand Down
20 changes: 8 additions & 12 deletions config/rbac/vg_manager_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- ""
resources:
- pods
verbs:
- delete
- list
- watch
- apiGroups:
- lvm.topolvm.io
resources:
Expand Down Expand Up @@ -66,15 +74,3 @@ rules:
- create
- patch
- update
- apiGroups:
- ""
resources:
- configmaps
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
4 changes: 0 additions & 4 deletions internal/controllers/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ const (
DefaultCSISocket = "/run/topolvm/csi-topolvm.sock"
DeviceClassKey = "topolvm.io/device-class"

LVMDConfigMapName = "lvmd-config"
LVMDDefaultConfigDir = "/etc/topolvm"
LVMDDefaultFileConfigPath = "/etc/topolvm/lvmd.yaml"

// name of the lvm-operator container
LVMOperatorContainerName = "manager"

Expand Down
14 changes: 6 additions & 8 deletions internal/controllers/lvmcluster/resource/topolvm_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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"
"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"
Expand Down Expand Up @@ -156,12 +157,9 @@ func getNodeDaemonSet(lvmCluster *lvmv1alpha1.LVMCluster, namespace string, args
Type: &hostPathDirectoryOrCreateType}}},
{Name: "lvmd-config-dir",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: constants.LVMDConfigMapName,
},
},
}},
HostPath: &corev1.HostPathVolumeSource{
Path: filepath.Dir(lvmd.DefaultFileConfigPath),
Type: &hostPathDirectory}}},
{Name: "metrics-cert",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
Expand Down Expand Up @@ -233,7 +231,7 @@ func getNodeContainer(args []string) *corev1.Container {
command := []string{
"/topolvm-node",
"--embed-lvmd",
fmt.Sprintf("--config=%s", constants.LVMDDefaultFileConfigPath),
fmt.Sprintf("--config=%s", lvmd.DefaultFileConfigPath),
}

command = append(command, args...)
Expand All @@ -249,7 +247,7 @@ func getNodeContainer(args []string) *corev1.Container {

volumeMounts := []corev1.VolumeMount{
{Name: "node-plugin-dir", MountPath: filepath.Dir(constants.DefaultCSISocket)},
{Name: "lvmd-config-dir", MountPath: constants.LVMDDefaultConfigDir},
{Name: "lvmd-config-dir", MountPath: filepath.Dir(lvmd.DefaultFileConfigPath)},
{Name: "pod-volumes-dir",
MountPath: fmt.Sprintf("%spods", getAbsoluteKubeletPath(constants.CSIKubeletRootDir)),
MountPropagation: &mountPropagationMode},
Expand Down
66 changes: 29 additions & 37 deletions internal/controllers/vgmanager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/google/go-cmp/cmp"
lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1"
"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"
Expand All @@ -35,6 +36,8 @@ import (
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -44,10 +47,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const (
Expand Down Expand Up @@ -83,38 +84,9 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&lvmv1alpha1.LVMVolumeGroup{}).
Owns(&lvmv1alpha1.LVMVolumeGroupNodeStatus{}, builder.MatchEveryOwner, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(r.getObjsInNamespaceForReconcile)).
Complete(r)
}

// getObjsInNamespaceForReconcile reconciles the object anytime the given object is in the same namespace
// as the available LVMVolumeGroups.
func (r *Reconciler) getObjsInNamespaceForReconcile(ctx context.Context, obj client.Object) []reconcile.Request {
foundLVMVolumeGroupList := &lvmv1alpha1.LVMVolumeGroupList{}
listOps := &client.ListOptions{
Namespace: obj.GetNamespace(),
}

if err := r.List(ctx, foundLVMVolumeGroupList, listOps); err != nil {
log.FromContext(ctx).Error(err, "getObjsInNamespaceForReconcile: Failed to get LVMVolumeGroup objs")
return []reconcile.Request{}
}
if len(foundLVMVolumeGroupList.Items) < 1 {
return []reconcile.Request{}
}

var requests []reconcile.Request
for _, lvmVG := range foundLVMVolumeGroupList.Items {
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: lvmVG.GetName(),
Namespace: lvmVG.GetNamespace(),
},
})
}
return requests
}

type Reconciler struct {
client.Client
Scheme *runtime.Scheme
Expand Down Expand Up @@ -326,7 +298,7 @@ func (r *Reconciler) applyLVMDConfig(ctx context.Context, volumeGroup *lvmv1alph
logger := log.FromContext(ctx).WithValues("VGName", volumeGroup.Name)

// Read the lvmd config file
lvmdConfig, err := r.LVMD.Load(ctx)
lvmdConfig, err := r.LVMD.Load()
if err != nil {
err = fmt.Errorf("failed to read the lvmd config file: %w", err)
if _, err := r.setVolumeGroupFailedStatus(ctx, volumeGroup, nil, err); err != nil {
Expand Down Expand Up @@ -392,12 +364,32 @@ func (r *Reconciler) updateLVMDConfigAfterReconcile(
r.NormalEvent(ctx, volumeGroup, EventReasonLVMDConfigMissing, msg)
}

if err := r.LVMD.Save(ctx, newCFG); err != nil {
if err := r.LVMD.Save(newCFG); err != nil {
return fmt.Errorf("failed to update lvmd config file to update volume group %s: %w", volumeGroup.GetName(), err)
}
msg := "updated lvmd config with new deviceClasses"
msg := "updated lvmd config with new deviceClasses, restarting TopoLVM Node pods"
logger.Info(msg)
r.NormalEvent(ctx, volumeGroup, EventReasonLVMDConfigUpdated, msg)

nodePodList := &corev1.PodList{}
listOptions := &client.ListOptions{
Namespace: r.Namespace,
LabelSelector: labels.SelectorFromSet(map[string]string{constants.AppKubernetesComponentLabel: constants.TopolvmNodeLabelVal}),
FieldSelector: fields.OneTermEqualSelector("spec.nodeName", r.NodeName),
}
err := r.List(ctx, nodePodList, listOptions)
if err != nil {
return fmt.Errorf("failed to list TopoLVM Node pods: %w", err)
}
if len(nodePodList.Items) < 1 {
return fmt.Errorf("could not find any TopoLVM Node pods on %s namespace and on %s node", r.Namespace, r.NodeName)
}
for _, pod := range nodePodList.Items {
err := r.Delete(ctx, &pod)
if err != nil {
return fmt.Errorf("failed to remove TopoLVM Node pod %s: %w", pod.Name, err)
}
}
}
return nil
}
Expand All @@ -407,7 +399,7 @@ func (r *Reconciler) processDelete(ctx context.Context, volumeGroup *lvmv1alpha1
logger.Info("deleting")

// Read the lvmd config file
lvmdConfig, err := r.LVMD.Load(ctx)
lvmdConfig, err := r.LVMD.Load()
if err != nil {
// Failed to read lvmdconfig file. Reconcile again
return fmt.Errorf("failed to read the lvmd config file: %w", err)
Expand Down Expand Up @@ -475,14 +467,14 @@ func (r *Reconciler) processDelete(ctx context.Context, volumeGroup *lvmv1alpha1
// if there was no config file in the first place, nothing has to be removed.
if lvmdConfig != nil {
if len(lvmdConfig.DeviceClasses) > 0 {
if err = r.LVMD.Save(ctx, lvmdConfig); err != nil {
if err = r.LVMD.Save(lvmdConfig); err != nil {
return fmt.Errorf("failed to update lvmd.conf file for volume group %s: %w", volumeGroup.GetName(), err)
}
msg := "updated lvmd config after deviceClass was removed"
logger.Info(msg)
r.NormalEvent(ctx, volumeGroup, EventReasonLVMDConfigUpdated, msg)
} else {
if err = r.LVMD.Delete(ctx); err != nil {
if err = r.LVMD.Delete(); err != nil {
return fmt.Errorf("failed to delete lvmd.conf file for volume group %s: %w", volumeGroup.GetName(), err)
}
msg := "removed lvmd config after last deviceClass was removed"
Expand Down
Loading

0 comments on commit 2497a69

Please sign in to comment.