Skip to content

Commit 3776f3e

Browse files
fix: revert back to file-based lvmd config to support different configs for each node
Signed-off-by: Suleyman Akbas <[email protected]>
1 parent 382b4ae commit 3776f3e

File tree

8 files changed

+49
-174
lines changed

8 files changed

+49
-174
lines changed

bundle/manifests/lvms-operator.clusterserviceversion.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -679,18 +679,6 @@ spec:
679679
- create
680680
- patch
681681
- update
682-
- apiGroups:
683-
- ""
684-
resources:
685-
- configmaps
686-
verbs:
687-
- create
688-
- delete
689-
- get
690-
- list
691-
- patch
692-
- update
693-
- watch
694682
serviceAccountName: vg-manager
695683
strategy: deployment
696684
installModes:

cmd/vgmanager/vgmanager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error {
144144
}
145145

146146
lvmdConfig := &lvmd.Config{}
147-
if err := loadConfFile(ctx, lvmdConfig, constants.LVMDDefaultFileConfigPath); err != nil {
147+
if err := loadConfFile(ctx, lvmdConfig, lvmd.DefaultFileConfigPath); err != nil {
148148
opts.SetupLog.Error(err, "lvmd config could not be loaded, starting without topolvm components and attempting bootstrap")
149149
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
150150
return fmt.Errorf("unable to set up ready check: %w", err)
@@ -199,7 +199,7 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error {
199199
if err = (&vgmanager.Reconciler{
200200
Client: mgr.GetClient(),
201201
EventRecorder: mgr.GetEventRecorderFor(vgmanager.ControllerName),
202-
LVMD: lvmd.NewFileConfigurator(mgr.GetClient(), operatorNamespace),
202+
LVMD: lvmd.DefaultConfigurator(),
203203
Scheme: mgr.GetScheme(),
204204
LSBLK: lsblk.NewDefaultHostLSBLK(),
205205
Wipefs: wipefs.NewDefaultHostWipefs(),
@@ -238,7 +238,7 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error {
238238
fileNotExist := false
239239
for {
240240
// check if file exists, otherwise the watcher errors
241-
_, err := os.Lstat(constants.LVMDDefaultFileConfigPath)
241+
_, err := os.Lstat(lvmd.DefaultFileConfigPath)
242242
if err != nil {
243243
if os.IsNotExist(err) {
244244
time.Sleep(100 * time.Millisecond)
@@ -251,7 +251,7 @@ func run(cmd *cobra.Command, _ []string, opts *Options) error {
251251
if fileNotExist {
252252
cancel()
253253
}
254-
err = watcher.Add(constants.LVMDDefaultFileConfigPath)
254+
err = watcher.Add(lvmd.DefaultFileConfigPath)
255255
if err != nil {
256256
opts.SetupLog.Error(err, "unable to add file path to watcher")
257257
}

config/rbac/vg_manager_role.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,3 @@ rules:
6666
- create
6767
- patch
6868
- update
69-
- apiGroups:
70-
- ""
71-
resources:
72-
- configmaps
73-
verbs:
74-
- create
75-
- delete
76-
- get
77-
- list
78-
- patch
79-
- update
80-
- watch

internal/controllers/constants/constants.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ const (
3232
DeviceClassKey = "topolvm.io/device-class"
3333
DefaultPluginRegistrationPath = "/registration"
3434

35-
LVMDConfigMapName = "lvmd-config"
36-
LVMDDefaultConfigDir = "/etc/topolvm"
37-
LVMDDefaultFileConfigPath = "/etc/topolvm/lvmd.yaml"
38-
3935
// name of the lvm-operator container
4036
LVMOperatorContainerName = "manager"
4137

internal/controllers/lvmcluster/resource/vgmanager_daemonset.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
lvmv1alpha1 "github.com/openshift/lvm-operator/api/v1alpha1"
2525
"github.com/openshift/lvm-operator/internal/controllers/constants"
2626
"github.com/openshift/lvm-operator/internal/controllers/lvmcluster/selector"
27+
"github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvmd"
2728
appsv1 "k8s.io/api/apps/v1"
2829
corev1 "k8s.io/api/core/v1"
2930
"k8s.io/apimachinery/pkg/api/resource"
@@ -109,16 +110,15 @@ var (
109110
LVMDConfMapVol = corev1.Volume{
110111
Name: LVMDConfMapVolName,
111112
VolumeSource: corev1.VolumeSource{
112-
ConfigMap: &corev1.ConfigMapVolumeSource{
113-
Optional: ptr.To(true),
114-
LocalObjectReference: corev1.LocalObjectReference{
115-
Name: constants.LVMDConfigMapName,
116-
},
117-
},
113+
HostPath: &corev1.HostPathVolumeSource{
114+
Path: filepath.Dir(lvmd.DefaultFileConfigPath),
115+
Type: &HostPathDirectoryOrCreate},
118116
},
119117
}
120118
LVMDConfMapVolMount = corev1.VolumeMount{
121-
Name: LVMDConfMapVolName, MountPath: constants.LVMDDefaultConfigDir,
119+
Name: LVMDConfMapVolName,
120+
MountPath: filepath.Dir(lvmd.DefaultFileConfigPath),
121+
MountPropagation: &hostContainerPropagation,
122122
}
123123
)
124124

internal/controllers/vgmanager/controller.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ import (
4343
"sigs.k8s.io/controller-runtime/pkg/builder"
4444
"sigs.k8s.io/controller-runtime/pkg/client"
4545
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
46-
"sigs.k8s.io/controller-runtime/pkg/handler"
4746
"sigs.k8s.io/controller-runtime/pkg/log"
4847
"sigs.k8s.io/controller-runtime/pkg/predicate"
49-
"sigs.k8s.io/controller-runtime/pkg/reconcile"
5048
)
5149

5250
const (
@@ -82,38 +80,9 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
8280
return ctrl.NewControllerManagedBy(mgr).
8381
For(&lvmv1alpha1.LVMVolumeGroup{}).
8482
Owns(&lvmv1alpha1.LVMVolumeGroupNodeStatus{}, builder.MatchEveryOwner, builder.WithPredicates(predicate.GenerationChangedPredicate{})).
85-
Watches(&corev1.ConfigMap{}, handler.EnqueueRequestsFromMapFunc(r.getObjsInNamespaceForReconcile)).
8683
Complete(r)
8784
}
8885

89-
// getObjsInNamespaceForReconcile reconciles the object anytime the given object is in the same namespace
90-
// as the available LVMVolumeGroups.
91-
func (r *Reconciler) getObjsInNamespaceForReconcile(ctx context.Context, obj client.Object) []reconcile.Request {
92-
foundLVMVolumeGroupList := &lvmv1alpha1.LVMVolumeGroupList{}
93-
listOps := &client.ListOptions{
94-
Namespace: obj.GetNamespace(),
95-
}
96-
97-
if err := r.List(ctx, foundLVMVolumeGroupList, listOps); err != nil {
98-
log.FromContext(ctx).Error(err, "getObjsInNamespaceForReconcile: Failed to get LVMVolumeGroup objs")
99-
return []reconcile.Request{}
100-
}
101-
if len(foundLVMVolumeGroupList.Items) < 1 {
102-
return []reconcile.Request{}
103-
}
104-
105-
var requests []reconcile.Request
106-
for _, lvmVG := range foundLVMVolumeGroupList.Items {
107-
requests = append(requests, reconcile.Request{
108-
NamespacedName: types.NamespacedName{
109-
Name: lvmVG.GetName(),
110-
Namespace: lvmVG.GetNamespace(),
111-
},
112-
})
113-
}
114-
return requests
115-
}
116-
11786
type Reconciler struct {
11887
client.Client
11988
Scheme *runtime.Scheme

internal/controllers/vgmanager/controller_test.go

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ import (
2424
"github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvmd"
2525
lvmdmocks "github.com/openshift/lvm-operator/internal/controllers/vgmanager/lvmd/mocks"
2626
wipefsmocks "github.com/openshift/lvm-operator/internal/controllers/vgmanager/wipefs/mocks"
27-
"github.com/stretchr/testify/assert"
2827
"github.com/stretchr/testify/mock"
2928
topolvmv1 "github.com/topolvm/topolvm/api/v1"
3029
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3130

3231
corev1 "k8s.io/api/core/v1"
3332
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/scheme"
3433
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35-
"k8s.io/apimachinery/pkg/runtime"
36-
"k8s.io/apimachinery/pkg/types"
3734
"k8s.io/client-go/tools/record"
3835
"k8s.io/utils/ptr"
3936
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -112,6 +109,7 @@ func setupInstances() testInstances {
112109
mockLSBLK := lsblkmocks.NewMockLSBLK(t)
113110
mockLVM := lvmmocks.NewMockLVM(t)
114111
mockWipefs := wipefsmocks.NewMockWipefs(t)
112+
testLVMD := lvmd.NewFileConfigurator(filepath.Join(t.TempDir(), "lvmd.yaml"))
115113

116114
hostname := "test-host.vgmanager.test.io"
117115
hostnameLabelKey := "kubernetes.io/hostname"
@@ -127,8 +125,6 @@ func setupInstances() testInstances {
127125
fakeRecorder := record.NewFakeRecorder(100)
128126
fakeRecorder.IncludeObject = true
129127

130-
testLVMD := lvmd.NewFileConfigurator(fakeClient, namespace.GetName())
131-
132128
return testInstances{
133129
LVM: mockLVM,
134130
LSBLK: mockLSBLK,
@@ -836,52 +832,3 @@ func testReconcileFailure(ctx context.Context) {
836832
Expect(err).To(MatchError(expectedError))
837833
})
838834
}
839-
840-
func TestGetObjsInNamespaceForReconcile(t *testing.T) {
841-
tests := []struct {
842-
name string
843-
objs []client.Object
844-
list error
845-
expect []reconcile.Request
846-
}{
847-
{
848-
name: "test lvmvolumegroup not fetch error",
849-
list: assert.AnError,
850-
},
851-
{
852-
name: "test lvmvolumegroup found in a different namespace",
853-
objs: []client.Object{
854-
&lvmv1alpha1.LVMVolumeGroup{ObjectMeta: metav1.ObjectMeta{Name: "test-vg", Namespace: "not-test"}},
855-
},
856-
},
857-
{
858-
name: "test lvmvolumegroup found in the same namespace",
859-
objs: []client.Object{
860-
&lvmv1alpha1.LVMVolumeGroup{ObjectMeta: metav1.ObjectMeta{Name: "test-vg", Namespace: "test"}},
861-
},
862-
expect: []reconcile.Request{{NamespacedName: types.NamespacedName{Name: "test-vg", Namespace: "test"}}},
863-
},
864-
}
865-
866-
for _, tt := range tests {
867-
t.Run(tt.name, func(t *testing.T) {
868-
newScheme := runtime.NewScheme()
869-
assert.NoError(t, lvmv1alpha1.AddToScheme(newScheme))
870-
assert.NoError(t, corev1.AddToScheme(newScheme))
871-
clnt := fake.NewClientBuilder().WithObjects(tt.objs...).
872-
WithScheme(newScheme).WithInterceptorFuncs(interceptor.Funcs{
873-
List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
874-
if tt.list != nil {
875-
return tt.list
876-
}
877-
return client.List(ctx, list, opts...)
878-
},
879-
}).Build()
880-
881-
r := &Reconciler{Client: clnt}
882-
requests := r.getObjsInNamespaceForReconcile(context.Background(),
883-
&corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "test-vg", Namespace: "test"}})
884-
assert.ElementsMatch(t, tt.expect, requests)
885-
})
886-
}
887-
}

internal/controllers/vgmanager/lvmd/lvmd.go

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ package lvmd
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"os"
68

7-
"github.com/openshift/lvm-operator/internal/controllers/constants"
89
"github.com/topolvm/topolvm/lvmd"
910
lvmdCMD "github.com/topolvm/topolvm/pkg/lvmd/cmd"
1011

11-
corev1 "k8s.io/api/core/v1"
12-
k8serrors "k8s.io/apimachinery/pkg/api/errors"
13-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14-
ctrl "sigs.k8s.io/controller-runtime"
15-
"sigs.k8s.io/controller-runtime/pkg/client"
1612
"sigs.k8s.io/controller-runtime/pkg/log"
1713
"sigs.k8s.io/yaml"
1814
)
@@ -24,9 +20,18 @@ type ThinPoolConfig = lvmd.ThinPoolConfig
2420

2521
var TypeThin = lvmd.TypeThin
2622

27-
func NewFileConfigurator(client client.Client, namespace string) *CachedFileConfig {
23+
const (
24+
DefaultFileConfigPath = "/etc/topolvm/lvmd.yaml"
25+
maxReadLength = 2 * 1 << 20 // 2MB
26+
)
27+
28+
func DefaultConfigurator() *CachedFileConfig {
29+
return NewFileConfigurator(DefaultFileConfigPath)
30+
}
31+
32+
func NewFileConfigurator(path string) *CachedFileConfig {
2833
return &CachedFileConfig{
29-
FileConfig: FileConfig{Client: client, Namespace: namespace},
34+
FileConfig: FileConfig{path: path},
3035
}
3136
}
3237

@@ -61,69 +66,51 @@ func (c *CachedFileConfig) Save(ctx context.Context, config *Config) error {
6166
}
6267

6368
type FileConfig struct {
64-
client.Client
65-
Namespace string
69+
path string
6670
}
6771

6872
func (c FileConfig) Load(ctx context.Context) (*Config, error) {
69-
cm := &corev1.ConfigMap{
70-
ObjectMeta: metav1.ObjectMeta{
71-
Name: constants.LVMDConfigMapName,
72-
Namespace: c.Namespace,
73-
},
74-
}
75-
err := c.Client.Get(ctx, client.ObjectKeyFromObject(cm), cm)
76-
if k8serrors.IsNotFound(err) {
73+
file, err := os.Open(c.path)
74+
if os.IsNotExist(err) {
7775
// If the file does not exist, return nil for both
7876
return nil, nil
7977
} else if err != nil {
80-
return nil, fmt.Errorf("failed to get ConfigMap %s: %w", cm.Name, err)
78+
return nil, fmt.Errorf("failed to open config file %s: %w", c.path, err)
79+
}
80+
defer file.Close()
81+
82+
limitedReader := &io.LimitedReader{R: file, N: maxReadLength}
83+
cfgBytes, err := io.ReadAll(limitedReader)
84+
if err != nil {
85+
return nil, fmt.Errorf("failed to read config file %s: %w", c.path, err)
86+
}
87+
88+
if limitedReader.N <= 0 {
89+
return nil, fmt.Errorf("the read limit is reached for config file %s", c.path)
8190
}
8291

8392
config := &Config{}
84-
if err = yaml.Unmarshal([]byte(cm.Data["lvmd.yaml"]), config); err != nil {
85-
return nil, fmt.Errorf("failed to unmarshal config file: %w", err)
93+
if err = yaml.Unmarshal(cfgBytes, config); err != nil {
94+
return nil, fmt.Errorf("failed to unmarshal config file %s: %w", c.path, err)
8695
}
8796
return config, nil
8897
}
8998

9099
func (c FileConfig) Save(ctx context.Context, config *Config) error {
91100
out, err := yaml.Marshal(config)
92-
if err != nil {
93-
return fmt.Errorf("failed to marshal config file: %w", err)
101+
if err == nil {
102+
err = os.WriteFile(c.path, out, 0600)
94103
}
95-
96-
cm := &corev1.ConfigMap{
97-
ObjectMeta: metav1.ObjectMeta{
98-
Name: constants.LVMDConfigMapName,
99-
Namespace: c.Namespace,
100-
},
101-
}
102-
_, err = ctrl.CreateOrUpdate(ctx, c.Client, cm, func() error {
103-
cm.Data = map[string]string{"lvmd.yaml": string(out)}
104-
return nil
105-
})
106104
if err != nil {
107-
return fmt.Errorf("failed to apply ConfigMap %s: %w", cm.GetName(), err)
105+
return fmt.Errorf("failed to save config file %s: %w", c.path, err)
108106
}
109-
110107
return nil
111108
}
112109

113110
func (c FileConfig) Delete(ctx context.Context) error {
114-
cm := &corev1.ConfigMap{
115-
ObjectMeta: metav1.ObjectMeta{
116-
Name: constants.LVMDConfigMapName,
117-
Namespace: c.Namespace,
118-
},
119-
}
120-
if err := c.Client.Delete(ctx, cm); err != nil {
121-
if k8serrors.IsNotFound(err) {
122-
// If the file does not exist, return nil
123-
return nil
124-
}
125-
return fmt.Errorf("failed to delete ConfigMap: %w", err)
111+
err := os.Remove(c.path)
112+
if err != nil {
113+
return fmt.Errorf("failed to delete config file %s: %w", c.path, err)
126114
}
127-
128-
return nil
115+
return err
129116
}

0 commit comments

Comments
 (0)