/
pool.go
183 lines (156 loc) · 4.93 KB
/
pool.go
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
package aao
import (
"context"
"fmt"
"os"
"sort"
"strconv"
awsv1alpha1 "github.com/openshift/aws-account-operator/api/v1alpha1"
"github.com/openshift/osdctl/pkg/printer"
"github.com/spf13/cobra"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// newCmdPool gets the current status of the AWS Account Operator AccountPool
func newCmdPool(client client.Client) *cobra.Command {
ops := newPoolOptions(client)
poolCmd := &cobra.Command{
Use: "pool",
Short: "Get the status of the AWS Account Operator AccountPool",
Args: cobra.NoArgs,
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(ops.complete(cmd))
cmdutil.CheckErr(ops.run())
},
}
return poolCmd
}
// poolOptions defines the struct for running the pool command
type poolOptions struct {
kubeCli client.Client
}
func newPoolOptions(client client.Client) *poolOptions {
return &poolOptions{
kubeCli: client,
}
}
func (o *poolOptions) complete(cmd *cobra.Command) error {
return nil
}
type legalEntityStats struct {
name string
id string
claimedCount int
unusedCount int
totalCount int
}
func (o *poolOptions) run() error {
ctx := context.TODO()
var accounts awsv1alpha1.AccountList
if err := o.kubeCli.List(ctx, &accounts, &client.ListOptions{
Namespace: "aws-account-operator",
}); err != nil {
return err
}
// mapping legalentityid to count
defaultMap := make(map[string]legalEntityStats)
fmMap := make(map[string]legalEntityStats)
availabilityCount := 0
for _, account := range accounts.Items {
if !account.Status.Claimed && account.Status.State == "Ready" && account.Spec.LegalEntity.ID == "" && !account.Spec.BYOC {
availabilityCount += 1
}
if account.Spec.LegalEntity.ID == "" || account.Spec.BYOC {
continue
}
if account.Spec.AccountPool == "fm-accountpool" { // non-default accountpool
handlePoolCounting(fmMap, account)
} else {
handlePoolCounting(defaultMap, account)
}
}
fmt.Printf("Available Accounts: %d\n", availabilityCount)
fmt.Println("========================================================================================================================")
fmt.Println("Default Account Pool")
fmt.Println("========================================================================================================================")
printSortedCount(getSortedCount(defaultMap, 10))
fmt.Println("========================================================================================================================")
fmt.Println("fm-accountpool")
fmt.Println("========================================================================================================================")
printSortedCount(getSortedCount(fmMap, 10))
return nil
}
func handlePoolCounting(myMap map[string]legalEntityStats, account awsv1alpha1.Account) {
key := fmt.Sprintf("%s %s", account.Spec.LegalEntity.ID, account.Spec.LegalEntity.Name)
if account.Status.Claimed {
if entry, ok := myMap[key]; ok {
entry.claimedCount += 1
myMap[key] = entry
} else {
myMap[key] = legalEntityStats{
name: account.Spec.LegalEntity.Name,
id: account.Spec.LegalEntity.ID,
claimedCount: 1,
}
}
} else {
if entry, ok := myMap[key]; ok {
entry.unusedCount += 1
myMap[key] = entry
} else {
myMap[key] = legalEntityStats{
name: account.Spec.LegalEntity.Name,
id: account.Spec.LegalEntity.ID,
unusedCount: 1,
}
}
}
}
func prepareLESMapToArray(input map[string]legalEntityStats) []legalEntityStats {
result := make([]legalEntityStats, 0)
for _, v := range input {
v.totalCount = v.claimedCount + v.unusedCount
result = append(result, v)
}
return result
}
func getSortedCount(inputMap map[string]legalEntityStats, maxLen int) []legalEntityStats {
inputCounts := prepareLESMapToArray(inputMap)
sort.Slice(inputCounts, func(i, j int) bool {
return inputCounts[i].totalCount > inputCounts[j].totalCount
})
limit := len(inputCounts)
if maxLen > limit {
return inputCounts[:limit]
}
return inputCounts[:maxLen]
}
func printSortedCount(lec []legalEntityStats) {
totalClaimed := 0
totalUnused := 0
totalTotal := 0
table := printer.NewTablePrinter(os.Stdout, 20, 1, 3, ' ')
table.AddRow([]string{"Claimed", "Unused", "Total", "ID", "Name"})
for i := range lec {
table.AddRow([]string{
strconv.Itoa(lec[i].claimedCount),
strconv.Itoa(lec[i].unusedCount),
strconv.Itoa(lec[i].totalCount),
lec[i].id, lec[i].name,
})
totalClaimed += lec[i].claimedCount
totalUnused += lec[i].unusedCount
totalTotal += lec[i].totalCount
}
// Add empty row for readability
table.AddRow([]string{})
err := table.Flush()
if err != nil {
fmt.Println("error while flushing table: ", err.Error())
}
fmt.Printf("Total Claimed: %d\n", totalClaimed)
fmt.Printf("Total Unused: %d\n", totalUnused)
fmt.Printf("Total Accounts: %d\n", totalTotal)
fmt.Println()
}