Skip to content

Commit

Permalink
Add Watch to NodeSet for Deployments
Browse files Browse the repository at this point in the history
Instead of the OpenStackDataPlaneDeployment controller updating the
status of OpenStackDataPlaneNodeSet after a deployment, use a watch
instead. The watch is added for the OpenStackDataPlaneNodeSet reconciler
and it when a reconcile is triggerd, the deployment condition will be
set to True if there any associated deployed
OpenStackDataPlaneDeployments.

Signed-off-by: James Slagle <[email protected]>
  • Loading branch information
slagle committed Sep 18, 2023
1 parent ff7bbd6 commit 4547ac0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
11 changes: 0 additions & 11 deletions controllers/openstackdataplanedeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -235,16 +234,6 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
condition.TrueCondition(
condition.Type(fmt.Sprintf(dataplanev1.NodeSetDeploymentReadyCondition, nodeSet.Name)),
condition.DeploymentReadyMessage))

logger.Info("Set NodeSet DeploymentReadyCondition true", "nodeSet", nodeSet.Name)
_, err = controllerutil.CreateOrPatch(ctx, helper.GetClient(), &nodeSet, func() error {
nodeSet.Status.Conditions.MarkTrue(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage)
return nil
})
if err != nil {
util.LogErrorForObject(helper, err, "Unable to set Status on NodeSet", &nodeSet)
return ctrl.Result{}, err
}
}
}

Expand Down
77 changes: 77 additions & 0 deletions controllers/openstackdataplanenodeset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,65 @@ func (r *OpenStackDataPlaneNodeSetReconciler) Reconcile(ctx context.Context, req
instance.Status.Conditions.Set(condition.FalseCondition(condition.DeploymentReadyCondition, condition.NotRequestedReason, condition.SeverityInfo, condition.DeploymentReadyInitMessage))
}

deployedDeploymentsForNodeSet, err := r.GetDeployedDeploymentsForNodeSet(instance.Name)
if err != nil {
logger.Error(err, "Unable to get deployed OpenStackDataPlaneDeployments.")
return ctrl.Result{}, err
}
if len(deployedDeploymentsForNodeSet.Items) > 0 {
logger.Info("Set NodeSet DeploymentReadyCondition true")
instance.Status.Conditions.MarkTrue(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage)
}

return ctrl.Result{}, nil
}

// GetDeployedDeploymentsForNodeSet - Get the OpenStackDataPlaneDeployment
// resources that have been deployed and are for the given
// OpenStackDataPlaneNodeSet.
func (r *OpenStackDataPlaneNodeSetReconciler) GetDeployedDeploymentsForNodeSet(nodeSetName string) (*dataplanev1.OpenStackDataPlaneDeploymentList, error) {
// Get all deployments
deployments := &dataplanev1.OpenStackDataPlaneDeploymentList{}
err := r.Client.List(context.Background(), deployments)
if err != nil {
r.Log.Error(err, "Unable to retrieve OpenStackDataPlaneDeployment CRs %v")
return deployments, nil
}
deployedDeploymentsForNodeSet := &dataplanev1.OpenStackDataPlaneDeploymentList{}
for _, deployment := range deployments.Items {
for _, nodeSet := range deployment.Spec.NodeSets {
if nodeSet == nodeSetName {
if deployment.Status.Conditions.IsTrue(condition.Type(fmt.Sprintf(dataplanev1.NodeSetDeploymentReadyCondition, nodeSetName))) {
deployedDeploymentsForNodeSet.Items = append(deployedDeploymentsForNodeSet.Items, deployment)
}
}
}
}

return deployedDeploymentsForNodeSet, err
}

// GetNodeSetsForDeployment - Get the OpenStackDataPlaneNodeSet
// resources for the given OpenStackDataPlaneDeployment
func (r *OpenStackDataPlaneNodeSetReconciler) GetNodeSetsForDeployment(namespace string, deployment *dataplanev1.OpenStackDataPlaneDeployment) (dataplanev1.OpenStackDataPlaneNodeSetList, error) {
nodeSets := dataplanev1.OpenStackDataPlaneNodeSetList{}
var err error
for _, nodeSetName := range deployment.Spec.NodeSets {
nodeSet := &dataplanev1.OpenStackDataPlaneNodeSet{}
namespacedName := client.ObjectKey{
Namespace: namespace,
Name: nodeSetName}
err = r.Client.Get(context.Background(), namespacedName, nodeSet)
if err != nil {
r.Log.Error(err, "Unable to retrieve OpenStackDataPlaneNodeSet CR %v")
return nodeSets, nil
}
nodeSets.Items = append(nodeSets.Items, *nodeSet)
}

return nodeSets, err
}

// SetupWithManager sets up the controller with the Manager.
func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
reconcileFunction := handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
Expand Down Expand Up @@ -296,6 +352,25 @@ func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(mgr ctrl.Manager)
return nil
})

deploymentWatcher := handler.EnqueueRequestsFromMapFunc(func(obj client.Object) []reconcile.Request {
var namespace string = obj.GetNamespace()
result := []reconcile.Request{}

deployment := obj.(*dataplanev1.OpenStackDataPlaneDeployment)
nodeSets, err := r.GetNodeSetsForDeployment(namespace, deployment)
if err != nil {
r.Log.Error(err, "Unable to retrieve OpenStackDataPlaneNodeSets %w")
return nil
}
for _, nodeSet := range nodeSets.Items {
name := client.ObjectKey{
Namespace: namespace,
Name: nodeSet.Name}
result = append(result, reconcile.Request{NamespacedName: name})
}
return result
})

return ctrl.NewControllerManagedBy(mgr).
For(&dataplanev1.OpenStackDataPlaneNodeSet{}).
Owns(&v1alpha1.OpenStackAnsibleEE{}).
Expand All @@ -305,5 +380,7 @@ func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(mgr ctrl.Manager)
Owns(&corev1.Secret{}).
Watches(&source.Kind{Type: &infranetworkv1.DNSMasq{}},
reconcileFunction).
Watches(&source.Kind{Type: &dataplanev1.OpenStackDataPlaneDeployment{}},
deploymentWatcher).
Complete(r)
}

0 comments on commit 4547ac0

Please sign in to comment.