|
| 1 | +package clusterapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "google.golang.org/api/dns/v1" |
| 10 | + |
| 11 | + "github.com/openshift/installer/pkg/asset/installconfig" |
| 12 | + gcpic "github.com/openshift/installer/pkg/asset/installconfig/gcp" |
| 13 | + "github.com/openshift/installer/pkg/types" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + errNotFound = errors.New("not found") |
| 18 | +) |
| 19 | + |
| 20 | +func getDNSZoneName(ctx context.Context, ic *installconfig.InstallConfig, isPublic bool) (string, error) { |
| 21 | + ctx, cancel := context.WithTimeout(ctx, time.Minute*1) |
| 22 | + defer cancel() |
| 23 | + |
| 24 | + client, err := gcpic.NewClient(ctx) |
| 25 | + if err != nil { |
| 26 | + return "", fmt.Errorf("failed to create new client: %w", err) |
| 27 | + } |
| 28 | + |
| 29 | + cctx, ccancel := context.WithTimeout(ctx, time.Minute*1) |
| 30 | + defer ccancel() |
| 31 | + |
| 32 | + domain := ic.Config.ClusterDomain() |
| 33 | + if isPublic { |
| 34 | + domain = ic.Config.BaseDomain |
| 35 | + } |
| 36 | + |
| 37 | + zone, err := client.GetDNSZone(cctx, ic.Config.GCP.ProjectID, domain, isPublic) |
| 38 | + if err != nil { |
| 39 | + return "", fmt.Errorf("failed to get dns zone name: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + if zone != nil { |
| 43 | + return zone.Name, nil |
| 44 | + } |
| 45 | + |
| 46 | + return "", errNotFound |
| 47 | +} |
| 48 | + |
| 49 | +type recordSet struct { |
| 50 | + projectID string |
| 51 | + zoneName string |
| 52 | + record *dns.ResourceRecordSet |
| 53 | +} |
| 54 | + |
| 55 | +// createRecordSets will create a list of records that will be created during the install. |
| 56 | +func createRecordSets(ctx context.Context, ic *installconfig.InstallConfig, clusterID, apiIP, apiIntIP string) ([]recordSet, error) { |
| 57 | + ctx, cancel := context.WithTimeout(ctx, time.Minute*1) |
| 58 | + defer cancel() |
| 59 | + |
| 60 | + // A shared VPC install allows a user to preconfigure a private zone. If there is a private zone found, |
| 61 | + // use the existing private zone otherwise default to the installer private zone pattern below. |
| 62 | + privateZoneName, err := getDNSZoneName(ctx, ic, false) |
| 63 | + if err != nil { |
| 64 | + if !errors.Is(err, errNotFound) { |
| 65 | + return nil, fmt.Errorf("failed to find private zone: %w", err) |
| 66 | + } |
| 67 | + privateZoneName = fmt.Sprintf("%s-private-zone", clusterID) |
| 68 | + } |
| 69 | + |
| 70 | + records := []recordSet{ |
| 71 | + { |
| 72 | + // api_internal |
| 73 | + projectID: ic.Config.GCP.ProjectID, |
| 74 | + zoneName: privateZoneName, |
| 75 | + record: &dns.ResourceRecordSet{ |
| 76 | + Name: fmt.Sprintf("api-int.%s.", ic.Config.ClusterDomain()), |
| 77 | + Type: "A", |
| 78 | + Ttl: 60, |
| 79 | + Rrdatas: []string{apiIntIP}, |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + // api_external_internal_zone |
| 84 | + projectID: ic.Config.GCP.ProjectID, |
| 85 | + zoneName: privateZoneName, |
| 86 | + record: &dns.ResourceRecordSet{ |
| 87 | + Name: fmt.Sprintf("api.%s.", ic.Config.ClusterDomain()), |
| 88 | + Type: "A", |
| 89 | + Ttl: 60, |
| 90 | + Rrdatas: []string{apiIntIP}, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + if ic.Config.Publish == types.ExternalPublishingStrategy { |
| 96 | + existingPublicZoneName, err := getDNSZoneName(ctx, ic, true) |
| 97 | + if err != nil { |
| 98 | + return nil, fmt.Errorf("failed to find a public zone: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + apiRecord := recordSet{ |
| 102 | + projectID: ic.Config.GCP.ProjectID, |
| 103 | + zoneName: existingPublicZoneName, |
| 104 | + record: &dns.ResourceRecordSet{ |
| 105 | + Name: fmt.Sprintf("api.%s.", ic.Config.ClusterDomain()), |
| 106 | + Type: "A", |
| 107 | + Ttl: 60, |
| 108 | + Rrdatas: []string{apiIP}, |
| 109 | + }, |
| 110 | + } |
| 111 | + records = append(records, apiRecord) |
| 112 | + } |
| 113 | + |
| 114 | + return records, nil |
| 115 | +} |
| 116 | + |
| 117 | +// createDNSRecords will get the list of records to be created and execute their creation through the gcp dns api. |
| 118 | +func createDNSRecords(ctx context.Context, ic *installconfig.InstallConfig, clusterID, apiIP, apiIntIP string) error { |
| 119 | + // TODO: use the opts for the service to restrict scopes see google.golang.org/api/option.WithScopes |
| 120 | + dnsService, err := dns.NewService(ctx) |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("failed to create the gcp dns service: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + records, err := createRecordSets(ctx, ic, clusterID, apiIP, apiIntIP) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + // 1 minute timeout for each record |
| 131 | + ctx, cancel := context.WithTimeout(ctx, time.Minute*time.Duration(len(records))) |
| 132 | + defer cancel() |
| 133 | + |
| 134 | + for _, record := range records { |
| 135 | + if _, err := dnsService.ResourceRecordSets.Create(record.projectID, record.zoneName, record.record).Context(ctx).Do(); err != nil { |
| 136 | + return fmt.Errorf("failed to create record set %s: %w", record.record.Name, err) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +// createPrivateManagedZone will create a private managed zone in the GCP project specified in the install config. The |
| 144 | +// private managed zone should only be created when one is not specified in the install config. |
| 145 | +func createPrivateManagedZone(ctx context.Context, ic *installconfig.InstallConfig, clusterID, network string) error { |
| 146 | + // TODO: use the opts for the service to restrict scopes see google.golang.org/api/option.WithScopes |
| 147 | + dnsService, err := dns.NewService(ctx) |
| 148 | + if err != nil { |
| 149 | + return fmt.Errorf("failed to create the gcp dns service: %w", err) |
| 150 | + } |
| 151 | + |
| 152 | + managedZone := &dns.ManagedZone{ |
| 153 | + Name: fmt.Sprintf("%s-private-zone", clusterID), |
| 154 | + Description: resourceDescription, |
| 155 | + DnsName: fmt.Sprintf("%s.", ic.Config.ClusterDomain()), |
| 156 | + Visibility: "private", |
| 157 | + Labels: mergeLabels(ic, clusterID), |
| 158 | + PrivateVisibilityConfig: &dns.ManagedZonePrivateVisibilityConfig{ |
| 159 | + Networks: []*dns.ManagedZonePrivateVisibilityConfigNetwork{ |
| 160 | + { |
| 161 | + NetworkUrl: network, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + ctx, cancel := context.WithTimeout(ctx, time.Minute*1) |
| 168 | + defer cancel() |
| 169 | + |
| 170 | + if _, err = dnsService.ManagedZones.Create(ic.Config.GCP.ProjectID, managedZone).Context(ctx).Do(); err != nil { |
| 171 | + return fmt.Errorf("failed to create private managed zone: %w", err) |
| 172 | + } |
| 173 | + |
| 174 | + return nil |
| 175 | +} |
0 commit comments