support scoped nodes and workloads for global rules (#5279)

Signed-off-by: junot <junotxiang@kubesphere.io>

Signed-off-by: junot <junotxiang@kubesphere.io>
This commit is contained in:
junot
2022-10-11 17:26:27 +08:00
committed by GitHub
parent 14aa059c63
commit 3810db2879
6 changed files with 240 additions and 46 deletions

View File

@@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: (devel)
controller-gen.kubebuilder.io/version: ""
creationTimestamp: null
name: clusterrulegroups.alerting.kubesphere.io
spec:

View File

@@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: (devel)
controller-gen.kubebuilder.io/version: ""
creationTimestamp: null
name: globalrulegroups.alerting.kubesphere.io
spec:
@@ -149,11 +149,22 @@ spec:
type: object
names:
items:
type: string
description: The cluster to which the node belongs
must be specified.
properties:
cluster:
type: string
names:
items:
type: string
type: array
required:
- cluster
- names
type: object
type: array
required:
- comparator
- metricThreshold
- names
type: object
workload:
@@ -203,7 +214,22 @@ spec:
type: object
names:
items:
type: string
description: The cluster and namespace to which the
workloads belongs must be specified.
properties:
cluster:
type: string
names:
items:
type: string
type: array
namespace:
type: string
required:
- cluster
- names
- namespace
type: object
type: array
required:
- comparator

View File

@@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: (devel)
controller-gen.kubebuilder.io/version: ""
creationTimestamp: null
name: rulegroups.alerting.kubesphere.io
spec:

View File

@@ -176,8 +176,8 @@ type ClusterRuleExprBuilder struct {
// Only one of its members may be specified.
type GlobalRuleExprBuilder struct {
Workload *WorkloadExprBuilder `json:"workload,omitempty"`
Node *NodeExprBuilder `json:"node,omitempty"`
Workload *ScopedWorkloadExprBuilder `json:"workload,omitempty"`
Node *ScopedNodeExprBuilder `json:"node,omitempty"`
}
type WorkloadKind string
@@ -196,6 +196,21 @@ type WorkloadExprBuilder struct {
MetricThreshold WorkloadMetricThreshold `json:"metricThreshold,omitempty"`
}
type ScopedWorkloadExprBuilder struct {
WorkloadKind WorkloadKind `json:"kind"`
WorkloadNames []ScopedWorkloadNames `json:"names"`
Comparator Comparator `json:"comparator"`
MetricThreshold WorkloadMetricThreshold `json:"metricThreshold,omitempty"`
}
// The cluster and namespace to which the workloads belongs must be specified.
type ScopedWorkloadNames struct {
Cluster string `json:"cluster"`
Namespace string `json:"namespace"`
Names []string `json:"names"`
}
const (
MetricWorkloadCpuUsage = "namespace:workload_cpu_usage:sum"
MetricWorkloadMemoryUsage = "namespace:workload_memory_usage:sum"
@@ -266,6 +281,29 @@ func (b *WorkloadExprBuilder) Build() string {
return ""
}
func (b *ScopedWorkloadExprBuilder) Build() string {
// include the workload names into builded expr only.
// the limited clusters and namespaces will be set to the clusterSelector and namespaceSelector separately.
var names = make(map[string]struct{})
for _, snames := range b.WorkloadNames {
for _, name := range snames.Names {
names[name] = struct{}{}
}
}
var eb = WorkloadExprBuilder{
WorkloadKind: b.WorkloadKind,
Comparator: b.Comparator,
MetricThreshold: b.MetricThreshold,
}
for name := range names {
eb.WorkloadNames = append(eb.WorkloadNames, name)
}
return eb.Build()
}
// Only one of its members may be specified.
type WorkloadMetricThreshold struct {
Cpu *WorkloadCpuThreshold `json:"cpu,omitempty"`
@@ -310,6 +348,19 @@ type NodeExprBuilder struct {
MetricThreshold NodeMetricThreshold `json:"metricThreshold"`
}
type ScopedNodeExprBuilder struct {
NodeNames []ScopedNodeNames `json:"names"`
Comparator Comparator `json:"comparator"`
MetricThreshold NodeMetricThreshold `json:"metricThreshold,omitempty"`
}
// The cluster to which the node belongs must be specified.
type ScopedNodeNames struct {
Cluster string `json:"cluster"`
Names []string `json:"names"`
}
const (
MetricNodeCpuUtilization = "node:node_cpu_utilisation:avg1m"
MetricNodeCpuLoad1m = "node:load1:ratio"
@@ -427,6 +478,28 @@ func (b *NodeExprBuilder) Build() string {
return ""
}
func (b *ScopedNodeExprBuilder) Build() string {
// include the node names into builded expr only.
// the limited clusters will be set to the clusterSelector.
var names = make(map[string]struct{})
for _, snames := range b.NodeNames {
for _, name := range snames.Names {
names[name] = struct{}{}
}
}
var eb = NodeExprBuilder{
Comparator: b.Comparator,
MetricThreshold: b.MetricThreshold,
}
for name := range names {
eb.NodeNames = append(eb.NodeNames, name)
}
return eb.Build()
}
// Only one of its members may be specified.
type NodeMetricThreshold struct {
Cpu *NodeCpuThreshold `json:"cpu,omitempty"`

View File

@@ -240,8 +240,51 @@ func (r *GlobalRuleGroup) Default() {
if rule.ExprBuilder != nil {
if rule.ExprBuilder.Node != nil {
rule.Expr = intstr.FromString(rule.ExprBuilder.Node.Build())
// limiting the clusters will take the union result for clusters from scoped nodes.
// eg. if specify nodeA of cluster1 and nodeB of cluster2 in rule.ExprBuilder.Node.NodeNames,
// the nodeA and nodeB in cluster1 and cluster2 are selected.
var clusters = make(map[string]struct{})
for _, sname := range rule.ExprBuilder.Node.NodeNames {
if sname.Cluster != "" {
clusters[sname.Cluster] = struct{}{}
}
}
if len(clusters) > 0 {
clusterSelector := &MetricLabelSelector{}
for cluster := range clusters {
clusterSelector.InValues = append(clusterSelector.InValues, cluster)
}
rule.ClusterSelector = clusterSelector
}
} else if rule.ExprBuilder.Workload != nil {
rule.Expr = intstr.FromString(rule.ExprBuilder.Workload.Build())
// limiting the clusters and namespaces will take the union result for clusters from scoped workloads.
// eg. if specify worloadA of cluster1-namespace1 and worloadB of cluster2-namespace2 in rule.ExprBuilder.Workload.WorkloadNames,
// the worloadA and worloadB in cluster1-namespace1, cluster1-namespace2 and cluster2-namespace1, cluster2-namespace2 are selected.
var clusters = make(map[string]struct{})
var namespaces = make(map[string]struct{})
for _, sname := range rule.ExprBuilder.Workload.WorkloadNames {
if sname.Cluster != "" {
clusters[sname.Cluster] = struct{}{}
}
if sname.Namespace != "" {
namespaces[sname.Namespace] = struct{}{}
}
}
if len(clusters) > 0 {
clusterSelector := &MetricLabelSelector{}
for cluster := range clusters {
clusterSelector.InValues = append(clusterSelector.InValues, cluster)
}
rule.ClusterSelector = clusterSelector
}
if len(namespaces) > 0 {
nsSelector := &MetricLabelSelector{}
for ns := range namespaces {
nsSelector.InValues = append(nsSelector.InValues, ns)
}
rule.NamespaceSelector = nsSelector
}
}
}
setRuleId(&rule.Rule)

View File

@@ -17,12 +17,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// Code generated by deepcopy-gen. DO NOT EDIT.
// Code generated by controller-gen. DO NOT EDIT.
package v2beta1
import (
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
)
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
@@ -34,7 +34,6 @@ func (in *ClusterRule) DeepCopyInto(out *ClusterRule) {
*out = new(ClusterRuleExprBuilder)
(*in).DeepCopyInto(*out)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterRule.
@@ -55,7 +54,6 @@ func (in *ClusterRuleExprBuilder) DeepCopyInto(out *ClusterRuleExprBuilder) {
*out = new(NodeExprBuilder)
(*in).DeepCopyInto(*out)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterRuleExprBuilder.
@@ -75,7 +73,6 @@ func (in *ClusterRuleGroup) DeepCopyInto(out *ClusterRuleGroup) {
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
out.Status = in.Status
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterRuleGroup.
@@ -108,7 +105,6 @@ func (in *ClusterRuleGroupList) DeepCopyInto(out *ClusterRuleGroupList) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterRuleGroupList.
@@ -139,7 +135,6 @@ func (in *ClusterRuleGroupSpec) DeepCopyInto(out *ClusterRuleGroupSpec) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterRuleGroupSpec.
@@ -155,7 +150,6 @@ func (in *ClusterRuleGroupSpec) DeepCopy() *ClusterRuleGroupSpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ClusterRuleGroupStatus) DeepCopyInto(out *ClusterRuleGroupStatus) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ClusterRuleGroupStatus.
@@ -187,7 +181,6 @@ func (in *GlobalRule) DeepCopyInto(out *GlobalRule) {
*out = new(GlobalRuleExprBuilder)
(*in).DeepCopyInto(*out)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalRule.
@@ -205,15 +198,14 @@ func (in *GlobalRuleExprBuilder) DeepCopyInto(out *GlobalRuleExprBuilder) {
*out = *in
if in.Workload != nil {
in, out := &in.Workload, &out.Workload
*out = new(WorkloadExprBuilder)
*out = new(ScopedWorkloadExprBuilder)
(*in).DeepCopyInto(*out)
}
if in.Node != nil {
in, out := &in.Node, &out.Node
*out = new(NodeExprBuilder)
*out = new(ScopedNodeExprBuilder)
(*in).DeepCopyInto(*out)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalRuleExprBuilder.
@@ -233,7 +225,6 @@ func (in *GlobalRuleGroup) DeepCopyInto(out *GlobalRuleGroup) {
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
out.Status = in.Status
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalRuleGroup.
@@ -266,7 +257,6 @@ func (in *GlobalRuleGroupList) DeepCopyInto(out *GlobalRuleGroupList) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalRuleGroupList.
@@ -297,7 +287,6 @@ func (in *GlobalRuleGroupSpec) DeepCopyInto(out *GlobalRuleGroupSpec) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalRuleGroupSpec.
@@ -313,7 +302,6 @@ func (in *GlobalRuleGroupSpec) DeepCopy() *GlobalRuleGroupSpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *GlobalRuleGroupStatus) DeepCopyInto(out *GlobalRuleGroupStatus) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new GlobalRuleGroupStatus.
@@ -329,7 +317,6 @@ func (in *GlobalRuleGroupStatus) DeepCopy() *GlobalRuleGroupStatus {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *Matcher) DeepCopyInto(out *Matcher) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Matcher.
@@ -355,7 +342,6 @@ func (in *MetricLabelSelector) DeepCopyInto(out *MetricLabelSelector) {
*out = new(Matcher)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new MetricLabelSelector.
@@ -377,7 +363,6 @@ func (in *NamespaceRule) DeepCopyInto(out *NamespaceRule) {
*out = new(NamespaceRuleExprBuilder)
(*in).DeepCopyInto(*out)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NamespaceRule.
@@ -398,7 +383,6 @@ func (in *NamespaceRuleExprBuilder) DeepCopyInto(out *NamespaceRuleExprBuilder)
*out = new(WorkloadExprBuilder)
(*in).DeepCopyInto(*out)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NamespaceRuleExprBuilder.
@@ -434,7 +418,6 @@ func (in *NodeCpuThreshold) DeepCopyInto(out *NodeCpuThreshold) {
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeCpuThreshold.
@@ -485,7 +468,6 @@ func (in *NodeDiskThreshold) DeepCopyInto(out *NodeDiskThreshold) {
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeDiskThreshold.
@@ -507,7 +489,6 @@ func (in *NodeExprBuilder) DeepCopyInto(out *NodeExprBuilder) {
copy(*out, *in)
}
in.MetricThreshold.DeepCopyInto(&out.MetricThreshold)
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeExprBuilder.
@@ -533,7 +514,6 @@ func (in *NodeMemoryThreshold) DeepCopyInto(out *NodeMemoryThreshold) {
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeMemoryThreshold.
@@ -574,7 +554,6 @@ func (in *NodeMetricThreshold) DeepCopyInto(out *NodeMetricThreshold) {
*out = new(NodePodThreshold)
(*in).DeepCopyInto(*out)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeMetricThreshold.
@@ -600,7 +579,6 @@ func (in *NodeNetworkThreshold) DeepCopyInto(out *NodeNetworkThreshold) {
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodeNetworkThreshold.
@@ -626,7 +604,6 @@ func (in *NodePodThreshold) DeepCopyInto(out *NodePodThreshold) {
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NodePodThreshold.
@@ -657,7 +634,6 @@ func (in *Rule) DeepCopyInto(out *Rule) {
(*out)[key] = val
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Rule.
@@ -677,7 +653,6 @@ func (in *RuleGroup) DeepCopyInto(out *RuleGroup) {
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
in.Spec.DeepCopyInto(&out.Spec)
out.Status = in.Status
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RuleGroup.
@@ -710,7 +685,6 @@ func (in *RuleGroupList) DeepCopyInto(out *RuleGroupList) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RuleGroupList.
@@ -741,7 +715,6 @@ func (in *RuleGroupSpec) DeepCopyInto(out *RuleGroupSpec) {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RuleGroupSpec.
@@ -757,7 +730,6 @@ func (in *RuleGroupSpec) DeepCopy() *RuleGroupSpec {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *RuleGroupStatus) DeepCopyInto(out *RuleGroupStatus) {
*out = *in
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RuleGroupStatus.
@@ -770,6 +742,92 @@ func (in *RuleGroupStatus) DeepCopy() *RuleGroupStatus {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ScopedNodeExprBuilder) DeepCopyInto(out *ScopedNodeExprBuilder) {
*out = *in
if in.NodeNames != nil {
in, out := &in.NodeNames, &out.NodeNames
*out = make([]ScopedNodeNames, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
in.MetricThreshold.DeepCopyInto(&out.MetricThreshold)
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ScopedNodeExprBuilder.
func (in *ScopedNodeExprBuilder) DeepCopy() *ScopedNodeExprBuilder {
if in == nil {
return nil
}
out := new(ScopedNodeExprBuilder)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ScopedNodeNames) DeepCopyInto(out *ScopedNodeNames) {
*out = *in
if in.Names != nil {
in, out := &in.Names, &out.Names
*out = make([]string, len(*in))
copy(*out, *in)
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ScopedNodeNames.
func (in *ScopedNodeNames) DeepCopy() *ScopedNodeNames {
if in == nil {
return nil
}
out := new(ScopedNodeNames)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ScopedWorkloadExprBuilder) DeepCopyInto(out *ScopedWorkloadExprBuilder) {
*out = *in
if in.WorkloadNames != nil {
in, out := &in.WorkloadNames, &out.WorkloadNames
*out = make([]ScopedWorkloadNames, len(*in))
for i := range *in {
(*in)[i].DeepCopyInto(&(*out)[i])
}
}
in.MetricThreshold.DeepCopyInto(&out.MetricThreshold)
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ScopedWorkloadExprBuilder.
func (in *ScopedWorkloadExprBuilder) DeepCopy() *ScopedWorkloadExprBuilder {
if in == nil {
return nil
}
out := new(ScopedWorkloadExprBuilder)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ScopedWorkloadNames) DeepCopyInto(out *ScopedWorkloadNames) {
*out = *in
if in.Names != nil {
in, out := &in.Names, &out.Names
*out = make([]string, len(*in))
copy(*out, *in)
}
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ScopedWorkloadNames.
func (in *ScopedWorkloadNames) DeepCopy() *ScopedWorkloadNames {
if in == nil {
return nil
}
out := new(ScopedWorkloadNames)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *WorkloadCpuThreshold) DeepCopyInto(out *WorkloadCpuThreshold) {
*out = *in
@@ -778,7 +836,6 @@ func (in *WorkloadCpuThreshold) DeepCopyInto(out *WorkloadCpuThreshold) {
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkloadCpuThreshold.
@@ -800,7 +857,6 @@ func (in *WorkloadExprBuilder) DeepCopyInto(out *WorkloadExprBuilder) {
copy(*out, *in)
}
in.MetricThreshold.DeepCopyInto(&out.MetricThreshold)
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkloadExprBuilder.
@@ -826,7 +882,6 @@ func (in *WorkloadMemoryThreshold) DeepCopyInto(out *WorkloadMemoryThreshold) {
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkloadMemoryThreshold.
@@ -862,7 +917,6 @@ func (in *WorkloadMetricThreshold) DeepCopyInto(out *WorkloadMetricThreshold) {
*out = new(WorkloadReplicaThreshold)
(*in).DeepCopyInto(*out)
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkloadMetricThreshold.
@@ -888,7 +942,6 @@ func (in *WorkloadNetworkThreshold) DeepCopyInto(out *WorkloadNetworkThreshold)
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkloadNetworkThreshold.
@@ -909,7 +962,6 @@ func (in *WorkloadReplicaThreshold) DeepCopyInto(out *WorkloadReplicaThreshold)
*out = new(float64)
**out = **in
}
return
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new WorkloadReplicaThreshold.