github.com/in4it/ecs-deploy@v0.0.42-0.20240508120354-ed77ff16df25/provider/ecs/servicediscovery.go (about) 1 package ecs 2 3 import ( 4 "errors" 5 "strconv" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/aws/session" 10 "github.com/aws/aws-sdk-go/service/servicediscovery" 11 "github.com/in4it/ecs-deploy/util" 12 "github.com/juju/loggo" 13 ) 14 15 // logging 16 var serviceDiscoveryLogger = loggo.GetLogger("servicediscovery") 17 18 // ECR struct 19 type ServiceDiscovery struct { 20 } 21 22 func (s *ServiceDiscovery) getNamespaceArnAndId(name string) (string, string, error) { 23 var result string 24 var id string 25 svc := servicediscovery.New(session.New()) 26 input := &servicediscovery.ListNamespacesInput{} 27 pageNum := 0 28 err := svc.ListNamespacesPages(input, 29 func(page *servicediscovery.ListNamespacesOutput, lastPage bool) bool { 30 pageNum++ 31 for _, v := range page.Namespaces { 32 if aws.StringValue(v.Name) == name { 33 result = aws.StringValue(v.Arn) 34 id = aws.StringValue(v.Id) 35 } 36 } 37 return pageNum <= 100 38 }) 39 40 if err != nil { 41 if aerr, ok := err.(awserr.Error); ok { 42 ecsLogger.Errorf(aerr.Error()) 43 } else { 44 ecsLogger.Errorf(err.Error()) 45 } 46 } 47 if result == "" { 48 return result, id, errors.New("Namespace not found namespace=" + name) 49 } 50 return result, id, nil 51 } 52 func (s *ServiceDiscovery) getServiceArn(serviceName, namespaceID string) (string, error) { 53 var result string 54 svc := servicediscovery.New(session.New()) 55 input := &servicediscovery.ListServicesInput{ 56 Filters: []*servicediscovery.ServiceFilter{ 57 { 58 Name: aws.String("NAMESPACE_ID"), 59 Condition: aws.String("EQ"), 60 Values: aws.StringSlice([]string{namespaceID}), 61 }, 62 }, 63 } 64 pageNum := 0 65 err := svc.ListServicesPages(input, 66 func(page *servicediscovery.ListServicesOutput, lastPage bool) bool { 67 pageNum++ 68 for _, v := range page.Services { 69 if aws.StringValue(v.Name) == serviceName { 70 result = aws.StringValue(v.Arn) 71 } 72 } 73 return pageNum <= 100 74 }) 75 76 if err != nil { 77 if aerr, ok := err.(awserr.Error); ok { 78 ecsLogger.Errorf(aerr.Error()) 79 } else { 80 ecsLogger.Errorf(err.Error()) 81 } 82 } 83 if result == "" { 84 return result, errors.New("Service not found service=" + serviceName) 85 } 86 return result, nil 87 } 88 func (s *ServiceDiscovery) createService(serviceName, namespaceID string) (string, error) { 89 var ( 90 ttl int64 91 failureThreshold int64 92 err error 93 output string 94 ) 95 ttl, err = strconv.ParseInt(util.GetEnv("SERVICE_DISCOVERY_TTL", "60"), 10, 64) 96 if err != nil { 97 ttl = 60 98 } 99 failureThreshold, err = strconv.ParseInt(util.GetEnv("SERVICE_DISCOVERY_FAILURETHRESHOLD", "1"), 10, 64) 100 if err != nil { 101 failureThreshold = 1 102 } 103 svc := servicediscovery.New(session.New()) 104 input := &servicediscovery.CreateServiceInput{ 105 CreatorRequestId: aws.String(serviceName + "-" + util.RandStringBytesMaskImprSrc(8)), 106 Description: aws.String(serviceName), 107 Name: aws.String(serviceName), 108 NamespaceId: aws.String(namespaceID), 109 DnsConfig: &servicediscovery.DnsConfig{ 110 DnsRecords: []*servicediscovery.DnsRecord{ 111 { 112 TTL: aws.Int64(ttl), 113 Type: aws.String("SRV"), 114 }, 115 { 116 TTL: aws.Int64(ttl), 117 Type: aws.String("A"), 118 }, 119 }, 120 }, 121 HealthCheckCustomConfig: &servicediscovery.HealthCheckCustomConfig{ 122 FailureThreshold: aws.Int64(failureThreshold), 123 }, 124 } 125 result, err := svc.CreateService(input) 126 if err != nil { 127 if aerr, ok := err.(awserr.Error); ok { 128 ecsLogger.Errorf("%v", aerr.Error()) 129 } else { 130 ecsLogger.Errorf("%v", err.Error()) 131 } 132 return output, err 133 } 134 135 output = aws.StringValue(result.Service.Arn) 136 137 return output, nil 138 }