/
add_silence.go
140 lines (116 loc) · 3.96 KB
/
add_silence.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
package silence
import (
"fmt"
"log"
"strings"
"github.com/openshift/osdctl/cmd/common"
ocmutils "github.com/openshift/osdctl/pkg/utils"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
type addSilenceCmd struct {
clusterID string
alertID []string
duration string
comment string
all bool
}
func NewCmdAddSilence() *cobra.Command {
addSilenceCmd := &addSilenceCmd{}
cmd := &cobra.Command{
Use: "add <cluster-id> [--all --duration --comment | --alertname --duration --comment]",
Short: "Add new silence for alert",
Long: `add new silence for specfic or all alert with comment and duration of alert`,
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
addSilenceCmd.clusterID = args[0]
AddSilence(addSilenceCmd)
},
}
cmd.Flags().StringSliceVar(&addSilenceCmd.alertID, "alertname", []string{}, "alertname (comma-separated)")
cmd.Flags().StringVarP(&addSilenceCmd.comment, "comment", "c", "", "add comment about silence")
cmd.Flags().StringVarP(&addSilenceCmd.duration, "duration", "d", "15d", "add duration for silence") //default duration set to 15 days
cmd.Flags().BoolVarP(&addSilenceCmd.all, "all", "a", false, "add silences for all alert")
return cmd
}
func AddSilence(cmd *addSilenceCmd) {
clusterID := cmd.clusterID
alertID := cmd.alertID
comment := cmd.comment
duration := cmd.duration
all := cmd.all
username, clustername := GetUserAndClusterInfo(clusterID)
_, kubeconfig, clientset, err := common.GetKubeConfigAndClient(clusterID)
if err != nil {
log.Fatal(err)
}
if all {
AddAllSilence(clusterID, duration, comment, username, clustername, kubeconfig, clientset)
} else if len(alertID) > 0 {
AddAlertNameSilence(alertID, duration, comment, username, kubeconfig, clientset)
} else {
fmt.Println("No valid option specified. Use --all or --alertname.")
}
}
func AddAllSilence(clusterID, duration, comment, username, clustername string, kubeconfig *rest.Config, clientset *kubernetes.Clientset) {
addCmd := []string{
"amtool",
"silence",
"add",
"cluster=" + clusterID,
"--alertmanager.url=" + LocalHostUrl,
"--duration=" + duration,
"--comment=" + comment,
}
output, err := ExecInPod(kubeconfig, clientset, addCmd)
if err != nil {
fmt.Println(err)
}
formattedOutput := strings.Replace(output, "\n", " ", -1)
fmt.Printf("All alerts for cluster %s has been silenced with id \"%s\" for a duration of %s by user \"%s\" \n", clustername, formattedOutput, duration, username)
}
func AddAlertNameSilence(alertID []string, duration, comment, username string, kubeconfig *rest.Config, clientset *kubernetes.Clientset) {
for _, alertname := range alertID {
addCmd := []string{
"amtool",
"silence",
"add",
"alertname=" + alertname,
"--alertmanager.url=" + LocalHostUrl,
"--duration=" + duration,
"--comment=" + comment,
}
output, err := ExecInPod(kubeconfig, clientset, addCmd)
if err != nil {
fmt.Println(err)
}
formattedOutput := strings.Replace(output, "\n", " ", -1)
fmt.Printf("Alert %s has been silenced with id \"%s\" for duration of %s by user \"%s\" \n", alertname, formattedOutput, duration, username)
}
}
// Get User name and clustername
func GetUserAndClusterInfo(clusterid string) (string, string) {
connection, err := ocmutils.CreateConnection()
if err != nil {
fmt.Printf("Error %s in create connection.", err)
}
//defer connection.Close()
defer func() {
if cerr := connection.Close(); cerr != nil {
fmt.Println("Error closing connection:", cerr)
}
}()
cluster, err := ocmutils.GetCluster(connection, clusterid)
if err != nil {
fmt.Printf("Error %s in getting cluster.", err)
}
clustername := cluster.Name()
account, err := connection.AccountsMgmt().V1().CurrentAccount().Get().Send()
if err != nil {
fmt.Printf("Error %s in retreving the details of account.", err)
}
name, _ := account.Body().GetUsername()
return name, clustername
}