github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/alert/util.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  	"net/url"
    25  	"strings"
    26  
    27  	"golang.org/x/exp/slices"
    28  	corev1 "k8s.io/api/core/v1"
    29  	"k8s.io/apimachinery/pkg/util/rand"
    30  	"k8s.io/apimachinery/pkg/util/yaml"
    31  
    32  	"github.com/1aal/kubeblocks/pkg/cli/util"
    33  )
    34  
    35  // strToMap parses string to map, string format is key1=value1,key2=value2
    36  func strToMap(set string) map[string]string {
    37  	m := make(map[string]string)
    38  	for _, s := range strings.Split(set, ",") {
    39  		pair := strings.Split(s, "=")
    40  		if len(pair) >= 2 {
    41  			m[pair[0]] = strings.Join(pair[1:], "=")
    42  		}
    43  	}
    44  	return m
    45  }
    46  
    47  func severities() []string {
    48  	return []string{string(severityCritical), string(severityWarning), string(severityInfo)}
    49  }
    50  
    51  func generateReceiverName() string {
    52  	return fmt.Sprintf("receiver-%s", rand.String(5))
    53  }
    54  
    55  func getConfigData(cm *corev1.ConfigMap, key string) (map[string]interface{}, error) {
    56  	dataStr, ok := cm.Data[key]
    57  	if !ok {
    58  		return nil, fmt.Errorf("configmap %s has no data named %s", cm.Name, key)
    59  	}
    60  
    61  	data := make(map[string]interface{})
    62  	if err := yaml.Unmarshal([]byte(dataStr), &data); err != nil {
    63  		return nil, err
    64  	}
    65  	return data, nil
    66  }
    67  
    68  func getReceiversFromData(data map[string]interface{}) []interface{} {
    69  	receivers, ok := data["receivers"]
    70  	if !ok || receivers == nil {
    71  		receivers = []interface{}{} // init receivers
    72  	}
    73  	return receivers.([]interface{})
    74  }
    75  
    76  func getRoutesFromData(data map[string]interface{}) []interface{} {
    77  	route, ok := data["route"]
    78  	if !ok || route == nil {
    79  		data["route"] = map[string]interface{}{"routes": []interface{}{}}
    80  	}
    81  	routes, ok := data["route"].(map[string]interface{})["routes"]
    82  	if !ok || routes == nil {
    83  		routes = []interface{}{}
    84  	}
    85  	return routes.([]interface{})
    86  }
    87  
    88  func getGlobalFromData(data map[string]interface{}) map[string]interface{} {
    89  	global, ok := data["global"]
    90  	if !ok || global == nil {
    91  		global = map[string]interface{}{}
    92  	}
    93  	return global.(map[string]interface{})
    94  }
    95  
    96  func getWebhookType(url string) webhookType {
    97  	if strings.Contains(url, "oapi.dingtalk.com") {
    98  		return dingtalkWebhookType
    99  	}
   100  	if strings.Contains(url, "qyapi.weixin.qq.com") {
   101  		return wechatWebhookType
   102  	}
   103  	if strings.Contains(url, "open.feishu.cn") {
   104  		return feishuWebhookType
   105  	}
   106  	return unknownWebhookType
   107  }
   108  
   109  func getWebhookAdaptorURL(name string, namespace string) string {
   110  	return fmt.Sprintf("http://%s.%s:5001/api/v1/notify/%s", util.BuildAddonReleaseName(webhookAdaptorAddonName), namespace, name)
   111  }
   112  
   113  func removeDuplicateStr(strArray []string) []string {
   114  	var result []string
   115  	for _, s := range strArray {
   116  		if !slices.Contains(result, s) {
   117  			result = append(result, s)
   118  		}
   119  	}
   120  	return result
   121  }
   122  
   123  func urlIsValid(urlStr string) (bool, error) {
   124  	_, err := url.ParseRequestURI(urlStr)
   125  	if err != nil {
   126  		return false, err
   127  	}
   128  	return true, nil
   129  }
   130  
   131  func getConfigMapName(addon string) string {
   132  	return fmt.Sprintf("%s-%s", util.BuildAddonReleaseName(addon), addonCMSuffix[addon])
   133  }
   134  
   135  func validEmail(email string) bool {
   136  	if email == "" {
   137  		return false
   138  	}
   139  	if !strings.Contains(email, "@") {
   140  		return false
   141  	}
   142  	return true
   143  }