-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
144 lines (129 loc) · 3.77 KB
/
utils.go
File metadata and controls
144 lines (129 loc) · 3.77 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
alibabacloudproviderv1alpha1 "github.com/cloudpilot-ai/lib/pkg/alibabacloud/karpenter-provider-alibabacloud/apis/v1alpha1"
alibabacloudcorev1 "github.com/cloudpilot-ai/lib/pkg/alibabacloud/karpenter/apis/v1"
"k8s.io/klog"
)
// deleteAll removes the exact listed NodeClasses and NodePools from the cluster.
// Uses foreground propagation to ensure cascading; adjust if your CRDs need background.
func deleteAll(c *Client) error {
nodepools, err := c.ListClusterRebalanceNodePools()
if err != nil {
return err
}
nodeclasses, err := c.ListClusterRebalanceNodeClasses()
if err != nil {
return err
}
// Delete NodePools
for i := range nodepools.ECSNodePools {
np := &nodepools.ECSNodePools[i]
klog.Infof("deleting nodepool: %s", np.Name)
if err := c.DeleteClusterRebalanceNodePool(np.Name); err != nil {
return fmt.Errorf("delete nodepool %q: %w", np.Name, err)
}
}
// Delete NodeClasses
for i := range nodeclasses.ECSNodeClasses {
nc := &nodeclasses.ECSNodeClasses[i]
klog.Infof("deleting nodeclass: %s", nc.Name)
if err := c.DeleteClusterRebalanceNodeClass(nc.Name); err != nil {
return fmt.Errorf("delete nodeclass %q: %w", nc.Name, err)
}
}
return nil
}
func uploadAll(
c *Client,
nodeclasses []alibabacloudproviderv1alpha1.ECSNodeClass,
nodepools []alibabacloudcorev1.NodePool,
) error {
// Upload NodeClasses
for i := range nodeclasses {
nc := &nodeclasses[i]
klog.Infof("uploading nodeclass: %s", nc.Name)
if err := c.ApplyNodeClass(RebalanceNodeClass{
ECSNodeClass: &ECSNodeClass{
Name: nc.Name,
NodeClassSpec: &nc.Spec,
},
}); err != nil {
return fmt.Errorf("apply nodeclass %q: %w", nc.Name, err)
}
}
// Upload NodePools
for i := range nodepools {
np := &nodepools[i]
klog.Infof("uploading nodepool: %s", np.Name)
if err := c.ApplyNodePool(RebalanceNodePool{
ECSNodePool: &ECSNodePool{
Name: np.Name,
Enable: true,
NodePoolSpec: &np.Spec,
},
}); err != nil {
return fmt.Errorf("apply nodepool %q: %w", np.Name, err)
}
}
return nil
}
// ---- Preview table & helpers ----
func printPreviewTables(
nodepools []alibabacloudcorev1.NodePool,
nodeclasses []alibabacloudproviderv1alpha1.ECSNodeClass,
) {
// Stable sort
sort.Slice(nodepools, func(i, j int) bool { return nodepools[i].Name < nodepools[j].Name })
sort.Slice(nodeclasses, func(i, j int) bool { return nodeclasses[i].Name < nodeclasses[j].Name })
// NodePools
fmt.Println("\n=== NodePools Preview ===")
npw := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintln(npw, "NAME\tSPEC (truncated)")
for _, np := range nodepools {
fmt.Fprintf(npw, "%s\t%s\n",
np.Name,
trim(compactJSON(np.Spec), 120),
)
}
npw.Flush()
// NodeClasses
fmt.Println("\n=== ECSNodeClasses Preview ===")
ncw := tabwriter.NewWriter(os.Stdout, 0, 8, 2, ' ', 0)
fmt.Fprintln(ncw, "NAME\tSPEC (truncated)")
for _, nc := range nodeclasses {
fmt.Fprintf(ncw, "%s\t%s\n",
nc.Name,
trim(compactJSON(nc.Spec), 120),
)
}
ncw.Flush()
fmt.Printf("\nSummary: %d NodePool(s), %d NodeClass(es)\n", len(nodepools), len(nodeclasses))
}
func compactJSON(v any) string {
b, err := json.Marshal(v)
if err != nil {
return fmt.Sprintf("<marshal error: %v>", err)
}
return string(b)
}
func trim(s string, max int) string {
if max <= 3 || len(s) <= max {
return s
}
return s[:max-3] + "..."
}
// requireExactInput prompts and returns true only if the exact expected (case-insensitive) token is entered.
func requireExactInput(prompt, expected string) bool {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
line, _ := reader.ReadString('\n')
resp := strings.ToLower(strings.TrimSpace(line))
return resp == strings.ToLower(expected)
}