github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/alert/config_smtpserver.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 25 "github.com/spf13/cobra" 26 "k8s.io/cli-runtime/pkg/genericiooptions" 27 cmdutil "k8s.io/kubectl/pkg/cmd/util" 28 ) 29 30 var ( 31 configSMTPServerExample = ` 32 # Set smtp server config 33 kbcli alert config-smtpserver --smtp-from alert-test@apecloud.com --smtp-smarthost smtp.feishu.cn:587 --smtp-auth-username alert-test@apecloud.com --smtp-auth-password 123456abc --smtp-auth-identity alert-test@apecloud.com 34 ` 35 ) 36 37 type configSMTPServerOptions struct { 38 smtpFrom string 39 smtpSmarthost string 40 smtpAuthUsername string 41 smtpAuthPassword string 42 smtpAuthIdentity string 43 44 baseOptions 45 } 46 47 func newConfigSMTPServerCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command { 48 o := &configSMTPServerOptions{baseOptions: baseOptions{IOStreams: streams}} 49 cmd := &cobra.Command{ 50 Use: "config-smtpserver", 51 Short: "Set smtp server config", 52 Example: configSMTPServerExample, 53 Run: func(cmd *cobra.Command, args []string) { 54 cmdutil.CheckErr(o.complete(f)) 55 cmdutil.CheckErr(o.validate()) 56 cmdutil.CheckErr(o.run()) 57 }, 58 } 59 60 cmd.Flags().StringVar(&o.smtpFrom, "smtp-from", "", "The email address to send alert.") 61 cmd.Flags().StringVar(&o.smtpSmarthost, "smtp-smarthost", "", "The smtp host to send alert.") 62 cmd.Flags().StringVar(&o.smtpAuthUsername, "smtp-auth-username", "", "The username to authenticate to the smarthost.") 63 cmd.Flags().StringVar(&o.smtpAuthPassword, "smtp-auth-password", "", "The password to authenticate to the smarthost.") 64 cmd.Flags().StringVar(&o.smtpAuthIdentity, "smtp-auth-identity", "", "The identity to authenticate to the smarthost.") 65 66 return cmd 67 } 68 69 func (o *configSMTPServerOptions) validate() error { 70 if !validEmail(o.smtpFrom) { 71 return fmt.Errorf("smtp-from is invalid") 72 } 73 74 if o.smtpSmarthost == "" { 75 return fmt.Errorf("smtp-smarthost is required") 76 } 77 78 if o.smtpAuthUsername == "" { 79 return fmt.Errorf("smtp-auth-username is required") 80 } 81 82 if o.smtpAuthPassword == "" { 83 return fmt.Errorf("smtp-auth-password is required") 84 } 85 86 return nil 87 } 88 89 func (o *configSMTPServerOptions) run() error { 90 data, err := getConfigData(o.alertConfigMap, alertConfigFileName) 91 if err != nil { 92 return err 93 } 94 95 // get global 96 global := getGlobalFromData(data) 97 98 // write smtp server config 99 global["smtp_from"] = o.smtpFrom 100 global["smtp_smarthost"] = o.smtpSmarthost 101 global["smtp_auth_username"] = o.smtpAuthUsername 102 global["smtp_auth_password"] = o.smtpAuthPassword 103 global["smtp_auth_identity"] = o.smtpAuthIdentity 104 105 data["global"] = global 106 107 // update global config 108 return updateConfig(o.client, o.alertConfigMap, alertConfigFileName, data) 109 }