-
Notifications
You must be signed in to change notification settings - Fork 20
/
limits.go
186 lines (155 loc) · 5.61 KB
/
limits.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
184
185
186
package cmd
import (
"fmt"
"os"
"strings"
"sync"
"github.com/exoscale/egoscale"
exov2 "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
const (
limitComputeInstances = "user_vm"
limitGPUs = "gpu"
limitSnapshots = "snapshot"
limitTemplates = "template"
limitIPAddresses = "public_elastic_ip"
limitPrivateNetworks = "network"
limitNLBs = "network_load_balancer"
limitIAMAPIKeys = "iam_key"
limitSOSBuckets = "bucket"
limitSKSClusters = "sks_cluster"
)
type LimitsItemOutput struct {
Resource string `json:"resource"`
Used int `json:"used"`
Max int `json:"max"`
}
type LimitsOutput []LimitsItemOutput
func (o *LimitsOutput) toJSON() { outputJSON(o) }
func (o *LimitsOutput) toText() { outputText(o) }
func (o *LimitsOutput) toTable() { outputTable(o) }
var limitsCmd = &cobra.Command{
Use: "limits",
Short: "Current account limits",
Long: fmt.Sprintf(`This command lists the safety limits currently enforced on your account.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&LimitsOutput{}), ", ")),
RunE: func(cmd *cobra.Command, args []string) error {
var curUsage sync.Map
// Global resources ///////////////////////////////////////////////
res, err := cs.RequestWithContext(gContext, &egoscale.ListAPIKeys{})
if err != nil {
return fmt.Errorf("unable to list IAM API keys: %s", err)
}
curUsage.Store(limitIAMAPIKeys, res.(*egoscale.ListAPIKeysResponse).Count)
res, err = cs.RequestWithContext(gContext, egoscale.ListBucketsUsage{})
if err != nil {
return fmt.Errorf("unable to list SOS buckets: %s", err)
}
curUsage.Store(limitSOSBuckets, res.(*egoscale.ListBucketsUsageResponse).Count)
// Zone-local resources /////////////////////////////////////////////
instanceTypes := make(map[string]*exov2.InstanceType) // For caching
err = forEachZone(allZones, func(zone string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zone))
instances, err := cs.ListInstances(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list Compute instances: %s", err)
}
curUsage.Store(limitGPUs, 0)
cur, _ := curUsage.LoadOrStore(limitComputeInstances, 0)
curUsage.Store(limitComputeInstances, cur.(int)+len(instances))
for _, instance := range instances {
instanceType, cached := instanceTypes[*instance.InstanceTypeID]
if !cached {
instanceType, err = cs.GetInstanceType(ctx, zone, *instance.InstanceTypeID)
if err != nil {
return fmt.Errorf(
"unable to retrieve Compute instance type %q: %s",
*instance.InstanceTypeID,
err)
}
instanceTypes[*instance.InstanceTypeID] = instanceType
}
if strings.HasSuffix(*instanceType.Family, "gpu") {
cur, _ = curUsage.Load(limitGPUs)
curUsage.Store(limitGPUs, cur.(int)+int(*instanceType.GPUs))
}
}
snapshots, err := cs.ListSnapshots(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list snapshots: %s", err)
}
cur, _ = curUsage.LoadOrStore(limitSnapshots, 0)
curUsage.Store(limitSnapshots, cur.(int)+len(snapshots))
templates, err := cs.ListTemplates(ctx, zone, "private", "")
if err != nil {
return fmt.Errorf("unable to list templates: %s", err)
}
cur, _ = curUsage.LoadOrStore(limitTemplates, 0)
curUsage.Store(limitTemplates, cur.(int)+len(templates))
elasticIPs, err := cs.ListElasticIPs(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list IP addresses: %s", err)
}
cur, _ = curUsage.LoadOrStore(limitIPAddresses, 0)
curUsage.Store(limitIPAddresses, cur.(int)+len(elasticIPs))
privateNetworks, err := cs.ListPrivateNetworks(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list Private Networks: %s", err)
}
cur, _ = curUsage.LoadOrStore(limitPrivateNetworks, 0)
curUsage.Store(limitPrivateNetworks, cur.(int)+len(privateNetworks))
nlbs, err := cs.ListNetworkLoadBalancers(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list Network Load Balancers: %s", err)
}
cur, _ = curUsage.LoadOrStore(limitNLBs, 0)
curUsage.Store(limitNLBs, cur.(int)+len(nlbs))
sksClusters, err := cs.ListSKSClusters(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list SKS clusters: %s", err)
}
cur, _ = curUsage.LoadOrStore(limitSKSClusters, 0)
curUsage.Store(limitSKSClusters, cur.(int)+len(sksClusters))
return nil
})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr,
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint
}
resourceLimitLabels := map[string]string{
limitComputeInstances: "Instances",
limitGPUs: "GPUs",
limitSnapshots: "Snapshots",
limitTemplates: "Templates",
limitIPAddresses: "IP addresses",
limitPrivateNetworks: "Private Networks",
limitNLBs: "Network Load Balancers",
limitIAMAPIKeys: "IAM API keys",
limitSOSBuckets: "SOS buckets",
limitSKSClusters: "SKS clusters",
}
out := LimitsOutput{}
limits, err := cs.ListWithContext(gContext, &egoscale.ResourceLimit{})
if err != nil {
return err
}
for _, key := range limits {
limit := key.(*egoscale.ResourceLimit)
cur, ok := curUsage.Load(limit.ResourceTypeName)
if ok {
out = append(out, LimitsItemOutput{
Resource: resourceLimitLabels[limit.ResourceTypeName],
Used: cur.(int),
Max: int(limit.Max),
})
}
}
return output(&out, nil)
},
}
func init() {
RootCmd.AddCommand(limitsCmd)
}