This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
/
metrics_disable.go
142 lines (118 loc) · 3.8 KB
/
metrics_disable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"context"
"fmt"
"io"
"strings"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"github.com/openservicemesh/osm/pkg/constants"
)
const metricsDisableDescription = `
This command will disable metrics scraping on all pods belonging to the given
namespace or set of namespaces.
`
type metricsDisableCmd struct {
out io.Writer
namespaces []string
clientSet kubernetes.Interface
}
func newMetricsDisable(out io.Writer) *cobra.Command {
disableCmd := &metricsDisableCmd{
out: out,
}
cmd := &cobra.Command{
Use: "disable ...",
Short: "disable metrics",
Long: metricsDisableDescription,
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, args []string) error {
config, err := settings.RESTClientGetter().ToRESTConfig()
if err != nil {
return fmt.Errorf("Error fetching kubeconfig: %w", err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return fmt.Errorf("Could not access Kubernetes cluster, check kubeconfig: %w", err)
}
disableCmd.clientSet = clientset
return disableCmd.run()
},
}
f := cmd.Flags()
f.StringSliceVar(&disableCmd.namespaces, "namespace", []string{}, "One or more namespaces to disable metrics on")
return cmd
}
func (cmd *metricsDisableCmd) run() error {
// Add metrics annotation on namespaces
for _, ns := range cmd.namespaces {
ns = strings.TrimSpace(ns)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
namespace, err := cmd.clientSet.CoreV1().Namespaces().Get(ctx, ns, metav1.GetOptions{})
if err != nil {
return fmt.Errorf("Failed to retrieve namespace [%s]: %w", ns, err)
}
// Check if the namespace belongs to a mesh, if not return an error
monitored, err := isMonitoredNamespace(*namespace, getMeshNames(cmd.clientSet))
if err != nil {
return err
}
if !monitored {
return fmt.Errorf("Namespace [%s] does not belong to a mesh, missing annotation %q",
ns, constants.OSMKubeResourceMonitorAnnotation)
}
// Patch the namespace to remove the metrics annotation.
patch := fmt.Sprintf(`
{
"metadata": {
"annotations": {
"%s": null
}
}
}`, constants.MetricsAnnotation)
_, err = cmd.clientSet.CoreV1().Namespaces().Patch(ctx, ns, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}, "")
if err != nil {
return fmt.Errorf("Failed to disable metrics in namespace [%s]: %w", ns, err)
}
// Disable metrics on pods belonging to this namespace
if err := cmd.disableMetricsForPods(ns); err != nil {
return fmt.Errorf("Failed to disable metrics for existing pod in namespace [%s]: %w", ns, err)
}
fmt.Fprintf(cmd.out, "Metrics successfully disabled in namespace [%s]\n", ns)
}
return nil
}
// disableMetricsForPods disables metrics for existing pods in the given namespace
func (cmd *metricsDisableCmd) disableMetricsForPods(namespace string) error {
listOptions := metav1.ListOptions{
// Matches on pods which are already a part of the mesh, which contain the Envoy ID label
LabelSelector: constants.EnvoyUniqueIDLabelName,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
podList, err := cmd.clientSet.CoreV1().Pods(namespace).List(ctx, listOptions)
if err != nil {
return err
}
for _, pod := range podList.Items {
// Patch existing pods in this namespace to remove metrics annotation
patch := fmt.Sprintf(`
{
"metadata": {
"annotations": {
"%s": null,
"%s": null,
"%s": null
}
}
}`, constants.PrometheusScrapeAnnotation, constants.PrometheusPortAnnotation, constants.PrometheusPathAnnotation)
_, err = cmd.clientSet.CoreV1().Pods(namespace).Patch(ctx, pod.Name, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}, "")
if err != nil {
return err
}
}
return nil
}