Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: alert config #220

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions pkg/cmd/alert/add_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,13 @@ var (

type baseOptions struct {
genericiooptions.IOStreams
alertConfigMap *corev1.ConfigMap
webhookConfigMap *corev1.ConfigMap
client kubernetes.Interface
Factory cmdutil.Factory
alertConfigMap *corev1.ConfigMap
webhookConfigMap *corev1.ConfigMap
client kubernetes.Interface
Factory cmdutil.Factory
AlertConfigMapKey apitypes.NamespacedName
AlertConfigFileName string
NoAdapter bool
}

type AddReceiverOptions struct {
Expand Down Expand Up @@ -165,8 +168,17 @@ func (o *baseOptions) complete() error {
return err
}

if len(o.AlertConfigFileName) == 0 {
o.AlertConfigFileName = alertConfigFileName
}
if len(o.AlertConfigMapKey.Name) == 0 {
o.AlertConfigMapKey.Name = alertConfigmapName
}
if len(o.AlertConfigMapKey.Namespace) == 0 {
o.AlertConfigMapKey.Namespace = namespace
}
// get alertmanager configmap
o.alertConfigMap, err = o.client.CoreV1().ConfigMaps(namespace).Get(ctx, alertConfigmapName, metav1.GetOptions{})
o.alertConfigMap, err = o.client.CoreV1().ConfigMaps(o.AlertConfigMapKey.Namespace).Get(ctx, o.AlertConfigMapKey.Name, metav1.GetOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -228,7 +240,7 @@ func (o *AddReceiverOptions) checkEmails() error {
}

errMsg := "SMTP %sis not configured, if you want to add email receiver, please use `kbcli alert config-smtpserver` configure it first"
data, err := getConfigData(o.alertConfigMap, alertConfigFileName)
data, err := getConfigData(o.alertConfigMap, o.AlertConfigFileName)
if err != nil {
return err
}
Expand Down Expand Up @@ -352,7 +364,7 @@ func (o *AddReceiverOptions) buildRoute() {

// addReceiver adds receiver to alertmanager config
func (o *AddReceiverOptions) addReceiver() error {
data, err := getConfigData(o.alertConfigMap, alertConfigFileName)
data, err := getConfigData(o.alertConfigMap, o.AlertConfigFileName)
if err != nil {
return err
}
Expand All @@ -372,7 +384,7 @@ func (o *AddReceiverOptions) addReceiver() error {
data["route"].(map[string]interface{})["routes"] = routes

// update alertmanager configmap
return updateConfig(o.client, o.alertConfigMap, alertConfigFileName, data)
return updateConfig(o.client, o.alertConfigMap, o.AlertConfigFileName, data)
}

func (o *AddReceiverOptions) addWebhookReceivers() error {
Expand Down Expand Up @@ -412,11 +424,12 @@ func (o *AddReceiverOptions) buildWebhook() ([]*webhookConfig, error) {
if valid, err := urlIsValid(v); !valid {
return nil, fmt.Errorf("invalid webhook url: %s, %v", v, err)
}
w.URL = getWebhookAdaptorURL(o.Name, o.webhookConfigMap.Namespace)
webhookType := getWebhookType(v)
if webhookType == unknownWebhookType {
return nil, fmt.Errorf("invalid webhook url: %s, failed to prase the webhook type", v)
if o.NoAdapter {
w.URL = v
} else {
w.URL = getWebhookAdaptorURL(o.Name, o.webhookConfigMap.Namespace)
}
webhookType := getWebhookType(v)
waReceiver.Type = string(webhookType)
waReceiver.Params.URL = v
case webhookToken:
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/alert/add_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ var _ = Describe("add receiver", func() {
baseOptions := mockBaseOptions(s)
o.alertConfigMap = baseOptions.alertConfigMap
o.InputName = []string{}
o.AlertConfigFileName = alertConfigFileName
Expect(o.validate()).Should(Succeed())
})

Expand Down Expand Up @@ -164,6 +165,7 @@ var _ = Describe("add receiver", func() {
o.baseOptions.webhookConfigMap = webhookAdaptorCM
o.client = testing.FakeClientSet(alertCM, webhookAdaptorCM)
o.Name = "receiver-test"
o.AlertConfigFileName = alertConfigFileName
Expect(o.addReceiver()).Should(Succeed())
Expect(o.addWebhookReceivers()).Should(Succeed())
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/alert/config_smtpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (o *configSMTPServerOptions) validate() error {
}

func (o *configSMTPServerOptions) run() error {
data, err := getConfigData(o.alertConfigMap, alertConfigFileName)
data, err := getConfigData(o.alertConfigMap, o.AlertConfigFileName)
if err != nil {
return err
}
Expand All @@ -105,5 +105,5 @@ func (o *configSMTPServerOptions) run() error {
data["global"] = global

// update global config
return updateConfig(o.client, o.alertConfigMap, alertConfigFileName, data)
return updateConfig(o.client, o.alertConfigMap, o.AlertConfigFileName, data)
}
1 change: 1 addition & 0 deletions pkg/cmd/alert/config_smtpserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ var _ = Describe("config smtpserver", func() {
o.smtpAuthPassword = "123456abc"
o.smtpAuthIdentity = "[email protected]"
o.client = testing.FakeClientSet(o.alertConfigMap, o.webhookConfigMap)
o.AlertConfigFileName = alertConfigFileName
Expect(o.validate()).Should(Succeed())
Expect(o.run()).Should(Succeed())
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/alert/delete_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (o *deleteReceiverOptions) run() error {
}

func (o *deleteReceiverOptions) deleteReceiver() error {
data, err := getConfigData(o.alertConfigMap, alertConfigFileName)
data, err := getConfigData(o.alertConfigMap, o.AlertConfigFileName)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (o *deleteReceiverOptions) deleteReceiver() error {

data["receivers"] = newReceivers
data["route"].(map[string]interface{})["routes"] = newRoutes
return updateConfig(o.client, o.alertConfigMap, alertConfigFileName, data)
return updateConfig(o.client, o.alertConfigMap, o.AlertConfigFileName, data)
}

func (o *deleteReceiverOptions) deleteWebhookReceivers() error {
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/alert/delete_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var _ = Describe("alert", func() {
o := &deleteReceiverOptions{baseOptions: mockBaseOptions(s)}
o.client = testing.FakeClientSet(o.baseOptions.alertConfigMap, o.baseOptions.webhookConfigMap)
o.names = []string{"receiver-7pb52"}
o.AlertConfigFileName = alertConfigFileName
Expect(o.run()).Should(Succeed())
})
})
2 changes: 1 addition & 1 deletion pkg/cmd/alert/list_receivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func newListReceiversCmd(f cmdutil.Factory, streams genericiooptions.IOStreams)
}

func (o *listReceiversOptions) run() error {
data, err := getConfigData(o.alertConfigMap, alertConfigFileName)
data, err := getConfigData(o.alertConfigMap, o.AlertConfigFileName)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/alert/list_receivers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var _ = Describe("alert", func() {
It("run", func() {
o := &listReceiversOptions{baseOptions: mockBaseOptions(s)}
o.client = testing.FakeClientSet(o.baseOptions.alertConfigMap, o.baseOptions.webhookConfigMap)
o.AlertConfigFileName = alertConfigFileName
Expect(o.run()).Should(Succeed())
})
})
2 changes: 1 addition & 1 deletion pkg/cmd/alert/list_smtpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func newListSMTPServerCmd(f cmdutil.Factory, streams genericiooptions.IOStreams)
}

func (o *listSMTPServerOptions) run() error {
data, err := getConfigData(o.alertConfigMap, alertConfigFileName)
data, err := getConfigData(o.alertConfigMap, o.AlertConfigFileName)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/alert/list_smtpserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var _ = Describe("list smtpserver", func() {
It("run", func() {
o := &listSMTPServerOptions{baseOptions: mockBaseOptions(s)}
o.client = testing.FakeClientSet(o.baseOptions.alertConfigMap, o.baseOptions.webhookConfigMap)
o.AlertConfigFileName = alertConfigFileName
Expect(o.run()).Should(Succeed())
})
})