From 4112d8db20c5550cc27d30d3cee2723db0f8e1c4 Mon Sep 17 00:00:00 2001 From: Javier Diaz-Montes Date: Mon, 1 Jun 2020 16:48:33 -0400 Subject: [PATCH] Adding new PodSpec field called setHostnameAsFQDN and SetHostnameAsFQDN gate These changes add a new field, called setHostnameAsFQDN, to the PodSpec. This field is a bool that will be used to indicate whether we would like FQDN be set as hostname or not. This is PART1 of the changes to enable KEP #1797 and addresses #91036 --- pkg/api/pod/util.go | 14 ++++++++++++++ pkg/apis/core/types.go | 6 ++++++ pkg/features/kube_features.go | 8 ++++++++ staging/src/k8s.io/api/core/v1/types.go | 7 +++++++ 4 files changed, 35 insertions(+) diff --git a/pkg/api/pod/util.go b/pkg/api/pod/util.go index 30a8c21a560..69589a11c1f 100644 --- a/pkg/api/pod/util.go +++ b/pkg/api/pod/util.go @@ -438,6 +438,12 @@ func dropDisabledFields( // does not specify any values for these fields. podSpec.PreemptionPolicy = nil } + + if !utilfeature.DefaultFeatureGate.Enabled(features.SetHostnameAsFQDN) && !setHostnameAsFQDNInUse(oldPodSpec) { + // Set SetHostnameAsFQDN to nil only if feature is disabled and it is not used + podSpec.SetHostnameAsFQDN = nil + } + } // dropDisabledRunAsGroupField removes disabled fields from PodSpec related @@ -720,3 +726,11 @@ func multiplePodIPsInUse(podStatus *api.PodStatus) bool { } return false } + +// setHostnameAsFQDNInUse returns true if any pod's spec defines setHostnameAsFQDN field. +func setHostnameAsFQDNInUse(podSpec *api.PodSpec) bool { + if podSpec == nil || podSpec.SetHostnameAsFQDN == nil { + return false + } + return *podSpec.SetHostnameAsFQDN +} diff --git a/pkg/apis/core/types.go b/pkg/apis/core/types.go index 6e1d9e6a91c..a7a42b6839a 100644 --- a/pkg/apis/core/types.go +++ b/pkg/apis/core/types.go @@ -2693,6 +2693,12 @@ type PodSpec struct { // If not specified, the pod will not have a domainname at all. // +optional Subdomain string + // If true the pod's hostname will be configured as the pod's FQDN, rather than the leaf name (the default). + // In Linux containers, this means setting the FQDN in the hostname field of the kernel (the nodename field of struct utsname). + // In Windows containers, this means setting the registry value of hostname for the registry key HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters to FQDN. + // If a pod does not have FQDN, this has no effect. + // +optional + SetHostnameAsFQDN *bool // If specified, the pod's scheduling constraints // +optional Affinity *Affinity diff --git a/pkg/features/kube_features.go b/pkg/features/kube_features.go index 63710e5e1c5..5779c7340a1 100644 --- a/pkg/features/kube_features.go +++ b/pkg/features/kube_features.go @@ -592,6 +592,13 @@ const ( // // Enables usage of any object for volume data source in PVCs AnyVolumeDataSource featuregate.Feature = "AnyVolumeDataSource" + + // owner: @javidiaz + // alpha: v1.19 + // + // Allow setting the Fully Qualified Domain Name (FQDN) in the hostname of a Pod. If a Pod does not + // have FQDN, this feature has no effect. + SetHostnameAsFQDN featuregate.Feature = "SetHostnameAsFQDN" ) func init() { @@ -683,6 +690,7 @@ var defaultKubernetesFeatureGates = map[featuregate.Feature]featuregate.FeatureS ExternalPolicyForExternalIP: {Default: true, PreRelease: featuregate.GA}, // remove in 1.20 AnyVolumeDataSource: {Default: false, PreRelease: featuregate.Alpha}, DefaultPodTopologySpread: {Default: false, PreRelease: featuregate.Alpha}, + SetHostnameAsFQDN: {Default: false, PreRelease: featuregate.Alpha}, // inherited features from generic apiserver, relisted here to get a conflict if it is changed // unintentionally on either side: diff --git a/staging/src/k8s.io/api/core/v1/types.go b/staging/src/k8s.io/api/core/v1/types.go index ba0b4152b21..3b3bd3402f6 100644 --- a/staging/src/k8s.io/api/core/v1/types.go +++ b/staging/src/k8s.io/api/core/v1/types.go @@ -3055,6 +3055,13 @@ type PodSpec struct { // +listMapKey=topologyKey // +listMapKey=whenUnsatisfiable TopologySpreadConstraints []TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty" patchStrategy:"merge" patchMergeKey:"topologyKey" protobuf:"bytes,33,opt,name=topologySpreadConstraints"` + // If true the pod's hostname will be configured as the pod's FQDN, rather than the leaf name (the default). + // In Linux containers, this means setting the FQDN in the hostname field of the kernel (the nodename field of struct utsname). + // In Windows containers, this means setting the registry value of hostname for the registry key HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters to FQDN. + // If a pod does not have FQDN, this has no effect. + // Default to false. + // +optional + SetHostnameAsFQDN *bool `json:"setHostnameAsFQDN,omitempty" protobuf:"varint,35,opt,name=setHostnameAsFQDN"` } type UnsatisfiableConstraintAction string