Skip to content

Commit 7e26d77

Browse files
authored
Add proper hostnames to GCP machines (#799)
1 parent 1ecf00b commit 7e26d77

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

pkg/cloudprovider/provider/gce/instance.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ limitations under the License.
2121
package gce
2222

2323
import (
24+
"fmt"
2425
"strconv"
2526

2627
"google.golang.org/api/compute/v1"
@@ -44,7 +45,9 @@ const (
4445

4546
// googleInstance implements instance.Instance for the Google compute instance.
4647
type googleInstance struct {
47-
ci *compute.Instance
48+
ci *compute.Instance
49+
projectID string
50+
zone string
4851
}
4952

5053
// Name implements instance.Instance.
@@ -66,7 +69,25 @@ func (gi *googleInstance) Addresses() map[string]v1.NodeAddressType {
6669
addrs[ac.NatIP] = v1.NodeExternalIP
6770
}
6871
}
69-
addrs[gi.ci.Hostname] = v1.NodeInternalDNS
72+
73+
// GCE has two types of the internal DNS, so we need to take both
74+
// into the account:
75+
// https://cloud.google.com/compute/docs/internal-dns#instance-fully-qualified-domain-names
76+
// Zonal DNS is present for newer projects and has the following FQDN format:
77+
// [INSTANCE_NAME].[ZONE].c.[PROJECT_ID].internal
78+
zonalDNS := fmt.Sprintf("%s.%s.c.%s.internal", gi.ci.Name, gi.zone, gi.projectID)
79+
addrs[zonalDNS] = v1.NodeInternalDNS
80+
81+
// Global DNS is present for older projects and has the following FQDN format:
82+
// [INSTANCE_NAME].c.[PROJECT_ID].internal
83+
globalDNS := fmt.Sprintf("%s.c.%s.internal", gi.ci.Name, gi.projectID)
84+
addrs[globalDNS] = v1.NodeInternalDNS
85+
86+
// GCP provides the search paths to resolve the machine's name,
87+
// so we add is as a DNS name
88+
// https://cloud.google.com/compute/docs/internal-dns#resolv.conf
89+
addrs[gi.ci.Name] = v1.NodeInternalDNS
90+
7091
return addrs
7192
}
7293

pkg/cloudprovider/provider/gce/provider.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ func (p *Provider) get(machine *v1alpha1.Machine) (*googleInstance, error) {
160160
if len(insts.Items) > 1 {
161161
return nil, newError(common.InvalidConfigurationMachineError, errGotTooManyInstances)
162162
}
163-
return &googleInstance{insts.Items[0]}, nil
163+
return &googleInstance{
164+
ci: insts.Items[0],
165+
projectID: cfg.projectID,
166+
zone: cfg.zone,
167+
}, nil
164168
}
165169

166170
// GetCloudConfig returns the cloud provider specific cloud-config for the kubelet.

0 commit comments

Comments
 (0)