mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 00:52:55 +00:00
allow accessing labels and annotations in vars
This commit is contained in:
71
k8sdeps/kunstruct/helper.go
Normal file
71
k8sdeps/kunstruct/helper.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors.
|
||||||
|
|
||||||
|
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 kunstruct provides unstructured from api machinery and factory for creating unstructured
|
||||||
|
package kunstruct
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseFields(path string) ([]string, error) {
|
||||||
|
if !strings.Contains(path, "[") {
|
||||||
|
return strings.Split(path, "."), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var fields []string
|
||||||
|
start := 0
|
||||||
|
insideParentheses := false
|
||||||
|
for i := range path {
|
||||||
|
switch path[i] {
|
||||||
|
case '.':
|
||||||
|
if !insideParentheses {
|
||||||
|
fields = append(fields, path[start:i])
|
||||||
|
start = i + 1
|
||||||
|
}
|
||||||
|
case '[':
|
||||||
|
if !insideParentheses {
|
||||||
|
if i == start {
|
||||||
|
start = i + 1
|
||||||
|
} else {
|
||||||
|
fields = append(fields, path[start:i])
|
||||||
|
start = i + 1
|
||||||
|
}
|
||||||
|
insideParentheses = true
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("nested parentheses are not allowed: %s", path)
|
||||||
|
}
|
||||||
|
case ']':
|
||||||
|
if insideParentheses {
|
||||||
|
fields = append(fields, path[start:i])
|
||||||
|
start = i + 1
|
||||||
|
insideParentheses = false
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("Invalid field path %s", path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if start < len(path)-1 {
|
||||||
|
fields = append(fields, path[start:])
|
||||||
|
}
|
||||||
|
for i, f := range fields {
|
||||||
|
if strings.HasPrefix(f, "\"") || strings.HasPrefix(f, "'") {
|
||||||
|
fields[i] = strings.Trim(f, "\"'")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fields, nil
|
||||||
|
}
|
||||||
@@ -20,7 +20,6 @@ package kunstruct
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
@@ -80,8 +79,12 @@ func (fs *UnstructAdapter) SetMap(m map[string]interface{}) {
|
|||||||
|
|
||||||
// GetFieldValue returns value at the given fieldpath.
|
// GetFieldValue returns value at the given fieldpath.
|
||||||
func (fs *UnstructAdapter) GetFieldValue(path string) (string, error) {
|
func (fs *UnstructAdapter) GetFieldValue(path string) (string, error) {
|
||||||
|
fields, err := parseFields(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
s, found, err := unstructured.NestedString(
|
s, found, err := unstructured.NestedString(
|
||||||
fs.UnstructuredContent(), strings.Split(path, ".")...)
|
fs.UnstructuredContent(), fields...)
|
||||||
if found || err != nil {
|
if found || err != nil {
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user