Merge pull request #42717 from andrewsykim/support-host-ip-downward-api
Automatic merge from submit-queue Support status.hostIP in downward API **What this PR does / why we need it**: Exposes pod's hostIP (node IP) via downward API. **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes https://github.com/kubernetes/kubernetes/issues/24657 **Special notes for your reviewer**: Not sure if there's more documentation that's needed, please point me in the right direction and I will add some :)
This commit is contained in:
@@ -1250,7 +1250,7 @@ type EnvVar struct {
|
||||
// Only one of its fields may be set.
|
||||
type EnvVarSource struct {
|
||||
// Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations,
|
||||
// spec.nodeName, spec.serviceAccountName, status.podIP.
|
||||
// spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.
|
||||
// +optional
|
||||
FieldRef *ObjectFieldSelector
|
||||
// Selects a resource of the container: only resources limits and requests
|
||||
|
||||
@@ -190,6 +190,7 @@ func addConversionFuncs(scheme *runtime.Scheme) error {
|
||||
"spec.restartPolicy",
|
||||
"spec.serviceAccountName",
|
||||
"status.phase",
|
||||
"status.hostIP",
|
||||
"status.podIP":
|
||||
return label, value, nil
|
||||
// This is for backwards compatibility with old v1 clients which send spec.host
|
||||
|
||||
@@ -899,7 +899,7 @@ message EnvVar {
|
||||
// EnvVarSource represents a source for the value of an EnvVar.
|
||||
message EnvVarSource {
|
||||
// Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations,
|
||||
// spec.nodeName, spec.serviceAccountName, status.podIP.
|
||||
// spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.
|
||||
// +optional
|
||||
optional ObjectFieldSelector fieldRef = 1;
|
||||
|
||||
|
||||
@@ -1349,7 +1349,7 @@ type EnvVar struct {
|
||||
// EnvVarSource represents a source for the value of an EnvVar.
|
||||
type EnvVarSource struct {
|
||||
// Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations,
|
||||
// spec.nodeName, spec.serviceAccountName, status.podIP.
|
||||
// spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.
|
||||
// +optional
|
||||
FieldRef *ObjectFieldSelector `json:"fieldRef,omitempty" protobuf:"bytes,1,opt,name=fieldRef"`
|
||||
// Selects a resource of the container: only resources limits and requests
|
||||
|
||||
@@ -481,7 +481,7 @@ func (EnvVar) SwaggerDoc() map[string]string {
|
||||
|
||||
var map_EnvVarSource = map[string]string{
|
||||
"": "EnvVarSource represents a source for the value of an EnvVar.",
|
||||
"fieldRef": "Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.podIP.",
|
||||
"fieldRef": "Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.",
|
||||
"resourceFieldRef": "Selects a resource of the container: only resources limits and requests (limits.cpu, limits.memory, requests.cpu and requests.memory) are currently supported.",
|
||||
"configMapKeyRef": "Selects a key of a ConfigMap.",
|
||||
"secretKeyRef": "Selects a key of a secret in the pod's namespace",
|
||||
|
||||
@@ -1416,7 +1416,7 @@ func ValidateEnv(vars []api.EnvVar, fldPath *field.Path) field.ErrorList {
|
||||
return allErrs
|
||||
}
|
||||
|
||||
var validFieldPathExpressionsEnv = sets.NewString("metadata.name", "metadata.namespace", "spec.nodeName", "spec.serviceAccountName", "status.podIP")
|
||||
var validFieldPathExpressionsEnv = sets.NewString("metadata.name", "metadata.namespace", "spec.nodeName", "spec.serviceAccountName", "status.hostIP", "status.podIP")
|
||||
var validContainerResourceFieldPathExpressions = sets.NewString("limits.cpu", "limits.memory", "requests.cpu", "requests.memory")
|
||||
|
||||
func validateEnvVarValueFrom(ev api.EnvVar, fldPath *field.Path) field.ErrorList {
|
||||
|
||||
@@ -2153,6 +2153,24 @@ func TestValidateEnv(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "abc",
|
||||
ValueFrom: &api.EnvVarSource{
|
||||
FieldRef: &api.ObjectFieldSelector{
|
||||
APIVersion: api.Registry.GroupOrDie(api.GroupName).GroupVersion.String(),
|
||||
FieldPath: "status.hostIP",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "abc",
|
||||
ValueFrom: &api.EnvVarSource{
|
||||
FieldRef: &api.ObjectFieldSelector{
|
||||
APIVersion: api.Registry.GroupOrDie(api.GroupName).GroupVersion.String(),
|
||||
FieldPath: "status.podIP",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "secret_value",
|
||||
ValueFrom: &api.EnvVarSource{
|
||||
@@ -2328,7 +2346,7 @@ func TestValidateEnv(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}},
|
||||
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.labels": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.podIP`,
|
||||
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.labels": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||
},
|
||||
{
|
||||
name: "invalid fieldPath annotations",
|
||||
@@ -2341,7 +2359,7 @@ func TestValidateEnv(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}},
|
||||
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.annotations": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.podIP`,
|
||||
expectedError: `[0].valueFrom.fieldRef.fieldPath: Unsupported value: "metadata.annotations": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||
},
|
||||
{
|
||||
name: "unsupported fieldPath",
|
||||
@@ -2354,7 +2372,7 @@ func TestValidateEnv(t *testing.T) {
|
||||
},
|
||||
},
|
||||
}},
|
||||
expectedError: `valueFrom.fieldRef.fieldPath: Unsupported value: "status.phase": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.podIP`,
|
||||
expectedError: `valueFrom.fieldRef.fieldPath: Unsupported value: "status.phase": supported values: metadata.name, metadata.namespace, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP`,
|
||||
},
|
||||
}
|
||||
for _, tc := range errorCases {
|
||||
|
||||
@@ -41,11 +41,6 @@ func FormatMap(m map[string]string) (fmtStr string) {
|
||||
// ExtractFieldPathAsString extracts the field from the given object
|
||||
// and returns it as a string. The object must be a pointer to an
|
||||
// API type.
|
||||
//
|
||||
// Currently, this API is limited to supporting the fieldpaths:
|
||||
//
|
||||
// 1. metadata.name - The name of an API object
|
||||
// 2. metadata.namespace - The namespace of an API object
|
||||
func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error) {
|
||||
accessor, err := meta.Accessor(obj)
|
||||
if err != nil {
|
||||
|
||||
@@ -3355,7 +3355,7 @@ func GetOpenAPIDefinitions(ref openapi.ReferenceCallback) map[string]openapi.Ope
|
||||
Properties: map[string]spec.Schema{
|
||||
"fieldRef": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.podIP.",
|
||||
Description: "Selects a field of the pod: supports metadata.name, metadata.namespace, metadata.labels, metadata.annotations, spec.nodeName, spec.serviceAccountName, status.hostIP, status.podIP.",
|
||||
Ref: ref("k8s.io/kubernetes/pkg/api/v1.ObjectFieldSelector"),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -46,7 +46,7 @@ type HandlerRunner interface {
|
||||
}
|
||||
|
||||
// RuntimeHelper wraps kubelet to make container runtime
|
||||
// able to get necessary informations like the RunContainerOptions, DNS settings.
|
||||
// able to get necessary informations like the RunContainerOptions, DNS settings, Host IP.
|
||||
type RuntimeHelper interface {
|
||||
GenerateRunContainerOptions(pod *v1.Pod, container *v1.Container, podIP string) (contOpts *RunContainerOptions, useClusterFirstPolicy bool, err error)
|
||||
GetClusterDNS(pod *v1.Pod) (dnsServers []string, dnsSearches []string, useClusterFirstPolicy bool, err error)
|
||||
|
||||
@@ -638,6 +638,12 @@ func (kl *Kubelet) podFieldSelectorRuntimeValue(fs *v1.ObjectFieldSelector, pod
|
||||
return pod.Spec.NodeName, nil
|
||||
case "spec.serviceAccountName":
|
||||
return pod.Spec.ServiceAccountName, nil
|
||||
case "status.hostIP":
|
||||
hostIP, err := kl.getHostIPAnyWay()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hostIP.String(), nil
|
||||
case "status.podIP":
|
||||
return podIP, nil
|
||||
}
|
||||
|
||||
@@ -502,6 +502,15 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "HOST_IP",
|
||||
ValueFrom: &v1.EnvVarSource{
|
||||
FieldRef: &v1.ObjectFieldSelector{
|
||||
APIVersion: api.Registry.GroupOrDie(v1.GroupName).GroupVersion.String(),
|
||||
FieldPath: "status.hostIP",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
masterServiceNs: "nothing",
|
||||
@@ -512,6 +521,7 @@ func TestMakeEnvironmentVariables(t *testing.T) {
|
||||
{Name: "POD_NODE_NAME", Value: "node-name"},
|
||||
{Name: "POD_SERVICE_ACCOUNT_NAME", Value: "special"},
|
||||
{Name: "POD_IP", Value: "1.2.3.4"},
|
||||
{Name: "HOST_IP", Value: testKubeletHostIP},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
||||
@@ -79,6 +79,7 @@ func init() {
|
||||
|
||||
const (
|
||||
testKubeletHostname = "127.0.0.1"
|
||||
testKubeletHostIP = "127.0.0.1"
|
||||
|
||||
testReservationCPU = "200m"
|
||||
testReservationMemory = "100M"
|
||||
@@ -166,7 +167,31 @@ func newTestKubeletWithImageList(
|
||||
kubelet.masterServiceNamespace = metav1.NamespaceDefault
|
||||
kubelet.serviceLister = testServiceLister{}
|
||||
kubelet.nodeLister = testNodeLister{}
|
||||
kubelet.nodeInfo = testNodeInfo{}
|
||||
kubelet.nodeInfo = testNodeInfo{
|
||||
nodes: []*v1.Node{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: string(kubelet.nodeName),
|
||||
},
|
||||
Status: v1.NodeStatus{
|
||||
Conditions: []v1.NodeCondition{
|
||||
{
|
||||
Type: v1.NodeReady,
|
||||
Status: v1.ConditionTrue,
|
||||
Reason: "Ready",
|
||||
Message: "Node ready",
|
||||
},
|
||||
},
|
||||
Addresses: []v1.NodeAddress{
|
||||
{
|
||||
Type: v1.NodeInternalIP,
|
||||
Address: testKubeletHostIP,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
kubelet.recorder = fakeRecorder
|
||||
if err := kubelet.setupDataDirs(); err != nil {
|
||||
t.Fatalf("can't initialize kubelet data dirs: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user