github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/alert/types.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package alert
    21  
    22  import (
    23  	"fmt"
    24  	"strings"
    25  )
    26  
    27  // addon name
    28  const (
    29  	// alertManagerAddonName is the name of alertmanager addon
    30  	alertManagerAddonName = "prometheus"
    31  
    32  	// webhookAdaptorAddonName is the name of webhook adaptor addon
    33  	webhookAdaptorAddonName = "alertmanager-webhook-adaptor"
    34  )
    35  
    36  var (
    37  	addonCMSuffix = map[string]string{
    38  		alertManagerAddonName:   alertConfigMapNameSuffix,
    39  		webhookAdaptorAddonName: webhookAdaptorConfigMapNameSuffix,
    40  	}
    41  )
    42  
    43  // configmap name suffix
    44  const (
    45  	// alertConfigMapNameSuffix is the suffix of alertmanager configmap name
    46  	alertConfigMapNameSuffix = "alertmanager-config"
    47  
    48  	// webhookAdaptorConfigMapNameSuffix is the suffix of webhook adaptor configmap name
    49  	webhookAdaptorConfigMapNameSuffix = "config"
    50  )
    51  
    52  // config file name
    53  const (
    54  	// alertConfigFileName is the name of alertmanager config file
    55  	alertConfigFileName = "alertmanager.yml"
    56  
    57  	// webhookAdaptorFileName is the name of webhook adaptor config file
    58  	webhookAdaptorFileName = "config.yml"
    59  )
    60  
    61  const (
    62  	routeMatcherClusterKey  = "app_kubernetes_io_instance"
    63  	routeMatcherSeverityKey = "severity"
    64  	routeMatcherOperator    = "=~"
    65  )
    66  
    67  const (
    68  	routeMatcherClusterType  = "cluster"
    69  	routeMatcherSeverityType = "severity"
    70  )
    71  
    72  // severity is the severity level of alert
    73  type severity string
    74  
    75  const (
    76  	// severityCritical is the critical severity
    77  	severityCritical severity = "critical"
    78  	// severityWarning is the warning severity
    79  	severityWarning severity = "warning"
    80  	// severityInfo is the info severity
    81  	severityInfo severity = "info"
    82  )
    83  
    84  type webhookKey string
    85  
    86  // webhook keys
    87  const (
    88  	webhookURL   webhookKey = "url"
    89  	webhookToken webhookKey = "token"
    90  )
    91  
    92  type webhookType string
    93  
    94  const (
    95  	feishuWebhookType   webhookType = "feishu-webhook"
    96  	wechatWebhookType   webhookType = "wechat-webhook"
    97  	dingtalkWebhookType webhookType = "dingtalk-webhook"
    98  	unknownWebhookType  webhookType = "unknown"
    99  )
   100  
   101  type slackKey string
   102  
   103  // slackConfig keys
   104  const (
   105  	slackAPIURL    slackKey = "api_url"
   106  	slackChannel   slackKey = "channel"
   107  	slackUsername  slackKey = "username"
   108  	slackTitleLink slackKey = "title_link"
   109  )
   110  
   111  // emailConfig is the email config of receiver
   112  type emailConfig struct {
   113  	To string `json:"to"`
   114  }
   115  
   116  // webhookConfig is the webhook config of receiver
   117  type webhookConfig struct {
   118  	URL          string `json:"url"`
   119  	SendResolved bool   `json:"send_resolved"`
   120  	MaxAlerts    int    `json:"max_alerts,omitempty"`
   121  }
   122  
   123  // slackConfig is the alertmanager slack config of receiver
   124  // ref: https://prometheus.io/docs/alerting/latest/configuration/#slack_config
   125  type slackConfig struct {
   126  	APIURL    string `json:"api_url,omitempty"`
   127  	Channel   string `json:"channel,omitempty"`
   128  	Username  string `json:"username,omitempty"`
   129  	TitleLink string `json:"title_link"`
   130  }
   131  
   132  // receiver is the receiver of alert
   133  type receiver struct {
   134  	Name           string           `json:"name"`
   135  	EmailConfigs   []*emailConfig   `json:"email_configs,omitempty"`
   136  	SlackConfigs   []*slackConfig   `json:"slack_configs,omitempty"`
   137  	WebhookConfigs []*webhookConfig `json:"webhook_configs,omitempty"`
   138  }
   139  
   140  // route is the route of receiver
   141  type route struct {
   142  	Receiver string   `json:"receiver"`
   143  	Continue bool     `json:"continue,omitempty"`
   144  	Matchers []string `json:"matchers,omitempty"`
   145  }
   146  
   147  type webhookAdaptorReceiverParams struct {
   148  	URL    string `json:"url"`
   149  	Secret string `json:"secret,omitempty"`
   150  }
   151  
   152  type webhookAdaptorReceiver struct {
   153  	Name   string                       `json:"name"`
   154  	Type   string                       `json:"type"`
   155  	Params webhookAdaptorReceiverParams `json:"params"`
   156  }
   157  
   158  func (w *webhookConfig) string() string {
   159  	return fmt.Sprintf("url=%s", w.URL)
   160  }
   161  
   162  func (s *slackConfig) string() string {
   163  	var cfgs []string
   164  	if s.APIURL != "" {
   165  		cfgs = append(cfgs, fmt.Sprintf("api_url=%s", s.APIURL))
   166  	}
   167  	if s.Channel != "" {
   168  		cfgs = append(cfgs, fmt.Sprintf("channel=%s", s.Channel))
   169  	}
   170  	if s.Username != "" {
   171  		cfgs = append(cfgs, fmt.Sprintf("username=%s", s.Username))
   172  	}
   173  	return strings.Join(cfgs, ",")
   174  }
   175  
   176  func (e *emailConfig) string() string {
   177  	return e.To
   178  }