fix rulegroup typemeta miss (#5349)

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

Signed-off-by: junot <junotxiang@kubesphere.io>
This commit is contained in:
junot
2022-11-04 18:40:59 +08:00
committed by GitHub
parent 846bfd9459
commit d487af7462
2 changed files with 35 additions and 0 deletions

View File

@@ -102,6 +102,7 @@ func (o *ruleGroupOperator) listRuleGroups(ctx context.Context, namespace string
// copy status info of statusRuleGroups to matched rulegroups
var groups = make([]runtime.Object, len(resourceRuleGroups))
for i := range resourceRuleGroups {
setRuleGroupResourceTypeMeta(resourceRuleGroups[i])
g := &kapialertingv2beta1.RuleGroup{
RuleGroup: *resourceRuleGroups[i],
Status: kapialertingv2beta1.RuleGroupStatus{
@@ -295,6 +296,8 @@ func (o *ruleGroupOperator) GetRuleGroup(ctx context.Context, namespace, name st
return nil, err
}
setRuleGroupResourceTypeMeta(resourceRuleGroup)
ret := &kapialertingv2beta1.RuleGroup{
RuleGroup: *resourceRuleGroup,
Status: kapialertingv2beta1.RuleGroupStatus{
@@ -384,6 +387,7 @@ func (o *ruleGroupOperator) listClusterRuleGroups(ctx context.Context, selector
// copy status info of statusRuleGroups to matched rulegroups
var groups = make([]runtime.Object, len(resourceRuleGroups))
for i := range resourceRuleGroups {
setRuleGroupResourceTypeMeta(resourceRuleGroups[i])
g := &kapialertingv2beta1.ClusterRuleGroup{
ClusterRuleGroup: *resourceRuleGroups[i],
Status: kapialertingv2beta1.RuleGroupStatus{
@@ -488,6 +492,8 @@ func (o *ruleGroupOperator) GetClusterRuleGroup(ctx context.Context, name string
return nil, err
}
setRuleGroupResourceTypeMeta(resourceRuleGroup)
ret := &kapialertingv2beta1.ClusterRuleGroup{
ClusterRuleGroup: *resourceRuleGroup,
Status: kapialertingv2beta1.RuleGroupStatus{
@@ -573,6 +579,7 @@ func (o *ruleGroupOperator) listGlobalRuleGroups(ctx context.Context, selector l
// copy status info of statusRuleGroups to matched rulegroups
var groups = make([]runtime.Object, len(resourceRuleGroups))
for i := range resourceRuleGroups {
setRuleGroupResourceTypeMeta(resourceRuleGroups[i])
g := &kapialertingv2beta1.GlobalRuleGroup{
GlobalRuleGroup: *resourceRuleGroups[i],
Status: kapialertingv2beta1.RuleGroupStatus{
@@ -731,6 +738,8 @@ func (o *ruleGroupOperator) GetGlobalRuleGroup(ctx context.Context, name string)
return nil, err
}
setRuleGroupResourceTypeMeta(resourceRuleGroup)
ret := &kapialertingv2beta1.GlobalRuleGroup{
GlobalRuleGroup: *resourceRuleGroup,
Status: kapialertingv2beta1.RuleGroupStatus{
@@ -889,3 +898,23 @@ type wrapAlert struct {
kapialertingv2beta1.Alert
runtime.Object
}
var (
apiVersion = alertingv2beta1.SchemeGroupVersion.String()
)
// set TypeMeta info because the objects getted by lister.List() and lister.Get() may have no TypeMeta
// related issue: https://github.com/kubernetes/client-go/issues/541
func setRuleGroupResourceTypeMeta(obj runtime.Object) {
switch o := obj.(type) {
case *alertingv2beta1.RuleGroup:
o.APIVersion = apiVersion
o.Kind = alertingv2beta1.ResourceKindRuleGroup
case *alertingv2beta1.ClusterRuleGroup:
o.APIVersion = apiVersion
o.Kind = alertingv2beta1.ResourceKindClusterRuleGroup
case *alertingv2beta1.GlobalRuleGroup:
o.APIVersion = apiVersion
o.Kind = alertingv2beta1.ResourceKindGlobalRuleGroup
}
}

View File

@@ -25,6 +25,12 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
)
const (
ResourceKindRuleGroup = "RuleGroup"
ResourceKindClusterRuleGroup = "ClusterRuleGroup"
ResourceKindGlobalRuleGroup = "GlobalRuleGroup"
)
// Duration is a valid time unit
// Supported units: y, w, d, h, m, s, ms Examples: `30s`, `1m`, `1h20m15s`
// +kubebuilder:validation:Pattern:="^(0|(([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?)$"