Skip to content

Commit

Permalink
fix: delete TopoLVM Node pods to reload config
Browse files Browse the repository at this point in the history
Signed-off-by: Suleyman Akbas <[email protected]>
  • Loading branch information
suleymanakbas91 committed Jan 25, 2024
1 parent 35ae7a5 commit 60c1819
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 9 deletions.
8 changes: 8 additions & 0 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
21 changes: 17 additions & 4 deletions cmd/vgmanager/vgmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"

"github.com/go-logr/logr"
"github.com/openshift/lvm-operator/internal/cluster"
"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"
Expand All @@ -36,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 @@ -84,14 +86,18 @@ func NewCmd(opts *Options) *cobra.Command {
func run(cmd *cobra.Command, _ []string, opts *Options) error {
lvm := lvm.NewDefaultHostLVM()
nodeName := os.Getenv("NODE_NAME")
namespace := os.Getenv("POD_NAMESPACE")

operatorNamespace, err := cluster.GetOperatorNamespace()
if err != nil {
return fmt.Errorf("unable to get operatorNamespace: %w", err)
}

setupClient, err := client.New(ctrl.GetConfigOrDie(), client.Options{Scheme: opts.Scheme})
if err != nil {
return fmt.Errorf("unable to initialize setup client for pre-manager startup checks: %w", err)
}

if err := tagging.AddTagToVGs(cmd.Context(), setupClient, lvm, nodeName, namespace); err != nil {
if err := tagging.AddTagToVGs(cmd.Context(), setupClient, lvm, nodeName, operatorNamespace); err != nil {
opts.SetupLog.Error(err, "failed to tag existing volume groups")
}

Expand All @@ -109,14 +115,21 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error {
LeaderElection: false,
Cache: cache.Options{
DefaultNamespaces: map[string]cache.Config{
os.Getenv("POD_NAMESPACE"): {},
operatorNamespace: {},
},
},
})
if err != nil {
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),
Expand All @@ -127,7 +140,7 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error {
Dmsetup: dmsetup.NewDefaultHostDmsetup(),
LVM: lvm,
NodeName: nodeName,
Namespace: namespace,
Namespace: operatorNamespace,
Filters: filter.DefaultFilters,
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller VGManager: %w", err)
Expand Down
8 changes: 8 additions & 0 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
3 changes: 0 additions & 3 deletions internal/controllers/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ const (
LivenessProbeMemRequest = "15Mi"
LivenessProbeCPURequest = "1m"

FileCheckerMemRequest = "10Mi"
FileCheckerCPURequest = "1m"

// topoLVM Node
TopolvmNodeServiceAccount = "topolvm-node"
TopolvmNodeDaemonsetName = "topolvm-node"
Expand Down
26 changes: 25 additions & 1 deletion internal/controllers/vgmanager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ 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"
"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"
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 Down Expand Up @@ -363,9 +367,29 @@ func (r *Reconciler) updateLVMDConfigAfterReconcile(
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 Down
21 changes: 20 additions & 1 deletion internal/controllers/vgmanager/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/openshift/lvm-operator/internal/controllers/constants"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -116,9 +117,21 @@ func setupInstances() testInstances {
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: "test-node", Labels: map[string]string{
hostnameLabelKey: hostname,
}}}
topolvmNodePod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "topolvm-node", Namespace: namespace.GetName(), Labels: map[string]string{
constants.AppKubernetesComponentLabel: constants.TopolvmNodeLabelVal,
}},
Spec: corev1.PodSpec{
NodeName: node.GetName(),
},
}

fakeClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).
WithObjects(node, namespace).
WithObjects(node, namespace, topolvmNodePod).
WithIndex(&corev1.Pod{}, "spec.nodeName", func(rawObj client.Object) []string {
pod := rawObj.(*corev1.Pod)
return []string{pod.Spec.NodeName}
}).
Build()
fakeRecorder := record.NewFakeRecorder(100)
fakeRecorder.IncludeObject = true
Expand Down Expand Up @@ -292,6 +305,12 @@ func testMockedBlockDeviceOnHost(ctx context.Context) {
}}}, nil).Once()
instances.LVM.EXPECT().ListVGs().Return([]lvm.VolumeGroup{createdVG}, nil).Twice()
instances.LVM.EXPECT().ActivateLV(thinPool.Name, vg.GetName()).Return(nil).Once()

//topolvmNodePod := corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "topolvm-node"}}
//podList := &corev1.PodList{Items: []corev1.Pod{topolvmNodePod}}
//
//Expect(instances.client.List(ctx, podList)).To(Succeed())
//Expect(instances.client.Delete(ctx, &topolvmNodePod)).To(Succeed())
})

By("triggering the next reconciliation after the creation of the thin pool", func() {
Expand Down

0 comments on commit 60c1819

Please sign in to comment.