mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-22 10:27:56 +00:00
adding downward api volume plugin
This commit is contained in:
@@ -22,6 +22,15 @@ import (
|
||||
"k8s.io/kubernetes/pkg/api/meta"
|
||||
)
|
||||
|
||||
// formatMap formats map[string]string to a string.
|
||||
func formatMap(m map[string]string) string {
|
||||
var l string
|
||||
for key, value := range m {
|
||||
l += key + "=" + fmt.Sprintf("%q", value) + "\n"
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
// ExtractFieldPathAsString extracts the field from the given object
|
||||
// and returns it as a string. The object must be a pointer to an
|
||||
// API type.
|
||||
@@ -37,6 +46,10 @@ func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error)
|
||||
}
|
||||
|
||||
switch fieldPath {
|
||||
case "metadata.annotations":
|
||||
return formatMap(accessor.Annotations()), nil
|
||||
case "metadata.labels":
|
||||
return formatMap(accessor.Labels()), nil
|
||||
case "metadata.name":
|
||||
return accessor.Name(), nil
|
||||
case "metadata.namespace":
|
||||
|
@@ -57,6 +57,37 @@ func TestExtractFieldPathAsString(t *testing.T) {
|
||||
},
|
||||
expectedValue: "object-name",
|
||||
},
|
||||
{
|
||||
name: "ok - labels",
|
||||
fieldPath: "metadata.labels",
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Labels: map[string]string{"key": "value"},
|
||||
},
|
||||
},
|
||||
expectedValue: "key=\"value\"\n",
|
||||
},
|
||||
{
|
||||
name: "ok - labels bslash n",
|
||||
fieldPath: "metadata.labels",
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Labels: map[string]string{"key": "value\n"},
|
||||
},
|
||||
},
|
||||
expectedValue: "key=\"value\\n\"\n",
|
||||
},
|
||||
{
|
||||
name: "ok - annotations",
|
||||
fieldPath: "metadata.annotations",
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Annotations: map[string]string{"builder": "john-doe"},
|
||||
},
|
||||
},
|
||||
expectedValue: "builder=\"john-doe\"\n",
|
||||
},
|
||||
|
||||
{
|
||||
name: "invalid expression",
|
||||
fieldPath: "metadata.whoops",
|
||||
|
Reference in New Issue
Block a user