Skip to content
This repository has been archived by the owner on Jun 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #421 from rabi/simplify_watcher
Browse files Browse the repository at this point in the history
Simplify deployment watcher for nodeset
  • Loading branch information
openshift-merge-robot authored Sep 20, 2023
2 parents 38a7244 + 9d81de8 commit b382656
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 57 deletions.
5 changes: 2 additions & 3 deletions controllers/openstackdataplanedeployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,10 @@ func (r *OpenStackDataPlaneDeploymentReconciler) Reconcile(ctx context.Context,
return ctrl.Result{}, nil
}

logger.Info("Set status deploy true", "instance", instance)
instance.Status.Deployed = true
logger.Info("Set DeploymentReadyCondition true", "instance", instance)
instance.Status.Conditions.Set(condition.TrueCondition(condition.DeploymentReadyCondition, condition.DeploymentReadyMessage))

instance.Status.Deployed = true
logger.Info("Set status deploy true", "instance", instance)
return ctrl.Result{}, nil
}

Expand Down
72 changes: 19 additions & 53 deletions controllers/openstackdataplanenodeset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -259,63 +260,38 @@ 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)
isDeploymentReady, err := checkDeploymentReady(helper, req)
if err != nil {
logger.Error(err, "Unable to get deployed OpenStackDataPlaneDeployments.")
return ctrl.Result{}, err
}
if len(deployedDeploymentsForNodeSet.Items) > 0 {
if isDeploymentReady {
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
func checkDeploymentReady(helper *helper.Helper,
request ctrl.Request) (bool, error) {
// Get all completed deployments
deployments := &dataplanev1.OpenStackDataPlaneDeploymentList{}
err := r.Client.List(context.Background(), deployments)
opts := []client.ListOption{
client.InNamespace(request.NamespacedName.Namespace),
}
err := helper.GetClient().List(context.Background(), deployments, opts...)
if err != nil {
r.Log.Error(err, "Unable to retrieve OpenStackDataPlaneDeployment CRs %v")
return deployments, nil
helper.GetLogger().Error(err, "Unable to retrieve OpenStackDataPlaneDeployment CRs %v")
return false, err
}
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)
}
}
if slices.Contains(
deployment.Spec.NodeSets, request.NamespacedName.Name) && deployment.Status.Deployed {
return true, nil
}
}

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
return false, nil
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -344,28 +320,18 @@ func (r *OpenStackDataPlaneNodeSetReconciler) SetupWithManager(mgr ctrl.Manager)
}
result = append(result, reconcile.Request{NamespacedName: name})
}
if len(result) > 0 {
r.Log.Info(fmt.Sprintf("Reconcile request for: %+v", result))

return result
}
return nil
return result
})

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 {
for _, nodeSet := range deployment.Spec.NodeSets {
name := client.ObjectKey{
Namespace: namespace,
Name: nodeSet.Name}
Name: nodeSet}
result = append(result, reconcile.Request{NamespacedName: name})
}
return result
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/openstack-k8s-operators/lib-common/modules/test v0.1.2-0.20230913075424-2680ce4b6ad2
github.com/openstack-k8s-operators/openstack-ansibleee-operator/api v0.1.1-0.20230913102927-2edee48d935a
github.com/openstack-k8s-operators/openstack-baremetal-operator/api v0.1.1-0.20230915055858-ecb378f804c9
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
gopkg.in/yaml.v3 v3.0.1
k8s.io/api v0.26.9
k8s.io/apimachinery v0.26.9
Expand Down Expand Up @@ -68,7 +69,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
Expand Down

0 comments on commit b382656

Please sign in to comment.