Use resid.ResId in replacements.

This commit is contained in:
monopole
2021-04-22 20:41:18 -07:00
parent d5d44ce3fe
commit c828b1e49a
10 changed files with 195 additions and 84 deletions

View File

@@ -9,7 +9,7 @@ import (
func TestPatchEquals(t *testing.T) {
selector := Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "group",
Version: "version",
@@ -40,7 +40,7 @@ func TestPatchEquals(t *testing.T) {
Path: "foo",
Patch: "bar",
Target: &Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "group",
Version: "version",
@@ -57,7 +57,7 @@ func TestPatchEquals(t *testing.T) {
Path: "foo",
Patch: "bar",
Target: &Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "group",
Version: "version",

View File

@@ -3,6 +3,13 @@
package types
import (
"fmt"
"strings"
"sigs.k8s.io/kustomize/api/resid"
)
const DefaultReplacementFieldPath = "metadata.name"
// Replacement defines how to perform a substitution
@@ -18,7 +25,7 @@ type Replacement struct {
// SourceSelector is the source of the replacement transformer.
type SourceSelector struct {
// A specific object to read it from.
KrmId `json:",inline,omitempty" yaml:",inline,omitempty"`
resid.ResId `json:",inline,omitempty" yaml:",inline,omitempty"`
// Structured field path expected in the allowed object.
FieldPath string `json:"fieldPath" yaml:"fieldPath"`
@@ -27,6 +34,20 @@ type SourceSelector struct {
Options *FieldOptions `json:"options" yaml:"options"`
}
func (s *SourceSelector) String() string {
if s == nil {
return ""
}
result := []string{s.ResId.String()}
if s.FieldPath != "" {
result = append(result, s.FieldPath)
}
if opts := s.Options.String(); opts != "" {
result = append(result, opts)
}
return strings.Join(result, ":")
}
// TargetSelector specifies fields in one or more objects.
type TargetSelector struct {
// Include objects that match this.
@@ -57,3 +78,10 @@ type FieldOptions struct {
// If field missing, add it.
Create bool `json:"create" yaml:"create"`
}
func (fo *FieldOptions) String() string {
if fo == nil || fo.Delimiter == "" {
return ""
}
return fmt.Sprintf("%s(%d)", fo.Delimiter, fo.Index)
}

View File

@@ -4,6 +4,7 @@
package types
import (
"fmt"
"regexp"
"sigs.k8s.io/kustomize/api/resid"
@@ -13,8 +14,8 @@ import (
// Any resource that matches intersection of all conditions
// is included in this set.
type Selector struct {
// KrmId refers to a GVKN/Ns of a resource.
KrmId `json:",inline,omitempty" yaml:",inline,omitempty"`
// ResId refers to a GVKN/Ns of a resource.
resid.ResId `json:",inline,omitempty" yaml:",inline,omitempty"`
// AnnotationSelector is a string that follows the label selection expression
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#api
@@ -27,21 +28,9 @@ type Selector struct {
LabelSelector string `json:"labelSelector,omitempty" yaml:"labelSelector,omitempty"`
}
// KrmId refers to a GVKN/Ns of a resource.
type KrmId struct {
resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"`
}
// Match returns true if id selects other, i.e. id's fields
// either match other's or are empty
func (id *KrmId) Match(other *KrmId) bool {
return (id.Group == "" || id.Group == other.Group) &&
(id.Version == "" || id.Version == other.Version) &&
(id.Kind == "" || id.Kind == other.Kind) &&
(id.Name == "" || id.Name == other.Name) &&
(id.Namespace == "" || id.Namespace == other.Namespace)
func (s *Selector) String() string {
return fmt.Sprintf(
"%s:a=%s:l=%s", s.ResId, s.AnnotationSelector, s.LabelSelector)
}
// SelectorRegex is a Selector with regex in GVK

View File

@@ -15,7 +15,7 @@ func TestSelectorRegexMatchGvk(t *testing.T) {
}{
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "group",
Version: "version",
@@ -32,7 +32,7 @@ func TestSelectorRegexMatchGvk(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "group",
Version: "",
@@ -49,7 +49,7 @@ func TestSelectorRegexMatchGvk(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "group",
Version: "version",
@@ -66,7 +66,7 @@ func TestSelectorRegexMatchGvk(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "group",
Version: "version",
@@ -83,7 +83,7 @@ func TestSelectorRegexMatchGvk(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "g.*",
Version: "\\d+",
@@ -100,7 +100,7 @@ func TestSelectorRegexMatchGvk(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Gvk: resid.Gvk{
Group: "g.*",
Version: "\\d+",
@@ -137,7 +137,7 @@ func TestSelectorRegexMatchName(t *testing.T) {
}{
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Name: "foo",
Namespace: "bar",
},
@@ -147,7 +147,7 @@ func TestSelectorRegexMatchName(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Name: "foo",
Namespace: "bar",
},
@@ -157,7 +157,7 @@ func TestSelectorRegexMatchName(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Name: "f.*",
},
},
@@ -166,7 +166,7 @@ func TestSelectorRegexMatchName(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Name: "b.*",
},
},
@@ -194,7 +194,7 @@ func TestSelectorRegexMatchNamespace(t *testing.T) {
}{
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Name: "bar",
Namespace: "foo",
},
@@ -204,7 +204,7 @@ func TestSelectorRegexMatchNamespace(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Name: "foo",
Namespace: "bar",
},
@@ -214,7 +214,7 @@ func TestSelectorRegexMatchNamespace(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Namespace: "f.*",
},
},
@@ -223,7 +223,7 @@ func TestSelectorRegexMatchNamespace(t *testing.T) {
},
{
S: Selector{
KrmId: KrmId{
ResId: resid.ResId{
Namespace: "b.*",
},
},