Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions datasource/metadata/gce/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package gce

import (
"fmt"
"log"
"net"
"net/http"
"strings"

"github.com/flatcar/coreos-cloudinit/datasource"
"github.com/flatcar/coreos-cloudinit/datasource/metadata"
Expand Down Expand Up @@ -51,6 +53,15 @@ func (ms metadataService) FetchMetadata() (datasource.Metadata, error) {
return datasource.Metadata{}, err
}

if len(hostname) > 63 {
log.Printf("Hostname too long. Truncating hostname %s to %s", hostname, strings.Split(hostname, ".")[0])
hostname = strings.Split(hostname, ".")[0]
if len(hostname) > 63 {
log.Printf("Hostname still too long. Truncating hostname %s further to 63 bytes (%s)", hostname, hostname[:63])
hostname = hostname[:63]
}
}

return datasource.Metadata{
PublicIPv4: public,
PrivateIPv4: local,
Expand Down
30 changes: 30 additions & 0 deletions datasource/metadata/gce/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ func TestFetchMetadata(t *testing.T) {
PublicIPv4: net.ParseIP("5.6.7.8"),
},
},
{
// test long host names are truncated on first `.`
root: "/",
metadataPath: "computeMetadata/v1/instance/",
resources: map[string]string{
"/computeMetadata/v1/instance/hostname": "this-hostname-is-larger-than-sixty-three-characters-long-and.will.be.truncated.local",
"/computeMetadata/v1/instance/network-interfaces/0/ip": "1.2.3.4",
"/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip": "5.6.7.8",
},
expect: datasource.Metadata{
Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and",
PrivateIPv4: net.ParseIP("1.2.3.4"),
PublicIPv4: net.ParseIP("5.6.7.8"),
},
},
{
// test long host names are truncated if segment before first `.` is too long
root: "/",
metadataPath: "computeMetadata/v1/instance/",
resources: map[string]string{
"/computeMetadata/v1/instance/hostname": "this-hostname-is-larger-than-sixty-three-characters-long-and-will-be-truncated.local",
"/computeMetadata/v1/instance/network-interfaces/0/ip": "1.2.3.4",
"/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip": "5.6.7.8",
},
expect: datasource.Metadata{
Hostname: "this-hostname-is-larger-than-sixty-three-characters-long-and-wi",
PrivateIPv4: net.ParseIP("1.2.3.4"),
PublicIPv4: net.ParseIP("5.6.7.8"),
},
},
{
clientErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
expectErr: pkg.ErrTimeout{Err: fmt.Errorf("test error")},
Expand Down