Skip to content

Commit 3151b88

Browse files
Merge pull request #9922 from barbacbd/OCPBUGS-61111
OCPBUGS-61111: Allow user to BYO private zone without specifying name
2 parents 5f4e6cd + a9e8f2a commit 3151b88

File tree

5 files changed

+79
-77
lines changed

5 files changed

+79
-77
lines changed

pkg/asset/cluster/tfvars/tfvars.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,11 @@ func (t *TerraformVariables) Generate(ctx context.Context, parents asset.Parents
517517
}
518518

519519
// Set the private zone
520-
privateZoneName, _, err = manifests.GetGCPPrivateZoneName(ctx, client, installConfig, clusterID.InfraID)
520+
params, err := manifests.GetGCPPrivateZoneInfo(ctx, client, installConfig, clusterID.InfraID)
521521
if err != nil {
522522
return fmt.Errorf("failed to find gcp private dns zone: %w", err)
523523
}
524+
privateZoneName = params.Name
524525
}
525526

526527
ctx, cancel := context.WithTimeout(ctx, 60*time.Second)

pkg/asset/manifests/dns.go

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,12 @@ func (d *DNS) Generate(ctx context.Context, dependencies asset.Parents) error {
181181
// projects/{projectID}/managedZones/{zoneID}. This will allow
182182
// the installer to pass the project without a new field in the
183183
// DNSZone struct.
184-
dnsZoneProject, privateZoneID := GetPrivateDNSZoneAndProject(installConfig)
185-
if privateZoneID == "" {
186-
privateZoneID = GCPDefaultPrivateZoneID(clusterID.InfraID)
184+
params, err := GetGCPPrivateZoneInfo(ctx, client, installConfig, clusterID.InfraID)
185+
if err != nil {
186+
return fmt.Errorf("failed to get private zone info: %w", err)
187187
}
188-
privateZoneName := fmt.Sprintf("projects/%s/managedZones/%s", dnsZoneProject, privateZoneID)
188+
189+
privateZoneName := fmt.Sprintf("projects/%s/managedZones/%s", params.Project, params.Name)
189190
logrus.Infof("generating GCP Private DNS Zone %s", privateZoneName)
190191
config.Spec.PrivateZone = &configv1.DNSZone{ID: privateZoneName}
191192

@@ -252,69 +253,62 @@ func GCPNetworkName(project, network string) string {
252253
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/networks/%s", project, network)
253254
}
254255

255-
// GetPrivateDNSZoneAndProject gets the private dns zone name and project where the dns records should reside.
256-
func GetPrivateDNSZoneAndProject(installConfig *installconfig.InstallConfig) (string, string) {
257-
project := installConfig.Config.GCP.ProjectID
258-
zone := ""
259-
if installConfig.Config.GCP.Network == "" || installConfig.Config.GCP.NetworkProjectID == "" {
260-
return project, zone
261-
}
262-
263-
icdns := installConfig.Config.GCP.DNS
264-
if icdns != nil && icdns.PrivateZone != nil {
265-
if icdns.PrivateZone.ProjectID != "" {
266-
project = icdns.PrivateZone.ProjectID
267-
}
268-
zone = icdns.PrivateZone.Name
269-
}
270-
return project, zone
271-
}
272-
273256
// GCPDefaultPrivateZoneID returns the default name for a gcp private dns zone. This zone name will be used during
274257
// installations where the user has not provided a private zone name (xpn installs only), no
275258
// preexisting private dns zone is found (xpn installs only), and default installation cases.
276259
func GCPDefaultPrivateZoneID(clusterID string) string {
277260
return fmt.Sprintf("%s-private-zone", clusterID)
278261
}
279262

280-
// GetGCPPrivateZoneName attempts to find the name of the private zone for GCP installs. When a shared vpc install
263+
// GetGCPPrivateZoneInfo attempts to find the name of the private zone for GCP installs. When a shared vpc install
281264
// occurs, a precreated zone may be used. If a zone is found (in this instance), then the zone should be paired with
282265
// the network that is supplied through the install config (when applicable).
283-
func GetGCPPrivateZoneName(ctx context.Context, client *icgcp.Client, installConfig *installconfig.InstallConfig, clusterID string) (string, bool, error) {
284-
privateZoneID := GCPDefaultPrivateZoneID(clusterID)
285-
shouldCreateZone := true
266+
func GetGCPPrivateZoneInfo(ctx context.Context, client *icgcp.Client, installConfig *installconfig.InstallConfig, clusterID string) (gcptypes.DNSZoneParams, error) {
267+
params := gcptypes.DNSZoneParams{
268+
// Force set the private zone ID to an empty string to ensure
269+
// the search for DNS zones looks for ANY not a specific zone.
270+
// This is required, because the user may enter no zone information
271+
// but still wish to bring a private zone during xpn installs (in this
272+
// case it must exist in the `projectID`).
273+
Name: "",
274+
InstallerCreated: true,
275+
IsPublic: false,
276+
BaseDomain: installConfig.Config.ClusterDomain(),
277+
Project: installConfig.Config.GCP.ProjectID,
278+
}
286279

287-
if installConfig.Config.GCP.NetworkProjectID != "" {
288-
project, privateZoneName := GetPrivateDNSZoneAndProject(installConfig)
289-
if privateZoneName != "" {
280+
if installConfig.Config.GCP.NetworkProjectID != "" && installConfig.Config.GCP.Network != "" {
281+
icdns := installConfig.Config.GCP.DNS
282+
if icdns != nil && icdns.PrivateZone != nil {
283+
if icdns.PrivateZone.ProjectID != "" {
284+
params.Project = icdns.PrivateZone.ProjectID
285+
}
290286
// Override the default with the name provided. If this zone does not exist, then
291-
// this should still be returned.
292-
privateZoneID = privateZoneName
287+
// this should still be returned as valid.
288+
params.Name = icdns.PrivateZone.Name
293289
}
294290

295-
zone, err := client.GetDNSZoneFromParams(ctx, gcptypes.DNSZoneParams{
296-
Project: project,
297-
Name: privateZoneID,
298-
IsPublic: false,
299-
BaseDomain: installConfig.Config.ClusterDomain(),
300-
})
291+
zone, err := client.GetDNSZoneFromParams(ctx, params)
301292
if err != nil {
302293
// Currently, the only time that a private zone lookup will produce an error is if we
303294
// failed to find the dns zones. That should result in an error returned here too.
304-
return privateZoneID, true, fmt.Errorf("private dns zone %s does not exist or is invalid: %w", privateZoneID, err)
295+
return params, fmt.Errorf("private dns zone %s does not exist or is invalid: %w", params.Name, err)
305296
}
306297
if zone == nil {
307298
// CORS-4012: The user may specify a zone to be created if it does not exist.
308299
// Do not fail if the specified zone does not exist.
309-
return privateZoneID, true, nil
300+
if params.Name == "" {
301+
params.Name = GCPDefaultPrivateZoneID(clusterID)
302+
}
303+
return params, nil
310304
}
311305

312-
if installConfig.Config.GCP.Network != "" {
313-
privateZoneID = zone.Name
314-
shouldCreateZone = false
315-
}
306+
params.Name = zone.Name
307+
params.InstallerCreated = false
308+
} else {
309+
params.Name = GCPDefaultPrivateZoneID(clusterID)
316310
}
317-
return privateZoneID, shouldCreateZone, nil
311+
return params, nil
318312
}
319313

320314
// Files returns the files generated by the asset.

pkg/infrastructure/gcp/clusterapi/clusterapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
238238
}
239239

240240
// Create the public (optional) and private dns records
241-
if err := createDNSRecords(ctx, in.InstallConfig, in.InfraID, apiIPAddress, apiIntIPAddress); err != nil {
241+
if err := createDNSRecords(ctx, client, in.InstallConfig, in.InfraID, apiIPAddress, apiIntIPAddress); err != nil {
242242
return fmt.Errorf("failed to create DNS records: %w", err)
243243
}
244244
}

pkg/infrastructure/gcp/clusterapi/dns.go

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
errNotFound = errors.New("not found")
2020
)
2121

22-
func getDNSZoneName(ctx context.Context, ic *installconfig.InstallConfig, isPublic bool) (string, error) {
22+
func getDNSZoneName(ctx context.Context, ic *installconfig.InstallConfig, clusterID string, isPublic bool) (string, error) {
2323
ctx, cancel := context.WithTimeout(ctx, time.Minute*1)
2424
defer cancel()
2525

@@ -31,11 +31,16 @@ func getDNSZoneName(ctx context.Context, ic *installconfig.InstallConfig, isPubl
3131
cctx, ccancel := context.WithTimeout(ctx, time.Minute*1)
3232
defer ccancel()
3333

34-
domain := ic.Config.BaseDomain
35-
project := ic.Config.GCP.ProjectID
36-
if !isPublic {
37-
project, _ = manifests.GetPrivateDNSZoneAndProject(ic)
38-
domain = ic.Config.ClusterDomain()
34+
params, err := manifests.GetGCPPrivateZoneInfo(ctx, client, ic, clusterID)
35+
if err != nil {
36+
return "", fmt.Errorf("failed to get private zone info: %w", err)
37+
}
38+
39+
domain := params.BaseDomain
40+
project := params.Project
41+
if isPublic {
42+
project = ic.Config.GCP.ProjectID
43+
domain = ic.Config.BaseDomain
3944
}
4045

4146
zone, err := client.GetDNSZone(cctx, project, domain, isPublic)
@@ -57,20 +62,20 @@ type recordSet struct {
5762
}
5863

5964
// createRecordSets will create a list of records that will be created during the install.
60-
func createRecordSets(ctx context.Context, ic *installconfig.InstallConfig, clusterID, apiIP, apiIntIP string) ([]recordSet, error) {
65+
func createRecordSets(ctx context.Context, client *gcpic.Client, ic *installconfig.InstallConfig, clusterID, apiIP, apiIntIP string) ([]recordSet, error) {
6166
ctx, cancel := context.WithTimeout(ctx, time.Minute*1)
6267
defer cancel()
6368

64-
project, privateZoneName := manifests.GetPrivateDNSZoneAndProject(ic)
65-
if privateZoneName == "" {
66-
privateZoneName = manifests.GCPDefaultPrivateZoneID(clusterID)
69+
privateZoneParams, err := manifests.GetGCPPrivateZoneInfo(ctx, client, ic, clusterID)
70+
if err != nil {
71+
return nil, fmt.Errorf("failed to get private zone info for record creation: %w", err)
6772
}
6873

6974
records := []recordSet{
7075
{
7176
// api_internal
72-
projectID: project,
73-
zoneName: privateZoneName,
77+
projectID: privateZoneParams.Project,
78+
zoneName: privateZoneParams.Name,
7479
record: &dns.ResourceRecordSet{
7580
Name: fmt.Sprintf("api-int.%s.", ic.Config.ClusterDomain()),
7681
Type: "A",
@@ -80,8 +85,8 @@ func createRecordSets(ctx context.Context, ic *installconfig.InstallConfig, clus
8085
},
8186
{
8287
// api_external_internal_zone
83-
projectID: project,
84-
zoneName: privateZoneName,
88+
projectID: privateZoneParams.Project,
89+
zoneName: privateZoneParams.Name,
8590
record: &dns.ResourceRecordSet{
8691
Name: fmt.Sprintf("api.%s.", ic.Config.ClusterDomain()),
8792
Type: "A",
@@ -92,7 +97,7 @@ func createRecordSets(ctx context.Context, ic *installconfig.InstallConfig, clus
9297
}
9398

9499
if ic.Config.Publish == types.ExternalPublishingStrategy {
95-
existingPublicZoneName, err := getDNSZoneName(ctx, ic, true)
100+
existingPublicZoneName, err := getDNSZoneName(ctx, ic, clusterID, true)
96101
if err != nil {
97102
return nil, fmt.Errorf("failed to find a public zone: %w", err)
98103
}
@@ -114,14 +119,14 @@ func createRecordSets(ctx context.Context, ic *installconfig.InstallConfig, clus
114119
}
115120

116121
// createDNSRecords will get the list of records to be created and execute their creation through the gcp dns api.
117-
func createDNSRecords(ctx context.Context, ic *installconfig.InstallConfig, clusterID, apiIP, apiIntIP string) error {
122+
func createDNSRecords(ctx context.Context, client *gcpic.Client, ic *installconfig.InstallConfig, clusterID, apiIP, apiIntIP string) error {
118123
// TODO: use the opts for the service to restrict scopes see google.golang.org/api/option.WithScopes
119124
dnsService, err := gcpic.GetDNSService(ctx, ic.Config.GCP.ServiceEndpoints)
120125
if err != nil {
121126
return fmt.Errorf("failed to create the gcp dns service: %w", err)
122127
}
123128

124-
records, err := createRecordSets(ctx, ic, clusterID, apiIP, apiIntIP)
129+
records, err := createRecordSets(ctx, client, ic, clusterID, apiIP, apiIntIP)
125130
if err != nil {
126131
return err
127132
}
@@ -147,25 +152,23 @@ func createPrivateManagedZone(ctx context.Context, ic *installconfig.InstallConf
147152
return err
148153
}
149154

150-
privateZoneID := manifests.GCPDefaultPrivateZoneID(clusterID)
155+
params, err := manifests.GetGCPPrivateZoneInfo(ctx, client, ic, clusterID)
156+
if err != nil {
157+
return err
158+
}
159+
151160
if ic.Config.GCP.NetworkProjectID != "" {
152-
privateZoneName, shouldCreateZone, err := manifests.GetGCPPrivateZoneName(ctx, client, ic, clusterID)
153-
if err != nil {
154-
return err
155-
}
156-
if !shouldCreateZone {
157-
logrus.Debugf("found private zone %s, skipping creation of private zone", privateZoneName)
158-
privateZoneProject, _ := manifests.GetPrivateDNSZoneAndProject(ic)
161+
if !params.InstallerCreated {
162+
logrus.Debugf("found private zone %s, skipping creation of private zone", params.Name)
159163
// The private zone already exists, so we need to add the shared label to the zone.
160164
labels := mergeLabels(ic, clusterID, sharedLabelValue)
161-
if err := client.UpdateDNSPrivateZoneLabels(ctx, ic.Config.ClusterDomain(), privateZoneProject, privateZoneName, labels); err != nil {
165+
if err := client.UpdateDNSPrivateZoneLabels(ctx, ic.Config.ClusterDomain(), params.Project, params.Name, labels); err != nil {
162166
return fmt.Errorf("failed to update dns private zone labels: %w", err)
163167
}
164168
return nil
165169
}
166-
privateZoneID = privateZoneName
167170
}
168-
logrus.Debugf("creating private zone %s", privateZoneID)
171+
logrus.Debugf("creating private zone %s", params.Name)
169172

170173
// TODO: use the opts for the service to restrict scopes see google.golang.org/api/option.WithScopes
171174
dnsService, err := gcpic.GetDNSService(ctx, ic.Config.GCP.ServiceEndpoints)
@@ -174,7 +177,7 @@ func createPrivateManagedZone(ctx context.Context, ic *installconfig.InstallConf
174177
}
175178

176179
managedZone := &dns.ManagedZone{
177-
Name: privateZoneID,
180+
Name: params.Name,
178181
Description: resourceDescription,
179182
DnsName: fmt.Sprintf("%s.", ic.Config.ClusterDomain()),
180183
Visibility: "private",
@@ -191,8 +194,7 @@ func createPrivateManagedZone(ctx context.Context, ic *installconfig.InstallConf
191194
ctx, cancel := context.WithTimeout(ctx, time.Minute*1)
192195
defer cancel()
193196

194-
project, _ := manifests.GetPrivateDNSZoneAndProject(ic)
195-
if _, err = dnsService.ManagedZones.Create(project, managedZone).Context(ctx).Do(); err != nil {
197+
if _, err = dnsService.ManagedZones.Create(params.Project, managedZone).Context(ctx).Do(); err != nil {
196198
return fmt.Errorf("failed to create private managed zone: %w", err)
197199
}
198200

pkg/types/gcp/dns.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ type DNSZoneParams struct {
1717
// BaseDomain is the base domain of the DNS zone.
1818
// Note that either `Name` or `BaseDomain` must be provided.
1919
BaseDomain string
20+
21+
// InstallerCreated is true when the DNS zone should be created
22+
// by the OpenShift Installer (and will be owned by the
23+
// OpenShift Installer).
24+
InstallerCreated bool
2025
}

0 commit comments

Comments
 (0)