/
subnet.go
86 lines (76 loc) · 1.74 KB
/
subnet.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
package ec2
import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/awslabs/eksdemo/pkg/cmd"
"github.com/awslabs/eksdemo/pkg/manifest"
"github.com/awslabs/eksdemo/pkg/resource"
"github.com/awslabs/eksdemo/pkg/template"
)
type SubnetOptions struct {
resource.CommonOptions
CidrBlock string
VpcId string
}
func NewSubnetResource() *resource.Resource {
res := &resource.Resource{
Command: cmd.Command{
Name: "subnet",
Description: "EC2 Subnet",
Aliases: []string{"subnets"},
Args: []string{"NAME"},
},
Manager: &manifest.ResourceManager{
Template: &template.TextTemplate{
Template: subnetYamlTemplate,
},
},
}
options := &SubnetOptions{
CommonOptions: resource.CommonOptions{
Name: "ack-ec2-subnet",
Namespace: "default",
NamespaceFlag: true,
},
}
flags := cmd.Flags{
&cmd.StringFlag{
CommandFlag: cmd.CommandFlag{
Name: "cidr",
Description: "ipv4 network range for the VPC, in CIDR notation",
Required: true,
},
Option: &options.CidrBlock,
},
&cmd.StringFlag{
CommandFlag: cmd.CommandFlag{
Name: "vpc-id",
Description: "ip of the VPC to create the subnet (defaults to cluster VPC)",
},
Option: &options.VpcId,
},
}
res.Options = options
res.CreateFlags = flags
return res
}
func (o *SubnetOptions) PreCreate() error {
if o.VpcId == "" {
o.VpcId = aws.ToString(o.Cluster.ResourcesVpcConfig.VpcId)
}
return nil
}
const subnetYamlTemplate = `---
apiVersion: ec2.services.k8s.aws/v1alpha1
kind: Subnet
metadata:
name: {{ .Name }}
namespace: {{ .Namespace }}
spec:
cidrBlock: {{ .CidrBlock }}
tagSpecifications:
- resourceType: subnet
tags:
- key: Name
value: {{ .Name }}
vpcID: {{ .VpcId }}
`