internal/downward: add support for (optional) pod name

Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
Andrew Keesler 2020-12-11 10:57:20 -05:00
parent 0246e57d7f
commit 22c5b102ed
No known key found for this signature in database
GPG Key ID: 27CE0444346F9413
5 changed files with 25 additions and 0 deletions

View File

@ -13,6 +13,8 @@ import (
"path/filepath"
"strconv"
"strings"
"go.pinniped.dev/internal/plog"
)
// PodInfo contains pod metadata about the current pod.
@ -20,6 +22,9 @@ type PodInfo struct {
// Namespace where the current pod is running.
Namespace string
// Name of the current pod.
Name string
// Labels of the current pod.
Labels map[string]string
}
@ -33,6 +38,13 @@ func Load(directory string) (*PodInfo, error) {
}
result.Namespace = strings.TrimSpace(string(ns))
name, err := ioutil.ReadFile(filepath.Join(directory, "name"))
if err != nil {
plog.Warning("could not read 'name' downward API file")
} else {
result.Name = strings.TrimSpace(string(name))
}
labels, err := ioutil.ReadFile(filepath.Join(directory, "labels"))
if err != nil {
return nil, fmt.Errorf("could not load labels: %w", err)

View File

@ -35,6 +35,15 @@ func TestLoad(t *testing.T) {
{
name: "valid",
inputDir: "./testdata/valid",
want: &PodInfo{
Namespace: "test-namespace",
Name: "test-name",
Labels: map[string]string{"foo": "bar", "bat": "baz"},
},
},
{
name: "valid without name",
inputDir: "./testdata/validwithoutname",
want: &PodInfo{
Namespace: "test-namespace",
Labels: map[string]string{"foo": "bar", "bat": "baz"},

1
internal/downward/testdata/valid/name vendored Normal file
View File

@ -0,0 +1 @@
test-name

View File

@ -0,0 +1,2 @@
foo="bar"
bat="baz"

View File

@ -0,0 +1 @@
test-namespace