Skip to content

Commit

Permalink
Changed ServiceMonitor to use port instead of targetPort. Using "named"
Browse files Browse the repository at this point in the history
port from mg.Spec.Ports.
  • Loading branch information
laetho committed Aug 19, 2021
1 parent 9637142 commit bdf47f5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions pkg/modules/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/laetho/metagraf/internal/pkg/params"
"github.com/laetho/metagraf/pkg/metagraf"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
log "k8s.io/klog"
)

Expand All @@ -49,10 +48,8 @@ func GenServiceMonitor(mg *metagraf.MetaGraf) {

eps := []monitoringv1.Endpoint{}
ep := monitoringv1.Endpoint{
TargetPort: &intstr.IntOrString{
IntVal: FindServiceMonitorPort(mg),
},
Path: FindServiceMonitorPath(mg),
Port: GetServiceMonitorNamedPort(mg),
Path: GetServiceMonitorPath(mg),
Scheme: params.ServiceMonitorScheme,
Interval: params.ServiceMonitorInterval,
}
Expand Down Expand Up @@ -86,7 +83,7 @@ func GenServiceMonitor(mg *metagraf.MetaGraf) {

// Parses metaGraf specification to look for annotation to
// control scrape path for ServiceMonitor resource.
func FindServiceMonitorPath(mg *metagraf.MetaGraf) string {
func GetServiceMonitorPath(mg *metagraf.MetaGraf) string {
// mg cli value, return provided if not default.
if len(params.ServiceMonitorPath) > 0 && params.ServiceMonitorPath != params.ServiceMonitorPathDefault {
return params.ServiceMonitorPath
Expand Down Expand Up @@ -120,6 +117,16 @@ func FindServiceMonitorPort(mg *metagraf.MetaGraf) int32 {
return params.ServiceMonitorPortDefault
}

func GetServiceMonitorNamedPort(mg *metagraf.MetaGraf) string {
monitorPort := FindServiceMonitorPort(mg)
for specPortName, specPort := range mg.Spec.Ports {
if monitorPort == specPort {
return specPortName
}
}
return string(monitorPort)
}

func StoreServiceMonitor(obj monitoringv1.ServiceMonitor) {
client := k8sclient.GetMonitoringV1Client().ServiceMonitors(NameSpace)
res, _ := client.Get(context.TODO(), obj.Name, metav1.GetOptions{})
Expand Down

0 comments on commit bdf47f5

Please sign in to comment.