forked from kubernetes-retired/kubefed
/
placement.go
109 lines (94 loc) · 3.67 KB
/
placement.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sync
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
fedv1a1 "github.com/kubernetes-sigs/federation-v2/pkg/apis/core/v1alpha1"
"github.com/kubernetes-sigs/federation-v2/pkg/controller/util"
)
// computeNamespacedPlacement determines placement for namespaced
// federated resources (e.g. FederatedConfigMap).
//
// If federation is deployed cluster-wide, placement is the
// intersection of the placement for the federated resource and the
// placement of the federated namespace containing the resource.
//
// If federation is limited to a single namespace, placement is
// determined as the intersection of resource and namespace placement
// if namespace placement exists. If namespace placement does not
// exist, resource placement will be used verbatim. This is possible
// because the single namespace by definition must exist on member
// clusters, so namespace placement becomes a mechanism for limiting
// rather than allowing propagation.
func computeNamespacedPlacement(resource, namespace *unstructured.Unstructured, clusters []*fedv1a1.FederatedCluster, limitedScope bool) (selectedClusters sets.String, err error) {
resourceClusters, err := computePlacement(resource, clusters)
if err != nil {
return nil, err
}
if namespace == nil {
if limitedScope {
// Use the resource placement verbatim if no federated
// namespace is present and federation is targeting a
// single namespace.
return resourceClusters, nil
}
// Resource should not exist in any member clusters.
return sets.String{}, nil
}
namespaceClusters, err := computePlacement(namespace, clusters)
if err != nil {
return nil, err
}
// If both namespace and resource placement exist, the desired
// list of clusters is their intersection.
return resourceClusters.Intersection(namespaceClusters), nil
}
// computePlacement determines the selected clusters for a federated
// resource.
func computePlacement(resource *unstructured.Unstructured, clusters []*fedv1a1.FederatedCluster) (selectedClusters sets.String, err error) {
selectedNames, err := selectedClusterNames(resource, clusters)
if err != nil {
return nil, err
}
clusterNames := getClusterNames(clusters)
return clusterNames.Intersection(selectedNames), nil
}
func selectedClusterNames(resource *unstructured.Unstructured, clusters []*fedv1a1.FederatedCluster) (sets.String, error) {
directive, err := util.GetPlacementDirective(resource)
if err != nil {
return nil, err
}
selectedNames := sets.String{}
// Explicit cluster names take precedence over a selector.
if directive.ClusterNames != nil {
for _, clusterName := range directive.ClusterNames {
selectedNames.Insert(clusterName)
}
} else {
for _, cluster := range clusters {
if directive.ClusterSelector.Matches(labels.Set(cluster.Labels)) {
selectedNames.Insert(cluster.Name)
}
}
}
return selectedNames, nil
}
func getClusterNames(clusters []*fedv1a1.FederatedCluster) sets.String {
clusterNames := sets.String{}
for _, cluster := range clusters {
clusterNames.Insert(cluster.Name)
}
return clusterNames
}