Skip to content

Commit 71c3cf1

Browse files
committed
use local types in var reference
1 parent b954232 commit 71c3cf1

File tree

4 files changed

+132
-12
lines changed

4 files changed

+132
-12
lines changed

pkg/expansion/expand_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ package expansion
1818

1919
import (
2020
"testing"
21-
22-
api "k8s.io/api/core/v1"
2321
)
2422

2523
func TestMapReference(t *testing.T) {
26-
envs := []api.EnvVar{
24+
type env struct {
25+
Name string
26+
Value string
27+
}
28+
envs := []env{
2729
{
2830
Name: "FOO",
2931
Value: "bar",

pkg/target/kusttarget.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"sigs.k8s.io/kustomize/pkg/constants"
3030
"sigs.k8s.io/kustomize/pkg/crds"
3131
"sigs.k8s.io/kustomize/pkg/fs"
32-
"sigs.k8s.io/kustomize/pkg/gvk"
3332
interror "sigs.k8s.io/kustomize/pkg/internal/error"
3433
"sigs.k8s.io/kustomize/pkg/loader"
3534
"sigs.k8s.io/kustomize/pkg/patch"
@@ -296,8 +295,7 @@ func (kt *KustTarget) resolveRefVars(m resmap.ResMap) (map[string]string, error)
296295
return result, err
297296
}
298297
for _, v := range vars {
299-
id := resource.NewResId(
300-
gvk.FromSchemaGvk(v.ObjRef.GroupVersionKind()), v.ObjRef.Name)
298+
id := resource.NewResId(v.ObjRef.GVK(), v.ObjRef.Name)
301299
if r, found := m.DemandOneMatchForId(id); found {
302300
s, err := r.GetFieldValue(v.FieldRef.FieldPath)
303301
if err != nil {

pkg/types/var.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ limitations under the License.
1717
package types
1818

1919
import (
20-
corev1 "k8s.io/api/core/v1"
20+
"sigs.k8s.io/kustomize/pkg/gvk"
21+
"strings"
2122
)
2223

2324
// Var represents a variable whose value will be sourced
@@ -31,18 +32,51 @@ type Var struct {
3132
// purview of this kustomization. ObjRef should use the
3233
// raw name of the object (the name specified in its YAML,
3334
// before addition of a namePrefix).
34-
ObjRef corev1.ObjectReference `json:"objref" yaml:"objref"`
35+
ObjRef Target `json:"objref" yaml:"objref"`
3536

3637
// FieldRef refers to the field of the object referred to by
3738
// ObjRef whose value will be extracted for use in
3839
// replacing $(FOO).
39-
// If unspecified, this defaults to fieldpath: metadata.name
40-
FieldRef corev1.ObjectFieldSelector `json:"fieldref,omitempty" yaml:"objref,omitempty"`
40+
// If unspecified, this defaults to fieldPath: metadata.name
41+
FieldRef FieldSelector `json:"fieldref,omitempty" yaml:"fieldref,omitempty"`
42+
}
43+
44+
// Target refers to a kubernetes object by Group, Version, Kind and Name
45+
// gvk.Gvk contains Group, Version and Kind
46+
// APIVersion is added to keep the backward compatibility of using ObjectReference
47+
// for Var.ObjRef
48+
type Target struct {
49+
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
50+
gvk.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
51+
Name string `json:"name" yaml:"name"`
52+
}
53+
54+
// FieldSelector contains the fieldPath to an object field, such as metadata.name
55+
// This struct is added to keep the backward compatibility of using ObjectFieldSelector
56+
// for Var.FieldRef
57+
type FieldSelector struct {
58+
FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"`
4159
}
4260

4361
// Defaulting sets reference to field used by default.
4462
func (v *Var) Defaulting() {
45-
if (corev1.ObjectFieldSelector{}) == v.FieldRef {
46-
v.FieldRef = corev1.ObjectFieldSelector{FieldPath: "metadata.name"}
63+
if v.FieldRef.FieldPath == "" {
64+
v.FieldRef.FieldPath = "metadata.name"
65+
}
66+
}
67+
68+
// GVK returns the Gvk object in Target
69+
func (t *Target) GVK() gvk.Gvk {
70+
if t.APIVersion == "" {
71+
return t.Gvk
72+
}
73+
versions := strings.Split(t.APIVersion, "/")
74+
if len(versions) == 2 {
75+
t.Group = versions[0]
76+
t.Version = versions[1]
77+
}
78+
if len(versions) == 1 {
79+
t.Version = versions[0]
4780
}
81+
return t.Gvk
4882
}

pkg/types/var_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package types
18+
19+
import (
20+
"gopkg.in/yaml.v2"
21+
"reflect"
22+
"sigs.k8s.io/kustomize/pkg/gvk"
23+
"testing"
24+
)
25+
26+
func TestGVK(t *testing.T) {
27+
type testcase struct {
28+
data string
29+
expected gvk.Gvk
30+
}
31+
32+
testcases := []testcase{
33+
{
34+
data: `
35+
apiVersion: v1
36+
kind: Secret
37+
name: my-secret
38+
`,
39+
expected: gvk.Gvk{Group: "", Version: "v1", Kind: "Secret"},
40+
},
41+
{
42+
data: `
43+
apiVersion: myapps/v1
44+
kind: MyKind
45+
name: my-kind
46+
`,
47+
expected: gvk.Gvk{Group: "myapps", Version: "v1", Kind: "MyKind"},
48+
},
49+
{
50+
data: `
51+
version: v2
52+
kind: MyKind
53+
name: my-kind
54+
`,
55+
expected: gvk.Gvk{Version: "v2", Kind: "MyKind"},
56+
},
57+
}
58+
59+
for _, tc := range testcases {
60+
var targ Target
61+
err := yaml.Unmarshal([]byte(tc.data), &targ)
62+
if err != nil {
63+
t.Fatalf("Unexpected error %v", err)
64+
}
65+
if !reflect.DeepEqual(targ.GVK(), tc.expected) {
66+
t.Fatalf("Expected %v, but got %v", tc.expected, targ.GVK())
67+
}
68+
}
69+
}
70+
71+
func TestDefaulting(t *testing.T) {
72+
v := &Var{
73+
Name: "SOME_VARIABLE_NAME",
74+
ObjRef: Target{
75+
Gvk: gvk.Gvk{
76+
Version: "v1",
77+
Kind: "Secret",
78+
},
79+
Name: "my-secret",
80+
},
81+
}
82+
v.Defaulting()
83+
if v.FieldRef.FieldPath != "metadata.name" {
84+
t.Fatalf("var defaulting doesn't behave correctly.\n expected metadata.name, but got %v", v.FieldRef.FieldPath)
85+
}
86+
}

0 commit comments

Comments
 (0)