-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
97 lines (80 loc) · 2.87 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
// Package aws extracts AWS metadata from install configurations.
package aws
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/pkg/errors"
"github.com/bailey84j/terraform_installer/pkg/asset/installconfig"
"github.com/bailey84j/terraform_installer/pkg/types"
awstypes "github.com/bailey84j/terraform_installer/pkg/types/aws"
)
// Metadata converts an install configuration to AWS metadata.
func Metadata(clusterID, infraID string, config *types.InstallConfig) *awstypes.Metadata {
return &awstypes.Metadata{
Region: config.Platform.AWS.Region,
Identifier: []map[string]string{{
fmt.Sprintf("kubernetes.io/cluster/%s", infraID): "owned",
}, {
"openshiftClusterID": clusterID,
}},
//ServiceEndpoints: config.AWS.ServiceEndpoints,
ClusterDomain: config.ClusterDomain(),
}
}
// PreTerraform performs any infrastructure initialization which must
// happen before Terraform creates the remaining infrastructure.
func PreTerraform(ctx context.Context, clusterID string, installConfig *installconfig.InstallConfig) error {
if err := tagSharedVPCResources(ctx, clusterID, installConfig); err != nil {
return err
}
return nil
}
func tagSharedVPCResources(ctx context.Context, clusterID string, installConfig *installconfig.InstallConfig) error {
if len(installConfig.Config.Platform.AWS.Subnets) == 0 {
return nil
}
privateSubnets, err := installConfig.AWS.PrivateSubnets(ctx)
if err != nil {
return err
}
publicSubnets, err := installConfig.AWS.PublicSubnets(ctx)
if err != nil {
return err
}
ids := make([]*string, 0, len(privateSubnets)+len(publicSubnets))
for id := range privateSubnets {
ids = append(ids, aws.String(id))
}
for id := range publicSubnets {
ids = append(ids, aws.String(id))
}
session, err := installConfig.AWS.Session(ctx)
if err != nil {
return errors.Wrap(err, "could not create AWS session")
}
tagKey, tagValue := sharedTag(clusterID)
ec2Client := ec2.New(session, aws.NewConfig().WithRegion(installConfig.Config.Platform.AWS.Region))
if _, err = ec2Client.CreateTagsWithContext(ctx, &ec2.CreateTagsInput{
Resources: ids,
Tags: []*ec2.Tag{{Key: &tagKey, Value: &tagValue}},
}); err != nil {
return errors.Wrap(err, "could not add tags to subnets")
}
if zone := installConfig.Config.AWS.HostedZone; zone != "" {
route53Client := route53.New(session)
if _, err := route53Client.ChangeTagsForResourceWithContext(ctx, &route53.ChangeTagsForResourceInput{
ResourceType: aws.String("hostedzone"),
ResourceId: aws.String(zone),
AddTags: []*route53.Tag{{Key: &tagKey, Value: &tagValue}},
}); err != nil {
return errors.Wrap(err, "could not add tags to hosted zone")
}
}
return nil
}
func sharedTag(clusterID string) (string, string) {
return fmt.Sprintf("kubernetes.io/cluster/%s", clusterID), "shared"
}