-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcluster.go
More file actions
243 lines (203 loc) · 7.76 KB
/
cluster.go
File metadata and controls
243 lines (203 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (c) 2024 Parseable, Inc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"fmt"
"log"
"os"
"pb/pkg/common"
"pb/pkg/helm"
"pb/pkg/installer"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
var verbose bool
var InstallOssCmd = &cobra.Command{
Use: "install",
Short: "Deploy Parseable",
Example: "pb cluster install",
Run: func(cmd *cobra.Command, _ []string) {
// Add verbose flag
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose logging")
installer.Installer(verbose)
},
}
// ListOssCmd lists the Parseable OSS servers
var ListOssCmd = &cobra.Command{
Use: "list",
Short: "List available Parseable servers",
Example: "pb list",
Run: func(_ *cobra.Command, _ []string) {
_, err := common.PromptK8sContext()
if err != nil {
log.Fatalf("Failed to prompt for kubernetes context: %v", err)
}
k8sClient, err := common.CreateK8sClient()
if err != nil {
log.Fatalf("Error creating k8s client %v", err)
}
// Read the installer data from the ConfigMap
entries, err := common.ReadInstallerConfigMap(k8sClient)
if err != nil {
log.Fatalf("Failed to list servers: %v", err)
}
// Check if there are no entries
if len(entries) == 0 {
fmt.Println("No clusters found.")
return
}
// Display the entries in a table format
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Namespace", "Version", "Status"})
for _, entry := range entries {
table.Append([]string{entry.Name, entry.Namespace, entry.Version, entry.Status})
}
table.Render()
},
}
// ShowValuesCmd lists the Parseable OSS servers
var ShowValuesCmd = &cobra.Command{
Use: "show values",
Short: "Show values available in Parseable servers",
Example: "pb show values",
Run: func(_ *cobra.Command, _ []string) {
_, err := common.PromptK8sContext()
if err != nil {
log.Fatalf("Failed to prompt for Kubernetes context: %v", err)
}
k8sClient, err := common.CreateK8sClient()
if err != nil {
log.Fatalf("Error creating k8s client %v", err)
}
// Read the installer data from the ConfigMap
entries, err := common.ReadInstallerConfigMap(k8sClient)
if err != nil {
log.Fatalf("Failed to list OSS servers: %v", err)
}
// Check if there are no entries
if len(entries) == 0 {
fmt.Println("No OSS servers found.")
return
}
// Prompt user to select a cluster
selectedCluster, err := common.PromptClusterSelection(entries)
if err != nil {
log.Fatalf("Failed to select a cluster: %v", err)
}
values, err := helm.GetReleaseValues(selectedCluster.Name, selectedCluster.Namespace)
if err != nil {
log.Fatalf("Failed to get values for release: %v", err)
}
// Marshal values to YAML for nice formatting
yamlOutput, err := yaml.Marshal(values)
if err != nil {
log.Fatalf("Failed to marshal values to YAML: %v", err)
}
// Print the YAML output
fmt.Println(string(yamlOutput))
// Print instructions for fetching secret values
fmt.Printf("\nTo get secret values of the Parseable cluster, run the following command:\n")
fmt.Printf("kubectl get secret -n %s parseable-env-secret -o jsonpath='{.data}' | jq -r 'to_entries[] | \"\\(.key): \\(.value | @base64d)\"'\n", selectedCluster.Namespace)
},
}
// UninstallOssCmd removes Parseable OSS servers
var UninstallOssCmd = &cobra.Command{
Use: "uninstall",
Short: "Uninstall Parseable servers",
Example: "pb uninstall",
Run: func(_ *cobra.Command, _ []string) {
_, err := common.PromptK8sContext()
if err != nil {
log.Fatalf("Failed to prompt for Kubernetes context: %v", err)
}
k8sClient, err := common.CreateK8sClient()
if err != nil {
log.Fatalf("Error creating k8s client %v", err)
}
// Read the installer data from the ConfigMap
entries, err := common.ReadInstallerConfigMap(k8sClient)
if err != nil {
log.Fatalf("Failed to fetch OSS servers: %v", err)
}
// Check if there are no entries
if len(entries) == 0 {
fmt.Println(common.Yellow + "\nNo Parseable OSS servers found to uninstall.")
return
}
// Prompt user to select a cluster
selectedCluster, err := common.PromptClusterSelection(entries)
if err != nil {
log.Fatalf("Failed to select a cluster: %v", err)
}
// Display a warning banner
fmt.Println("\n────────────────────────────────────────────────────────────────────────────")
fmt.Println("⚠️ Deleting this cluster will not delete any data on object storage.")
fmt.Println(" This operation will clean up the Parseable deployment on Kubernetes.")
fmt.Println("────────────────────────────────────────────────────────────────────────────")
// Confirm uninstallation
fmt.Printf("\nYou have selected to uninstall the cluster '%s' in namespace '%s'.\n", selectedCluster.Name, selectedCluster.Namespace)
if !common.PromptConfirmation(fmt.Sprintf("Do you want to proceed with uninstalling '%s'?", selectedCluster.Name)) {
fmt.Println(common.Yellow + "Uninstall operation canceled.")
return
}
//Perform uninstallation
if err := uninstallCluster(selectedCluster); err != nil {
log.Fatalf("Failed to uninstall cluster: %v", err)
}
// Remove entry from ConfigMap
if err := common.RemoveInstallerEntry(selectedCluster.Name, k8sClient); err != nil {
log.Fatalf("Failed to remove entry from ConfigMap: %v", err)
}
// Delete secret
if err := deleteSecret(selectedCluster.Namespace, "parseable-env-secret", k8sClient); err != nil {
log.Printf("Warning: Failed to delete secret 'parseable-env-secret': %v", err)
} else {
fmt.Println(common.Green + "Secret 'parseable-env-secret' deleted successfully." + common.Reset)
}
fmt.Println(common.Green + "Uninstallation completed successfully." + common.Reset)
},
}
func uninstallCluster(entry common.InstallerEntry) error {
helmApp := helm.Helm{
ReleaseName: entry.Name,
Namespace: entry.Namespace,
RepoName: "parseable",
RepoURL: "https://charts.parseable.com",
ChartName: "parseable",
Version: entry.Version,
}
fmt.Println(common.Yellow + "Starting uninstallation process..." + common.Reset)
spinner := common.CreateDeploymentSpinner(fmt.Sprintf("Uninstalling Parseable OSS '%s'...", entry.Name))
spinner.Start()
_, err := helm.Uninstall(helmApp, false)
spinner.Stop()
if err != nil {
return fmt.Errorf("failed to uninstall Parseable OSS: %v", err)
}
fmt.Printf(common.Green+"Successfully uninstalled '%s' from namespace '%s'.\n"+common.Reset, entry.Name, entry.Namespace)
return nil
}
func deleteSecret(namespace, secretName string, k8sClient *kubernetes.Clientset) error {
// Delete the secrets
err := k8sClient.CoreV1().Secrets(namespace).Delete(context.TODO(), "parseable-env-secret", metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete secret '%s': %v", secretName, err)
}
return nil
}