Feat: allow empty cluster for cluster selector (#4880)

Signed-off-by: Somefive <yd219913@alibaba-inc.com>

Signed-off-by: Somefive <yd219913@alibaba-inc.com>
This commit is contained in:
Somefive
2022-10-18 17:19:16 +08:00
committed by GitHub
parent c0e1a1a323
commit f1107c5018
6 changed files with 20 additions and 2 deletions

View File

@@ -49,6 +49,10 @@ type Placement struct {
// Exclusive to "clusters"
ClusterLabelSelector map[string]string `json:"clusterLabelSelector,omitempty"`
// AllowEmpty ignore empty cluster error when no cluster returned for label
// selector
AllowEmpty bool `json:"allowEmpty,omitempty"`
// DeprecatedClusterSelector is a depreciated alias for ClusterLabelSelector.
// Deprecated: Use clusterLabelSelector instead.
DeprecatedClusterSelector map[string]string `json:"clusterSelector,omitempty"`

View File

@@ -16,6 +16,8 @@ spec:
clusters?: [...string]
// +usage=Specify the label selector for clusters
clusterLabelSelector?: [string]: string
// +usage=Ignore empty cluster error
allowEmpty?: bool
// +usage=Deprecated: Use clusterLabelSelector instead.
clusterSelector?: [string]: string
// +usage=Specify the target namespace to deploy in the selected clusters, default inherit the original namespace.

View File

@@ -16,6 +16,8 @@ spec:
clusters?: [...string]
// +usage=Specify the label selector for clusters
clusterLabelSelector?: [string]: string
// +usage=Ignore empty cluster error
allowEmpty?: bool
// +usage=Deprecated: Use clusterLabelSelector instead.
clusterSelector?: [string]: string
// +usage=Specify the target namespace to deploy in the selected clusters, default inherit the original namespace.

View File

@@ -46,7 +46,7 @@ func GetClusterLabelSelectorInTopology(topology *v1alpha1.TopologyPolicySpec) ma
// GetPlacementsFromTopologyPolicies get placements from topology policies with provided client
func GetPlacementsFromTopologyPolicies(ctx context.Context, cli client.Client, appNs string, policies []v1beta1.AppPolicy, allowCrossNamespace bool) ([]v1alpha1.PlacementDecision, error) {
var placements []v1alpha1.PlacementDecision
placements := make([]v1alpha1.PlacementDecision, 0)
placementMap := map[string]struct{}{}
addCluster := func(cluster string, ns string, validateCluster bool) error {
if validateCluster {
@@ -89,7 +89,7 @@ func GetPlacementsFromTopologyPolicies(ctx context.Context, cli client.Client, a
if err != nil {
return nil, errors.Wrapf(err, "failed to find clusters in topology %s", policy.Name)
}
if len(clusterList.Items) == 0 {
if len(clusterList.Items) == 0 && !topologySpec.AllowEmpty {
return nil, errors.New("failed to find any cluster matches given labels")
}
for _, cluster := range clusterList.Items {

View File

@@ -112,6 +112,14 @@ func TestGetClusterLabelSelectorInTopology(t *testing.T) {
}},
Error: "failed to find any cluster matches given labels",
},
"topology-by-cluster-selector-ignore-404": {
Inputs: []v1beta1.AppPolicy{{
Name: "topology-policy",
Type: "topology",
Properties: &runtime.RawExtension{Raw: []byte(`{"clusterSelector":{"key":"bad-value"},"allowEmpty":true}`)},
}},
Outputs: []v1alpha1.PlacementDecision{},
},
"topology-by-cluster-selector": {
Inputs: []v1beta1.AppPolicy{{
Name: "topology-policy",

View File

@@ -12,6 +12,8 @@ template: {
clusters?: [...string]
// +usage=Specify the label selector for clusters
clusterLabelSelector?: [string]: string
// +usage=Ignore empty cluster error
allowEmpty?: bool
// +usage=Deprecated: Use clusterLabelSelector instead.
clusterSelector?: [string]: string
// +usage=Specify the target namespace to deploy in the selected clusters, default inherit the original namespace.