github.com/jenkins-x/jx/v2@v2.1.155/pkg/cloud/amazon/routes.go (about) 1 package amazon 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/jenkins-x/jx/v2/pkg/cloud/amazon/session" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/service/route53" 11 "github.com/jenkins-x/jx-logging/pkg/log" 12 "github.com/jenkins-x/jx/v2/pkg/util" 13 "k8s.io/apimachinery/pkg/util/uuid" 14 ) 15 16 // RegisterAwsCustomDomain registers a wildcard ALIAS for the custom domain 17 // to point at the given ELB host name 18 func RegisterAwsCustomDomain(customDomain string, elbAddress string) error { 19 sess, err := session.NewAwsSessionWithoutOptions() 20 if err != nil { 21 return err 22 } 23 svc := route53.New(sess) 24 25 // find the hosted zone for the domain name 26 var hostedZoneId *string 27 listZonesInput := &route53.ListHostedZonesInput{} 28 err = svc.ListHostedZonesPages(listZonesInput, func(page *route53.ListHostedZonesOutput, hasNext bool) bool { 29 if page != nil { 30 customDomainParts := strings.Split(customDomain, ".") 31 for _, r := range page.HostedZones { 32 strings.Split(customDomain, ".") 33 if r != nil && r.Name != nil && (*r.Name == customDomain || *r.Name == customDomain+"." || *r.Name == strings.Join(customDomainParts[1:], ".")+".") { 34 hostedZoneId = r.Id 35 return false 36 } 37 } 38 } 39 return true 40 }) 41 if err != nil { 42 return err 43 } 44 45 if hostedZoneId == nil { 46 // lets create the hosted zone! 47 callerRef := string(uuid.NewUUID()) 48 createInput := &route53.CreateHostedZoneInput{ 49 Name: aws.String(customDomain), 50 CallerReference: aws.String(callerRef), 51 } 52 results, err := svc.CreateHostedZone(createInput) 53 if err != nil { 54 return err 55 } 56 if results.HostedZone == nil { 57 return fmt.Errorf("No HostedZone created for name %s!", customDomain) 58 } 59 60 hostedZoneId = results.HostedZone.Id 61 if hostedZoneId == nil { 62 return fmt.Errorf("No HostedZone ID created for name %s!", customDomain) 63 } 64 } 65 66 upsert := route53.ChangeActionUpsert 67 ttl := int64(300) 68 recordType := "CNAME" 69 wildcard := "*." + customDomain 70 info := util.ColorInfo 71 log.Logger().Infof("About to insert/update DNS %s record into HostedZone %s with wildcard %s pointing to %s", info(recordType), info(*hostedZoneId), info(wildcard), info(elbAddress)) 72 73 changeInput := &route53.ChangeResourceRecordSetsInput{ 74 HostedZoneId: hostedZoneId, 75 ChangeBatch: &route53.ChangeBatch{ 76 Changes: []*route53.Change{ 77 { 78 Action: &upsert, 79 ResourceRecordSet: &route53.ResourceRecordSet{ 80 Name: aws.String(wildcard), 81 Type: aws.String(recordType), 82 TTL: &ttl, 83 ResourceRecords: []*route53.ResourceRecord{ 84 { 85 Value: aws.String(elbAddress), 86 }, 87 }, 88 }, 89 }, 90 }, 91 }, 92 } 93 _, err = svc.ChangeResourceRecordSets(changeInput) 94 if err != nil { 95 return fmt.Errorf("Failed to update record for hostedZoneID %s: %s", *hostedZoneId, err) 96 } 97 log.Logger().Infof("Updated HostZone ID %s successfully", info(*hostedZoneId)) 98 return nil 99 }