Skip to content

Commit

Permalink
[CHORE] adding analyze service monitor
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Takashi <nicolas.tcs@hotmail.com>
  • Loading branch information
nicolastakashi committed Jul 9, 2024
1 parent 49a19ac commit db37534
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 82 deletions.
88 changes: 88 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2024 The prometheus-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"strings"

"github.com/prometheus-operator/poctl/internal/analyzers"
"github.com/prometheus-operator/poctl/internal/k8sutil"
"github.com/spf13/cobra"
)

type AnalyzeKind string

const (
ServiceMonitor AnalyzeKind = "servicemonitor"
)

type AnalyzeFlags struct {
Kind string
Name string
Namespace string
}

var (
analyzerFlags = AnalyzeFlags{}
)

// analyzeCmd represents the analyze command

Check failure on line 42 in cmd/analyze.go

View workflow job for this annotation

GitHub Actions / Golang linter

Comment should end in a period (godot)
var analyzeCmd = &cobra.Command{
Use: "analyze",
Short: "Analyzes the given object and runs a set of rules on it to determine if it is compliant with the given rules.",
Long: `Analyzes the given object and runs a set of rules on it to determine if it is compliant with the given rules.
For example:
- Analyze if the service monitor is selecting any service or using the correct service port.`,
RunE: run,
}

func run(cmd *cobra.Command, args []string) error {

Check failure on line 52 in cmd/analyze.go

View workflow job for this annotation

GitHub Actions / Golang linter

unused-parameter: parameter 'args' seems to be unused, consider removing or renaming it as _ (revive)
if analyzerFlags.Kind == "" {
return fmt.Errorf("kind is required")
}

if analyzerFlags.Name == "" {
return fmt.Errorf("name is required")
}

if analyzerFlags.Namespace == "" {
return fmt.Errorf("namespace is required")
}

kClient, mClient, err := k8sutil.GetClientSets(kubeconfig)
if err != nil {
return fmt.Errorf("error while getting clientsets: %v", err)
}

clientSets := k8sutil.ClientSets{
KClient: kClient,
MClient: mClient,
}

switch AnalyzeKind(strings.ToLower(analyzerFlags.Kind)) {
case ServiceMonitor:
return analyzers.RunServiceMonitorAnalyzer(cmd.Context(), clientSets, analyzerFlags.Name, analyzerFlags.Namespace)
default:
return fmt.Errorf("kind %s not supported", analyzerFlags.Kind)
}
}

func init() {
rootCmd.AddCommand(analyzeCmd)
analyzeCmd.PersistentFlags().StringVarP(&analyzerFlags.Kind, "kind", "k", "", "The kind of object to analyze. For example, ServiceMonitor")
analyzeCmd.PersistentFlags().StringVarP(&analyzerFlags.Name, "name", "n", "", "The name of the object to analyze")
analyzeCmd.PersistentFlags().StringVarP(&analyzerFlags.Namespace, "namespace", "s", "", "The namespace of the object to analyze")
}
3 changes: 2 additions & 1 deletion cmd/servicemonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
applyConfigMetav1 "k8s.io/client-go/applyconfigurations/meta/v1"

"k8s.io/client-go/kubernetes"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -93,7 +94,7 @@ func createFromService(
Labels: service.Labels,
},
Spec: &monitoringv1.ServiceMonitorSpecApplyConfiguration{
Selector: &metav1.LabelSelector{
Selector: &applyConfigMetav1.LabelSelectorApplyConfiguration{
MatchLabels: service.Spec.Selector,
},
},
Expand Down
40 changes: 22 additions & 18 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ module github.com/prometheus-operator/poctl
go 1.22.0

require (
k8s.io/apiextensions-apiserver v0.30.1
k8s.io/client-go v0.30.1
github.com/stretchr/testify v1.9.0
k8s.io/apiextensions-apiserver v0.30.2
k8s.io/client-go v0.30.2
)

require (
github.com/evanphx/json-patch v5.9.0+incompatible // indirect
github.com/google/go-querystring v1.1.0 // indirect
sigs.k8s.io/controller-runtime v0.18.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
sigs.k8s.io/controller-runtime v0.18.4 // indirect
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
Expand All @@ -31,24 +35,24 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.74.0
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.75.1
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.19.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/term v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240322212309-b815d8309940 // indirect
k8s.io/utils v0.0.0-20240310230437-4693a0247e57
k8s.io/api v0.30.2
k8s.io/apimachinery v0.30.2
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240620174524-b456828f718b // indirect
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
Expand All @@ -57,5 +61,5 @@ require (
require (
github.com/google/go-github/v62 v62.0.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/prometheus-operator/prometheus-operator/pkg/client v0.74.0
github.com/prometheus-operator/prometheus-operator/pkg/client v0.75.1
)
Loading

0 comments on commit db37534

Please sign in to comment.