| 1 |
// Copyright 2021 The Kubernetes Authors. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package types |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"sigs.k8s.io/kustomize/kyaml/resid" |
| 11 |
) |
| 12 |
|
| 13 |
const DefaultReplacementFieldPath = "metadata.name" |
| 14 |
|
| 15 |
// Replacement defines how to perform a substitution |
| 16 |
// where it is from and where it is to. |
| 17 |
type Replacement struct { |
| 18 |
// The source of the value. |
| 19 |
Source *SourceSelector `json:"source,omitempty" yaml:"source,omitempty"` |
| 20 |
|
| 21 |
// The N fields to write the value to. |
| 22 |
Targets []*TargetSelector `json:"targets,omitempty" yaml:"targets,omitempty"` |
| 23 |
|
| 24 |
// Used to define an static value |
| 25 |
SourceValue *string `json:"sourceValue,omitempty" yaml:"sourceValue,omitempty"` |
| 26 |
} |
| 27 |
|
| 28 |
// SourceSelector is the source of the replacement transformer. |
| 29 |
type SourceSelector struct { |
| 30 |
// A specific object to read it from. |
| 31 |
resid.ResId `json:",inline,omitempty" yaml:",inline,omitempty"` |
| 32 |
|
| 33 |
// Structured field path expected in the allowed object. |
| 34 |
FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"` |
| 35 |
|
| 36 |
// Used to refine the interpretation of the field. |
| 37 |
Options *FieldOptions `json:"options,omitempty" yaml:"options,omitempty"` |
| 38 |
} |
| 39 |
|
| 40 |
func (s *SourceSelector) String() string { |
| 41 |
if s == nil { |
| 42 |
return "" |
| 43 |
} |
| 44 |
result := []string{s.ResId.String()} |
| 45 |
if s.FieldPath != "" { |
| 46 |
result = append(result, s.FieldPath) |
| 47 |
} |
| 48 |
if opts := s.Options.String(); opts != "" { |
| 49 |
result = append(result, opts) |
| 50 |
} |
| 51 |
return strings.Join(result, ":") |
| 52 |
} |
| 53 |
|
| 54 |
// TargetSelector specifies fields in one or more objects. |
| 55 |
type TargetSelector struct { |
| 56 |
// Include objects that match this. |
| 57 |
Select *Selector `json:"select" yaml:"select"` |
| 58 |
|
| 59 |
// From the allowed set, remove objects that match this. |
| 60 |
Reject []*Selector `json:"reject,omitempty" yaml:"reject,omitempty"` |
| 61 |
|
| 62 |
// Structured field paths expected in each allowed object. |
| 63 |
FieldPaths []string `json:"fieldPaths,omitempty" yaml:"fieldPaths,omitempty"` |
| 64 |
|
| 65 |
// Used to refine the interpretation of the field. |
| 66 |
Options *FieldOptions `json:"options,omitempty" yaml:"options,omitempty"` |
| 67 |
} |
| 68 |
|
| 69 |
type TargetSelectorRegex struct { |
| 70 |
targetSelector *TargetSelector |
| 71 |
selectRegex *SelectorRegex |
| 72 |
rejectRegex []*SelectorRegex |
| 73 |
} |
| 74 |
|
| 75 |
func NewTargetSelectorRegex(ts *TargetSelector) (*TargetSelectorRegex, error) { |
| 76 |
tsr := new(TargetSelectorRegex) |
| 77 |
tsr.targetSelector = ts |
| 78 |
var err error |
| 79 |
|
| 80 |
tsr.selectRegex, err = NewSelectorRegex(ts.Select) |
| 81 |
if err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
|
| 85 |
rej := []*SelectorRegex{} |
| 86 |
for _, r := range ts.Reject { |
| 87 |
rr, err := NewSelectorRegex(r) |
| 88 |
if err != nil { |
| 89 |
return nil, err |
| 90 |
} |
| 91 |
rej = append(rej, rr) |
| 92 |
} |
| 93 |
tsr.rejectRegex = rej |
| 94 |
|
| 95 |
return tsr, nil |
| 96 |
} |
| 97 |
|
| 98 |
func (tsr *TargetSelectorRegex) Selects(id resid.ResId) bool { |
| 99 |
return tsr.selectRegex.MatchGvk(id.Gvk) && tsr.selectRegex.MatchName(id.Name) && tsr.selectRegex.MatchNamespace(id.Namespace) |
| 100 |
} |
| 101 |
|
| 102 |
func (tsr *TargetSelectorRegex) RejectsAny(ids []resid.ResId) bool { |
| 103 |
for _, r := range tsr.rejectRegex { |
| 104 |
if r.selector.ResId.IsEmpty() { |
| 105 |
continue |
| 106 |
} |
| 107 |
for _, id := range ids { |
| 108 |
if r.MatchGvk(id.Gvk) && r.MatchName(id.Name) && r.MatchNamespace(id.Namespace) { |
| 109 |
return true |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
return false |
| 114 |
} |
| 115 |
|
| 116 |
// FieldOptions refine the interpretation of FieldPaths. |
| 117 |
type FieldOptions struct { |
| 118 |
// Used to split/join the field. |
| 119 |
Delimiter string `json:"delimiter,omitempty" yaml:"delimiter,omitempty"` |
| 120 |
|
| 121 |
// Which position in the split to consider. |
| 122 |
Index int `json:"index,omitempty" yaml:"index,omitempty"` |
| 123 |
|
| 124 |
// TODO (#3492): Implement use of this option |
| 125 |
// None, Base64, URL, Hex, etc |
| 126 |
Encoding string `json:"encoding,omitempty" yaml:"encoding,omitempty"` |
| 127 |
|
| 128 |
// If field missing, add it. |
| 129 |
Create bool `json:"create,omitempty" yaml:"create,omitempty"` |
| 130 |
} |
| 131 |
|
| 132 |
func (fo *FieldOptions) String() string { |
| 133 |
if fo == nil || (fo.Delimiter == "" && !fo.Create) { |
| 134 |
return "" |
| 135 |
} |
| 136 |
return fmt.Sprintf("%s(%d), create=%t", fo.Delimiter, fo.Index, fo.Create) |
| 137 |
} |