-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dangling service monitor check (#547)
Co-authored-by: abrad3 <[email protected]>
- Loading branch information
Showing
15 changed files
with
541 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: "dangling-servicemonitor" | ||
description: "Indicates when a service monitor's selectors don't match any service. ServiceMonitors are a custom resource only used by the Prometheus operator (https://prometheus-operator.dev/docs/operator/design/#servicemonitor)." | ||
remediation: "Check selectors and your services." | ||
scope: | ||
objectKinds: | ||
- ServiceMonitor | ||
template: "dangling-servicemonitor" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mocks | ||
|
||
import ( | ||
"testing" | ||
|
||
k8sMonitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
"github.com/stretchr/testify/require" | ||
"golang.stackrox.io/kube-linter/pkg/objectkinds" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// AddMockServiceMonitor adds a mock ServiceMonitor to LintContext | ||
func (l *MockLintContext) AddMockServiceMonitor(t *testing.T, name string) { | ||
require.NotEmpty(t, name) | ||
l.objects[name] = &k8sMonitoring.ServiceMonitor{ | ||
TypeMeta: metaV1.TypeMeta{ | ||
Kind: objectkinds.ServiceMonitor, | ||
APIVersion: objectkinds.GetServiceMonitorAPIVersion(), | ||
}, | ||
ObjectMeta: metaV1.ObjectMeta{Name: name}, | ||
Spec: k8sMonitoring.ServiceMonitorSpec{}, | ||
} | ||
} | ||
|
||
// ModifyServiceMonitor modifies a given servicemonitor in the context via the passed function | ||
func (l *MockLintContext) ModifyServiceMonitor(t *testing.T, name string, f func(servicemonitor *k8sMonitoring.ServiceMonitor)) { | ||
r, ok := l.objects[name].(*k8sMonitoring.ServiceMonitor) | ||
require.True(t, ok) | ||
f(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package objectkinds | ||
|
||
import ( | ||
k8sMonitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
) | ||
|
||
const ( | ||
// ServiceMonitor represents Prometheus Service Monitor objects. | ||
ServiceMonitor = k8sMonitoring.ServiceMonitorsKind | ||
) | ||
|
||
var ( | ||
serviceMonitorGVK = k8sMonitoring.SchemeGroupVersion.WithKind(ServiceMonitor) | ||
) | ||
|
||
func init() { | ||
RegisterObjectKind(ServiceMonitor, MatcherFunc(func(gvk schema.GroupVersionKind) bool { | ||
return gvk == serviceMonitorGVK | ||
})) | ||
} | ||
|
||
// GetServiceMonitorAPIVersion returns servicemonitor's apiversion | ||
func GetServiceMonitorAPIVersion() string { | ||
return serviceMonitorGVK.GroupVersion().String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
pkg/templates/danglingservicemonitor/internal/params/gen-params.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
pkg/templates/danglingservicemonitor/internal/params/params.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package params | ||
|
||
// Params represents the params accepted by this template. | ||
type Params struct { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package danglingservicemonitor | ||
|
||
import ( | ||
"fmt" | ||
|
||
k8sMonitoring "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" | ||
"golang.stackrox.io/kube-linter/pkg/check" | ||
"golang.stackrox.io/kube-linter/pkg/config" | ||
"golang.stackrox.io/kube-linter/pkg/diagnostic" | ||
"golang.stackrox.io/kube-linter/pkg/lintcontext" | ||
"golang.stackrox.io/kube-linter/pkg/objectkinds" | ||
"golang.stackrox.io/kube-linter/pkg/templates" | ||
"golang.stackrox.io/kube-linter/pkg/templates/danglingservicemonitor/internal/params" | ||
v1 "k8s.io/api/core/v1" | ||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/labels" | ||
) | ||
|
||
func init() { | ||
templates.Register(check.Template{ | ||
HumanName: "Dangling Service Monitor", | ||
Key: "dangling-servicemonitor", | ||
Description: "Flag service monitors which do not match any service", | ||
SupportedObjectKinds: config.ObjectKindsDesc{ | ||
ObjectKinds: []string{objectkinds.ServiceMonitor}, | ||
}, | ||
Parameters: params.ParamDescs, | ||
ParseAndValidateParams: params.ParseAndValidate, | ||
Instantiate: params.WrapInstantiateFunc(func(p params.Params) (check.Func, error) { | ||
return func(lintCtx lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic { | ||
serviceMonitor, ok := object.K8sObject.(*k8sMonitoring.ServiceMonitor) | ||
if !ok { | ||
return nil | ||
} | ||
nsSelector := serviceMonitor.Spec.NamespaceSelector | ||
nsSelectorSet := len(nsSelector.MatchNames) != 0 || nsSelector.Any | ||
|
||
labelSelectors := serviceMonitor.Spec.Selector.MatchLabels | ||
labelSelectorSet := len(labelSelectors) != 0 | ||
if !labelSelectorSet && !nsSelectorSet { | ||
return []diagnostic.Diagnostic{{ | ||
Message: "service monitor has no selector specified", | ||
}} | ||
} | ||
labelSelector, err := metaV1.LabelSelectorAsSelector(&metaV1.LabelSelector{MatchLabels: serviceMonitor.Spec.Selector.MatchLabels}) | ||
if err != nil { | ||
return []diagnostic.Diagnostic{{ | ||
Message: fmt.Sprintf("service monitor has invalid label selector: %v", err), | ||
}} | ||
} | ||
for _, obj := range lintCtx.Objects() { | ||
services, found := obj.K8sObject.(*v1.Service) | ||
if !found { | ||
continue | ||
} | ||
if checkNamespaceSelector(nsSelector, services) { | ||
if !labelSelectorSet { | ||
return nil | ||
} | ||
if labelSelectorSet && labelSelector.Matches(labels.Set(services.Labels)) { | ||
return nil | ||
} else { | ||
continue | ||
} | ||
} | ||
if labelSelector.Matches(labels.Set(services.Labels)) && labelSelectorSet && !nsSelectorSet { | ||
// Found! | ||
return nil | ||
} | ||
|
||
} | ||
return []diagnostic.Diagnostic{{Message: fmt.Sprintf("no services found matching the service monitor's label selector (%s) and namespace selector (%s)", labelSelector, nsSelector.MatchNames)}} | ||
}, nil | ||
}), | ||
}) | ||
} | ||
|
||
func checkNamespaceSelector(namespaceSelector k8sMonitoring.NamespaceSelector, service *v1.Service) bool { | ||
if namespaceSelector.Any { | ||
return true | ||
} | ||
for _, ns := range namespaceSelector.MatchNames { | ||
if ns == service.Namespace { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.