-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig.go
149 lines (132 loc) · 4.76 KB
/
config.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
package aws
import (
"fmt"
"io"
"os"
"github.com/virtual-kubelet/aws-fargate/fargate"
"github.com/virtual-kubelet/virtual-kubelet/providers"
"github.com/BurntSushi/toml"
"k8s.io/apimachinery/pkg/api/resource"
)
const (
// Provider configuration defaults.
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/platform_versions.html
defaultPlatformVersion = "LATEST"
defaultClusterName = "default"
defaultAssignPublicIPv4Address = false
defaultOperatingSystem = providers.OperatingSystemLinux
// Default resource capacity advertised by Fargate provider.
// These are intentionally low to prevent any accidental overuse.
defaultCPUCapacity = "20"
defaultMemoryCapacity = "40Gi"
defaultStorageCapacity = "40Gi"
defaultPodCapacity = "20"
// Minimum resource capacity advertised by Fargate provider.
// These values correspond to the minimum Fargate task size.
minCPUCapacity = "250m"
minMemoryCapacity = "512Mi"
minPodCapacity = "1"
)
// ProviderConfig represents the contents of the provider configuration file.
type providerConfig struct {
Region string
ClusterName string
Subnets []string
SecurityGroups []string
AssignPublicIPv4Address bool
ExecutionRoleArn string
CloudWatchLogGroupName string
PlatformVersion string
OperatingSystem string
CPU string
Memory string
Storage string
Pods string
}
// loadConfigFile loads the given Fargate provider configuration file.
func (p *FargateProvider) loadConfigFile(filePath string) error {
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()
err = p.loadConfig(f)
return err
}
// loadConfig loads the given Fargate provider TOML configuration stream.
func (p *FargateProvider) loadConfig(r io.Reader) error {
var config providerConfig
var q resource.Quantity
// Set defaults for optional fields.
config.ClusterName = defaultClusterName
config.AssignPublicIPv4Address = defaultAssignPublicIPv4Address
config.PlatformVersion = defaultPlatformVersion
config.OperatingSystem = defaultOperatingSystem
config.CPU = defaultCPUCapacity
config.Memory = defaultMemoryCapacity
config.Storage = defaultStorageCapacity
config.Pods = defaultPodCapacity
// Read the user-supplied configuration.
_, err := toml.DecodeReader(r, &config)
if err != nil {
return err
}
// Validate aggregate configuration.
if config.Region == "" {
return fmt.Errorf("Region is a required field")
}
if !fargate.FargateRegions.Include(config.Region) {
return fmt.Errorf(
"Fargate is available only in regions %v and not available in %v",
fargate.FargateRegions.Names(), config.Region)
}
if config.Subnets == nil || len(config.Subnets) == 0 {
return fmt.Errorf("Subnets is a required field")
}
if config.SecurityGroups == nil {
config.SecurityGroups = []string{}
}
if config.OperatingSystem != providers.OperatingSystemLinux {
return fmt.Errorf("Fargate does not support operating system %v", config.OperatingSystem)
}
if config.CloudWatchLogGroupName != "" && config.ExecutionRoleArn == "" {
return fmt.Errorf("Execution role required if CloudWatch log group is specified")
}
// Validate advertised capacity.
if q, err = resource.ParseQuantity(config.CPU); err != nil {
return fmt.Errorf("Invalid CPU value %v", config.CPU)
}
if q.Cmp(resource.MustParse(minCPUCapacity)) == -1 {
return fmt.Errorf("CPU value %v is less than the minimum %v", config.CPU, minCPUCapacity)
}
if q, err = resource.ParseQuantity(config.Memory); err != nil {
return fmt.Errorf("Invalid memory value %v", config.Memory)
}
if q.Cmp(resource.MustParse(minMemoryCapacity)) == -1 {
return fmt.Errorf("Memory value %v is less than the minimum %v", config.Memory, minMemoryCapacity)
}
if q, err = resource.ParseQuantity(config.Storage); err != nil {
return fmt.Errorf("Invalid storage value %v", config.Storage)
}
if q, err = resource.ParseQuantity(config.Pods); err != nil {
return fmt.Errorf("Invalid pods value %v", config.Pods)
}
if q.Cmp(resource.MustParse(minPodCapacity)) == -1 {
return fmt.Errorf("Pod value %v is less than the minimum %v", config.Pods, minPodCapacity)
}
// Populate provider fields.
p.region = config.Region
p.subnets = config.Subnets
p.securityGroups = config.SecurityGroups
p.clusterName = config.ClusterName
p.assignPublicIPv4Address = config.AssignPublicIPv4Address
p.executionRoleArn = config.ExecutionRoleArn
p.cloudWatchLogGroupName = config.CloudWatchLogGroupName
p.platformVersion = config.PlatformVersion
p.operatingSystem = config.OperatingSystem
p.capacity.cpu = config.CPU
p.capacity.memory = config.Memory
p.capacity.storage = config.Storage
p.capacity.pods = config.Pods
return nil
}