Commit 34a7b3de authored by Yang Guo's avatar Yang Guo

Create a separate conversion function for the field labels used by downward API

parent 82c9eec1
...@@ -51,6 +51,7 @@ filegroup( ...@@ -51,6 +51,7 @@ filegroup(
"//pkg/apis/core/fuzzer:all-srcs", "//pkg/apis/core/fuzzer:all-srcs",
"//pkg/apis/core/helper:all-srcs", "//pkg/apis/core/helper:all-srcs",
"//pkg/apis/core/install:all-srcs", "//pkg/apis/core/install:all-srcs",
"//pkg/apis/core/pods:all-srcs",
"//pkg/apis/core/v1:all-srcs", "//pkg/apis/core/v1:all-srcs",
"//pkg/apis/core/validation:all-srcs", "//pkg/apis/core/validation:all-srcs",
], ],
......
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["helpers.go"],
importpath = "k8s.io/kubernetes/pkg/apis/core/pods",
visibility = ["//visibility:public"],
deps = ["//pkg/fieldpath:go_default_library"],
)
go_test(
name = "go_default_test",
srcs = ["helpers_test.go"],
importpath = "k8s.io/kubernetes/pkg/apis/core/pods",
library = ":go_default_library",
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pods
import (
"fmt"
"k8s.io/kubernetes/pkg/fieldpath"
)
func ConvertDownwardAPIFieldLabel(version, label, value string) (string, string, error) {
if version != "v1" {
return "", "", fmt.Errorf("unsupported pod version: %s", version)
}
path, _ := fieldpath.SplitMaybeSubscriptedPath(label)
switch path {
case "metadata.annotations",
"metadata.labels",
"metadata.name",
"metadata.namespace",
"metadata.uid",
"spec.nodeName",
"spec.restartPolicy",
"spec.serviceAccountName",
"spec.schedulerName",
"status.phase",
"status.hostIP",
"status.podIP":
return label, value, nil
// This is for backwards compatibility with old v1 clients which send spec.host
case "spec.host":
return "spec.nodeName", value, nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
}
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pods
import (
"testing"
)
func TestConvertDownwardAPIFieldLabel(t *testing.T) {
testCases := []struct {
version string
label string
value string
expectedErr bool
expectedLabel string
expectedValue string
}{
{
version: "v2",
label: "metadata.name",
value: "test-pod",
expectedErr: true,
},
{
version: "v1",
label: "invalid-label",
value: "value",
expectedErr: true,
},
{
version: "v1",
label: "metadata.name",
value: "test-pod",
expectedLabel: "metadata.name",
expectedValue: "test-pod",
},
{
version: "v1",
label: "metadata.annotations",
value: "myValue",
expectedLabel: "metadata.annotations",
expectedValue: "myValue",
},
{
version: "v1",
label: "metadata.annotations['myKey']",
value: "myValue",
expectedLabel: "metadata.annotations['myKey']",
expectedValue: "myValue",
},
{
version: "v1",
label: "spec.host",
value: "127.0.0.1",
expectedLabel: "spec.nodeName",
expectedValue: "127.0.0.1",
},
}
for _, tc := range testCases {
label, value, err := ConvertDownwardAPIFieldLabel(tc.version, tc.label, tc.value)
if err != nil {
if tc.expectedErr {
continue
}
t.Errorf("ConvertDownwardAPIFieldLabel(%s, %s, %s) failed: %s",
tc.version, tc.label, tc.value, err)
}
if tc.expectedLabel != label || tc.expectedValue != value {
t.Errorf("ConvertDownwardAPIFieldLabel(%s, %s, %s) = (%s, %s, nil), expected (%s, %s, nil)",
tc.version, tc.label, tc.value, label, value, tc.expectedLabel, tc.expectedValue)
}
}
}
...@@ -159,17 +159,12 @@ func addConversionFuncs(scheme *runtime.Scheme) error { ...@@ -159,17 +159,12 @@ func addConversionFuncs(scheme *runtime.Scheme) error {
func(label, value string) (string, string, error) { func(label, value string) (string, string, error) {
path, _ := fieldpath.SplitMaybeSubscriptedPath(label) path, _ := fieldpath.SplitMaybeSubscriptedPath(label)
switch path { switch path {
case "metadata.annotations", case "metadata.name",
"metadata.labels",
"metadata.name",
"metadata.namespace", "metadata.namespace",
"metadata.uid",
"spec.nodeName", "spec.nodeName",
"spec.restartPolicy", "spec.restartPolicy",
"spec.serviceAccountName",
"spec.schedulerName", "spec.schedulerName",
"status.phase", "status.phase",
"status.hostIP",
"status.podIP": "status.podIP":
return label, value, nil return label, value, nil
// This is for backwards compatibility with old v1 clients which send spec.host // This is for backwards compatibility with old v1 clients which send spec.host
......
...@@ -18,6 +18,7 @@ go_library( ...@@ -18,6 +18,7 @@ go_library(
"//pkg/api/service:go_default_library", "//pkg/api/service:go_default_library",
"//pkg/apis/core:go_default_library", "//pkg/apis/core:go_default_library",
"//pkg/apis/core/helper:go_default_library", "//pkg/apis/core/helper:go_default_library",
"//pkg/apis/core/pods:go_default_library",
"//pkg/apis/core/v1:go_default_library", "//pkg/apis/core/v1:go_default_library",
"//pkg/apis/core/v1/helper:go_default_library", "//pkg/apis/core/v1/helper:go_default_library",
"//pkg/capabilities:go_default_library", "//pkg/capabilities:go_default_library",
......
...@@ -42,10 +42,10 @@ import ( ...@@ -42,10 +42,10 @@ import (
"k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/kubernetes/pkg/api/legacyscheme"
apiservice "k8s.io/kubernetes/pkg/api/service" apiservice "k8s.io/kubernetes/pkg/api/service"
"k8s.io/kubernetes/pkg/apis/core" "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/core/helper" "k8s.io/kubernetes/pkg/apis/core/helper"
podshelper "k8s.io/kubernetes/pkg/apis/core/pods"
corev1 "k8s.io/kubernetes/pkg/apis/core/v1" corev1 "k8s.io/kubernetes/pkg/apis/core/v1"
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper" v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
"k8s.io/kubernetes/pkg/capabilities" "k8s.io/kubernetes/pkg/capabilities"
...@@ -1964,7 +1964,7 @@ func validateObjectFieldSelector(fs *core.ObjectFieldSelector, expressions *sets ...@@ -1964,7 +1964,7 @@ func validateObjectFieldSelector(fs *core.ObjectFieldSelector, expressions *sets
return allErrs return allErrs
} }
internalFieldPath, _, err := legacyscheme.Scheme.ConvertFieldLabel(fs.APIVersion, "Pod", fs.FieldPath, "") internalFieldPath, _, err := podshelper.ConvertDownwardAPIFieldLabel(fs.APIVersion, fs.FieldPath, "")
if err != nil { if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath.Child("fieldPath"), fs.FieldPath, fmt.Sprintf("error converting fieldPath: %v", err))) allErrs = append(allErrs, field.Invalid(fldPath.Child("fieldPath"), fs.FieldPath, fmt.Sprintf("error converting fieldPath: %v", err)))
return allErrs return allErrs
......
...@@ -29,10 +29,10 @@ go_library( ...@@ -29,10 +29,10 @@ go_library(
], ],
importpath = "k8s.io/kubernetes/pkg/kubelet", importpath = "k8s.io/kubernetes/pkg/kubelet",
deps = [ deps = [
"//pkg/api/legacyscheme:go_default_library",
"//pkg/api/v1/pod:go_default_library", "//pkg/api/v1/pod:go_default_library",
"//pkg/api/v1/resource:go_default_library", "//pkg/api/v1/resource:go_default_library",
"//pkg/apis/core:go_default_library", "//pkg/apis/core:go_default_library",
"//pkg/apis/core/pods:go_default_library",
"//pkg/apis/core/v1:go_default_library", "//pkg/apis/core/v1:go_default_library",
"//pkg/apis/core/v1/helper:go_default_library", "//pkg/apis/core/v1/helper:go_default_library",
"//pkg/apis/core/v1/helper/qos:go_default_library", "//pkg/apis/core/v1/helper/qos:go_default_library",
......
...@@ -43,9 +43,9 @@ import ( ...@@ -43,9 +43,9 @@ import (
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/client-go/tools/remotecommand" "k8s.io/client-go/tools/remotecommand"
"k8s.io/kubernetes/pkg/api/legacyscheme"
podutil "k8s.io/kubernetes/pkg/api/v1/pod" podutil "k8s.io/kubernetes/pkg/api/v1/pod"
"k8s.io/kubernetes/pkg/api/v1/resource" "k8s.io/kubernetes/pkg/api/v1/resource"
podshelper "k8s.io/kubernetes/pkg/apis/core/pods"
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper" v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos" v1qos "k8s.io/kubernetes/pkg/apis/core/v1/helper/qos"
"k8s.io/kubernetes/pkg/apis/core/v1/validation" "k8s.io/kubernetes/pkg/apis/core/v1/validation"
...@@ -733,7 +733,7 @@ func (kl *Kubelet) makeEnvironmentVariables(pod *v1.Pod, container *v1.Container ...@@ -733,7 +733,7 @@ func (kl *Kubelet) makeEnvironmentVariables(pod *v1.Pod, container *v1.Container
// podFieldSelectorRuntimeValue returns the runtime value of the given // podFieldSelectorRuntimeValue returns the runtime value of the given
// selector for a pod. // selector for a pod.
func (kl *Kubelet) podFieldSelectorRuntimeValue(fs *v1.ObjectFieldSelector, pod *v1.Pod, podIP string) (string, error) { func (kl *Kubelet) podFieldSelectorRuntimeValue(fs *v1.ObjectFieldSelector, pod *v1.Pod, podIP string) (string, error) {
internalFieldPath, _, err := legacyscheme.Scheme.ConvertFieldLabel(fs.APIVersion, "Pod", fs.FieldPath, "") internalFieldPath, _, err := podshelper.ConvertDownwardAPIFieldLabel(fs.APIVersion, fs.FieldPath, "")
if err != nil { if err != nil {
return "", err return "", err
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment