github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/cluster_commands.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 engines 21 22 import ( 23 "fmt" 24 "sort" 25 "strings" 26 27 corev1 "k8s.io/api/core/v1" 28 29 "github.com/1aal/kubeblocks/pkg/lorry/engines/models" 30 ) 31 32 const ( 33 HOST = "host" 34 PORT = "port" 35 USER = "user" 36 PASSWORD = "password" 37 COMMAND = "command" 38 ) 39 40 type NewCommandFunc func() ClusterCommands 41 42 var ( 43 EnvVarMap = map[string]string{ 44 HOST: "KB_HOST", 45 PORT: "KB_PORT", 46 USER: "KB_USER", 47 PASSWORD: "KB_PASSWD", 48 } 49 50 NewCommandFuncs = map[string]NewCommandFunc{} 51 ) 52 53 // AuthInfo is the authentication information for the database 54 type AuthInfo struct { 55 UserName string 56 UserPasswd string 57 } 58 59 type ClusterCommands interface { 60 ConnectCommand(info *AuthInfo) []string 61 Container() string 62 ConnectExample(info *ConnectionInfo, client string) string 63 ExecuteCommand([]string) ([]string, []corev1.EnvVar, error) 64 } 65 66 type EngineInfo struct { 67 Client string 68 Container string 69 PasswordEnv string 70 UserEnv string 71 Database string 72 } 73 74 func newClusterCommands(typeName string) (ClusterCommands, error) { 75 newFunc, ok := NewCommandFuncs[typeName] 76 if !ok { 77 return nil, fmt.Errorf("unsupported engine type: %s", typeName) 78 } 79 80 return newFunc(), nil 81 } 82 83 type ConnectionInfo struct { 84 Host string 85 User string 86 Password string 87 Database string 88 Port string 89 ClusterName string 90 ComponentName string 91 HeadlessEndpoint string 92 } 93 94 type BuildConnectExample func(info *ConnectionInfo) string 95 96 func BuildExample(info *ConnectionInfo, client string, examples map[models.ClientType]BuildConnectExample) string { 97 // if client is not specified, output all examples 98 if len(client) == 0 { 99 var keys = make([]string, len(examples)) 100 var i = 0 101 for k := range examples { 102 keys[i] = k.String() 103 i++ 104 } 105 sort.Strings(keys) 106 107 var b strings.Builder 108 for _, k := range keys { 109 buildFn := examples[models.ClientType(k)] 110 b.WriteString(fmt.Sprintf("========= %s connection example =========\n", k)) 111 b.WriteString(buildFn(info)) 112 b.WriteString("\n") 113 } 114 return b.String() 115 } 116 117 // return specified example 118 if buildFn, ok := examples[models.ClientType(client)]; ok { 119 return buildFn(info) 120 } 121 122 return "" 123 }