diff --git a/internal/downward/downward.go b/internal/downward/downward.go index ee4d65b7..75119dc4 100644 --- a/internal/downward/downward.go +++ b/internal/downward/downward.go @@ -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) diff --git a/internal/downward/downward_test.go b/internal/downward/downward_test.go index 9bc954d5..adc15b40 100644 --- a/internal/downward/downward_test.go +++ b/internal/downward/downward_test.go @@ -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"}, diff --git a/internal/downward/testdata/valid/name b/internal/downward/testdata/valid/name new file mode 100644 index 00000000..2fdecf1e --- /dev/null +++ b/internal/downward/testdata/valid/name @@ -0,0 +1 @@ +test-name diff --git a/internal/downward/testdata/validwithoutname/labels b/internal/downward/testdata/validwithoutname/labels new file mode 100644 index 00000000..e5880cda --- /dev/null +++ b/internal/downward/testdata/validwithoutname/labels @@ -0,0 +1,2 @@ +foo="bar" +bat="baz" diff --git a/internal/downward/testdata/validwithoutname/namespace b/internal/downward/testdata/validwithoutname/namespace new file mode 100644 index 00000000..f2605f23 --- /dev/null +++ b/internal/downward/testdata/validwithoutname/namespace @@ -0,0 +1 @@ +test-namespace