Skip to content

Commit

Permalink
feat: updates the status in LVMCluster object
Browse files Browse the repository at this point in the history
The LVMCluster controller will update the LVMCluster.Status with
the consolidated status information in the LVMVolumeGroupNodeStatus
objects.

Signed-off-by: N Balachandran <[email protected]>
  • Loading branch information
nbalacha committed Jan 18, 2022
1 parent 199ae64 commit c6eb697
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
57 changes: 52 additions & 5 deletions controllers/lvmcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type LVMClusterReconciler struct {
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroups,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroups/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroups/finalizers,verbs=update
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroupnodestatuses,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroupnodestatuses/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=lvm.topolvm.io,resources=lvmvolumegroupnodestatuses/finalizers,verbs=update
//+kubebuilder:rbac:groups=security.openshift.io,resources=securitycontextconstraints,verbs=get;create;update;delete
//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch

Expand Down Expand Up @@ -100,12 +103,9 @@ func (r *LVMClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
result, reconcileError := r.reconcile(ctx, lvmCluster)

// Apply status changes
statusError := r.Client.Status().Update(ctx, lvmCluster)
statusError := r.updateLVMClusterStatus(ctx, lvmCluster)
if statusError != nil {
if errors.IsNotFound(err) {
r.Log.Error(statusError, "failed to update status")
}
r.Log.Error(statusError, "failed to update VG Node status")
}

// Reconcile errors have higher priority than status update errors
Expand Down Expand Up @@ -197,6 +197,53 @@ func (r *LVMClusterReconciler) reconcile(ctx context.Context, instance *lvmv1alp
return ctrl.Result{}, nil
}

func (r *LVMClusterReconciler) updateLVMClusterStatus(ctx context.Context, instance *lvmv1alpha1.LVMCluster) error {

vgNodeMap := make(map[string][]lvmv1alpha1.NodeStatus)

vgNodeStatusList := &lvmv1alpha1.LVMVolumeGroupNodeStatusList{}
err := r.Client.List(ctx, vgNodeStatusList, client.InNamespace(r.Namespace))
if err != nil {
r.Log.Error(err, "failed to list LVMVolumeGroupNodeStatus")
return err
}

for _, nodeItem := range vgNodeStatusList.Items {
for _, item := range nodeItem.Spec.LVMVGStatus {
val, ok := vgNodeMap[item.Name]
if !ok {
vgNodeMap[item.Name] = []lvmv1alpha1.NodeStatus{
{
Node: nodeItem.Name,
Status: item.Status,
},
}
} else {
new := lvmv1alpha1.NodeStatus{Node: nodeItem.Name, Status: item.Status}
val = append(val, new)
vgNodeMap[item.Name] = val
}
}
}

allVgStatuses := []lvmv1alpha1.DeviceClassStatus{}
for k := range vgNodeMap {
//r.Log.Info("vgnode map ", "key", k, "NodeStatus", vgNodeMap[k])
new := lvmv1alpha1.DeviceClassStatus{Name: k, NodeStatus: vgNodeMap[k]}
allVgStatuses = append(allVgStatuses, new)
}

instance.Status.DeviceClassStatuses = allVgStatuses
// Apply status changes
err = r.Client.Status().Update(ctx, instance)
if err != nil {
if errors.IsNotFound(err) {
r.Log.Error(err, "failed to update status")
}
}
return err
}

// NOTE: when updating this, please also update doc/design/operator.md
type resourceManager interface {

Expand Down
38 changes: 38 additions & 0 deletions controllers/lvmcluster_controller_watches.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,53 @@ limitations under the License.
package controllers

import (
"context"

lvmv1alpha1 "github.com/red-hat-storage/lvm-operator/api/v1alpha1"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

// SetupWithManager sets up the controller with the Manager.
func (r *LVMClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&lvmv1alpha1.LVMCluster{}).
Owns(&appsv1.DaemonSet{}).
Owns(&lvmv1alpha1.LVMVolumeGroup{}).
Owns(&lvmv1alpha1.LVMVolumeGroupNodeStatus{}).
Watches(
&source.Kind{Type: &lvmv1alpha1.LVMVolumeGroupNodeStatus{}},
handler.EnqueueRequestsFromMapFunc(r.getLVMClusterObjsForReconcile),
// builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Complete(r)
}

func (r *LVMClusterReconciler) getLVMClusterObjsForReconcile(obj client.Object) []reconcile.Request {
foundLVMClusterList := &lvmv1alpha1.LVMClusterList{}
listOps := &client.ListOptions{
Namespace: obj.GetNamespace(),
}

err := r.Client.List(context.TODO(), foundLVMClusterList, listOps)
if err != nil {
r.Log.Error(err, "getLVMClusterObjsForReconcile: Failed to get LVMCluster objs")
return []reconcile.Request{}
}

requests := make([]reconcile.Request, len(foundLVMClusterList.Items))
for i, item := range foundLVMClusterList.Items {
requests[i] = reconcile.Request{
NamespacedName: types.NamespacedName{
Name: item.GetName(),
Namespace: item.GetNamespace(),
},
}
}
return requests
}

0 comments on commit c6eb697

Please sign in to comment.