mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-15 06:01:50 +00:00
Add downward API for environment vars
This commit is contained in:
19
pkg/fieldpath/doc.go
Normal file
19
pkg/fieldpath/doc.go
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
Copyright 2015 Google Inc. All rights reserved.
|
||||
|
||||
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 fieldpath supplies methods for extracting fields from objects
|
||||
// given a path to a field.
|
||||
package fieldpath
|
47
pkg/fieldpath/fieldpath.go
Normal file
47
pkg/fieldpath/fieldpath.go
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
Copyright 2015 Google Inc. All rights reserved.
|
||||
|
||||
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 fieldpath
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
|
||||
)
|
||||
|
||||
// ExtractFieldPathAsString extracts the field from the given object
|
||||
// and returns it as a string. The object must be a pointer to an
|
||||
// API type.
|
||||
//
|
||||
// Currently, this API is limited to supporting the fieldpaths:
|
||||
//
|
||||
// 1. metadata.name - The name of an API object
|
||||
// 2. metadata.namespace - The namespace of an API object
|
||||
func ExtractFieldPathAsString(obj interface{}, fieldPath string) (string, error) {
|
||||
accessor, err := meta.Accessor(obj)
|
||||
if err != nil {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
switch fieldPath {
|
||||
case "metadata.name":
|
||||
return accessor.Name(), nil
|
||||
case "metadata.namespace":
|
||||
return accessor.Namespace(), nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Unsupported fieldPath: %v", fieldPath)
|
||||
}
|
86
pkg/fieldpath/fieldpath_test.go
Normal file
86
pkg/fieldpath/fieldpath_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
Copyright 2015 Google Inc. All rights reserved.
|
||||
|
||||
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 fieldpath
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
)
|
||||
|
||||
func TestExtractFieldPathAsString(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
fieldPath string
|
||||
obj interface{}
|
||||
expectedValue string
|
||||
expectedMessageFragment string
|
||||
}{
|
||||
{
|
||||
name: "not an API object",
|
||||
fieldPath: "metadata.name",
|
||||
obj: "",
|
||||
expectedMessageFragment: "expected struct",
|
||||
},
|
||||
{
|
||||
name: "ok - namespace",
|
||||
fieldPath: "metadata.namespace",
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: "object-namespace",
|
||||
},
|
||||
},
|
||||
expectedValue: "object-namespace",
|
||||
},
|
||||
{
|
||||
name: "ok - name",
|
||||
fieldPath: "metadata.name",
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "object-name",
|
||||
},
|
||||
},
|
||||
expectedValue: "object-name",
|
||||
},
|
||||
{
|
||||
name: "invalid expression",
|
||||
fieldPath: "metadata.whoops",
|
||||
obj: &api.Pod{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: "object-namespace",
|
||||
},
|
||||
},
|
||||
expectedMessageFragment: "Unsupported fieldPath",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
actual, err := ExtractFieldPathAsString(tc.obj, tc.fieldPath)
|
||||
if err != nil {
|
||||
if tc.expectedMessageFragment != "" {
|
||||
if !strings.Contains(err.Error(), tc.expectedMessageFragment) {
|
||||
t.Errorf("%v: Unexpected error message: %q, expected to contain %q", tc.name, err, tc.expectedMessageFragment)
|
||||
}
|
||||
} else {
|
||||
t.Errorf("%v: unexpected error: %v", tc.name, err)
|
||||
}
|
||||
} else if e := tc.expectedValue; e != "" && e != actual {
|
||||
t.Errorf("%v: Unexpected result; got %q, expected %q", tc.name, actual, e)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user