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

[topology] Add optional default for topologyConstraint LabelSelector #597

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Changes from 1 commit
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
26 changes: 25 additions & 1 deletion modules/common/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ package topology
import (
"context"
"fmt"

topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand All @@ -33,6 +35,7 @@ func EnsureTopologyRef(
h *helper.Helper,
topologyRef *TopoRef,
finalizer string,
defaultLabelSelector *metav1.LabelSelector,
) (*topologyv1.Topology, string, error) {

var err error
Expand All @@ -43,7 +46,7 @@ func EnsureTopologyRef(
return nil, "", fmt.Errorf("No valid TopologyRef input passed")
}

topology, hash, err := topologyv1.GetTopologyByName(
topology, _, err := topologyv1.GetTopologyByName(
ctx,
h,
topologyRef.Name,
Expand All @@ -52,12 +55,33 @@ func EnsureTopologyRef(
if err != nil {
return topology, hash, err
}

// Add finalizer (if not present) to the resource consumed by the Service
if controllerutil.AddFinalizer(topology, fmt.Sprintf("%s-%s", h.GetFinalizer(), finalizer)) {
if err := h.GetClient().Update(ctx, topology); err != nil {
return topology, hash, err
}
}

// Set default LabelSelector on topologyConstraints if not set, similar to cluster level default:
// https://kubernetes.io/docs/concepts/scheduling-eviction/topology-spread-constraints/#cluster-level-default-constraints
topology = topology.DeepCopy()

topologyConstraints := topology.Spec.TopologySpreadConstraints
if topologyConstraints != nil {
olliewalsh marked this conversation as resolved.
Show resolved Hide resolved
for i := 0; i < len(*topologyConstraints); i++ {
current := &(*topologyConstraints)[i]
if current.LabelSelector == nil {
current.LabelSelector = defaultLabelSelector
fmount marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

hash, err = util.ObjectHash(topology.Spec)
olliewalsh marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return topology, hash, err
}

return topology, hash, nil
}

Expand Down