Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Driver Controller: Ensure CSI Config Creation #104

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/controller/clientprofile_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ func (r *ClientProfileReconcile) reconcileCephConnection() error {

if needsUpdate, err := utils.ToggleOwnerReference(
!r.cleanUp,
&r.clientProfile,
&r.cephConn,
&r.clientProfile,
r.Scheme,
); err != nil {
r.log.Error(err, "Failed to toggle owner reference on CephConnection")
Expand Down Expand Up @@ -249,8 +249,8 @@ func (r *ClientProfileReconcile) reconcileCephCsiClusterInfo() error {
_, err := ctrlutil.CreateOrUpdate(r.ctx, r.Client, &csiConfigMap, func() error {
if _, err := utils.ToggleOwnerReference(
!r.cleanUp,
&r.clientProfile,
&csiConfigMap,
&r.clientProfile,
r.Scheme,
); err != nil {
log.Error(err, "Failed toggling owner reference for Ceph CSI config map")
Expand Down
31 changes: 30 additions & 1 deletion internal/controller/driver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (
//+kubebuilder:rbac:groups=csi.ceph.io,resources=drivers/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=csi.ceph.io,resources=drivers/finalizers,verbs=update
//+kubebuilder:rbac:groups=csi.ceph.io,resources=operatorconfigs,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list
//+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update
//+kubebuilder:rbac:groups=storage.k8s.io,resources=csidrivers,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=daemonsets,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -175,6 +175,14 @@ func (r *DriverReconciler) SetupWithManager(mgr ctrl.Manager) error {
&corev1.Service{},
builder.WithPredicates(genChangedPredicate),
).
Owns(
&corev1.ConfigMap{},
builder.MatchEveryOwner,
builder.WithPredicates(
utils.NamePredicate(utils.CsiConfigVolume.Name),
utils.EventTypePredicate(false, false, true, false),
),
).
Watches(
&csiv1a1.OperatorConfig{},
enqueueAllDrivers,
Expand Down Expand Up @@ -221,6 +229,7 @@ func (r *driverReconcile) reconcile() error {
// Concurrently reconcile different aspects of the clusters actual state to meet
// the desired state defined on the driver object
errChan := utils.RunConcurrently(
r.reconcileCsiConfigMap,
r.reconcileK8sCsiDriver,
r.reconcileControllerPluginDeployment,
r.reconcileNodePluginDeamonSet,
Expand Down Expand Up @@ -318,6 +327,26 @@ func (r *driverReconcile) LoadAndValidateDesiredState() error {
return nil
}

func (r *driverReconcile) reconcileCsiConfigMap() error {
csiConfigMap := &corev1.ConfigMap{}
csiConfigMap.Name = utils.CsiConfigVolume.Name
csiConfigMap.Namespace = r.driver.Namespace

log := r.log.WithValues("csiConfigMap", csiConfigMap.Name)
log.Info("Reconciling CSI Config Map")

opResult, err := ctrlutil.CreateOrUpdate(r.ctx, r.Client, csiConfigMap, func() error {
if _, err := utils.ToggleOwnerReference(true, csiConfigMap, &r.driver, r.Scheme); err != nil {
log.Error(err, "Failed adding an owner referce on Ceph CSI config map")
return err
}
return nil
})

logCreateOrUpdateResult(log, "CSI Config Map", csiConfigMap, opResult, err)
return err
}

func (r *driverReconcile) reconcileK8sCsiDriver() error {
csiDriver := &storagev1.CSIDriver{}
csiDriver.Name = r.driver.Name
Expand Down
13 changes: 4 additions & 9 deletions internal/utils/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,14 @@ func IsOwnedBy(obj, owner metav1.Object) bool {
// The function return true if the owner reference list had changed and false it it didn't
func ToggleOwnerReference(on bool, obj, owner metav1.Object, scheme *runtime.Scheme) (bool, error) {
ownerRefExists := IsOwnedBy(obj, owner)

if on {
if !ownerRefExists {
if err := ctrlutil.SetOwnerReference(obj, owner, scheme); err != nil {
return false, err
}
return true, nil
err := ctrlutil.SetOwnerReference(owner, obj, scheme)
return err == nil, err
}
} else if ownerRefExists {
if err := ctrlutil.RemoveOwnerReference(obj, owner, scheme); err != nil {
return false, err
}
return true, nil
err := ctrlutil.RemoveOwnerReference(owner, obj, scheme)
return err == nil, err
}
return false, nil
}
52 changes: 52 additions & 0 deletions internal/utils/predicates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright 2024.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package utils

import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)

// Name Predicate return a predicate the filter events produced
// by resources that matches the given name
func NamePredicate(name string) predicate.Predicate {
return predicate.NewPredicateFuncs(func(obj client.Object) bool {
return obj.GetName() == name
})
}

// EventTypePredicate return a predicate the filter events based on their
// respective event type. This helper allows for the selection of multiple
// types resulting in a predicate that can filter in more then a single event
// type
func EventTypePredicate(create, update, del, generic bool) predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(_ event.CreateEvent) bool {
return create
},
UpdateFunc: func(_ event.UpdateEvent) bool {
return update
},
DeleteFunc: func(_ event.DeleteEvent) bool {
return del
},
GenericFunc: func(_ event.GenericEvent) bool {
return generic
},
}
}
Loading