-
Notifications
You must be signed in to change notification settings - Fork 7
/
aws.go
124 lines (103 loc) · 3.25 KB
/
aws.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
package provider
import (
"context"
"io"
"text/template"
"github.com/giantswarm/apiextensions/v3/pkg/annotation"
"github.com/giantswarm/k8sclient/v5/pkg/k8sclient"
"github.com/giantswarm/microerror"
"sigs.k8s.io/yaml"
"github.com/giantswarm/kubectl-gs/cmd/template/cluster/provider/templates/aws"
"github.com/giantswarm/kubectl-gs/internal/key"
)
func WriteAWSTemplate(ctx context.Context, client k8sclient.Interface, out io.Writer, config ClusterCRsConfig) error {
var err error
isCapiVersion, err := key.IsCAPIVersion(config.ReleaseVersion)
if err != nil {
return microerror.Mask(err)
}
if isCapiVersion {
if config.EKS {
err = WriteCAPAEKSTemplate(ctx, client, out, config)
if err != nil {
return microerror.Mask(err)
}
} else {
err = WriteCAPATemplate(ctx, client, out, config)
if err != nil {
return microerror.Mask(err)
}
}
} else {
err = WriteGSAWSTemplate(out, config)
if err != nil {
return microerror.Mask(err)
}
}
return nil
}
func WriteGSAWSTemplate(out io.Writer, config ClusterCRsConfig) error {
var err error
crsConfig := aws.ClusterCRsConfig{
ClusterID: config.Name,
ExternalSNAT: config.ExternalSNAT,
MasterAZ: config.ControlPlaneAZ,
Description: config.Description,
PodsCIDR: config.PodsCIDR,
Owner: config.Organization,
ReleaseVersion: config.ReleaseVersion,
Labels: config.Labels,
}
crs, err := aws.NewClusterCRs(crsConfig)
if err != nil {
return microerror.Mask(err)
}
if config.ControlPlaneSubnet != "" {
crs.AWSCluster.Annotations[annotation.AWSSubnetSize] = config.ControlPlaneSubnet
}
if key.IsOrgNamespaceVersion(config.ReleaseVersion) {
crs = moveCRsToOrgNamespace(crs, config.Organization)
}
clusterCRYaml, err := yaml.Marshal(crs.Cluster)
if err != nil {
return microerror.Mask(err)
}
awsClusterCRYaml, err := yaml.Marshal(crs.AWSCluster)
if err != nil {
return microerror.Mask(err)
}
g8sControlPlaneCRYaml, err := yaml.Marshal(crs.G8sControlPlane)
if err != nil {
return microerror.Mask(err)
}
awsControlPlaneCRYaml, err := yaml.Marshal(crs.AWSControlPlane)
if err != nil {
return microerror.Mask(err)
}
data := struct {
AWSClusterCR string
AWSControlPlaneCR string
ClusterCR string
G8sControlPlaneCR string
}{
AWSClusterCR: string(awsClusterCRYaml),
ClusterCR: string(clusterCRYaml),
G8sControlPlaneCR: string(g8sControlPlaneCRYaml),
AWSControlPlaneCR: string(awsControlPlaneCRYaml),
}
t := template.Must(template.New(config.FileName).Parse(key.ClusterAWSCRsTemplate))
err = t.Execute(out, data)
if err != nil {
return microerror.Mask(err)
}
return nil
}
func moveCRsToOrgNamespace(crs aws.ClusterCRs, organization string) aws.ClusterCRs {
crs.Cluster.SetNamespace(key.OrganizationNamespaceFromName(organization))
crs.Cluster.Spec.InfrastructureRef.Namespace = key.OrganizationNamespaceFromName(organization)
crs.AWSCluster.SetNamespace(key.OrganizationNamespaceFromName(organization))
crs.G8sControlPlane.SetNamespace(key.OrganizationNamespaceFromName(organization))
crs.G8sControlPlane.Spec.InfrastructureRef.Namespace = key.OrganizationNamespaceFromName(organization)
crs.AWSControlPlane.SetNamespace(key.OrganizationNamespaceFromName(organization))
return crs
}