|
| 1 | +// Copyright 2015-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package servicediscovery |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + |
| 19 | + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/cloudformation" |
| 20 | + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags" |
| 21 | + "github.com/aws/amazon-ecs-cli/ecs-cli/modules/config" |
| 22 | + "github.com/aws/aws-sdk-go/aws" |
| 23 | + "github.com/aws/aws-sdk-go/service/ecs" |
| 24 | + "github.com/sirupsen/logrus" |
| 25 | + "github.com/urfave/cli" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + privateDNSNamespaceStackNameFormat = "amazon-ecs-cli-setup-%s-%s-private-dns-namespace" |
| 30 | + serviceDiscoveryServiceStackNameFormat = "amazon-ecs-cli-setup-%s-%s-service-discovery-service" |
| 31 | +) |
| 32 | + |
| 33 | +// CloudFormation template parameters |
| 34 | +const ( |
| 35 | + parameterKeyNamespaceDescription = "NamespaceDescription" |
| 36 | + parameterKeyVPCID = "VPCID" |
| 37 | + parameterKeyNamespaceName = "NamespaceName" |
| 38 | +) |
| 39 | + |
| 40 | +const ( |
| 41 | + parameterKeySDSDescription = "SDSDescription" |
| 42 | + parameterKeySDSName = "SDSName" |
| 43 | + parameterKeyNamespaceID = "NamespaceID" |
| 44 | + parameterKeyDNSType = "DNSType" |
| 45 | + parameterKeyDNSTTL = "DNSTTL" |
| 46 | + parameterKeyHealthCheckCustomConfigFailureThreshold = "FailureThreshold" |
| 47 | +) |
| 48 | + |
| 49 | +const ( |
| 50 | + CFNTemplateOutputPrivateNamespaceID = "PrivateDNSNamespaceID" |
| 51 | + CFNTemplateOutputSDSARN = "ServiceDiscoveryServiceARN" |
| 52 | +) |
| 53 | + |
| 54 | +const ( |
| 55 | + dnsRecordTypeA = "A" |
| 56 | + dnsRecordTypeSRV = "SRV" |
| 57 | +) |
| 58 | + |
| 59 | +var requiredParamsSDS = []string{parameterKeyNamespaceID, parameterKeySDSName, parameterKeyDNSType} |
| 60 | +var requiredParamsNamespace = []string{parameterKeyVPCID, parameterKeyNamespaceName} |
| 61 | + |
| 62 | +// Create creates resources for service discovery and returns the ID of the Service Discovery Service |
| 63 | +func Create(c *cli.Context, networkMode, serviceName, clusterName string) (*string, error) { |
| 64 | + rdwr, err := config.NewReadWriter() |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + commandConfig, err := config.NewCommandConfig(c, rdwr) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + cfnClient := cloudformation.NewCloudformationClient(commandConfig) |
| 75 | + |
| 76 | + return create(c, networkMode, serviceName, clusterName, cfnClient) |
| 77 | +} |
| 78 | + |
| 79 | +func create(c *cli.Context, networkMode, serviceName, clusterName string, cfnClient cloudformation.CloudformationClient) (*string, error) { |
| 80 | + // create namespace |
| 81 | + namespaceParams := namespaceCFNParams(c) |
| 82 | + if err := namespaceParams.Validate(); err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + namespaceStackName := cfnStackName(privateDNSNamespaceStackNameFormat, clusterName, serviceName) |
| 87 | + if _, err := cfnClient.CreateStack(cloudformation.GetPrivateNamespaceTemplate(), namespaceStackName, false, namespaceParams); err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + logrus.Info("Waiting for the private DNS namespace to be created...") |
| 92 | + // Wait for stack creation |
| 93 | + cfnClient.WaitUntilCreateComplete(namespaceStackName) |
| 94 | + |
| 95 | + // Get the ID of the namespace we just created |
| 96 | + namespaceID, err := getOutputIDFromStack(cfnClient, namespaceStackName, CFNTemplateOutputPrivateNamespaceID) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + // create SDS |
| 102 | + sdsParams := sdsCFNParams(aws.StringValue(namespaceID), serviceName, networkMode) |
| 103 | + if err := sdsParams.Validate(); err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + |
| 107 | + sdsStackName := cfnStackName(serviceDiscoveryServiceStackNameFormat, clusterName, serviceName) |
| 108 | + if _, err := cfnClient.CreateStack(cloudformation.GetSDSTemplate(), sdsStackName, false, sdsParams); err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + logrus.Info("Waiting for the Service Discovery Service to be created...") |
| 113 | + // Wait for stack creation |
| 114 | + cfnClient.WaitUntilCreateComplete(sdsStackName) |
| 115 | + |
| 116 | + // Return the ID of the SDS we just created |
| 117 | + return getOutputIDFromStack(cfnClient, sdsStackName, CFNTemplateOutputSDSARN) |
| 118 | +} |
| 119 | + |
| 120 | +func getOutputIDFromStack(cfnClient cloudformation.CloudformationClient, stackName, outputKey string) (*string, error) { |
| 121 | + response, err := cfnClient.DescribeStacks(stackName) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + if len(response.Stacks) == 0 { |
| 126 | + return nil, fmt.Errorf("Could not find CloudFormation stack: %s", stackName) |
| 127 | + } |
| 128 | + |
| 129 | + for _, output := range response.Stacks[0].Outputs { |
| 130 | + if aws.StringValue(output.OutputKey) == outputKey { |
| 131 | + return output.OutputValue, nil |
| 132 | + } |
| 133 | + } |
| 134 | + return nil, fmt.Errorf("Failed to find output %s in stack %s", outputKey, stackName) |
| 135 | + |
| 136 | +} |
| 137 | + |
| 138 | +func cfnStackName(stackName, cluster, service string) string { |
| 139 | + return fmt.Sprintf(stackName, cluster, service) |
| 140 | +} |
| 141 | + |
| 142 | +func sdsCFNParams(namespaceID, sdsName, networkMode string) *cloudformation.CfnStackParams { |
| 143 | + cfnParams := cloudformation.NewCfnStackParams(requiredParamsSDS) |
| 144 | + |
| 145 | + cfnParams.Add(parameterKeyNamespaceID, namespaceID) |
| 146 | + cfnParams.Add(parameterKeySDSName, sdsName) |
| 147 | + |
| 148 | + dnsType := dnsRecordTypeSRV |
| 149 | + if networkMode == ecs.NetworkModeAwsvpc { |
| 150 | + dnsType = dnsRecordTypeA |
| 151 | + } |
| 152 | + cfnParams.Add(parameterKeyDNSType, dnsType) |
| 153 | + |
| 154 | + return cfnParams |
| 155 | +} |
| 156 | + |
| 157 | +func namespaceCFNParams(context *cli.Context) *cloudformation.CfnStackParams { |
| 158 | + cfnParams := cloudformation.NewCfnStackParams(requiredParamsNamespace) |
| 159 | + |
| 160 | + namespaceName := context.String(flags.PrivateDNSNamespaceNameFlag) |
| 161 | + cfnParams.Add(parameterKeyNamespaceName, namespaceName) |
| 162 | + |
| 163 | + vpcID := context.String(flags.VpcIdFlag) |
| 164 | + cfnParams.Add(parameterKeyVPCID, vpcID) |
| 165 | + |
| 166 | + return cfnParams |
| 167 | +} |
0 commit comments