Skip to content

Commit

Permalink
Merge pull request #56 from nbalacha/ns
Browse files Browse the repository at this point in the history
fix: create all resources in the operator namespace
  • Loading branch information
nbalacha authored Dec 31, 2021
2 parents 90392e6 + ebfa89d commit e0c41e7
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 33 deletions.
4 changes: 2 additions & 2 deletions bundle/manifests/lvm-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ spec:
command:
- /manager
env:
- name: WATCH_NAMESPACE
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.annotations['olm.targetNamespaces']
fieldPath: metadata.namespace
envFrom:
- configMapRef:
name: lvm-operator-manager-config
Expand Down
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ spec:
cpu: 100m
memory: 20Mi
env:
- name: WATCH_NAMESPACE
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Expand Down
1 change: 1 addition & 0 deletions controllers/lvmcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type LVMClusterReconciler struct {
Log logr.Logger
ClusterType ClusterType
SecurityClient secv1client.SecurityV1Interface
Namespace string
}

//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmclusters,verbs=get;list;watch;create;update;patch;delete
Expand Down
4 changes: 2 additions & 2 deletions controllers/scc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c openshiftSccs) ensureCreated(r *LVMClusterReconciler, ctx context.Contex
r.Log.Info("not creating SCCs as this is not an Openshift cluster")
return nil
}
sccs := getAllSCCs(lvmCluster.Namespace)
sccs := getAllSCCs(r.Namespace)
for _, scc := range sccs {
_, err := r.SecurityClient.SecurityContextConstraints().Get(ctx, scc.Name, metav1.GetOptions{})
if err != nil && errors.IsNotFound(err) {
Expand All @@ -69,7 +69,7 @@ func (c openshiftSccs) ensureCreated(r *LVMClusterReconciler, ctx context.Contex
func (c openshiftSccs) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error {
if IsOpenshift(r) {
var err error
sccs := getAllSCCs(lvmCluster.Namespace)
sccs := getAllSCCs(r.Namespace)
for _, scc := range sccs {
err = r.SecurityClient.SecurityContextConstraints().Delete(ctx, scc.Name, metav1.DeleteOptions{})
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var _ = BeforeSuite(func() {
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
SecurityClient: secv1client.NewForConfigOrDie(k8sManager.GetConfig()),
Namespace: testLvmClusterNamespace,
Log: ctrl.Log.WithName("controllers").WithName("LvmCluster"),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())
Expand Down
10 changes: 5 additions & 5 deletions controllers/topolvm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c topolvmController) getName() string {
func (c topolvmController) ensureCreated(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error {

// get the desired state of topolvm controller deployment
desiredDeployment := getControllerDeployment(lvmCluster)
desiredDeployment := getControllerDeployment(lvmCluster, r.Namespace)
existingDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: desiredDeployment.Name,
Expand All @@ -57,7 +57,7 @@ func (c topolvmController) ensureCreated(r *LVMClusterReconciler, ctx context.Co

func (c topolvmController) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error {
existingDeployment := &appsv1.Deployment{}
err := r.Client.Get(ctx, types.NamespacedName{Name: TopolvmControllerDeploymentName, Namespace: lvmCluster.Namespace}, existingDeployment)
err := r.Client.Get(ctx, types.NamespacedName{Name: TopolvmControllerDeploymentName, Namespace: r.Namespace}, existingDeployment)

if err != nil {
// already deleted in previous reconcile
Expand Down Expand Up @@ -105,7 +105,7 @@ func (c topolvmController) setTopolvmControllerDesiredState(existing, desired *a
return nil
}

func getControllerDeployment(lvmCluster *lvmv1alpha1.LVMCluster) *appsv1.Deployment {
func getControllerDeployment(lvmCluster *lvmv1alpha1.LVMCluster, namespace string) *appsv1.Deployment {

// Topolvm CSI Controller Deployment
var replicas int32 = 1
Expand All @@ -129,7 +129,7 @@ func getControllerDeployment(lvmCluster *lvmv1alpha1.LVMCluster) *appsv1.Deploym
controllerDeployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: TopolvmControllerDeploymentName,
Namespace: lvmCluster.Namespace,
Namespace: namespace,
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Expand All @@ -141,7 +141,7 @@ func getControllerDeployment(lvmCluster *lvmv1alpha1.LVMCluster) *appsv1.Deploym
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: TopolvmControllerDeploymentName,
Namespace: lvmCluster.Namespace,
Namespace: namespace,
Labels: map[string]string{
AppAttr: TopolvmControllerDeploymentName,
},
Expand Down
8 changes: 4 additions & 4 deletions controllers/topolvm_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (n topolvmNode) ensureCreated(r *LVMClusterReconciler, ctx context.Context,
unitLogger := r.Log.WithValues("topolvmNode", n.getName())

// get desired daemonSet spec
dsTemplate := getNodeDaemonSet(lvmCluster)
dsTemplate := getNodeDaemonSet(lvmCluster, r.Namespace)
// create desired daemonSet or update mutable fields on existing one
ds := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -93,7 +93,7 @@ func (n topolvmNode) ensureCreated(r *LVMClusterReconciler, ctx context.Context,
func (n topolvmNode) ensureDeleted(r *LVMClusterReconciler, ctx context.Context, lvmCluster *lvmv1alpha1.LVMCluster) error {
NodeDaemonSet := &appsv1.DaemonSet{}
err := r.Client.Get(ctx,
types.NamespacedName{Name: TopolvmNodeDaemonsetName, Namespace: lvmCluster.Namespace},
types.NamespacedName{Name: TopolvmNodeDaemonsetName, Namespace: r.Namespace},
NodeDaemonSet)

if err != nil {
Expand Down Expand Up @@ -127,7 +127,7 @@ func (n topolvmNode) updateStatus(r *LVMClusterReconciler, ctx context.Context,
return nil
}

func getNodeDaemonSet(lvmCluster *lvmv1alpha1.LVMCluster) *appsv1.DaemonSet {
func getNodeDaemonSet(lvmCluster *lvmv1alpha1.LVMCluster, namespace string) *appsv1.DaemonSet {
hostPathDirectory := corev1.HostPathDirectory
hostPathDirectoryOrCreateType := corev1.HostPathDirectoryOrCreate
storageMedium := corev1.StorageMediumMemory
Expand Down Expand Up @@ -179,7 +179,7 @@ func getNodeDaemonSet(lvmCluster *lvmv1alpha1.LVMCluster) *appsv1.DaemonSet {
nodeDaemonSet := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: TopolvmNodeDaemonsetName,
Namespace: lvmCluster.Namespace,
Namespace: namespace,
Labels: labels,
},
Spec: appsv1.DaemonSetSpec{
Expand Down
2 changes: 1 addition & 1 deletion controllers/vgmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (v vgManager) ensureCreated(r *LVMClusterReconciler, ctx context.Context, l
unitLogger := r.Log.WithValues("resourceManager", v.getName())

// get desired daemonset spec
dsTemplate := newVGManagerDaemonset(*lvmCluster)
dsTemplate := newVGManagerDaemonset(*lvmCluster, r.Namespace)

// controller reference
err := ctrl.SetControllerReference(lvmCluster, &dsTemplate, r.Scheme)
Expand Down
4 changes: 2 additions & 2 deletions controllers/vgmanager_daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var (
)

// newVGManagerDaemonset returns the desired vgmanager daemonset for a given LVMCluster
func newVGManagerDaemonset(lvmCluster lvmv1alpha1.LVMCluster) appsv1.DaemonSet {
func newVGManagerDaemonset(lvmCluster lvmv1alpha1.LVMCluster, namespace string) appsv1.DaemonSet {
// aggregate nodeSelector and tolerations from all deviceClasses
nodeSelector, tolerations := extractNodeSelectorAndTolerations(lvmCluster)
volumes := []corev1.Volume{LVMDConfVol, DevHostDirVol, UDevHostDirVol, SysHostDirVol}
Expand Down Expand Up @@ -163,7 +163,7 @@ func newVGManagerDaemonset(lvmCluster lvmv1alpha1.LVMCluster) appsv1.DaemonSet {
ds := appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: VGManagerUnit,
Namespace: lvmCluster.Namespace,
Namespace: namespace,
Labels: labels,
},
Spec: appsv1.DaemonSetSpec{
Expand Down
7 changes: 4 additions & 3 deletions controllers/vgmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ func newFakeLVMClusterReconciler(t *testing.T, objs ...client.Object) *LVMCluste
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()

return &LVMClusterReconciler{
Client: client,
Scheme: scheme,
Log: logf.Log.WithName("LVMCLusterTest"),
Client: client,
Scheme: scheme,
Log: logf.Log.WithName("LVMCLusterTest"),
Namespace: "default",
}
}

Expand Down
29 changes: 16 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ import (
)

var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
watchNamespaceEnvVar = "WATCH_NAMESPACE"
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
operatorNamespaceEnvVar = "POD_NAMESPACE"
)

func init() {
Expand All @@ -67,17 +67,19 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

watchNamespace, err := getWatchNamespace()
operatorNamespace, err := getOperatorNamespace()
if err != nil {
setupLog.Error(err, "unable to get WatchNamespace, "+
"the manager will watch and manage resources in all namespaces")
setupLog.Error(err, "unable to get operatorNamespace"+
"Exiting")
os.Exit(1)
}
setupLog.Info("Watching namespace ", operatorNamespace)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
Namespace: watchNamespace,
Namespace: operatorNamespace,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "1136b8a6.topolvm.io",
Expand All @@ -91,6 +93,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
SecurityClient: secv1client.NewForConfigOrDie(mgr.GetConfig()),
Namespace: operatorNamespace,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "LVMCluster")
os.Exit(1)
Expand All @@ -113,14 +116,14 @@ func main() {
}
}

// getWatchNamespace returns the Namespace the operator should be watching for changes
func getWatchNamespace() (string, error) {
// The env variable WATCH_NAMESPACE which specifies the Namespace to watch.
// An empty value means the operator is running with cluster scope.
// getOperatorNamespace returns the Namespace the operator should be watching for changes
func getOperatorNamespace() (string, error) {
// The env variable POD_NAMESPACE which specifies the Namespace the pod is running in
// and hence will watch.

ns, found := os.LookupEnv(watchNamespaceEnvVar)
ns, found := os.LookupEnv(operatorNamespaceEnvVar)
if !found {
return "", fmt.Errorf("%s not found", watchNamespaceEnvVar)
return "", fmt.Errorf("%s not found", operatorNamespaceEnvVar)
}
return ns, nil
}

0 comments on commit e0c41e7

Please sign in to comment.