create_subdomain.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package porter_app
  2. import (
  3. "context"
  4. "github.com/porter-dev/porter/internal/integrations/dns"
  5. "github.com/porter-dev/porter/internal/kubernetes"
  6. "github.com/porter-dev/porter/internal/kubernetes/domain"
  7. "github.com/porter-dev/porter/internal/repository"
  8. "github.com/porter-dev/porter/internal/telemetry"
  9. )
  10. // CreatePorterSubdomainInput is the input to the CreatePorterSubdomain function
  11. type CreatePorterSubdomainInput struct {
  12. AppName string
  13. RootDomain string
  14. KubernetesAgent *kubernetes.Agent
  15. DNSClient *dns.Client
  16. DNSRecordRepository repository.DNSRecordRepository
  17. }
  18. // CreatePorterSubdomain creates a subdomain for the porter app
  19. func CreatePorterSubdomain(ctx context.Context, input CreatePorterSubdomainInput) (string, error) {
  20. ctx, span := telemetry.NewSpan(ctx, "create-porter-subdomain")
  21. defer span.End()
  22. var createdDomain string
  23. if input.KubernetesAgent == nil {
  24. return "", telemetry.Error(ctx, span, nil, "k8s agent is nil")
  25. }
  26. if input.DNSClient == nil {
  27. return "", telemetry.Error(ctx, span, nil, "powerdns client is nil")
  28. }
  29. if input.AppName == "" {
  30. return "", telemetry.Error(ctx, span, nil, "app name is empty")
  31. }
  32. if input.RootDomain == "" {
  33. return "", telemetry.Error(ctx, span, nil, "root domain is empty")
  34. }
  35. endpoint, found, err := domain.GetNGINXIngressServiceIP(input.KubernetesAgent.Clientset)
  36. if err != nil {
  37. return createdDomain, telemetry.Error(ctx, span, err, "error getting nginx ingress service ip")
  38. }
  39. if !found {
  40. return createdDomain, telemetry.Error(ctx, span, nil, "target cluster does not have nginx ingress")
  41. }
  42. createDomainConf := domain.CreateDNSRecordConfig{
  43. ReleaseName: input.AppName,
  44. RootDomain: input.RootDomain,
  45. Endpoint: endpoint,
  46. }
  47. record := createDomainConf.NewDNSRecordForEndpoint()
  48. record, err = input.DNSRecordRepository.CreateDNSRecord(record)
  49. if err != nil {
  50. return createdDomain, telemetry.Error(ctx, span, nil, "error creating dns record")
  51. }
  52. if record == nil {
  53. return createdDomain, telemetry.Error(ctx, span, nil, "dns record is nil")
  54. }
  55. _record := domain.DNSRecord(*record)
  56. err = _record.CreateDomain(input.DNSClient)
  57. if err != nil {
  58. return createdDomain, telemetry.Error(ctx, span, err, "error creating domain")
  59. }
  60. createdDomain = _record.Hostname
  61. return createdDomain, nil
  62. }