internal/downward: add support for (optional) pod name
Signed-off-by: Andrew Keesler <akeesler@vmware.com>
This commit is contained in:
parent
0246e57d7f
commit
22c5b102ed
@ -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)
|
||||
|
@ -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
1
internal/downward/testdata/valid/name
vendored
Normal file
@ -0,0 +1 @@
|
||||
test-name
|
2
internal/downward/testdata/validwithoutname/labels
vendored
Normal file
2
internal/downward/testdata/validwithoutname/labels
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
foo="bar"
|
||||
bat="baz"
|
1
internal/downward/testdata/validwithoutname/namespace
vendored
Normal file
1
internal/downward/testdata/validwithoutname/namespace
vendored
Normal file
@ -0,0 +1 @@
|
||||
test-namespace
|
Loading…
Reference in New Issue
Block a user