Skip to content

Commit

Permalink
fix: bcs-service-prometheus panic, issue TencentBlueKing#671
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperJim committed Nov 23, 2020
1 parent 0300b07 commit 98b4b72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 12 additions & 3 deletions bcs-mesos/pkg/apis/monitor/v1/servicemonitor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package v1

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
Expand All @@ -32,30 +33,38 @@ type ServiceMonitorSpec struct {
Selector LabelSelector `json:"selector,omitempty"`
}

// LabelSelector selector for service
type LabelSelector struct {
MatchLabels map[string]string `json:"matchLabels,omitempty"`
}

// Endpoint collecte enpoint path information
type Endpoint struct {
Port string `json:"port,omitempty"`
Path string `json:"path,omitempty"`
Interval string `json:"interval,omitempty"`
Params map[string][]string `json:"params,omitempty"`
}

// GetUuid key generator
func (s *ServiceMonitor) GetUuid() string {
return fmt.Sprintf("%s.%s", s.Namespace, s.Name)
}

func (s *ServiceMonitor) GetSelector() labels.Requirements {
// GetSelector get k8s selector implementation
func (s *ServiceMonitor) GetSelector() (labels.Requirements, error) {
rms := labels.Requirements{}
for k, v := range s.Spec.Selector.MatchLabels {
r, _ := labels.NewRequirement(k, selection.Equals, []string{v})
r, err := labels.NewRequirement(k, selection.Equals, []string{v})
if err != nil {
return nil, err
}
rms = append(rms, *r)
}
return rms
return rms, nil
}

// Match check selector match
func (s *ServiceMonitor) Match(labels map[string]string) bool {
for k, v := range s.Spec.Selector.MatchLabels {
val, ok := labels[k]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ func (disc *serviceMonitor) handlerServiceMonitorChanged(serviceM *apismonitorv1
blog.Infof("ServiceMonitor(%s) have endpoint(%s:%s)", serviceM.GetUuid(), endpoint.Port, endpoint.Path)
}
rms := labels.NewSelector()
for _, o := range serviceM.GetSelector() {
reqs, err := serviceM.GetSelector()
if err != nil {
blog.Errorf("ServiceMonitor(%s) selector definition err, %s. skip handling", serviceM.GetUuid(), err.Error())
return
}
for _, o := range reqs {
rms.Add(o)
}
endpoints, err := disc.endpointLister.BcsEndpoints(serviceM.Namespace).List(rms)
Expand Down

0 comments on commit 98b4b72

Please sign in to comment.