working commit
This commit is contained in:
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
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.
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package annotations
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/filters/fsslice"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
type annoMap map[string]string
|
||||
|
||||
type Filter struct {
|
||||
// Annotations is the set of annotations to apply to the inputs
|
||||
Annotations annoMap `yaml:"annotations,omitempty"`
|
||||
|
||||
// FsSlice contains the FieldSpecs to locate the namespace field
|
||||
FsSlice types.FsSlice
|
||||
|
||||
trackableSetter filtersutil.TrackableSetter
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
var _ kio.TrackableFilter = &Filter{}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (f *Filter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
|
||||
f.trackableSetter.WithMutationTracker(callback)
|
||||
}
|
||||
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
keys := yaml.SortedMapKeys(f.Annotations)
|
||||
_, err := kio.FilterAll(yaml.FilterFunc(
|
||||
func(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
for _, k := range keys {
|
||||
if err := node.PipeE(fsslice.Filter{
|
||||
FsSlice: f.FsSlice,
|
||||
SetValue: f.trackableSetter.SetEntry(
|
||||
k, f.Annotations[k], yaml.NodeTagString),
|
||||
CreateKind: yaml.MappingNode, // Annotations are MappingNodes.
|
||||
CreateTag: yaml.NodeTagMap,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return node, nil
|
||||
})).Filter(nodes)
|
||||
return nodes, err
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package annotations contains a kio.Filter implementation of the kustomize
|
||||
// annotations transformer.
|
||||
package annotations
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package fieldspec contains a yaml.Filter to modify a resource
|
||||
// that matches the FieldSpec.
|
||||
package fieldspec
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package fieldspec
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
"sigs.k8s.io/kustomize/kyaml/utils"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
var _ yaml.Filter = Filter{}
|
||||
|
||||
// Filter possibly mutates its object argument using a FieldSpec.
|
||||
// If the object matches the FieldSpec, and the node found
|
||||
// by following the fieldSpec's path is non-null, this filter calls
|
||||
// the setValue function on the node at the end of the path.
|
||||
// If any part of the path doesn't exist, the filter returns
|
||||
// without doing anything and without error, unless it was set
|
||||
// to create the path. If set to create, it creates a tree of maps
|
||||
// along the path, and the leaf node gets the setValue called on it.
|
||||
// Error on GVK mismatch, empty or poorly formed path.
|
||||
// Filter expect kustomize style paths, not JSON paths.
|
||||
// Filter stores internal state and should not be reused
|
||||
type Filter struct {
|
||||
// FieldSpec contains the path to the value to set.
|
||||
FieldSpec types.FieldSpec `yaml:"fieldSpec"`
|
||||
|
||||
// Set the field using this function
|
||||
SetValue filtersutil.SetFn
|
||||
|
||||
// CreateKind defines the type of node to create if the field is not found
|
||||
CreateKind yaml.Kind
|
||||
|
||||
CreateTag string
|
||||
|
||||
// path keeps internal state about the current path
|
||||
path []string
|
||||
}
|
||||
|
||||
func (fltr Filter) Filter(obj *yaml.RNode) (*yaml.RNode, error) {
|
||||
// check if the FieldSpec applies to the object
|
||||
if match := isMatchGVK(fltr.FieldSpec, obj); !match {
|
||||
return obj, nil
|
||||
}
|
||||
fltr.path = utils.PathSplitter(fltr.FieldSpec.Path, "/")
|
||||
if err := fltr.filter(obj); err != nil {
|
||||
return nil, errors.WrapPrefixf(err,
|
||||
"considering field '%s' of object %s", fltr.FieldSpec.Path, resid.FromRNode(obj))
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
// Recursively called.
|
||||
func (fltr Filter) filter(obj *yaml.RNode) error {
|
||||
if len(fltr.path) == 0 {
|
||||
// found the field -- set its value
|
||||
return fltr.SetValue(obj)
|
||||
}
|
||||
if obj.IsTaggedNull() || obj.IsNil() {
|
||||
return nil
|
||||
}
|
||||
switch obj.YNode().Kind {
|
||||
case yaml.SequenceNode:
|
||||
return fltr.handleSequence(obj)
|
||||
case yaml.MappingNode:
|
||||
return fltr.handleMap(obj)
|
||||
case yaml.AliasNode:
|
||||
return fltr.filter(yaml.NewRNode(obj.YNode().Alias))
|
||||
default:
|
||||
return errors.Errorf("expected sequence or mapping node")
|
||||
}
|
||||
}
|
||||
|
||||
// handleMap calls filter on the map field matching the next path element
|
||||
func (fltr Filter) handleMap(obj *yaml.RNode) error {
|
||||
fieldName, isSeq := isSequenceField(fltr.path[0])
|
||||
if fieldName == "" {
|
||||
return fmt.Errorf("cannot set or create an empty field name")
|
||||
}
|
||||
// lookup the field matching the next path element
|
||||
var operation yaml.Filter
|
||||
var kind yaml.Kind
|
||||
tag := yaml.NodeTagEmpty
|
||||
switch {
|
||||
case !fltr.FieldSpec.CreateIfNotPresent || fltr.CreateKind == 0 || isSeq:
|
||||
// don't create the field if we don't find it
|
||||
operation = yaml.Lookup(fieldName)
|
||||
if isSeq {
|
||||
// The query path thinks this field should be a sequence;
|
||||
// accept this hint for use later if the tag is NodeTagNull.
|
||||
kind = yaml.SequenceNode
|
||||
}
|
||||
case len(fltr.path) <= 1:
|
||||
// create the field if it is missing: use the provided node kind
|
||||
operation = yaml.LookupCreate(fltr.CreateKind, fieldName)
|
||||
kind = fltr.CreateKind
|
||||
tag = fltr.CreateTag
|
||||
default:
|
||||
// create the field if it is missing: must be a mapping node
|
||||
operation = yaml.LookupCreate(yaml.MappingNode, fieldName)
|
||||
kind = yaml.MappingNode
|
||||
tag = yaml.NodeTagMap
|
||||
}
|
||||
|
||||
// locate (or maybe create) the field
|
||||
field, err := obj.Pipe(operation)
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "fieldName: %s", fieldName)
|
||||
}
|
||||
if field == nil {
|
||||
// No error if field not found.
|
||||
return nil
|
||||
}
|
||||
|
||||
// if the value exists, but is null and kind is set,
|
||||
// then change it to the creation type
|
||||
// TODO: update yaml.LookupCreate to support this
|
||||
if field.YNode().Tag == yaml.NodeTagNull && yaml.IsCreate(kind) {
|
||||
field.YNode().Kind = kind
|
||||
field.YNode().Tag = tag
|
||||
}
|
||||
|
||||
// copy the current fltr and change the path on the copy
|
||||
var next = fltr
|
||||
// call filter for the next path element on the matching field
|
||||
next.path = fltr.path[1:]
|
||||
return next.filter(field)
|
||||
}
|
||||
|
||||
// seq calls filter on all sequence elements
|
||||
func (fltr Filter) handleSequence(obj *yaml.RNode) error {
|
||||
if err := obj.VisitElements(func(node *yaml.RNode) error {
|
||||
// set an accurate FieldPath for nested elements
|
||||
node.AppendToFieldPath(obj.FieldPath()...)
|
||||
// recurse on each element -- re-allocating a Filter is
|
||||
// not strictly required, but is more consistent with field
|
||||
// and less likely to have side effects
|
||||
// keep the entire path -- it does not contain parts for sequences
|
||||
return fltr.filter(node)
|
||||
}); err != nil {
|
||||
return errors.WrapPrefixf(err,
|
||||
"visit traversal on path: %v", fltr.path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isSequenceField returns true if the path element is for a sequence field.
|
||||
// isSequence also returns the path element with the '[]' suffix trimmed
|
||||
func isSequenceField(name string) (string, bool) {
|
||||
shorter := strings.TrimSuffix(name, "[]")
|
||||
return shorter, shorter != name
|
||||
}
|
||||
|
||||
// isMatchGVK returns true if the fs.GVK matches the obj GVK.
|
||||
func isMatchGVK(fs types.FieldSpec, obj *yaml.RNode) bool {
|
||||
if kind := obj.GetKind(); fs.Kind != "" && fs.Kind != kind {
|
||||
// kind doesn't match
|
||||
return false
|
||||
}
|
||||
|
||||
// parse the group and version from the apiVersion field
|
||||
group, version := resid.ParseGroupVersion(obj.GetApiVersion())
|
||||
|
||||
if fs.Group != "" && fs.Group != group {
|
||||
// group doesn't match
|
||||
return false
|
||||
}
|
||||
|
||||
if fs.Version != "" && fs.Version != version {
|
||||
// version doesn't match
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package filtersutil
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// SetFn is a function that accepts an RNode to possibly modify.
|
||||
type SetFn func(*yaml.RNode) error
|
||||
|
||||
// SetScalar returns a SetFn to set a scalar value
|
||||
func SetScalar(value string) SetFn {
|
||||
return SetEntry("", value, yaml.NodeTagEmpty)
|
||||
}
|
||||
|
||||
// SetEntry returns a SetFn to set a field or a map entry to a value.
|
||||
// It can be used with an empty name to set both a value and a tag on a scalar node.
|
||||
// When setting only a value on a scalar node, use SetScalar instead.
|
||||
func SetEntry(name, value, tag string) SetFn {
|
||||
n := &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Value: value,
|
||||
Tag: tag,
|
||||
}
|
||||
return func(node *yaml.RNode) error {
|
||||
return node.PipeE(yaml.FieldSetter{
|
||||
Name: name,
|
||||
Value: yaml.NewRNode(n),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type TrackableSetter struct {
|
||||
// SetValueCallback will be invoked each time a field is set
|
||||
setValueCallback func(name, value, tag string, node *yaml.RNode)
|
||||
}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (s *TrackableSetter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) *TrackableSetter {
|
||||
s.setValueCallback = callback
|
||||
return s
|
||||
}
|
||||
|
||||
// SetScalar returns a SetFn to set a scalar value.
|
||||
// if a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time a scalar is set
|
||||
func (s TrackableSetter) SetScalar(value string) SetFn {
|
||||
return s.SetEntry("", value, yaml.NodeTagEmpty)
|
||||
}
|
||||
|
||||
// SetScalarIfEmpty returns a SetFn to set a scalar value only if it isn't already set.
|
||||
// If a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time a scalar is actually set.
|
||||
func (s TrackableSetter) SetScalarIfEmpty(value string) SetFn {
|
||||
return s.SetEntryIfEmpty("", value, yaml.NodeTagEmpty)
|
||||
}
|
||||
|
||||
// SetEntry returns a SetFn to set a field or a map entry to a value.
|
||||
// It can be used with an empty name to set both a value and a tag on a scalar node.
|
||||
// When setting only a value on a scalar node, use SetScalar instead.
|
||||
// If a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time an entry is set.
|
||||
func (s TrackableSetter) SetEntry(name, value, tag string) SetFn {
|
||||
origSetEntry := SetEntry(name, value, tag)
|
||||
return func(node *yaml.RNode) error {
|
||||
if s.setValueCallback != nil {
|
||||
s.setValueCallback(name, value, tag, node)
|
||||
}
|
||||
return origSetEntry(node)
|
||||
}
|
||||
}
|
||||
|
||||
// SetEntryIfEmpty returns a SetFn to set a field or a map entry to a value only if it isn't already set.
|
||||
// It can be used with an empty name to set both a value and a tag on a scalar node.
|
||||
// When setting only a value on a scalar node, use SetScalar instead.
|
||||
// If a mutation tracker has been registered, the tracker will be invoked each
|
||||
// time an entry is actually set.
|
||||
func (s TrackableSetter) SetEntryIfEmpty(key, value, tag string) SetFn {
|
||||
origSetEntry := SetEntry(key, value, tag)
|
||||
return func(node *yaml.RNode) error {
|
||||
if hasExistingValue(node, key) {
|
||||
return nil
|
||||
}
|
||||
if s.setValueCallback != nil {
|
||||
s.setValueCallback(key, value, tag, node)
|
||||
}
|
||||
return origSetEntry(node)
|
||||
}
|
||||
}
|
||||
|
||||
func hasExistingValue(node *yaml.RNode, key string) bool {
|
||||
if node.IsNilOrEmpty() {
|
||||
return false
|
||||
}
|
||||
if err := yaml.ErrorIfInvalid(node, yaml.ScalarNode); err == nil {
|
||||
return yaml.GetValue(node) != ""
|
||||
}
|
||||
entry := node.Field(key)
|
||||
if entry.IsNilOrEmpty() {
|
||||
return false
|
||||
}
|
||||
return yaml.GetValue(entry.Value) != ""
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package fsslice contains a yaml.Filter to modify a resource if
|
||||
// it matches one or more FieldSpec entries.
|
||||
package fsslice
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package fsslice
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
var _ yaml.Filter = Filter{}
|
||||
|
||||
// Filter ranges over an FsSlice to modify fields on a single object.
|
||||
// An FsSlice is a range of FieldSpecs. A FieldSpec is a GVK plus a path.
|
||||
type Filter struct {
|
||||
// FieldSpecList list of FieldSpecs to set
|
||||
FsSlice types.FsSlice `yaml:"fsSlice"`
|
||||
|
||||
// SetValue is called on each field that matches one of the FieldSpecs
|
||||
SetValue filtersutil.SetFn
|
||||
|
||||
// CreateKind is used to create fields that do not exist
|
||||
CreateKind yaml.Kind
|
||||
|
||||
// CreateTag is used to set the tag if encountering a null field
|
||||
CreateTag string
|
||||
}
|
||||
|
||||
func (fltr Filter) Filter(obj *yaml.RNode) (*yaml.RNode, error) {
|
||||
for i := range fltr.FsSlice {
|
||||
// apply this FieldSpec
|
||||
// create a new filter for each iteration because they
|
||||
// store internal state about the field paths
|
||||
_, err := (&fieldspec.Filter{
|
||||
FieldSpec: fltr.FsSlice[i],
|
||||
SetValue: fltr.SetValue,
|
||||
CreateKind: fltr.CreateKind,
|
||||
CreateTag: fltr.CreateTag,
|
||||
}).Filter(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package gkesagenerator contains a kio.Filter that that generates a
|
||||
// iampolicy-related resources for a given cloud provider
|
||||
package iampolicygenerator
|
||||
Generated
Vendored
+55
@@ -0,0 +1,55 @@
|
||||
// Copyright 2021 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package iampolicygenerator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
type Filter struct {
|
||||
IAMPolicyGenerator types.IAMPolicyGeneratorArgs `json:",inline,omitempty" yaml:",inline,omitempty"`
|
||||
}
|
||||
|
||||
// Filter adds a GKE service account object to nodes
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
switch f.IAMPolicyGenerator.Cloud {
|
||||
case types.GKE:
|
||||
IAMPolicyResources, err := f.generateGkeIAMPolicyResources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes = append(nodes, IAMPolicyResources...)
|
||||
default:
|
||||
return nil, fmt.Errorf("cloud provider %s not supported yet", f.IAMPolicyGenerator.Cloud)
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func (f Filter) generateGkeIAMPolicyResources() ([]*yaml.RNode, error) {
|
||||
var result []*yaml.RNode
|
||||
input := fmt.Sprintf(`
|
||||
apiVersion: v1
|
||||
kind: ServiceAccount
|
||||
metadata:
|
||||
annotations:
|
||||
iam.gke.io/gcp-service-account: %s@%s.iam.gserviceaccount.com
|
||||
name: %s
|
||||
`, f.IAMPolicyGenerator.ServiceAccount.Name,
|
||||
f.IAMPolicyGenerator.ProjectId,
|
||||
f.IAMPolicyGenerator.KubernetesService.Name)
|
||||
|
||||
if f.IAMPolicyGenerator.Namespace != "" {
|
||||
input += fmt.Sprintf("\n namespace: %s", f.IAMPolicyGenerator.Namespace)
|
||||
}
|
||||
|
||||
sa, err := yaml.Parse(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return append(result, sa), nil
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package imagetag contains two kio.Filter implementations to cover the
|
||||
// functionality of the kustomize imagetag transformer.
|
||||
//
|
||||
// Filter updates fields based on a FieldSpec and an ImageTag.
|
||||
//
|
||||
// LegacyFilter doesn't use a FieldSpec, and instead only updates image
|
||||
// references if the field is name image and it is underneath a field called
|
||||
// either containers or initContainers.
|
||||
package imagetag
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package imagetag
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/filters/fsslice"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Filter modifies an "image tag", the value used to specify the
|
||||
// name, tag, version digest etc. of (docker) container images
|
||||
// used by a pod template.
|
||||
type Filter struct {
|
||||
// imageTag is the tag we want to apply to the inputs
|
||||
// The name of the image is used as a key, and other fields
|
||||
// can specify a new name, tag, etc.
|
||||
ImageTag types.Image `json:"imageTag,omitempty" yaml:"imageTag,omitempty"`
|
||||
|
||||
// FsSlice contains the FieldSpecs to locate an image field,
|
||||
// e.g. Path: "spec/myContainers[]/image"
|
||||
FsSlice types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
|
||||
trackableSetter filtersutil.TrackableSetter
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
var _ kio.TrackableFilter = &Filter{}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (f *Filter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
|
||||
f.trackableSetter.WithMutationTracker(callback)
|
||||
}
|
||||
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
_, err := kio.FilterAll(yaml.FilterFunc(f.filter)).Filter(nodes)
|
||||
return nodes, err
|
||||
}
|
||||
|
||||
func (f Filter) filter(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
// FsSlice is an allowlist, not a denyList, so to deny
|
||||
// something via configuration a new config mechanism is
|
||||
// needed. Until then, hardcode it.
|
||||
if f.isOnDenyList(node) {
|
||||
return node, nil
|
||||
}
|
||||
if err := node.PipeE(fsslice.Filter{
|
||||
FsSlice: f.FsSlice,
|
||||
SetValue: imageTagUpdater{
|
||||
ImageTag: f.ImageTag,
|
||||
trackableSetter: f.trackableSetter,
|
||||
}.SetImageValue,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func (f Filter) isOnDenyList(node *yaml.RNode) bool {
|
||||
meta, err := node.GetMeta()
|
||||
if err != nil {
|
||||
// A missing 'meta' field will cause problems elsewhere;
|
||||
// ignore it here to keep the signature simple.
|
||||
return false
|
||||
}
|
||||
// Ignore CRDs
|
||||
// https://github.com/kubernetes-sigs/kustomize/issues/890
|
||||
return meta.Kind == `CustomResourceDefinition`
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package imagetag
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/internal/utils"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// LegacyFilter is an implementation of the kio.Filter interface
|
||||
// that scans through the provided kyaml data structure and updates
|
||||
// any values of any image fields that is inside a sequence under
|
||||
// a field called either containers or initContainers. The field is only
|
||||
// update if it has a value that matches and image reference and the name
|
||||
// of the image is a match with the provided ImageTag.
|
||||
type LegacyFilter struct {
|
||||
ImageTag types.Image `json:"imageTag,omitempty" yaml:"imageTag,omitempty"`
|
||||
}
|
||||
|
||||
var _ kio.Filter = LegacyFilter{}
|
||||
|
||||
func (lf LegacyFilter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
return kio.FilterAll(yaml.FilterFunc(lf.filter)).Filter(nodes)
|
||||
}
|
||||
|
||||
func (lf LegacyFilter) filter(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
meta, err := node.GetMeta()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// We do not make any changes if the type of the resource
|
||||
// is CustomResourceDefinition.
|
||||
if meta.Kind == `CustomResourceDefinition` {
|
||||
return node, nil
|
||||
}
|
||||
|
||||
fff := findFieldsFilter{
|
||||
fields: []string{"containers", "initContainers"},
|
||||
fieldCallback: checkImageTagsFn(lf.ImageTag),
|
||||
}
|
||||
if err := node.PipeE(fff); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return node, nil
|
||||
}
|
||||
|
||||
type fieldCallback func(node *yaml.RNode) error
|
||||
|
||||
// findFieldsFilter is an implementation of the kio.Filter
|
||||
// interface. It will walk the data structure and look for fields
|
||||
// that matches the provided list of field names. For each match,
|
||||
// the value of the field will be passed in as a parameter to the
|
||||
// provided fieldCallback.
|
||||
// TODO: move this to kyaml/filterutils
|
||||
type findFieldsFilter struct {
|
||||
fields []string
|
||||
|
||||
fieldCallback fieldCallback
|
||||
}
|
||||
|
||||
func (f findFieldsFilter) Filter(obj *yaml.RNode) (*yaml.RNode, error) {
|
||||
return obj, f.walk(obj)
|
||||
}
|
||||
|
||||
func (f findFieldsFilter) walk(node *yaml.RNode) error {
|
||||
switch node.YNode().Kind {
|
||||
case yaml.MappingNode:
|
||||
return node.VisitFields(func(n *yaml.MapNode) error {
|
||||
err := f.walk(n.Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
key := n.Key.YNode().Value
|
||||
if utils.StringSliceContains(f.fields, key) {
|
||||
return f.fieldCallback(n.Value)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
case yaml.SequenceNode:
|
||||
return errors.Wrap(node.VisitElements(f.walk))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkImageTagsFn(imageTag types.Image) fieldCallback {
|
||||
return func(node *yaml.RNode) error {
|
||||
if node.YNode().Kind != yaml.SequenceNode {
|
||||
return nil
|
||||
}
|
||||
|
||||
return node.VisitElements(func(n *yaml.RNode) error {
|
||||
// Look up any fields on the provided node that is named
|
||||
// image.
|
||||
return n.PipeE(yaml.Get("image"), imageTagUpdater{
|
||||
ImageTag: imageTag,
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package imagetag
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/internal/image"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// imageTagUpdater is an implementation of the kio.Filter interface
|
||||
// that will update the value of the yaml node based on the provided
|
||||
// ImageTag if the current value matches the format of an image reference.
|
||||
type imageTagUpdater struct {
|
||||
Kind string `yaml:"kind,omitempty"`
|
||||
ImageTag types.Image `yaml:"imageTag,omitempty"`
|
||||
trackableSetter filtersutil.TrackableSetter
|
||||
}
|
||||
|
||||
func (u imageTagUpdater) SetImageValue(rn *yaml.RNode) error {
|
||||
if err := yaml.ErrorIfInvalid(rn, yaml.ScalarNode); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
value := rn.YNode().Value
|
||||
|
||||
if !image.IsImageMatched(value, u.ImageTag.Name) {
|
||||
return nil
|
||||
}
|
||||
|
||||
name, tag, digest := image.Split(value)
|
||||
if u.ImageTag.NewName != "" {
|
||||
name = u.ImageTag.NewName
|
||||
}
|
||||
|
||||
// overriding tag or digest will replace both original tag and digest values
|
||||
switch {
|
||||
case u.ImageTag.NewTag != "" && u.ImageTag.Digest != "":
|
||||
tag = u.ImageTag.NewTag
|
||||
digest = u.ImageTag.Digest
|
||||
case u.ImageTag.NewTag != "":
|
||||
tag = u.ImageTag.NewTag
|
||||
digest = ""
|
||||
case u.ImageTag.Digest != "":
|
||||
tag = ""
|
||||
digest = u.ImageTag.Digest
|
||||
case u.ImageTag.TagSuffix != "":
|
||||
tag += u.ImageTag.TagSuffix
|
||||
digest = ""
|
||||
}
|
||||
|
||||
// build final image name
|
||||
if tag != "" {
|
||||
name += ":" + tag
|
||||
}
|
||||
if digest != "" {
|
||||
name += "@" + digest
|
||||
}
|
||||
|
||||
return u.trackableSetter.SetScalar(name)(rn)
|
||||
}
|
||||
|
||||
func (u imageTagUpdater) Filter(rn *yaml.RNode) (*yaml.RNode, error) {
|
||||
if err := u.SetImageValue(rn); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rn, nil
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package labels contains a kio.Filter implementation of the kustomize
|
||||
// labels transformer.
|
||||
package labels
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package labels
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/filters/fsslice"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
type labelMap map[string]string
|
||||
|
||||
// Filter sets labels.
|
||||
type Filter struct {
|
||||
// Labels is the set of labels to apply to the inputs
|
||||
Labels labelMap `yaml:"labels,omitempty"`
|
||||
|
||||
// FsSlice identifies the label fields.
|
||||
FsSlice types.FsSlice
|
||||
|
||||
trackableSetter filtersutil.TrackableSetter
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
var _ kio.TrackableFilter = &Filter{}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (f *Filter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
|
||||
f.trackableSetter.WithMutationTracker(callback)
|
||||
}
|
||||
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
keys := yaml.SortedMapKeys(f.Labels)
|
||||
_, err := kio.FilterAll(yaml.FilterFunc(
|
||||
func(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
for _, k := range keys {
|
||||
if err := node.PipeE(fsslice.Filter{
|
||||
FsSlice: f.FsSlice,
|
||||
SetValue: f.trackableSetter.SetEntry(
|
||||
k, f.Labels[k], yaml.NodeTagString),
|
||||
CreateKind: yaml.MappingNode, // Labels are MappingNodes.
|
||||
CreateTag: yaml.NodeTagMap,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return node, nil
|
||||
})).Filter(nodes)
|
||||
return nodes, err
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package nameref contains a kio.Filter implementation of the kustomize
|
||||
// name reference transformer.
|
||||
package nameref
|
||||
+414
@@ -0,0 +1,414 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package nameref
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Filter updates a name references.
|
||||
type Filter struct {
|
||||
// Referrer refers to another resource X by X's name.
|
||||
// E.g. A Deployment can refer to a ConfigMap.
|
||||
// The Deployment is the Referrer,
|
||||
// the ConfigMap is the ReferralTarget.
|
||||
// This filter seeks to repair the reference in Deployment, given
|
||||
// that the ConfigMap's name may have changed.
|
||||
Referrer *resource.Resource
|
||||
|
||||
// NameFieldToUpdate is the field in the Referrer
|
||||
// that holds the name requiring an update.
|
||||
// This is the field to write.
|
||||
NameFieldToUpdate types.FieldSpec
|
||||
|
||||
// ReferralTarget is the source of the new value for
|
||||
// the name, always in the 'metadata/name' field.
|
||||
// This is the field to read.
|
||||
ReferralTarget resid.Gvk
|
||||
|
||||
// Set of resources to scan to find the ReferralTarget.
|
||||
ReferralCandidates resmap.ResMap
|
||||
}
|
||||
|
||||
// At time of writing, in practice this is called with a slice with only
|
||||
// one entry, the node also referred to be the resource in the Referrer field.
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
return kio.FilterAll(yaml.FilterFunc(f.run)).Filter(nodes)
|
||||
}
|
||||
|
||||
// The node passed in here is the same node as held in Referrer;
|
||||
// that's how the referrer's name field is updated.
|
||||
// Currently, however, this filter still needs the extra methods on Referrer
|
||||
// to consult things like the resource Id, its namespace, etc.
|
||||
// TODO(3455): No filter should use the Resource api; all information
|
||||
// about names should come from annotations, with helper methods
|
||||
// on the RNode object. Resource should get stupider, RNode smarter.
|
||||
func (f Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
if err := f.confirmNodeMatchesReferrer(node); err != nil {
|
||||
// sanity check.
|
||||
return nil, err
|
||||
}
|
||||
f.NameFieldToUpdate.Gvk = f.Referrer.GetGvk()
|
||||
if err := node.PipeE(fieldspec.Filter{
|
||||
FieldSpec: f.NameFieldToUpdate,
|
||||
SetValue: f.set,
|
||||
}); err != nil {
|
||||
return nil, errors.WrapPrefixf(
|
||||
err, "updating name reference in '%s' field of '%s'",
|
||||
f.NameFieldToUpdate.Path, f.Referrer.CurId().String())
|
||||
}
|
||||
return node, nil
|
||||
}
|
||||
|
||||
// This function is called on the node found at FieldSpec.Path.
|
||||
// It's some node in the Referrer.
|
||||
func (f Filter) set(node *yaml.RNode) error {
|
||||
if yaml.IsMissingOrNull(node) {
|
||||
return nil
|
||||
}
|
||||
switch node.YNode().Kind {
|
||||
case yaml.ScalarNode:
|
||||
return f.setScalar(node)
|
||||
case yaml.MappingNode:
|
||||
return f.setMapping(node)
|
||||
case yaml.SequenceNode:
|
||||
return applyFilterToSeq(seqFilter{
|
||||
setScalarFn: f.setScalar,
|
||||
setMappingFn: f.setMapping,
|
||||
}, node)
|
||||
default:
|
||||
return fmt.Errorf("node must be a scalar, sequence or map")
|
||||
}
|
||||
}
|
||||
|
||||
// This method used when NameFieldToUpdate doesn't lead to
|
||||
// one scalar field (typically called 'name'), but rather
|
||||
// leads to a map field (called anything). In this case we
|
||||
// must complete the field path, looking for both a 'name'
|
||||
// and a 'namespace' field to help select the proper
|
||||
// ReferralTarget to read the name and namespace from.
|
||||
func (f Filter) setMapping(node *yaml.RNode) error {
|
||||
if node.YNode().Kind != yaml.MappingNode {
|
||||
return fmt.Errorf("expect a mapping node")
|
||||
}
|
||||
nameNode, err := node.Pipe(yaml.FieldMatcher{Name: "name"})
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "trying to match 'name' field")
|
||||
}
|
||||
if nameNode == nil {
|
||||
// This is a _configuration_ error; the field path
|
||||
// specified in NameFieldToUpdate.Path doesn't resolve
|
||||
// to a map with a 'name' field, so we have no idea what
|
||||
// field to update with a new name.
|
||||
return fmt.Errorf("path config error; no 'name' field in node")
|
||||
}
|
||||
candidates, err := f.filterMapCandidatesByNamespace(node)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
oldName := nameNode.YNode().Value
|
||||
// use allNamesAndNamespacesAreTheSame to compare referral candidates for functional identity,
|
||||
// because we source both name and namespace values from the referral in this case.
|
||||
referral, err := f.selectReferral(oldName, candidates, allNamesAndNamespacesAreTheSame)
|
||||
if err != nil || referral == nil {
|
||||
// Nil referral means nothing to do.
|
||||
return err
|
||||
}
|
||||
f.recordTheReferral(referral)
|
||||
if referral.GetName() == oldName && referral.GetNamespace() == "" {
|
||||
// The name has not changed, nothing to do.
|
||||
return nil
|
||||
}
|
||||
if err = node.PipeE(yaml.FieldSetter{
|
||||
Name: "name",
|
||||
StringValue: referral.GetName(),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if referral.GetNamespace() == "" {
|
||||
// Don't write an empty string into the namespace field, as
|
||||
// it should not replace the value "default". The empty
|
||||
// string is handled as a wild card here, not as an implicit
|
||||
// specification of the "default" k8s namespace.
|
||||
return nil
|
||||
}
|
||||
return node.PipeE(yaml.FieldSetter{
|
||||
Name: "namespace",
|
||||
StringValue: referral.GetNamespace(),
|
||||
})
|
||||
}
|
||||
|
||||
func (f Filter) filterMapCandidatesByNamespace(
|
||||
node *yaml.RNode) ([]*resource.Resource, error) {
|
||||
namespaceNode, err := node.Pipe(yaml.FieldMatcher{Name: "namespace"})
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "trying to match 'namespace' field")
|
||||
}
|
||||
if namespaceNode == nil {
|
||||
return f.ReferralCandidates.Resources(), nil
|
||||
}
|
||||
namespace := namespaceNode.YNode().Value
|
||||
nsMap := f.ReferralCandidates.GroupedByOriginalNamespace()
|
||||
if candidates, ok := nsMap[namespace]; ok {
|
||||
return candidates, nil
|
||||
}
|
||||
nsMap = f.ReferralCandidates.GroupedByCurrentNamespace()
|
||||
// This could be nil, or an empty list.
|
||||
return nsMap[namespace], nil
|
||||
}
|
||||
|
||||
func (f Filter) setScalar(node *yaml.RNode) error {
|
||||
// use allNamesAreTheSame to compare referral candidates for functional identity,
|
||||
// because we only source the name from the referral in this case.
|
||||
referral, err := f.selectReferral(
|
||||
node.YNode().Value, f.ReferralCandidates.Resources(), allNamesAreTheSame)
|
||||
if err != nil || referral == nil {
|
||||
// Nil referral means nothing to do.
|
||||
return err
|
||||
}
|
||||
f.recordTheReferral(referral)
|
||||
if referral.GetName() == node.YNode().Value {
|
||||
// The name has not changed, nothing to do.
|
||||
return nil
|
||||
}
|
||||
return node.PipeE(yaml.FieldSetter{StringValue: referral.GetName()})
|
||||
}
|
||||
|
||||
// In the resource, make a note that it is referred to by the Referrer.
|
||||
func (f Filter) recordTheReferral(referral *resource.Resource) {
|
||||
referral.AppendRefBy(f.Referrer.CurId())
|
||||
}
|
||||
|
||||
// getRoleRefGvk returns a Gvk in the roleRef field. Return error
|
||||
// if the roleRef, roleRef/apiGroup or roleRef/kind is missing.
|
||||
func getRoleRefGvk(n *resource.Resource) (*resid.Gvk, error) {
|
||||
roleRef, err := n.Pipe(yaml.Lookup("roleRef"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if roleRef.IsNil() {
|
||||
return nil, fmt.Errorf("roleRef cannot be found in %s", n.MustString())
|
||||
}
|
||||
apiGroup, err := roleRef.Pipe(yaml.Lookup("apiGroup"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if apiGroup.IsNil() {
|
||||
return nil, fmt.Errorf("apiGroup cannot be found in roleRef %s", roleRef.MustString())
|
||||
}
|
||||
kind, err := roleRef.Pipe(yaml.Lookup("kind"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if kind.IsNil() {
|
||||
return nil, fmt.Errorf("kind cannot be found in roleRef %s", roleRef.MustString())
|
||||
}
|
||||
return &resid.Gvk{
|
||||
Group: apiGroup.YNode().Value,
|
||||
Kind: kind.YNode().Value,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// sieveFunc returns true if the resource argument satisfies some criteria.
|
||||
type sieveFunc func(*resource.Resource) bool
|
||||
|
||||
// doSieve uses a function to accept or ignore resources from a list.
|
||||
// If list is nil, returns immediately.
|
||||
// It's a filter obviously, but that term is overloaded here.
|
||||
func doSieve(list []*resource.Resource, fn sieveFunc) (s []*resource.Resource) {
|
||||
for _, r := range list {
|
||||
if fn(r) {
|
||||
s = append(s, r)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func acceptAll(r *resource.Resource) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func previousNameMatches(name string) sieveFunc {
|
||||
return func(r *resource.Resource) bool {
|
||||
for _, id := range r.PrevIds() {
|
||||
if id.Name == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func previousIdSelectedByGvk(gvk *resid.Gvk) sieveFunc {
|
||||
return func(r *resource.Resource) bool {
|
||||
for _, id := range r.PrevIds() {
|
||||
if id.IsSelected(gvk) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// If the we are updating a 'roleRef/name' field, the 'apiGroup' and 'kind'
|
||||
// fields in the same 'roleRef' map must be considered.
|
||||
// If either object is cluster-scoped, there can be a referral.
|
||||
// E.g. a RoleBinding (which exists in a namespace) can refer
|
||||
// to a ClusterRole (cluster-scoped) object.
|
||||
// https://kubernetes.io/docs/reference/access-authn-authz/rbac/#role-and-clusterrole
|
||||
// Likewise, a ClusterRole can refer to a Secret (in a namespace).
|
||||
// Objects in different namespaces generally cannot refer to other
|
||||
// with some exceptions (e.g. RoleBinding and ServiceAccount are both
|
||||
// namespaceable, but the former can refer to accounts in other namespaces).
|
||||
func (f Filter) roleRefFilter() sieveFunc {
|
||||
if !strings.HasSuffix(f.NameFieldToUpdate.Path, "roleRef/name") {
|
||||
return acceptAll
|
||||
}
|
||||
roleRefGvk, err := getRoleRefGvk(f.Referrer)
|
||||
if err != nil {
|
||||
return acceptAll
|
||||
}
|
||||
return previousIdSelectedByGvk(roleRefGvk)
|
||||
}
|
||||
|
||||
func prefixSuffixEquals(other resource.ResCtx, allowEmpty bool) sieveFunc {
|
||||
return func(r *resource.Resource) bool {
|
||||
return r.PrefixesSuffixesEquals(other, allowEmpty)
|
||||
}
|
||||
}
|
||||
|
||||
func (f Filter) sameCurrentNamespaceAsReferrer() sieveFunc {
|
||||
referrerCurId := f.Referrer.CurId()
|
||||
if referrerCurId.IsClusterScoped() {
|
||||
// If the referrer is cluster-scoped, let anything through.
|
||||
return acceptAll
|
||||
}
|
||||
return func(r *resource.Resource) bool {
|
||||
if r.CurId().IsClusterScoped() {
|
||||
// Allow cluster-scoped through.
|
||||
return true
|
||||
}
|
||||
if r.GetKind() == "ServiceAccount" {
|
||||
// Allow service accounts through, even though they
|
||||
// are in a namespace. A RoleBinding in another namespace
|
||||
// can reference them.
|
||||
return true
|
||||
}
|
||||
return referrerCurId.IsNsEquals(r.CurId())
|
||||
}
|
||||
}
|
||||
|
||||
// selectReferral picks the best referral from a list of candidates.
|
||||
func (f Filter) selectReferral(
|
||||
// The name referral that may need to be updated.
|
||||
oldName string,
|
||||
candidates []*resource.Resource,
|
||||
// function that returns whether two referrals are identical for the purposes of the transformation
|
||||
candidatesIdentical func(resources []*resource.Resource) bool) (*resource.Resource, error) {
|
||||
candidates = doSieve(candidates, previousNameMatches(oldName))
|
||||
candidates = doSieve(candidates, previousIdSelectedByGvk(&f.ReferralTarget))
|
||||
candidates = doSieve(candidates, f.roleRefFilter())
|
||||
candidates = doSieve(candidates, f.sameCurrentNamespaceAsReferrer())
|
||||
if len(candidates) == 1 {
|
||||
return candidates[0], nil
|
||||
}
|
||||
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer, true))
|
||||
if len(candidates) > 1 {
|
||||
candidates = doSieve(candidates, prefixSuffixEquals(f.Referrer, false))
|
||||
}
|
||||
if len(candidates) == 1 {
|
||||
return candidates[0], nil
|
||||
}
|
||||
if len(candidates) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
if candidatesIdentical(candidates) {
|
||||
// Just take the first one.
|
||||
return candidates[0], nil
|
||||
}
|
||||
ids := getIds(candidates)
|
||||
return nil, fmt.Errorf("found multiple possible referrals: %s\n%s", ids, f.failureDetails(candidates))
|
||||
}
|
||||
|
||||
func (f Filter) failureDetails(resources []*resource.Resource) string {
|
||||
msg := strings.Builder{}
|
||||
msg.WriteString(fmt.Sprintf("\n**** Too many possible referral targets to referrer:\n%s\n", f.Referrer.MustYaml()))
|
||||
for i, r := range resources {
|
||||
msg.WriteString(fmt.Sprintf("--- possible referral %d:\n%s\n", i, r.MustYaml()))
|
||||
}
|
||||
return msg.String()
|
||||
}
|
||||
|
||||
func allNamesAreTheSame(resources []*resource.Resource) bool {
|
||||
name := resources[0].GetName()
|
||||
for i := 1; i < len(resources); i++ {
|
||||
if name != resources[i].GetName() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func allNamesAndNamespacesAreTheSame(resources []*resource.Resource) bool {
|
||||
name := resources[0].GetName()
|
||||
namespace := resources[0].GetNamespace()
|
||||
for i := 1; i < len(resources); i++ {
|
||||
if name != resources[i].GetName() || namespace != resources[i].GetNamespace() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func getIds(rs []*resource.Resource) string {
|
||||
var result []string
|
||||
for _, r := range rs {
|
||||
result = append(result, r.CurId().String())
|
||||
}
|
||||
return strings.Join(result, ", ")
|
||||
}
|
||||
|
||||
func checkEqual(k, a, b string) error {
|
||||
if a != b {
|
||||
return fmt.Errorf(
|
||||
"node-referrerOriginal '%s' mismatch '%s' != '%s'",
|
||||
k, a, b)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Filter) confirmNodeMatchesReferrer(node *yaml.RNode) error {
|
||||
meta, err := node.GetMeta()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
gvk := f.Referrer.GetGvk()
|
||||
if err = checkEqual(
|
||||
"APIVersion", meta.APIVersion, gvk.ApiVersion()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = checkEqual(
|
||||
"Kind", meta.Kind, gvk.Kind); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = checkEqual(
|
||||
"Name", meta.Name, f.Referrer.GetName()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = checkEqual(
|
||||
"Namespace", meta.Namespace, f.Referrer.GetNamespace()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package nameref
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
type setFn func(*yaml.RNode) error
|
||||
|
||||
type seqFilter struct {
|
||||
setScalarFn setFn
|
||||
setMappingFn setFn
|
||||
}
|
||||
|
||||
func (sf seqFilter) Filter(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
if yaml.IsMissingOrNull(node) {
|
||||
return node, nil
|
||||
}
|
||||
switch node.YNode().Kind {
|
||||
case yaml.ScalarNode:
|
||||
// Kind: Role/ClusterRole
|
||||
// FieldSpec is rules.resourceNames
|
||||
err := sf.setScalarFn(node)
|
||||
return node, err
|
||||
case yaml.MappingNode:
|
||||
// Kind: RoleBinding/ClusterRoleBinding
|
||||
// FieldSpec is subjects
|
||||
// Note: The corresponding fieldSpec had been changed from
|
||||
// from path: subjects/name to just path: subjects. This is
|
||||
// what get mutatefield to request the mapping of the whole
|
||||
// map containing namespace and name instead of just a simple
|
||||
// string field containing the name
|
||||
err := sf.setMappingFn(node)
|
||||
return node, err
|
||||
default:
|
||||
return node, fmt.Errorf(
|
||||
"%#v is expected to be either a string or a map of string", node)
|
||||
}
|
||||
}
|
||||
|
||||
// applyFilterToSeq will apply the filter to each element in the sequence node
|
||||
func applyFilterToSeq(filter yaml.Filter, node *yaml.RNode) error {
|
||||
if node.YNode().Kind != yaml.SequenceNode {
|
||||
return fmt.Errorf("expect a sequence node but got %v", node.YNode().Kind)
|
||||
}
|
||||
|
||||
for _, elem := range node.Content() {
|
||||
rnode := yaml.NewRNode(elem)
|
||||
err := rnode.PipeE(filter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package namespace contains a kio.Filter implementation of the kustomize
|
||||
// namespace transformer.
|
||||
//
|
||||
// Special cases for known Kubernetes resources have been hardcoded in addition
|
||||
// to those defined by the FsSlice.
|
||||
package namespace
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package namespace
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/filters/fsslice"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
type Filter struct {
|
||||
// Namespace is the namespace to apply to the inputs
|
||||
Namespace string `yaml:"namespace,omitempty"`
|
||||
|
||||
// FsSlice contains the FieldSpecs to locate the namespace field
|
||||
FsSlice types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
|
||||
// UnsetOnly means only blank namespace fields will be set
|
||||
UnsetOnly bool `json:"unsetOnly" yaml:"unsetOnly"`
|
||||
|
||||
// SetRoleBindingSubjects determines which subject fields in RoleBinding and ClusterRoleBinding
|
||||
// objects will have their namespace fields set. Overrides field specs provided for these types, if any.
|
||||
// - defaultOnly (default): namespace will be set only on subjects named "default".
|
||||
// - allServiceAccounts: namespace will be set on all subjects with "kind: ServiceAccount"
|
||||
// - none: all subjects will be skipped.
|
||||
SetRoleBindingSubjects RoleBindingSubjectMode `json:"setRoleBindingSubjects" yaml:"setRoleBindingSubjects"`
|
||||
|
||||
trackableSetter filtersutil.TrackableSetter
|
||||
}
|
||||
|
||||
type RoleBindingSubjectMode string
|
||||
|
||||
const (
|
||||
DefaultSubjectsOnly RoleBindingSubjectMode = "defaultOnly"
|
||||
SubjectModeUnspecified RoleBindingSubjectMode = ""
|
||||
AllServiceAccountSubjects RoleBindingSubjectMode = "allServiceAccounts"
|
||||
NoSubjects RoleBindingSubjectMode = "none"
|
||||
)
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
var _ kio.TrackableFilter = &Filter{}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (ns *Filter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
|
||||
ns.trackableSetter.WithMutationTracker(callback)
|
||||
}
|
||||
|
||||
func (ns Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
return kio.FilterAll(yaml.FilterFunc(ns.run)).Filter(nodes)
|
||||
}
|
||||
|
||||
// Run runs the filter on a single node rather than a slice
|
||||
func (ns Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
// Special handling for metadata.namespace and metadata.name -- :(
|
||||
// never let SetEntry handle metadata.namespace--it will incorrectly include cluster-scoped resources
|
||||
// only update metadata.name if api version is expected one--so-as it leaves other resources of kind namespace alone
|
||||
apiVersion := node.GetApiVersion()
|
||||
ns.FsSlice = ns.removeUnneededMetaFieldSpecs(apiVersion, ns.FsSlice)
|
||||
gvk := resid.GvkFromNode(node)
|
||||
if err := ns.metaNamespaceHack(node, gvk); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Special handling for (cluster) role binding subjects -- :(
|
||||
if isRoleBinding(gvk.Kind) {
|
||||
ns.FsSlice = ns.removeRoleBindingSubjectFieldSpecs(ns.FsSlice)
|
||||
if err := ns.roleBindingHack(node); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// transformations based on data -- :)
|
||||
err := node.PipeE(fsslice.Filter{
|
||||
FsSlice: ns.FsSlice,
|
||||
SetValue: ns.fieldSetter(),
|
||||
CreateKind: yaml.ScalarNode, // Namespace is a ScalarNode
|
||||
CreateTag: yaml.NodeTagString,
|
||||
})
|
||||
invalidKindErr := &yaml.InvalidNodeKindError{}
|
||||
if err != nil && errors.As(err, &invalidKindErr) && invalidKindErr.ActualNodeKind() != yaml.ScalarNode {
|
||||
return nil, errors.WrapPrefixf(err, "namespace field specs must target scalar nodes")
|
||||
}
|
||||
return node, errors.WrapPrefixf(err, "namespace transformation failed")
|
||||
}
|
||||
|
||||
// metaNamespaceHack is a hack for implementing the namespace transform
|
||||
// for the metadata.namespace field on namespace scoped resources.
|
||||
func (ns Filter) metaNamespaceHack(obj *yaml.RNode, gvk resid.Gvk) error {
|
||||
if gvk.IsClusterScoped() {
|
||||
return nil
|
||||
}
|
||||
f := fsslice.Filter{
|
||||
FsSlice: []types.FieldSpec{
|
||||
{Path: types.MetadataNamespacePath, CreateIfNotPresent: true},
|
||||
},
|
||||
SetValue: ns.fieldSetter(),
|
||||
CreateKind: yaml.ScalarNode, // Namespace is a ScalarNode
|
||||
}
|
||||
_, err := f.Filter(obj)
|
||||
return err
|
||||
}
|
||||
|
||||
// roleBindingHack is a hack for implementing the transformer's SetRoleBindingSubjects option
|
||||
// for RoleBinding and ClusterRoleBinding resource types.
|
||||
//
|
||||
// In NoSubjects mode, it does nothing.
|
||||
//
|
||||
// In AllServiceAccountSubjects mode, it sets the namespace on subjects with "kind: ServiceAccount".
|
||||
//
|
||||
// In DefaultSubjectsOnly mode (default mode), RoleBinding and ClusterRoleBinding have namespace set on
|
||||
// elements of the "subjects" field if and only if the subject elements
|
||||
// "name" is "default". Otherwise the namespace is not set.
|
||||
// Example:
|
||||
//
|
||||
// kind: RoleBinding
|
||||
// subjects:
|
||||
// - name: "default" # this will have the namespace set
|
||||
// ...
|
||||
// - name: "something-else" # this will not have the namespace set
|
||||
// ...
|
||||
func (ns Filter) roleBindingHack(obj *yaml.RNode) error {
|
||||
var visitor filtersutil.SetFn
|
||||
switch ns.SetRoleBindingSubjects {
|
||||
case NoSubjects:
|
||||
return nil
|
||||
case DefaultSubjectsOnly, SubjectModeUnspecified:
|
||||
visitor = ns.setSubjectsNamedDefault
|
||||
case AllServiceAccountSubjects:
|
||||
visitor = ns.setServiceAccountNamespaces
|
||||
default:
|
||||
return errors.Errorf("invalid value %q for setRoleBindingSubjects: "+
|
||||
"must be one of %q, %q or %q", ns.SetRoleBindingSubjects,
|
||||
DefaultSubjectsOnly, NoSubjects, AllServiceAccountSubjects)
|
||||
}
|
||||
|
||||
// Lookup the subjects field on all elements.
|
||||
obj, err := obj.Pipe(yaml.Lookup(subjectsField))
|
||||
if err != nil || yaml.IsMissingOrNull(obj) {
|
||||
return err
|
||||
}
|
||||
// Use the appropriate visitor to set the namespace field on the correct subset of subjects
|
||||
return errors.WrapPrefixf(obj.VisitElements(visitor), "setting namespace on (cluster)role binding subjects")
|
||||
}
|
||||
|
||||
func isRoleBinding(kind string) bool {
|
||||
return kind == roleBindingKind || kind == clusterRoleBindingKind
|
||||
}
|
||||
|
||||
func (ns Filter) setServiceAccountNamespaces(o *yaml.RNode) error {
|
||||
name, err := o.Pipe(yaml.Lookup("kind"), yaml.Match("ServiceAccount"))
|
||||
if err != nil || yaml.IsMissingOrNull(name) {
|
||||
return errors.WrapPrefixf(err, "looking up kind on (cluster)role binding subject")
|
||||
}
|
||||
return setNamespaceField(o, ns.fieldSetter())
|
||||
}
|
||||
|
||||
func (ns Filter) setSubjectsNamedDefault(o *yaml.RNode) error {
|
||||
name, err := o.Pipe(yaml.Lookup("name"), yaml.Match("default"))
|
||||
if err != nil || yaml.IsMissingOrNull(name) {
|
||||
return errors.WrapPrefixf(err, "looking up name on (cluster)role binding subject")
|
||||
}
|
||||
return setNamespaceField(o, ns.fieldSetter())
|
||||
}
|
||||
|
||||
func setNamespaceField(node *yaml.RNode, setter filtersutil.SetFn) error {
|
||||
node, err := node.Pipe(yaml.LookupCreate(yaml.ScalarNode, "namespace"))
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "setting namespace field on (cluster)role binding subject")
|
||||
}
|
||||
return setter(node)
|
||||
}
|
||||
|
||||
// removeRoleBindingSubjectFieldSpecs removes from the list fieldspecs that
|
||||
// have hardcoded implementations
|
||||
func (ns Filter) removeRoleBindingSubjectFieldSpecs(fs types.FsSlice) types.FsSlice {
|
||||
var val types.FsSlice
|
||||
for i := range fs {
|
||||
if isRoleBinding(fs[i].Kind) && fs[i].Path == subjectsNamespacePath {
|
||||
continue
|
||||
}
|
||||
val = append(val, fs[i])
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (ns Filter) removeUnneededMetaFieldSpecs(apiVersion string, fs types.FsSlice) types.FsSlice {
|
||||
var val types.FsSlice
|
||||
for i := range fs {
|
||||
if fs[i].Path == types.MetadataNamespacePath {
|
||||
continue
|
||||
}
|
||||
if apiVersion != types.MetadataNamespaceApiVersion && fs[i].Path == types.MetadataNamePath {
|
||||
continue
|
||||
}
|
||||
val = append(val, fs[i])
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func (ns *Filter) fieldSetter() filtersutil.SetFn {
|
||||
if ns.UnsetOnly {
|
||||
return ns.trackableSetter.SetEntryIfEmpty("", ns.Namespace, yaml.NodeTagString)
|
||||
}
|
||||
return ns.trackableSetter.SetEntry("", ns.Namespace, yaml.NodeTagString)
|
||||
}
|
||||
|
||||
const (
|
||||
subjectsField = "subjects"
|
||||
subjectsNamespacePath = "subjects/namespace"
|
||||
roleBindingKind = "RoleBinding"
|
||||
clusterRoleBindingKind = "ClusterRoleBinding"
|
||||
)
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package namespace contains a kio.Filter implementation of the kustomize
|
||||
// patchjson6902 transformer
|
||||
package patchjson6902
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package patchjson6902
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
jsonpatch "gopkg.in/evanphx/json-patch.v4"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
k8syaml "sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type Filter struct {
|
||||
Patch string
|
||||
|
||||
decodedPatch jsonpatch.Patch
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
|
||||
func (pf Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
decodedPatch, err := pf.decodePatch()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pf.decodedPatch = decodedPatch
|
||||
return kio.FilterAll(yaml.FilterFunc(pf.run)).Filter(nodes)
|
||||
}
|
||||
|
||||
func (pf Filter) decodePatch() (jsonpatch.Patch, error) {
|
||||
patch := pf.Patch
|
||||
// If the patch doesn't look like a JSON6902 patch, we
|
||||
// try to parse it to json.
|
||||
if !strings.HasPrefix(pf.Patch, "[") {
|
||||
p, err := k8syaml.YAMLToJSON([]byte(patch))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
patch = string(p)
|
||||
}
|
||||
decodedPatch, err := jsonpatch.DecodePatch([]byte(patch))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodedPatch, nil
|
||||
}
|
||||
|
||||
func (pf Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
// We don't actually use the kyaml library for manipulating the
|
||||
// yaml here. We just marshal it to json and rely on the
|
||||
// jsonpatch library to take care of applying the patch.
|
||||
// This means ordering might not be preserved with this filter.
|
||||
b, err := node.MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := pf.decodedPatch.Apply(b)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = node.UnmarshalJSON(res)
|
||||
return node, err
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package patchstrategicmerge contains a kio.Filter implementation of the
|
||||
// kustomize strategic merge patch transformer.
|
||||
package patchstrategicmerge
|
||||
Generated
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package patchstrategicmerge
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
|
||||
)
|
||||
|
||||
type Filter struct {
|
||||
Patch *yaml.RNode
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
|
||||
// Filter does a strategic merge patch, which can delete nodes.
|
||||
func (pf Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
var result []*yaml.RNode
|
||||
for i := range nodes {
|
||||
r, err := merge2.Merge(
|
||||
pf.Patch, nodes[i],
|
||||
yaml.MergeOptions{
|
||||
ListIncreaseDirection: yaml.MergeOptionsListPrepend,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if r != nil {
|
||||
result = append(result, r)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package prefix contains a kio.Filter implementation of the kustomize
|
||||
// PrefixTransformer.
|
||||
package prefix
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package prefix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Filter applies resource name prefix's using the fieldSpecs
|
||||
type Filter struct {
|
||||
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
|
||||
|
||||
FieldSpec types.FieldSpec `json:"fieldSpec,omitempty" yaml:"fieldSpec,omitempty"`
|
||||
|
||||
trackableSetter filtersutil.TrackableSetter
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
var _ kio.TrackableFilter = &Filter{}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (f *Filter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
|
||||
f.trackableSetter.WithMutationTracker(callback)
|
||||
}
|
||||
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
return kio.FilterAll(yaml.FilterFunc(f.run)).Filter(nodes)
|
||||
}
|
||||
|
||||
func (f Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
err := node.PipeE(fieldspec.Filter{
|
||||
FieldSpec: f.FieldSpec,
|
||||
SetValue: f.evaluateField,
|
||||
CreateKind: yaml.ScalarNode, // Name is a ScalarNode
|
||||
CreateTag: yaml.NodeTagString,
|
||||
})
|
||||
return node, err
|
||||
}
|
||||
|
||||
func (f Filter) evaluateField(node *yaml.RNode) error {
|
||||
return f.trackableSetter.SetScalar(fmt.Sprintf(
|
||||
"%s%s", f.Prefix, node.YNode().Value))(node)
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package refvar contains a kio.Filter implementation of the kustomize
|
||||
// refvar transformer (find and replace $(FOO) style variables in strings).
|
||||
package refvar
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package refvar
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
operator = '$'
|
||||
referenceOpener = '('
|
||||
referenceCloser = ')'
|
||||
)
|
||||
|
||||
// syntaxWrap returns the input string wrapped by the expansion syntax.
|
||||
func syntaxWrap(input string) string {
|
||||
var sb strings.Builder
|
||||
sb.WriteByte(operator)
|
||||
sb.WriteByte(referenceOpener)
|
||||
sb.WriteString(input)
|
||||
sb.WriteByte(referenceCloser)
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// MappingFunc maps a string to anything.
|
||||
type MappingFunc func(string) interface{}
|
||||
|
||||
// MakePrimitiveReplacer returns a MappingFunc that uses a map to do
|
||||
// replacements, and a histogram to count map hits.
|
||||
//
|
||||
// Func behavior:
|
||||
//
|
||||
// If the input key is NOT found in the map, the key is wrapped up as
|
||||
// as a variable declaration string and returned, e.g. key FOO becomes $(FOO).
|
||||
// This string is presumably put back where it was found, and might get replaced
|
||||
// later.
|
||||
//
|
||||
// If the key is found in the map, the value is returned if it is a primitive
|
||||
// type (string, bool, number), and the hit is counted.
|
||||
//
|
||||
// If it's not a primitive type (e.g. a map, struct, func, etc.) then this
|
||||
// function doesn't know what to do with it and it returns the key wrapped up
|
||||
// again as if it had not been replaced. This should probably be an error.
|
||||
func MakePrimitiveReplacer(
|
||||
counts map[string]int, someMap map[string]interface{}) MappingFunc {
|
||||
return func(key string) interface{} {
|
||||
if value, ok := someMap[key]; ok {
|
||||
switch typedV := value.(type) {
|
||||
case string, int, int32, int64, float32, float64, bool:
|
||||
counts[key]++
|
||||
return typedV
|
||||
default:
|
||||
// If the value is some complicated type (e.g. a map or struct),
|
||||
// this function doesn't know how to jam it into a string,
|
||||
// so just pretend it was a cache miss.
|
||||
// Likely this should be an error instead of a silent failure,
|
||||
// since the programmer passed an impossible value.
|
||||
log.Printf(
|
||||
"MakePrimitiveReplacer: bad replacement type=%T val=%v",
|
||||
typedV, typedV)
|
||||
return syntaxWrap(key)
|
||||
}
|
||||
}
|
||||
// If unable to return the mapped variable, return it
|
||||
// as it was found, and a later mapping might be able to
|
||||
// replace it.
|
||||
return syntaxWrap(key)
|
||||
}
|
||||
}
|
||||
|
||||
// DoReplacements replaces variable references in the input string
|
||||
// using the mapping function.
|
||||
func DoReplacements(input string, mapping MappingFunc) interface{} {
|
||||
var buf strings.Builder
|
||||
checkpoint := 0
|
||||
for cursor := 0; cursor < len(input); cursor++ {
|
||||
if input[cursor] == operator && cursor+1 < len(input) {
|
||||
// Copy the portion of the input string since the last
|
||||
// checkpoint into the buffer
|
||||
buf.WriteString(input[checkpoint:cursor])
|
||||
|
||||
// Attempt to read the variable name as defined by the
|
||||
// syntax from the input string
|
||||
read, isVar, advance := tryReadVariableName(input[cursor+1:])
|
||||
|
||||
if isVar {
|
||||
// We were able to read a variable name correctly;
|
||||
// apply the mapping to the variable name and copy the
|
||||
// bytes into the buffer
|
||||
mapped := mapping(read)
|
||||
if input == syntaxWrap(read) {
|
||||
// Preserve the type of variable
|
||||
return mapped
|
||||
}
|
||||
|
||||
// Variable is used in a middle of a string
|
||||
buf.WriteString(fmt.Sprintf("%v", mapped))
|
||||
} else {
|
||||
// Not a variable name; copy the read bytes into the buffer
|
||||
buf.WriteString(read)
|
||||
}
|
||||
|
||||
// Advance the cursor in the input string to account for
|
||||
// bytes consumed to read the variable name expression
|
||||
cursor += advance
|
||||
|
||||
// Advance the checkpoint in the input string
|
||||
checkpoint = cursor + 1
|
||||
}
|
||||
}
|
||||
|
||||
// Return the buffer and any remaining unwritten bytes in the
|
||||
// input string.
|
||||
return buf.String() + input[checkpoint:]
|
||||
}
|
||||
|
||||
// tryReadVariableName attempts to read a variable name from the input
|
||||
// string and returns the content read from the input, whether that content
|
||||
// represents a variable name to perform mapping on, and the number of bytes
|
||||
// consumed in the input string.
|
||||
//
|
||||
// The input string is assumed not to contain the initial operator.
|
||||
func tryReadVariableName(input string) (string, bool, int) {
|
||||
switch input[0] {
|
||||
case operator:
|
||||
// Escaped operator; return it.
|
||||
return input[0:1], false, 1
|
||||
case referenceOpener:
|
||||
// Scan to expression closer
|
||||
for i := 1; i < len(input); i++ {
|
||||
if input[i] == referenceCloser {
|
||||
return input[1:i], true, i + 1
|
||||
}
|
||||
}
|
||||
|
||||
// Incomplete reference; return it.
|
||||
return string(operator) + string(referenceOpener), false, 1
|
||||
default:
|
||||
// Not the beginning of an expression, ie, an operator
|
||||
// that doesn't begin an expression. Return the operator
|
||||
// and the first rune in the string.
|
||||
return string(operator) + string(input[0]), false, 1
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package refvar
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Filter updates $(VAR) style variables with values.
|
||||
// The fieldSpecs are the places to look for occurrences of $(VAR).
|
||||
type Filter struct {
|
||||
MappingFunc MappingFunc `json:"mappingFunc,omitempty" yaml:"mappingFunc,omitempty"`
|
||||
FieldSpec types.FieldSpec `json:"fieldSpec,omitempty" yaml:"fieldSpec,omitempty"`
|
||||
}
|
||||
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
return kio.FilterAll(yaml.FilterFunc(f.run)).Filter(nodes)
|
||||
}
|
||||
|
||||
func (f Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
err := node.PipeE(fieldspec.Filter{
|
||||
FieldSpec: f.FieldSpec,
|
||||
SetValue: f.set,
|
||||
})
|
||||
return node, err
|
||||
}
|
||||
|
||||
func (f Filter) set(node *yaml.RNode) error {
|
||||
if yaml.IsMissingOrNull(node) {
|
||||
return nil
|
||||
}
|
||||
switch node.YNode().Kind {
|
||||
case yaml.ScalarNode:
|
||||
return f.setScalar(node)
|
||||
case yaml.MappingNode:
|
||||
return f.setMap(node)
|
||||
case yaml.SequenceNode:
|
||||
return f.setSeq(node)
|
||||
default:
|
||||
return fmt.Errorf("invalid type encountered %v", node.YNode().Kind)
|
||||
}
|
||||
}
|
||||
|
||||
func updateNodeValue(node *yaml.Node, newValue interface{}) {
|
||||
switch newValue := newValue.(type) {
|
||||
case int:
|
||||
node.Value = strconv.FormatInt(int64(newValue), 10)
|
||||
node.Tag = yaml.NodeTagInt
|
||||
case int32:
|
||||
node.Value = strconv.FormatInt(int64(newValue), 10)
|
||||
node.Tag = yaml.NodeTagInt
|
||||
case int64:
|
||||
node.Value = strconv.FormatInt(newValue, 10)
|
||||
node.Tag = yaml.NodeTagInt
|
||||
case bool:
|
||||
node.SetString(strconv.FormatBool(newValue))
|
||||
node.Tag = yaml.NodeTagBool
|
||||
case float32:
|
||||
node.SetString(strconv.FormatFloat(float64(newValue), 'f', -1, 32))
|
||||
node.Tag = yaml.NodeTagFloat
|
||||
case float64:
|
||||
node.SetString(strconv.FormatFloat(newValue, 'f', -1, 64))
|
||||
node.Tag = yaml.NodeTagFloat
|
||||
default:
|
||||
node.SetString(newValue.(string))
|
||||
node.Tag = yaml.NodeTagString
|
||||
}
|
||||
node.Style = 0
|
||||
}
|
||||
|
||||
func (f Filter) setScalar(node *yaml.RNode) error {
|
||||
if !yaml.IsYNodeString(node.YNode()) {
|
||||
return nil
|
||||
}
|
||||
v := DoReplacements(node.YNode().Value, f.MappingFunc)
|
||||
updateNodeValue(node.YNode(), v)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Filter) setMap(node *yaml.RNode) error {
|
||||
contents := node.YNode().Content
|
||||
for i := 0; i < len(contents); i += 2 {
|
||||
if !yaml.IsYNodeString(contents[i]) {
|
||||
return fmt.Errorf(
|
||||
"invalid map key: value='%s', tag='%s'",
|
||||
contents[i].Value, contents[i].Tag)
|
||||
}
|
||||
if !yaml.IsYNodeString(contents[i+1]) {
|
||||
continue
|
||||
}
|
||||
newValue := DoReplacements(contents[i+1].Value, f.MappingFunc)
|
||||
updateNodeValue(contents[i+1], newValue)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f Filter) setSeq(node *yaml.RNode) error {
|
||||
for _, item := range node.YNode().Content {
|
||||
if !yaml.IsYNodeString(item) {
|
||||
return fmt.Errorf("invalid value type expect a string")
|
||||
}
|
||||
newValue := DoReplacements(item.Value, f.MappingFunc)
|
||||
updateNodeValue(item, newValue)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package replacement contains a kio.Filter implementation of the kustomize
|
||||
// replacement transformer (accepts sources and looks for targets to replace
|
||||
// their values with values from the sources).
|
||||
package replacement
|
||||
+401
@@ -0,0 +1,401 @@
|
||||
// Copyright 2021 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package replacement
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/internal/utils"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
kyaml_utils "sigs.k8s.io/kustomize/kyaml/utils"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
type Filter struct {
|
||||
Replacements []types.Replacement `json:"replacements,omitempty" yaml:"replacements,omitempty"`
|
||||
}
|
||||
|
||||
// Filter replaces values of targets with values from sources
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
for i, r := range f.Replacements {
|
||||
if (r.SourceValue == nil && r.Source == nil) || r.Targets == nil {
|
||||
return nil, fmt.Errorf("replacements must specify a source and at least one target")
|
||||
}
|
||||
value, err := getReplacement(nodes, &f.Replacements[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes, err = applyReplacement(nodes, value, r.Targets)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func getReplacement(nodes []*yaml.RNode, r *types.Replacement) (*yaml.RNode, error) {
|
||||
if r.SourceValue != nil && r.Source != nil {
|
||||
return nil, fmt.Errorf("value and resource selectors are mutually exclusive")
|
||||
}
|
||||
if r.SourceValue != nil {
|
||||
return yaml.NewScalarRNode(*r.SourceValue), nil
|
||||
}
|
||||
|
||||
source, err := selectSourceNode(nodes, r.Source)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if r.Source.FieldPath == "" {
|
||||
r.Source.FieldPath = types.DefaultReplacementFieldPath
|
||||
}
|
||||
fieldPath := kyaml_utils.SmarterPathSplitter(r.Source.FieldPath, ".")
|
||||
|
||||
rn, err := source.Pipe(yaml.Lookup(fieldPath...))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error looking up replacement source: %w", err)
|
||||
}
|
||||
if rn.IsNilOrEmpty() {
|
||||
return nil, fmt.Errorf("fieldPath `%s` is missing for replacement source %s", r.Source.FieldPath, r.Source.ResId)
|
||||
}
|
||||
|
||||
return getRefinedValue(r.Source.Options, rn)
|
||||
}
|
||||
|
||||
// selectSourceNode finds the node that matches the selector, returning
|
||||
// an error if multiple or none are found
|
||||
func selectSourceNode(nodes []*yaml.RNode, selector *types.SourceSelector) (*yaml.RNode, error) {
|
||||
var matches []*yaml.RNode
|
||||
for _, n := range nodes {
|
||||
ids, err := utils.MakeResIds(n)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting node IDs: %w", err)
|
||||
}
|
||||
for _, id := range ids {
|
||||
if id.IsSelectedBy(selector.ResId) {
|
||||
if len(matches) > 0 {
|
||||
return nil, fmt.Errorf(
|
||||
"multiple matches for selector %s", selector)
|
||||
}
|
||||
matches = append(matches, n)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(matches) == 0 {
|
||||
return nil, fmt.Errorf("nothing selected by %s", selector)
|
||||
}
|
||||
return matches[0], nil
|
||||
}
|
||||
|
||||
func getRefinedValue(options *types.FieldOptions, rn *yaml.RNode) (*yaml.RNode, error) {
|
||||
if options == nil || options.Delimiter == "" {
|
||||
return rn, nil
|
||||
}
|
||||
if rn.YNode().Kind != yaml.ScalarNode {
|
||||
return nil, fmt.Errorf("delimiter option can only be used with scalar nodes")
|
||||
}
|
||||
value := strings.Split(yaml.GetValue(rn), options.Delimiter)
|
||||
if options.Index >= len(value) || options.Index < 0 {
|
||||
return nil, fmt.Errorf("options.index %d is out of bounds for value %s", options.Index, yaml.GetValue(rn))
|
||||
}
|
||||
n := rn.Copy()
|
||||
n.YNode().Value = value[options.Index]
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func applyReplacement(nodes []*yaml.RNode, value *yaml.RNode, targetSelectors []*types.TargetSelector) ([]*yaml.RNode, error) {
|
||||
for _, selector := range targetSelectors {
|
||||
if selector.Select == nil {
|
||||
return nil, errors.Errorf("target must specify resources to select")
|
||||
}
|
||||
if len(selector.FieldPaths) == 0 {
|
||||
selector.FieldPaths = []string{types.DefaultReplacementFieldPath}
|
||||
}
|
||||
tsr, err := types.NewTargetSelectorRegex(selector)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating target selector: %w", err)
|
||||
}
|
||||
for _, possibleTarget := range nodes {
|
||||
ids, err := utils.MakeResIds(possibleTarget)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// filter targets by label and annotation selectors
|
||||
selectByAnnoAndLabel, err := selectByAnnoAndLabel(possibleTarget, selector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !selectByAnnoAndLabel {
|
||||
continue
|
||||
}
|
||||
|
||||
if tsr.RejectsAny(ids) {
|
||||
continue
|
||||
}
|
||||
|
||||
// filter targets by matching resource IDs
|
||||
for _, id := range ids {
|
||||
if tsr.Selects(id) {
|
||||
err := copyValueToTarget(possibleTarget, value, selector)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
func selectByAnnoAndLabel(n *yaml.RNode, t *types.TargetSelector) (bool, error) {
|
||||
if matchesSelect, err := matchesAnnoAndLabelSelector(n, t.Select); !matchesSelect || err != nil {
|
||||
return false, err
|
||||
}
|
||||
for _, reject := range t.Reject {
|
||||
if reject.AnnotationSelector == "" && reject.LabelSelector == "" {
|
||||
continue
|
||||
}
|
||||
if m, err := matchesAnnoAndLabelSelector(n, reject); m || err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func matchesAnnoAndLabelSelector(n *yaml.RNode, selector *types.Selector) (bool, error) {
|
||||
r := resource.Resource{RNode: *n}
|
||||
annoMatch, err := r.MatchesAnnotationSelector(selector.AnnotationSelector)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
labelMatch, err := r.MatchesLabelSelector(selector.LabelSelector)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return annoMatch && labelMatch, nil
|
||||
}
|
||||
|
||||
func copyValueToTarget(target *yaml.RNode, value *yaml.RNode, selector *types.TargetSelector) error {
|
||||
for _, fp := range selector.FieldPaths {
|
||||
createKind := yaml.Kind(0) // do not create
|
||||
if selector.Options != nil && selector.Options.Create {
|
||||
createKind = value.YNode().Kind
|
||||
}
|
||||
|
||||
// Check if this fieldPath contains structured data access
|
||||
if err := setValueInStructuredData(target, value, fp, createKind); err == nil {
|
||||
// Successfully handled as structured data
|
||||
continue
|
||||
}
|
||||
|
||||
// Fall back to normal path handling
|
||||
targetFieldList, err := target.Pipe(&yaml.PathMatcher{
|
||||
Path: kyaml_utils.SmarterPathSplitter(fp, "."),
|
||||
Create: createKind})
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "%s", fieldRetrievalError(fp, createKind != 0))
|
||||
}
|
||||
targetFields, err := targetFieldList.Elements()
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "%s", fieldRetrievalError(fp, createKind != 0))
|
||||
}
|
||||
if len(targetFields) == 0 {
|
||||
return errors.Errorf("%s", fieldRetrievalError(fp, createKind != 0))
|
||||
}
|
||||
|
||||
for _, t := range targetFields {
|
||||
if err := setFieldValue(selector.Options, t, value); err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func fieldRetrievalError(fieldPath string, isCreate bool) string {
|
||||
if isCreate {
|
||||
return fmt.Sprintf("unable to find or create field %q in replacement target", fieldPath)
|
||||
}
|
||||
return fmt.Sprintf("unable to find field %q in replacement target", fieldPath)
|
||||
}
|
||||
|
||||
func setFieldValue(options *types.FieldOptions, targetField *yaml.RNode, value *yaml.RNode) error {
|
||||
value = value.Copy()
|
||||
if options != nil && options.Delimiter != "" {
|
||||
if targetField.YNode().Kind != yaml.ScalarNode {
|
||||
return fmt.Errorf("delimiter option can only be used with scalar nodes")
|
||||
}
|
||||
tv := strings.Split(targetField.YNode().Value, options.Delimiter)
|
||||
v := yaml.GetValue(value)
|
||||
// TODO: Add a way to remove an element
|
||||
switch {
|
||||
case options.Index < 0: // prefix
|
||||
tv = append([]string{v}, tv...)
|
||||
case options.Index >= len(tv): // suffix
|
||||
tv = append(tv, v)
|
||||
default: // replace an element
|
||||
tv[options.Index] = v
|
||||
}
|
||||
value.YNode().Value = strings.Join(tv, options.Delimiter)
|
||||
}
|
||||
|
||||
if targetField.YNode().Kind == yaml.ScalarNode {
|
||||
// For scalar, only copy the value (leave any type intact to auto-convert int->string or string->int)
|
||||
targetField.YNode().Value = value.YNode().Value
|
||||
} else {
|
||||
targetField.SetYNode(value.YNode())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// setValueInStructuredData handles setting values within structured data (JSON/YAML) in scalar fields
|
||||
func setValueInStructuredData(target *yaml.RNode, value *yaml.RNode, fieldPath string, createKind yaml.Kind) error {
|
||||
pathParts := kyaml_utils.SmarterPathSplitter(fieldPath, ".")
|
||||
if len(pathParts) < 2 {
|
||||
return fmt.Errorf("not a structured data path")
|
||||
}
|
||||
|
||||
// Find the potential scalar field that might contain structured data
|
||||
var scalarFieldPath []string
|
||||
var structuredDataPath []string
|
||||
var foundScalar = false
|
||||
|
||||
// Try to find where the scalar field ends and structured data begins
|
||||
for i := 1; i <= len(pathParts); i++ {
|
||||
potentialScalarPath := pathParts[:i]
|
||||
scalarField, err := target.Pipe(yaml.Lookup(potentialScalarPath...))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if scalarField != nil && scalarField.YNode().Kind == yaml.ScalarNode && i < len(pathParts) {
|
||||
// Try to parse the scalar value as structured data
|
||||
scalarValue := scalarField.YNode().Value
|
||||
var parsedNode yaml.Node
|
||||
if err := yaml.Unmarshal([]byte(scalarValue), &parsedNode); err == nil {
|
||||
// Successfully parsed - this is structured data
|
||||
scalarFieldPath = potentialScalarPath
|
||||
structuredDataPath = pathParts[i:]
|
||||
foundScalar = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !foundScalar {
|
||||
return fmt.Errorf("no structured data found in path")
|
||||
}
|
||||
|
||||
// Get the scalar field containing structured data
|
||||
scalarField, err := target.Pipe(yaml.Lookup(scalarFieldPath...))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
// Parse the structured data
|
||||
scalarValue := scalarField.YNode().Value
|
||||
var parsedNode yaml.Node
|
||||
if err := yaml.Unmarshal([]byte(scalarValue), &parsedNode); err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
structuredData := yaml.NewRNode(&parsedNode)
|
||||
|
||||
// Navigate to the target location within the structured data
|
||||
targetInStructured, err := structuredData.Pipe(&yaml.PathMatcher{
|
||||
Path: structuredDataPath,
|
||||
Create: createKind,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
targetFields, err := targetInStructured.Elements()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
if len(targetFields) == 0 {
|
||||
return fmt.Errorf("unable to find field in structured data")
|
||||
}
|
||||
|
||||
// Set the value in the structured data
|
||||
for _, t := range targetFields {
|
||||
if t.YNode().Kind == yaml.ScalarNode {
|
||||
t.YNode().Value = value.YNode().Value
|
||||
} else {
|
||||
t.SetYNode(value.YNode())
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize the modified structured data back to the scalar field
|
||||
// Try to detect if original was JSON or YAML and preserve formatting
|
||||
serializedData, err := serializeStructuredData(structuredData, scalarValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w", err)
|
||||
}
|
||||
|
||||
// Update the original scalar field
|
||||
scalarField.YNode().Value = serializedData
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// serializeStructuredData handles the serialization of structured data back to string format
|
||||
// preserving the original format (JSON vs YAML) and style (pretty vs compact)
|
||||
func serializeStructuredData(structuredData *yaml.RNode, originalValue string) (string, error) {
|
||||
firstChar := rune(strings.TrimSpace(originalValue)[0])
|
||||
if firstChar == '{' || firstChar == '[' {
|
||||
return serializeAsJSON(structuredData, originalValue)
|
||||
}
|
||||
|
||||
// Fallback to YAML format
|
||||
return serializeAsYAML(structuredData)
|
||||
}
|
||||
|
||||
// serializeAsJSON converts structured data back to JSON format
|
||||
func serializeAsJSON(structuredData *yaml.RNode, originalValue string) (string, error) {
|
||||
modifiedData, err := structuredData.String()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to serialize structured data: %w", err)
|
||||
}
|
||||
|
||||
// Parse the YAML output as JSON
|
||||
var jsonData interface{}
|
||||
if err := yaml.Unmarshal([]byte(modifiedData), &jsonData); err != nil {
|
||||
return "", fmt.Errorf("failed to unmarshal YAML data: %w", err)
|
||||
}
|
||||
|
||||
// Check if original was pretty-printed by looking for newlines and indentation
|
||||
if strings.Contains(originalValue, "\n") && strings.Contains(originalValue, " ") {
|
||||
// Pretty-print the JSON to match original formatting
|
||||
if prettyJSON, err := json.MarshalIndent(jsonData, "", " "); err == nil {
|
||||
return string(prettyJSON), nil
|
||||
}
|
||||
}
|
||||
|
||||
// Compact JSON
|
||||
if compactJSON, err := json.Marshal(jsonData); err == nil {
|
||||
return string(compactJSON), nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("failed to marshal JSON data")
|
||||
}
|
||||
|
||||
// serializeAsYAML converts structured data back to YAML format
|
||||
func serializeAsYAML(structuredData *yaml.RNode) (string, error) {
|
||||
modifiedData, err := structuredData.String()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to serialize YAML data: %w", err)
|
||||
}
|
||||
|
||||
return strings.TrimSpace(modifiedData), nil
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package replicacount contains a kio.Filter implementation of the kustomize
|
||||
// ReplicaCountTransformer.
|
||||
package replicacount
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package replicacount
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Filter updates/sets replicas fields using the fieldSpecs
|
||||
type Filter struct {
|
||||
Replica types.Replica `json:"replica,omitempty" yaml:"replica,omitempty"`
|
||||
FieldSpec types.FieldSpec `json:"fieldSpec,omitempty" yaml:"fieldSpec,omitempty"`
|
||||
|
||||
trackableSetter filtersutil.TrackableSetter
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
var _ kio.TrackableFilter = &Filter{}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (rc *Filter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
|
||||
rc.trackableSetter.WithMutationTracker(callback)
|
||||
}
|
||||
|
||||
func (rc Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
return kio.FilterAll(yaml.FilterFunc(rc.run)).Filter(nodes)
|
||||
}
|
||||
|
||||
func (rc Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
err := node.PipeE(fieldspec.Filter{
|
||||
FieldSpec: rc.FieldSpec,
|
||||
SetValue: rc.set,
|
||||
CreateKind: yaml.ScalarNode, // replicas is a ScalarNode
|
||||
CreateTag: yaml.NodeTagInt,
|
||||
})
|
||||
return node, err
|
||||
}
|
||||
|
||||
func (rc Filter) set(node *yaml.RNode) error {
|
||||
return rc.trackableSetter.SetEntry("", strconv.FormatInt(rc.Replica.Count, 10), yaml.NodeTagInt)(node)
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
// Copyright 2021 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package suffix contains a kio.Filter implementation of the kustomize
|
||||
// SuffixTransformer.
|
||||
package suffix
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
// Copyright 2021 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package suffix
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/fieldspec"
|
||||
"sigs.k8s.io/kustomize/api/filters/filtersutil"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Filter applies resource name suffix's using the fieldSpecs
|
||||
type Filter struct {
|
||||
Suffix string `json:"suffix,omitempty" yaml:"suffix,omitempty"`
|
||||
|
||||
FieldSpec types.FieldSpec `json:"fieldSpec,omitempty" yaml:"fieldSpec,omitempty"`
|
||||
|
||||
trackableSetter filtersutil.TrackableSetter
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
var _ kio.TrackableFilter = &Filter{}
|
||||
|
||||
// WithMutationTracker registers a callback which will be invoked each time a field is mutated
|
||||
func (f *Filter) WithMutationTracker(callback func(key, value, tag string, node *yaml.RNode)) {
|
||||
f.trackableSetter.WithMutationTracker(callback)
|
||||
}
|
||||
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
return kio.FilterAll(yaml.FilterFunc(f.run)).Filter(nodes)
|
||||
}
|
||||
|
||||
func (f Filter) run(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
err := node.PipeE(fieldspec.Filter{
|
||||
FieldSpec: f.FieldSpec,
|
||||
SetValue: f.evaluateField,
|
||||
CreateKind: yaml.ScalarNode, // Name is a ScalarNode
|
||||
CreateTag: yaml.NodeTagString,
|
||||
})
|
||||
return node, err
|
||||
}
|
||||
|
||||
func (f Filter) evaluateField(node *yaml.RNode) error {
|
||||
return f.trackableSetter.SetScalar(fmt.Sprintf(
|
||||
"%s%s", node.YNode().Value, f.Suffix))(node)
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package valueadd
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// An 'Add' operation aspiring to IETF RFC 6902 JSON.
|
||||
//
|
||||
// The filter tries to add a value to a node at a particular field path.
|
||||
//
|
||||
// Kinds of target fields:
|
||||
//
|
||||
// - Non-existent target field.
|
||||
//
|
||||
// The field will be added and the value inserted.
|
||||
//
|
||||
// - Existing field, scalar or map.
|
||||
//
|
||||
// E.g. 'spec/template/spec/containers/[name:nginx]/image'
|
||||
//
|
||||
// This behaves like an IETF RFC 6902 Replace operation would;
|
||||
// the existing value is replaced without complaint, even though
|
||||
// this is an Add operation. In contrast, a Replace operation
|
||||
// must fail (report an error) if the field doesn't exist.
|
||||
//
|
||||
// - Existing field, list (array)
|
||||
// Not supported yet.
|
||||
// TODO: Honor fields with RFC-6902-style array indices
|
||||
// TODO: like 'spec/template/spec/containers/2'
|
||||
// TODO: Modify kyaml/yaml/PathGetter to allow this.
|
||||
// The value will be inserted into the array at the given position,
|
||||
// shifting other contents. To instead replace an array entry, use
|
||||
// an implementation of an IETF RFC 6902 Replace operation.
|
||||
//
|
||||
// For the common case of a filepath in the field value, and a desire
|
||||
// to add the value to the filepath (rather than replace the filepath),
|
||||
// use a non-zero value of FilePathPosition (see below).
|
||||
type Filter struct {
|
||||
// Value is the value to add.
|
||||
//
|
||||
// Empty values are disallowed, i.e. this filter isn't intended
|
||||
// for use in erasing or removing fields. For that, use a filter
|
||||
// more aligned with the IETF RFC 6902 JSON Remove operation.
|
||||
//
|
||||
// At the time of writing, Value's value should be a simple string,
|
||||
// not a JSON document. This particular filter focuses on easing
|
||||
// injection of a single-sourced cloud project and/or cluster name
|
||||
// into various fields, especially namespace and various filepath
|
||||
// specifications.
|
||||
Value string
|
||||
|
||||
// FieldPath is a JSON-style path to the field intended to hold the value.
|
||||
FieldPath string
|
||||
|
||||
// FilePathPosition is a filepath field index.
|
||||
//
|
||||
// Call the value of this field _i_.
|
||||
//
|
||||
// If _i_ is zero, negative or unspecified, this field has no effect.
|
||||
//
|
||||
// If _i_ is > 0, then it's assumed that
|
||||
// - 'Value' is a string that can work as a directory or file name,
|
||||
// - the field value intended for replacement holds a filepath.
|
||||
//
|
||||
// The filepath is split into a string slice, the value is inserted
|
||||
// at position [i-1], shifting the rest of the path to the right.
|
||||
// A value of i==1 puts the new value at the start of the path.
|
||||
// This change never converts an absolute path to a relative path,
|
||||
// meaning adding a new field at position i==1 will preserve a
|
||||
// leading slash. E.g. if Value == 'PEACH'
|
||||
//
|
||||
// OLD : NEW : FilePathPosition
|
||||
// --------------------------------------------------------
|
||||
// {empty} : PEACH : irrelevant
|
||||
// / : /PEACH : irrelevant
|
||||
// pie : PEACH/pie : 1 (or less to prefix)
|
||||
// /pie : /PEACH/pie : 1 (or less to prefix)
|
||||
// raw : raw/PEACH : 2 (or more to postfix)
|
||||
// /raw : /raw/PEACH : 2 (or more to postfix)
|
||||
// a/nice/warm/pie : a/nice/warm/PEACH/pie : 4
|
||||
// /a/nice/warm/pie : /a/nice/warm/PEACH/pie : 4
|
||||
//
|
||||
// For robustness (liberal input, conservative output) FilePathPosition
|
||||
// values that that are too large to index the split filepath result in a
|
||||
// postfix rather than an error. So use 1 to prefix, 9999 to postfix.
|
||||
FilePathPosition int `json:"filePathPosition,omitempty" yaml:"filePathPosition,omitempty"`
|
||||
}
|
||||
|
||||
var _ kio.Filter = Filter{}
|
||||
|
||||
func (f Filter) Filter(nodes []*yaml.RNode) ([]*yaml.RNode, error) {
|
||||
_, err := kio.FilterAll(yaml.FilterFunc(
|
||||
func(node *yaml.RNode) (*yaml.RNode, error) {
|
||||
var fields []string
|
||||
// if there is forward slash '/' in the field name, a back slash '\'
|
||||
// will be used to escape it.
|
||||
for _, f := range strings.Split(f.FieldPath, "/") {
|
||||
if len(fields) > 0 && strings.HasSuffix(fields[len(fields)-1], "\\") {
|
||||
concatField := strings.TrimSuffix(fields[len(fields)-1], "\\") + "/" + f
|
||||
fields = append(fields[:len(fields)-1], concatField)
|
||||
} else {
|
||||
fields = append(fields, f)
|
||||
}
|
||||
}
|
||||
// TODO: support SequenceNode.
|
||||
// Presumably here one could look for array indices (digits) at
|
||||
// the end of the field path (as described in IETF RFC 6902 JSON),
|
||||
// and if found, take it as a signal that this should be a
|
||||
// SequenceNode instead of a ScalarNode, and insert the value
|
||||
// into the proper slot, shifting every over.
|
||||
n, err := node.Pipe(yaml.LookupCreate(yaml.ScalarNode, fields...))
|
||||
if err != nil {
|
||||
return node, err
|
||||
}
|
||||
// TODO: allow more kinds
|
||||
if err := yaml.ErrorIfInvalid(n, yaml.ScalarNode); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newValue := f.Value
|
||||
if f.FilePathPosition > 0 {
|
||||
newValue = filesys.InsertPathPart(
|
||||
n.YNode().Value, f.FilePathPosition-1, newValue)
|
||||
}
|
||||
return n.Pipe(yaml.FieldSetter{StringValue: newValue})
|
||||
})).Filter(nodes)
|
||||
return nodes, err
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package hasher
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// SortArrayAndComputeHash sorts a string array and
|
||||
// returns a hash for it
|
||||
func SortArrayAndComputeHash(s []string) (string, error) {
|
||||
sort.Strings(s)
|
||||
data, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return encode(hex256(string(data)))
|
||||
}
|
||||
|
||||
// Copied from https://github.com/kubernetes/kubernetes
|
||||
// /blob/master/pkg/kubectl/util/hash/hash.go
|
||||
func encode(hex string) (string, error) {
|
||||
if len(hex) < 10 {
|
||||
return "", fmt.Errorf(
|
||||
"input length must be at least 10")
|
||||
}
|
||||
enc := []rune(hex[:10])
|
||||
for i := range enc {
|
||||
switch enc[i] {
|
||||
case '0':
|
||||
enc[i] = 'g'
|
||||
case '1':
|
||||
enc[i] = 'h'
|
||||
case '3':
|
||||
enc[i] = 'k'
|
||||
case 'a':
|
||||
enc[i] = 'm'
|
||||
case 'e':
|
||||
enc[i] = 't'
|
||||
}
|
||||
}
|
||||
return string(enc), nil
|
||||
}
|
||||
|
||||
// hex256 returns the hex form of the sha256 of the argument.
|
||||
func hex256(data string) string {
|
||||
return fmt.Sprintf("%x", sha256.Sum256([]byte(data)))
|
||||
}
|
||||
|
||||
// Hasher computes the hash of an RNode.
|
||||
type Hasher struct{}
|
||||
|
||||
// Hash returns a hash of the argument.
|
||||
func (h *Hasher) Hash(node *yaml.RNode) (r string, err error) {
|
||||
var encoded string
|
||||
switch node.GetKind() {
|
||||
case "ConfigMap":
|
||||
encoded, err = encodeConfigMap(node)
|
||||
case "Secret":
|
||||
encoded, err = encodeSecret(node)
|
||||
default:
|
||||
var encodedBytes []byte
|
||||
encodedBytes, err = json.Marshal(node.YNode())
|
||||
encoded = string(encodedBytes)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return encode(hex256(encoded))
|
||||
}
|
||||
|
||||
func getNodeValues(
|
||||
node *yaml.RNode, paths []string) (map[string]interface{}, error) {
|
||||
values := make(map[string]interface{})
|
||||
for _, p := range paths {
|
||||
vn, err := node.Pipe(yaml.Lookup(p))
|
||||
if err != nil {
|
||||
return map[string]interface{}{}, err
|
||||
}
|
||||
if vn == nil {
|
||||
values[p] = ""
|
||||
continue
|
||||
}
|
||||
if vn.YNode().Kind != yaml.ScalarNode {
|
||||
vs, err := vn.MarshalJSON()
|
||||
if err != nil {
|
||||
return map[string]interface{}{}, err
|
||||
}
|
||||
// data, binaryData and stringData are all maps
|
||||
var v map[string]interface{}
|
||||
json.Unmarshal(vs, &v)
|
||||
values[p] = v
|
||||
} else {
|
||||
values[p] = vn.YNode().Value
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
// encodeConfigMap encodes a ConfigMap.
|
||||
// Data, Kind, and Name are taken into account.
|
||||
// BinaryData is included if it's not empty to avoid useless key in output.
|
||||
func encodeConfigMap(node *yaml.RNode) (string, error) {
|
||||
// get fields
|
||||
paths := []string{"metadata/name", "data", "binaryData"}
|
||||
values, err := getNodeValues(node, paths)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m := map[string]interface{}{
|
||||
"kind": "ConfigMap",
|
||||
"name": values["metadata/name"],
|
||||
"data": values["data"],
|
||||
}
|
||||
if _, ok := values["binaryData"].(map[string]interface{}); ok {
|
||||
m["binaryData"] = values["binaryData"]
|
||||
}
|
||||
|
||||
// json.Marshal sorts the keys in a stable order in the encoding
|
||||
data, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// encodeSecret encodes a Secret.
|
||||
// Data, Kind, Name, and Type are taken into account.
|
||||
// StringData is included if it's not empty to avoid useless key in output.
|
||||
func encodeSecret(node *yaml.RNode) (string, error) {
|
||||
// get fields
|
||||
paths := []string{"type", "metadata/name", "data", "stringData"}
|
||||
values, err := getNodeValues(node, paths)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
m := map[string]interface{}{"kind": "Secret", "type": values["type"],
|
||||
"name": values["metadata/name"], "data": values["data"]}
|
||||
if _, ok := values["stringData"].(map[string]interface{}); ok {
|
||||
m["stringData"] = values["stringData"]
|
||||
}
|
||||
|
||||
// json.Marshal sorts the keys in a stable order in the encoding
|
||||
data, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(data), nil
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package ifc holds miscellaneous interfaces used by kustomize.
|
||||
package ifc
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Validator provides functions to validate annotations and labels
|
||||
type Validator interface {
|
||||
MakeAnnotationValidator() func(map[string]string) error
|
||||
MakeAnnotationNameValidator() func([]string) error
|
||||
MakeLabelValidator() func(map[string]string) error
|
||||
MakeLabelNameValidator() func([]string) error
|
||||
ValidateNamespace(string) []string
|
||||
ErrIfInvalidKey(string) error
|
||||
IsEnvVarName(k string) error
|
||||
}
|
||||
|
||||
// KvLoader reads and validates KV pairs.
|
||||
type KvLoader interface {
|
||||
Validator() Validator
|
||||
Load(args types.KvPairSources) (all []types.Pair, err error)
|
||||
}
|
||||
|
||||
// Loader interface exposes methods to read bytes.
|
||||
type Loader interface {
|
||||
|
||||
// Repo returns the repo location if this Loader was created from a url
|
||||
// or the empty string otherwise.
|
||||
Repo() string
|
||||
|
||||
// Root returns the root location for this Loader.
|
||||
Root() string
|
||||
|
||||
// New returns Loader located at newRoot.
|
||||
New(newRoot string) (Loader, error)
|
||||
|
||||
// Load returns the bytes read from the location or an error.
|
||||
Load(location string) ([]byte, error)
|
||||
|
||||
// Cleanup cleans the loader
|
||||
Cleanup() error
|
||||
}
|
||||
|
||||
// KustHasher returns a hash of the argument
|
||||
// or an error.
|
||||
type KustHasher interface {
|
||||
Hash(*yaml.RNode) (string, error)
|
||||
}
|
||||
|
||||
// See core.v1.SecretTypeOpaque
|
||||
const SecretTypeOpaque = "Opaque"
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package accumulator
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"k8s.io/kube-openapi/pkg/validation/spec"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// OpenAPIDefinition describes single type.
|
||||
// Normally these definitions are auto-generated using gen-openapi.
|
||||
// Same as in k8s.io / kube-openapi / pkg / common.
|
||||
type OpenAPIDefinition struct {
|
||||
Schema spec.Schema
|
||||
Dependencies []string
|
||||
}
|
||||
|
||||
type myProperties = map[string]spec.Schema
|
||||
type nameToApiMap map[string]OpenAPIDefinition
|
||||
|
||||
// LoadConfigFromCRDs parse CRD schemas from paths into a TransformerConfig
|
||||
func LoadConfigFromCRDs(
|
||||
ldr ifc.Loader, paths []string) (*builtinconfig.TransformerConfig, error) {
|
||||
tc := builtinconfig.MakeEmptyConfig()
|
||||
for _, path := range paths {
|
||||
content, err := ldr.Load(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m, err := makeNameToApiMap(content)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "unable to parse open API definition from '%s'", path)
|
||||
}
|
||||
otherTc, err := makeConfigFromApiMap(m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tc, err = tc.Merge(otherTc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return tc, nil
|
||||
}
|
||||
|
||||
func makeNameToApiMap(content []byte) (result nameToApiMap, err error) {
|
||||
if content[0] == '{' {
|
||||
err = json.Unmarshal(content, &result)
|
||||
} else {
|
||||
err = yaml.Unmarshal(content, &result)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func makeConfigFromApiMap(m nameToApiMap) (*builtinconfig.TransformerConfig, error) {
|
||||
result := builtinconfig.MakeEmptyConfig()
|
||||
for name, api := range m {
|
||||
if !looksLikeAk8sType(api.Schema.SchemaProps.Properties) {
|
||||
continue
|
||||
}
|
||||
tc := builtinconfig.MakeEmptyConfig()
|
||||
err := loadCrdIntoConfig(
|
||||
tc, makeGvkFromTypeName(name), m, name, []string{})
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result, err = result.Merge(tc)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// TODO: Get Group and Version for CRD from the
|
||||
// openAPI definition once
|
||||
// "x-kubernetes-group-version-kind" is available in CRD
|
||||
func makeGvkFromTypeName(n string) resid.Gvk {
|
||||
names := strings.Split(n, filesys.SelfDir)
|
||||
kind := names[len(names)-1]
|
||||
return resid.Gvk{Kind: kind}
|
||||
}
|
||||
|
||||
func looksLikeAk8sType(properties myProperties) bool {
|
||||
_, ok := properties["kind"]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
_, ok = properties["apiVersion"]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
_, ok = properties["metadata"]
|
||||
return ok
|
||||
}
|
||||
|
||||
const (
|
||||
// "x-kubernetes-annotation": ""
|
||||
xAnnotation = "x-kubernetes-annotation"
|
||||
|
||||
// "x-kubernetes-label-selector": ""
|
||||
xLabelSelector = "x-kubernetes-label-selector"
|
||||
|
||||
// "x-kubernetes-identity": ""
|
||||
xIdentity = "x-kubernetes-identity"
|
||||
|
||||
// "x-kubernetes-object-ref-api-version": <apiVersion name>
|
||||
xVersion = "x-kubernetes-object-ref-api-version"
|
||||
|
||||
// "x-kubernetes-object-ref-kind": <kind name>
|
||||
xKind = "x-kubernetes-object-ref-kind"
|
||||
|
||||
// "x-kubernetes-object-ref-name-key": "name"
|
||||
// default is "name"
|
||||
xNameKey = "x-kubernetes-object-ref-name-key"
|
||||
)
|
||||
|
||||
// loadCrdIntoConfig loads a CRD spec into a TransformerConfig
|
||||
func loadCrdIntoConfig(
|
||||
theConfig *builtinconfig.TransformerConfig, theGvk resid.Gvk, theMap nameToApiMap,
|
||||
typeName string, path []string) (err error) {
|
||||
api, ok := theMap[typeName]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
for propName, property := range api.Schema.SchemaProps.Properties {
|
||||
_, annotate := property.Extensions.GetString(xAnnotation)
|
||||
if annotate {
|
||||
err = theConfig.AddAnnotationFieldSpec(
|
||||
makeFs(theGvk, append(path, propName)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
_, label := property.Extensions.GetString(xLabelSelector)
|
||||
if label {
|
||||
err = theConfig.AddCommonLabelsFieldSpec(
|
||||
makeFs(theGvk, append(path, propName)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
_, identity := property.Extensions.GetString(xIdentity)
|
||||
if identity {
|
||||
err = theConfig.AddPrefixFieldSpec(
|
||||
makeFs(theGvk, append(path, propName)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
version, ok := property.Extensions.GetString(xVersion)
|
||||
if ok {
|
||||
kind, ok := property.Extensions.GetString(xKind)
|
||||
if ok {
|
||||
nameKey, ok := property.Extensions.GetString(xNameKey)
|
||||
if !ok {
|
||||
nameKey = "name"
|
||||
}
|
||||
err = theConfig.AddNamereferenceFieldSpec(
|
||||
builtinconfig.NameBackReferences{
|
||||
Gvk: resid.Gvk{Kind: kind, Version: version},
|
||||
Referrers: []types.FieldSpec{
|
||||
makeFs(theGvk, append(path, propName, nameKey))},
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if property.Ref.GetURL() != nil {
|
||||
err = loadCrdIntoConfig(
|
||||
theConfig, theGvk, theMap,
|
||||
property.Ref.String(), append(path, propName))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func makeFs(in resid.Gvk, path []string) types.FieldSpec {
|
||||
return types.FieldSpec{
|
||||
CreateIfNotPresent: false,
|
||||
Gvk: in,
|
||||
Path: strings.Join(path, "/"),
|
||||
}
|
||||
}
|
||||
Generated
Vendored
+164
@@ -0,0 +1,164 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package accumulator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/nameref"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
)
|
||||
|
||||
type nameReferenceTransformer struct {
|
||||
backRefs []builtinconfig.NameBackReferences
|
||||
}
|
||||
|
||||
const doDebug = false
|
||||
|
||||
var _ resmap.Transformer = &nameReferenceTransformer{}
|
||||
|
||||
type filterMap map[*resource.Resource][]nameref.Filter
|
||||
|
||||
// newNameReferenceTransformer constructs a nameReferenceTransformer
|
||||
// with a given slice of NameBackReferences.
|
||||
func newNameReferenceTransformer(
|
||||
br []builtinconfig.NameBackReferences) resmap.Transformer {
|
||||
if br == nil {
|
||||
log.Fatal("backrefs not expected to be nil")
|
||||
}
|
||||
return &nameReferenceTransformer{backRefs: br}
|
||||
}
|
||||
|
||||
// Transform updates name references in resource A that
|
||||
// refer to resource B, given that B's name may have
|
||||
// changed.
|
||||
//
|
||||
// For example, a HorizontalPodAutoscaler (HPA)
|
||||
// necessarily refers to a Deployment, the thing that
|
||||
// an HPA scales. In this case:
|
||||
//
|
||||
// - the HPA instance is the Referrer,
|
||||
// - the Deployment instance is the ReferralTarget.
|
||||
//
|
||||
// If the Deployment's name changes, e.g. a prefix is added,
|
||||
// then the HPA's reference to the Deployment must be fixed.
|
||||
//
|
||||
func (t *nameReferenceTransformer) Transform(m resmap.ResMap) error {
|
||||
fMap := t.determineFilters(m.Resources())
|
||||
debug(fMap)
|
||||
for r, fList := range fMap {
|
||||
c, err := m.SubsetThatCouldBeReferencedByResource(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, f := range fList {
|
||||
f.Referrer = r
|
||||
f.ReferralCandidates = c
|
||||
if err := f.Referrer.ApplyFilter(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func debug(fMap filterMap) {
|
||||
if !doDebug {
|
||||
return
|
||||
}
|
||||
fmt.Printf("filterMap has %d entries:\n", len(fMap))
|
||||
rCount := 0
|
||||
for r, fList := range fMap {
|
||||
yml, _ := r.AsYAML()
|
||||
rCount++
|
||||
fmt.Printf(`
|
||||
---- %3d. possible referrer -------------
|
||||
%s
|
||||
---------`, rCount, string(yml),
|
||||
)
|
||||
for i, f := range fList {
|
||||
fmt.Printf(`
|
||||
%3d/%3d update: %s
|
||||
from: %s
|
||||
`, rCount, i+1, f.NameFieldToUpdate.Path, f.ReferralTarget,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Produce a map from referrer resources that might need to be fixed
|
||||
// to filters that might fix them. The keys to this map are potential
|
||||
// referrers, so won't include resources like ConfigMap or Secret.
|
||||
//
|
||||
// In the inner loop over the resources below, say we
|
||||
// encounter an HPA instance. Then, in scanning the set
|
||||
// of all known backrefs, we encounter an entry like
|
||||
//
|
||||
// - kind: Deployment
|
||||
// fieldSpecs:
|
||||
// - kind: HorizontalPodAutoscaler
|
||||
// path: spec/scaleTargetRef/name
|
||||
//
|
||||
// This entry says that an HPA, via its
|
||||
// 'spec/scaleTargetRef/name' field, may refer to a
|
||||
// Deployment.
|
||||
//
|
||||
// This means that a filter will need to hunt for the right Deployment,
|
||||
// obtain it's new name, and write that name into the HPA's
|
||||
// 'spec/scaleTargetRef/name' field. Return a filter that can do that.
|
||||
func (t *nameReferenceTransformer) determineFilters(
|
||||
resources []*resource.Resource) (fMap filterMap) {
|
||||
// We cache the resource OrgId values because they don't change and otherwise are very visible in a memory pprof
|
||||
resourceOrgIds := make([]resid.ResId, len(resources))
|
||||
for i, resource := range resources {
|
||||
resourceOrgIds[i] = resource.OrgId()
|
||||
}
|
||||
|
||||
fMap = make(filterMap)
|
||||
for _, backReference := range t.backRefs {
|
||||
for _, referrerSpec := range backReference.Referrers {
|
||||
for i, res := range resources {
|
||||
if resourceOrgIds[i].IsSelected(&referrerSpec.Gvk) {
|
||||
// If this is true, the res might be a referrer, and if
|
||||
// so, the name reference it holds might need an update.
|
||||
if resHasField(res, referrerSpec.Path) {
|
||||
// Optimization - the referrer has the field
|
||||
// that might need updating.
|
||||
fMap[res] = append(fMap[res], nameref.Filter{
|
||||
// Name field to write in the Referrer.
|
||||
// If the path specified here isn't found in
|
||||
// the Referrer, nothing happens (no error,
|
||||
// no field creation).
|
||||
NameFieldToUpdate: referrerSpec,
|
||||
// Specification of object class to read from.
|
||||
// Always read from metadata/name field.
|
||||
ReferralTarget: backReference.Gvk,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return fMap
|
||||
}
|
||||
|
||||
// TODO: check res for field existence here to avoid extra work.
|
||||
// res.GetFieldValue, which uses yaml.Lookup under the hood, doesn't know
|
||||
// how to parse fieldspec-style paths that make no distinction
|
||||
// between maps and sequences. This means it cannot lookup commonly
|
||||
// used "indeterminate" paths like
|
||||
// spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
// ('containers' is a list, not a map).
|
||||
// However, the fieldspec filter does know how to handle this;
|
||||
// extract that code and call it here?
|
||||
func resHasField(res *resource.Resource, path string) bool {
|
||||
return true
|
||||
// fld := strings.Join(utils.PathSplitter(path), ".")
|
||||
// _, e := res.GetFieldValue(fld)
|
||||
// return e == nil
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package accumulator
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/refvar"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
)
|
||||
|
||||
type refVarTransformer struct {
|
||||
varMap map[string]interface{}
|
||||
replacementCounts map[string]int
|
||||
fieldSpecs []types.FieldSpec
|
||||
}
|
||||
|
||||
// newRefVarTransformer returns a new refVarTransformer
|
||||
// that replaces $(VAR) style variables with values.
|
||||
// The fieldSpecs are the places to look for occurrences of $(VAR).
|
||||
func newRefVarTransformer(
|
||||
varMap map[string]interface{}, fs []types.FieldSpec) *refVarTransformer {
|
||||
return &refVarTransformer{
|
||||
varMap: varMap,
|
||||
fieldSpecs: fs,
|
||||
}
|
||||
}
|
||||
|
||||
// UnusedVars returns slice of Var names that were unused
|
||||
// after a Transform run.
|
||||
func (rv *refVarTransformer) UnusedVars() []string {
|
||||
var unused []string
|
||||
for k := range rv.varMap {
|
||||
if _, ok := rv.replacementCounts[k]; !ok {
|
||||
unused = append(unused, k)
|
||||
}
|
||||
}
|
||||
return unused
|
||||
}
|
||||
|
||||
// Transform replaces $(VAR) style variables with values.
|
||||
func (rv *refVarTransformer) Transform(m resmap.ResMap) error {
|
||||
rv.replacementCounts = make(map[string]int)
|
||||
mf := refvar.MakePrimitiveReplacer(rv.replacementCounts, rv.varMap)
|
||||
for _, res := range m.Resources() {
|
||||
for _, fieldSpec := range rv.fieldSpecs {
|
||||
err := res.ApplyFilter(refvar.Filter{
|
||||
MappingFunc: mf,
|
||||
FieldSpec: fieldSpec,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package accumulator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
)
|
||||
|
||||
// ResAccumulator accumulates resources and the rules
|
||||
// used to customize those resources. It's a ResMap
|
||||
// plus stuff needed to modify the ResMap.
|
||||
type ResAccumulator struct {
|
||||
resMap resmap.ResMap
|
||||
tConfig *builtinconfig.TransformerConfig
|
||||
varSet types.VarSet
|
||||
}
|
||||
|
||||
func MakeEmptyAccumulator() *ResAccumulator {
|
||||
ra := &ResAccumulator{}
|
||||
ra.resMap = resmap.New()
|
||||
ra.tConfig = &builtinconfig.TransformerConfig{}
|
||||
ra.varSet = types.NewVarSet()
|
||||
return ra
|
||||
}
|
||||
|
||||
// ResMap returns a copy of the internal resMap.
|
||||
func (ra *ResAccumulator) ResMap() resmap.ResMap {
|
||||
return ra.resMap.ShallowCopy()
|
||||
}
|
||||
|
||||
// Vars returns a copy of underlying vars.
|
||||
func (ra *ResAccumulator) Vars() []types.Var {
|
||||
return ra.varSet.AsSlice()
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) AppendAll(resources resmap.ResMap) error {
|
||||
return ra.resMap.AppendAll(resources)
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) AbsorbAll(resources resmap.ResMap) error {
|
||||
return ra.resMap.AbsorbAll(resources)
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) MergeConfig(
|
||||
tConfig *builtinconfig.TransformerConfig) (err error) {
|
||||
ra.tConfig, err = ra.tConfig.Merge(tConfig)
|
||||
return err
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) GetTransformerConfig() *builtinconfig.TransformerConfig {
|
||||
return ra.tConfig
|
||||
}
|
||||
|
||||
// MergeVars accumulates vars into ResAccumulator.
|
||||
// A Var is a tuple of name, object reference and field reference.
|
||||
// This func takes a list of vars from the current kustomization file and
|
||||
// annotates the accumulated resources with the names of the vars that match
|
||||
// those resources. E.g. if there's a var named "sam" that wants to get
|
||||
// its data from a ConfigMap named "james", and the resource list contains a
|
||||
// ConfigMap named "james", then that ConfigMap will be annotated with the
|
||||
// var name "sam". Later this annotation is used to find the data for "sam"
|
||||
// by digging into a particular fieldpath of "james".
|
||||
func (ra *ResAccumulator) MergeVars(incoming []types.Var) error {
|
||||
for _, v := range incoming {
|
||||
targetId := resid.NewResIdWithNamespace(v.ObjRef.GVK(), v.ObjRef.Name, v.ObjRef.Namespace)
|
||||
idMatcher := targetId.GvknEquals
|
||||
if targetId.Namespace != "" || targetId.IsClusterScoped() {
|
||||
// Preserve backward compatibility. An empty namespace means
|
||||
// wildcard search on the namespace hence we still use GvknEquals
|
||||
idMatcher = targetId.Equals
|
||||
}
|
||||
matched := ra.resMap.GetMatchingResourcesByAnyId(idMatcher)
|
||||
if len(matched) > 1 {
|
||||
return fmt.Errorf(
|
||||
"found %d resId matches for var %s "+
|
||||
"(unable to disambiguate)",
|
||||
len(matched), v)
|
||||
}
|
||||
if len(matched) == 1 {
|
||||
matched[0].AppendRefVarName(v)
|
||||
}
|
||||
}
|
||||
return ra.varSet.MergeSlice(incoming)
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) MergeAccumulator(other *ResAccumulator) (err error) {
|
||||
err = ra.AppendAll(other.resMap)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = ra.MergeConfig(other.tConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ra.varSet.MergeSet(other.varSet)
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) findVarValueFromResources(v types.Var) (interface{}, error) {
|
||||
for _, res := range ra.resMap.Resources() {
|
||||
for _, varName := range res.GetRefVarNames() {
|
||||
if varName == v.Name {
|
||||
s, err := res.GetFieldValue(v.FieldRef.FieldPath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf(
|
||||
"field specified in var '%v' "+
|
||||
"not found in corresponding resource", v)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf(
|
||||
"var '%v' cannot be mapped to a field "+
|
||||
"in the set of known resources", v)
|
||||
}
|
||||
|
||||
// makeVarReplacementMap returns a map of Var names to
|
||||
// their final values. The values are strings intended
|
||||
// for substitution wherever the $(var.Name) occurs.
|
||||
func (ra *ResAccumulator) makeVarReplacementMap() (map[string]interface{}, error) {
|
||||
result := map[string]interface{}{}
|
||||
for _, v := range ra.Vars() {
|
||||
s, err := ra.findVarValueFromResources(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[v.Name] = s
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) Transform(t resmap.Transformer) error {
|
||||
return t.Transform(ra.resMap)
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) ResolveVars() error {
|
||||
replacementMap, err := ra.makeVarReplacementMap()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(replacementMap) == 0 {
|
||||
return nil
|
||||
}
|
||||
t := newRefVarTransformer(
|
||||
replacementMap, ra.tConfig.VarReference)
|
||||
err = ra.Transform(t)
|
||||
if len(t.UnusedVars()) > 0 {
|
||||
log.Printf(
|
||||
"well-defined vars that were never replaced: %s\n",
|
||||
strings.Join(t.UnusedVars(), ","))
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (ra *ResAccumulator) FixBackReferences() (err error) {
|
||||
if ra.tConfig.NameReference == nil {
|
||||
return nil
|
||||
}
|
||||
return ra.Transform(
|
||||
newNameReferenceTransformer(ra.tConfig.NameReference))
|
||||
}
|
||||
|
||||
// Intersection drops the resources which "other" does not have.
|
||||
func (ra *ResAccumulator) Intersection(other resmap.ResMap) error {
|
||||
otherIds := other.AllIds() //nolint:revive
|
||||
for _, curId := range ra.resMap.AllIds() {
|
||||
toDelete := true
|
||||
for _, otherId := range otherIds {
|
||||
if otherId == curId {
|
||||
toDelete = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if toDelete {
|
||||
err := ra.resMap.Remove(curId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
// Code generated by pluginator on AnnotationsTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/annotations"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Add the given annotations to the given field specifications.
|
||||
type AnnotationsTransformerPlugin struct {
|
||||
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
|
||||
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
}
|
||||
|
||||
func (p *AnnotationsTransformerPlugin) Config(
|
||||
_ *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.Annotations = nil
|
||||
p.FieldSpecs = nil
|
||||
return yaml.Unmarshal(c, p)
|
||||
}
|
||||
|
||||
func (p *AnnotationsTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
if len(p.Annotations) == 0 {
|
||||
return nil
|
||||
}
|
||||
return m.ApplyFilter(annotations.Filter{
|
||||
Annotations: p.Annotations,
|
||||
FsSlice: p.FieldSpecs,
|
||||
})
|
||||
}
|
||||
|
||||
func NewAnnotationsTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &AnnotationsTransformerPlugin{}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
// Code generated by pluginator on ConfigMapGenerator; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/kv"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type ConfigMapGeneratorPlugin struct {
|
||||
h *resmap.PluginHelpers
|
||||
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
|
||||
types.ConfigMapArgs
|
||||
}
|
||||
|
||||
func (p *ConfigMapGeneratorPlugin) Config(h *resmap.PluginHelpers, config []byte) (err error) {
|
||||
p.ConfigMapArgs = types.ConfigMapArgs{}
|
||||
err = yaml.Unmarshal(config, p)
|
||||
if p.ConfigMapArgs.Name == "" {
|
||||
p.ConfigMapArgs.Name = p.Name
|
||||
}
|
||||
if p.ConfigMapArgs.Namespace == "" {
|
||||
p.ConfigMapArgs.Namespace = p.Namespace
|
||||
}
|
||||
p.h = h
|
||||
return
|
||||
}
|
||||
|
||||
func (p *ConfigMapGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
||||
return p.h.ResmapFactory().FromConfigMapArgs(
|
||||
kv.NewLoader(p.h.Loader(), p.h.Validator()), p.ConfigMapArgs)
|
||||
}
|
||||
|
||||
func NewConfigMapGeneratorPlugin() resmap.GeneratorPlugin {
|
||||
return &ConfigMapGeneratorPlugin{}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// Code generated by pluginator on HashTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
)
|
||||
|
||||
type HashTransformerPlugin struct {
|
||||
hasher ifc.KustHasher
|
||||
}
|
||||
|
||||
func (p *HashTransformerPlugin) Config(
|
||||
h *resmap.PluginHelpers, _ []byte) (err error) {
|
||||
p.hasher = h.ResmapFactory().RF().Hasher()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Transform appends hash to generated resources.
|
||||
func (p *HashTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
for _, res := range m.Resources() {
|
||||
if res.NeedHashSuffix() {
|
||||
h, err := res.Hash(p.hasher)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res.StorePreviousId()
|
||||
res.SetName(fmt.Sprintf("%s-%s", res.GetName(), h))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewHashTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &HashTransformerPlugin{}
|
||||
}
|
||||
Generated
Vendored
+396
@@ -0,0 +1,396 @@
|
||||
// Code generated by pluginator on HelmChartInflationGenerator; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml/merge2"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Generate resources from a remote or local helm chart.
|
||||
type HelmChartInflationGeneratorPlugin struct {
|
||||
h *resmap.PluginHelpers
|
||||
types.HelmGlobals
|
||||
types.HelmChart
|
||||
tmpDir string
|
||||
}
|
||||
|
||||
const (
|
||||
valuesMergeOptionMerge = "merge"
|
||||
valuesMergeOptionOverride = "override"
|
||||
valuesMergeOptionReplace = "replace"
|
||||
)
|
||||
|
||||
var legalMergeOptions = []string{
|
||||
valuesMergeOptionMerge,
|
||||
valuesMergeOptionOverride,
|
||||
valuesMergeOptionReplace,
|
||||
}
|
||||
|
||||
// Config uses the input plugin configurations `config` to setup the generator
|
||||
// options
|
||||
func (p *HelmChartInflationGeneratorPlugin) Config(
|
||||
h *resmap.PluginHelpers, config []byte) (err error) {
|
||||
if h.GeneralConfig() == nil {
|
||||
return fmt.Errorf("unable to access general config")
|
||||
}
|
||||
if !h.GeneralConfig().HelmConfig.Enabled {
|
||||
return fmt.Errorf("must specify --enable-helm")
|
||||
}
|
||||
if h.GeneralConfig().HelmConfig.Command == "" {
|
||||
return fmt.Errorf("must specify --helm-command")
|
||||
}
|
||||
|
||||
// CLI args takes precedence
|
||||
if h.GeneralConfig().HelmConfig.KubeVersion != "" {
|
||||
p.HelmChart.KubeVersion = h.GeneralConfig().HelmConfig.KubeVersion
|
||||
}
|
||||
if len(h.GeneralConfig().HelmConfig.ApiVersions) != 0 {
|
||||
p.HelmChart.ApiVersions = h.GeneralConfig().HelmConfig.ApiVersions
|
||||
}
|
||||
if h.GeneralConfig().HelmConfig.Debug {
|
||||
p.HelmChart.Debug = h.GeneralConfig().HelmConfig.Debug
|
||||
}
|
||||
|
||||
p.h = h
|
||||
if err = yaml.Unmarshal(config, p); err != nil {
|
||||
return
|
||||
}
|
||||
return p.validateArgs()
|
||||
}
|
||||
|
||||
// This uses the real file system since tmpDir may be used
|
||||
// by the helm subprocess. Cannot use a chroot jail or fake
|
||||
// filesystem since we allow the user to use previously
|
||||
// downloaded charts. This is safe since this plugin is
|
||||
// owned by kustomize.
|
||||
func (p *HelmChartInflationGeneratorPlugin) establishTmpDir() (err error) {
|
||||
if p.tmpDir != "" {
|
||||
// already done.
|
||||
return nil
|
||||
}
|
||||
p.tmpDir, err = os.MkdirTemp("", "kustomize-helm-")
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *HelmChartInflationGeneratorPlugin) validateArgs() (err error) {
|
||||
if p.Name == "" {
|
||||
return fmt.Errorf("chart name cannot be empty")
|
||||
}
|
||||
|
||||
// ChartHome might be consulted by the plugin (to read
|
||||
// values files below it), so it must be located under
|
||||
// the loader root (unless root restrictions are
|
||||
// disabled, in which case this can be an absolute path).
|
||||
if p.ChartHome == "" {
|
||||
p.ChartHome = types.HelmDefaultHome
|
||||
}
|
||||
|
||||
// The ValuesFile(s) may be consulted by the plugin, so it must
|
||||
// be under the loader root (unless root restrictions are
|
||||
// disabled).
|
||||
if p.ValuesFile == "" {
|
||||
p.ValuesFile = filepath.Join(p.absChartHome(), p.Name, "values.yaml")
|
||||
}
|
||||
for i, file := range p.AdditionalValuesFiles {
|
||||
// use Load() to enforce root restrictions
|
||||
if _, err := p.h.Loader().Load(file); err != nil {
|
||||
return errors.WrapPrefixf(err, "could not load additionalValuesFile")
|
||||
}
|
||||
// the additional values filepaths must be relative to the kust root
|
||||
p.AdditionalValuesFiles[i] = filepath.Join(p.h.Loader().Root(), file)
|
||||
}
|
||||
|
||||
if err = p.errIfIllegalValuesMerge(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// ConfigHome is not loaded by the plugin, and can be located anywhere.
|
||||
if p.ConfigHome == "" {
|
||||
if err = p.establishTmpDir(); err != nil {
|
||||
return errors.WrapPrefixf(
|
||||
err, "unable to create tmp dir for HELM_CONFIG_HOME")
|
||||
}
|
||||
p.ConfigHome = filepath.Join(p.tmpDir, "helm")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *HelmChartInflationGeneratorPlugin) errIfIllegalValuesMerge() error {
|
||||
if p.ValuesMerge == "" {
|
||||
// Use the default.
|
||||
p.ValuesMerge = valuesMergeOptionOverride
|
||||
return nil
|
||||
}
|
||||
for _, opt := range legalMergeOptions {
|
||||
if p.ValuesMerge == opt {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("valuesMerge must be one of %v", legalMergeOptions)
|
||||
}
|
||||
|
||||
func (p *HelmChartInflationGeneratorPlugin) absChartHome() string {
|
||||
var chartHome string
|
||||
if filepath.IsAbs(p.ChartHome) {
|
||||
chartHome = p.ChartHome
|
||||
} else {
|
||||
chartHome = filepath.Join(p.h.Loader().Root(), p.ChartHome)
|
||||
}
|
||||
|
||||
if p.Version != "" && p.Repo != "" {
|
||||
return filepath.Join(chartHome, fmt.Sprintf("%s-%s", p.Name, p.Version))
|
||||
}
|
||||
return chartHome
|
||||
}
|
||||
|
||||
func (p *HelmChartInflationGeneratorPlugin) runHelmCommand(
|
||||
args []string) ([]byte, error) {
|
||||
stdout := new(bytes.Buffer)
|
||||
stderr := new(bytes.Buffer)
|
||||
cmd := exec.Command(p.h.GeneralConfig().HelmConfig.Command, args...)
|
||||
cmd.Stdout = stdout
|
||||
cmd.Stderr = stderr
|
||||
env := []string{
|
||||
fmt.Sprintf("HELM_CONFIG_HOME=%s", p.ConfigHome),
|
||||
fmt.Sprintf("HELM_CACHE_HOME=%s/.cache", p.ConfigHome),
|
||||
fmt.Sprintf("HELM_DATA_HOME=%s/.data", p.ConfigHome)}
|
||||
cmd.Env = append(os.Environ(), env...)
|
||||
err := cmd.Run()
|
||||
errorOutput := stderr.String()
|
||||
if slices.Contains(args, "--debug") {
|
||||
errorOutput = " Helm stack trace:\n" + errorOutput + "\nHelm template:\n" + stdout.String() + "\n"
|
||||
}
|
||||
if err != nil {
|
||||
helm := p.h.GeneralConfig().HelmConfig.Command
|
||||
err = errors.WrapPrefixf(
|
||||
fmt.Errorf(
|
||||
"unable to run: '%s %s' with env=%s (is '%s' installed?): %w",
|
||||
helm, strings.Join(args, " "), env, helm, err),
|
||||
"%s", errorOutput,
|
||||
)
|
||||
}
|
||||
return stdout.Bytes(), err
|
||||
}
|
||||
|
||||
// createNewMergedValuesFile replaces/merges original values file with ValuesInline.
|
||||
func (p *HelmChartInflationGeneratorPlugin) createNewMergedValuesFile() (
|
||||
path string, err error) {
|
||||
if p.ValuesMerge == valuesMergeOptionMerge ||
|
||||
p.ValuesMerge == valuesMergeOptionOverride {
|
||||
if err = p.replaceValuesInline(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
var b []byte
|
||||
b, err = yaml.Marshal(p.ValuesInline)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return p.writeValuesBytes(b)
|
||||
}
|
||||
|
||||
func (p *HelmChartInflationGeneratorPlugin) replaceValuesInline() error {
|
||||
pValues, err := p.h.Loader().Load(p.ValuesFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
chValues, err := kyaml.Parse(string(pValues))
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "could not parse values file into rnode")
|
||||
}
|
||||
inlineValues, err := kyaml.FromMap(p.ValuesInline)
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "could not parse values inline into rnode")
|
||||
}
|
||||
var outValues *kyaml.RNode
|
||||
switch p.ValuesMerge {
|
||||
// Function `merge2.Merge` overrides values in dest with values from src.
|
||||
// To achieve override or merge behavior, we pass parameters in different order.
|
||||
// Object passed as dest will be modified, so we copy it just in case someone
|
||||
// decides to use it after this is called.
|
||||
case valuesMergeOptionOverride:
|
||||
outValues, err = merge2.Merge(inlineValues, chValues.Copy(), kyaml.MergeOptions{})
|
||||
case valuesMergeOptionMerge:
|
||||
outValues, err = merge2.Merge(chValues, inlineValues.Copy(), kyaml.MergeOptions{})
|
||||
}
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "could not merge values")
|
||||
}
|
||||
mapValues, err := outValues.Map()
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "could not parse merged values into map")
|
||||
}
|
||||
p.ValuesInline = mapValues
|
||||
return err
|
||||
}
|
||||
|
||||
// copyValuesFile to avoid branching. TODO: get rid of this.
|
||||
func (p *HelmChartInflationGeneratorPlugin) copyValuesFile() (string, error) {
|
||||
b, err := p.h.Loader().Load(p.ValuesFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return p.writeValuesBytes(b)
|
||||
}
|
||||
|
||||
// Write a absolute path file in the tmp file system.
|
||||
func (p *HelmChartInflationGeneratorPlugin) writeValuesBytes(
|
||||
b []byte) (string, error) {
|
||||
if err := p.establishTmpDir(); err != nil {
|
||||
return "", fmt.Errorf("cannot create tmp dir to write helm values")
|
||||
}
|
||||
path := filepath.Join(p.tmpDir, p.Name+"-kustomize-values.yaml")
|
||||
return path, errors.WrapPrefixf(os.WriteFile(path, b, 0644), "failed to write values file")
|
||||
}
|
||||
|
||||
func (p *HelmChartInflationGeneratorPlugin) cleanup() {
|
||||
if p.tmpDir != "" {
|
||||
os.RemoveAll(p.tmpDir)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate implements generator
|
||||
func (p *HelmChartInflationGeneratorPlugin) Generate() (rm resmap.ResMap, err error) {
|
||||
defer p.cleanup()
|
||||
if err = p.checkHelmVersion(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if path, exists := p.chartExistsLocally(); !exists {
|
||||
if p.Repo == "" {
|
||||
return nil, fmt.Errorf(
|
||||
"no repo specified for pull, no chart found at '%s'", path)
|
||||
}
|
||||
if _, err := p.runHelmCommand(p.pullCommand()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if len(p.ValuesInline) > 0 {
|
||||
p.ValuesFile, err = p.createNewMergedValuesFile()
|
||||
} else {
|
||||
p.ValuesFile, err = p.copyValuesFile()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var stdout []byte
|
||||
stdout, err = p.runHelmCommand(p.AsHelmArgs(p.absChartHome()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rm, resMapErr := p.h.ResmapFactory().NewResMapFromBytes(stdout)
|
||||
if resMapErr == nil {
|
||||
if err := p.markHelmGeneratedResources(rm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rm, nil
|
||||
}
|
||||
// try to remove the contents before first "---" because
|
||||
// helm may produce messages to stdout before it
|
||||
r := &kio.ByteReader{Reader: bytes.NewBuffer(stdout), OmitReaderAnnotations: true}
|
||||
nodes, err := r.Read()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading helm output: %w", err)
|
||||
}
|
||||
|
||||
if len(nodes) != 0 {
|
||||
rm, err = p.h.ResmapFactory().NewResMapFromRNodeSlice(nodes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not parse rnode slice into resource map: %w", err)
|
||||
}
|
||||
if err := p.markHelmGeneratedResources(rm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rm, nil
|
||||
}
|
||||
return nil, fmt.Errorf("could not parse bytes into resource map: %w", resMapErr)
|
||||
}
|
||||
|
||||
func (p *HelmChartInflationGeneratorPlugin) pullCommand() []string {
|
||||
args := []string{
|
||||
"pull",
|
||||
"--untar",
|
||||
"--untardir", p.absChartHome(),
|
||||
}
|
||||
|
||||
switch {
|
||||
case strings.HasPrefix(p.Repo, "oci://"):
|
||||
args = append(args, strings.TrimSuffix(p.Repo, "/")+"/"+p.Name)
|
||||
case p.Repo != "":
|
||||
args = append(args, "--repo", p.Repo)
|
||||
fallthrough
|
||||
default:
|
||||
args = append(args, p.Name)
|
||||
}
|
||||
|
||||
if p.Version != "" {
|
||||
args = append(args, "--version", p.Version)
|
||||
}
|
||||
if p.Devel {
|
||||
args = append(args, "--devel")
|
||||
}
|
||||
return args
|
||||
}
|
||||
|
||||
// chartExistsLocally will return true if the chart does exist in
|
||||
// local chart home.
|
||||
func (p *HelmChartInflationGeneratorPlugin) chartExistsLocally() (string, bool) {
|
||||
path := filepath.Join(p.absChartHome(), p.Name)
|
||||
s, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return path, s.IsDir()
|
||||
}
|
||||
|
||||
func (p *HelmChartInflationGeneratorPlugin) markHelmGeneratedResources(rm resmap.ResMap) error {
|
||||
for _, r := range rm.Resources() {
|
||||
if err := r.RNode.PipeE(kyaml.SetAnnotation(konfig.HelmGeneratedAnnotation, "true")); err != nil {
|
||||
return fmt.Errorf("failed to set helm annotation: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkHelmVersion will return an error if the helm version is not V3 or V4
|
||||
func (p *HelmChartInflationGeneratorPlugin) checkHelmVersion() error {
|
||||
stdout, err := p.runHelmCommand([]string{"version", "--short"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r, err := regexp.Compile(`v?\d+(\.\d+)+`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v := r.FindString(string(stdout))
|
||||
if v == "" {
|
||||
return fmt.Errorf("cannot find version string in %s", string(stdout))
|
||||
}
|
||||
if v[0] == 'v' {
|
||||
v = v[1:]
|
||||
}
|
||||
majorVersion := strings.Split(v, ".")[0]
|
||||
if majorVersion != "3" && majorVersion != "4" {
|
||||
return fmt.Errorf("this plugin requires helm V3 or V4 but got v%s", v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewHelmChartInflationGeneratorPlugin() resmap.GeneratorPlugin {
|
||||
return &HelmChartInflationGeneratorPlugin{}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
// Code generated by pluginator on IAMPolicyGenerator; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/iampolicygenerator"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type IAMPolicyGeneratorPlugin struct {
|
||||
types.IAMPolicyGeneratorArgs
|
||||
}
|
||||
|
||||
func (p *IAMPolicyGeneratorPlugin) Config(h *resmap.PluginHelpers, config []byte) (err error) {
|
||||
p.IAMPolicyGeneratorArgs = types.IAMPolicyGeneratorArgs{}
|
||||
err = yaml.Unmarshal(config, p)
|
||||
return
|
||||
}
|
||||
|
||||
func (p *IAMPolicyGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
||||
r := resmap.New()
|
||||
err := r.ApplyFilter(iampolicygenerator.Filter{
|
||||
IAMPolicyGenerator: p.IAMPolicyGeneratorArgs,
|
||||
})
|
||||
return r, err
|
||||
}
|
||||
|
||||
func NewIAMPolicyGeneratorPlugin() resmap.GeneratorPlugin {
|
||||
return &IAMPolicyGeneratorPlugin{}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
// Code generated by pluginator on ImageTagTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/imagetag"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Find matching image declarations and replace
|
||||
// the name, tag and/or digest.
|
||||
type ImageTagTransformerPlugin struct {
|
||||
ImageTag types.Image `json:"imageTag,omitempty" yaml:"imageTag,omitempty"`
|
||||
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
}
|
||||
|
||||
func (p *ImageTagTransformerPlugin) Config(
|
||||
_ *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.ImageTag = types.Image{}
|
||||
p.FieldSpecs = nil
|
||||
return yaml.Unmarshal(c, p)
|
||||
}
|
||||
|
||||
func (p *ImageTagTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
if err := m.ApplyFilter(imagetag.LegacyFilter{
|
||||
ImageTag: p.ImageTag,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return m.ApplyFilter(imagetag.Filter{
|
||||
ImageTag: p.ImageTag,
|
||||
FsSlice: p.FieldSpecs,
|
||||
})
|
||||
}
|
||||
|
||||
func NewImageTagTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &ImageTagTransformerPlugin{}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
// Code generated by pluginator on LabelTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/filters/labels"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Add the given labels to the given field specifications.
|
||||
type LabelTransformerPlugin struct {
|
||||
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
|
||||
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
}
|
||||
|
||||
func (p *LabelTransformerPlugin) Config(
|
||||
_ *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.Labels = nil
|
||||
p.FieldSpecs = nil
|
||||
return yaml.Unmarshal(c, p)
|
||||
}
|
||||
|
||||
func (p *LabelTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
if len(p.Labels) == 0 {
|
||||
return nil
|
||||
}
|
||||
return m.ApplyFilter(labels.Filter{
|
||||
Labels: p.Labels,
|
||||
FsSlice: p.FieldSpecs,
|
||||
})
|
||||
}
|
||||
|
||||
func NewLabelTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &LabelTransformerPlugin{}
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
// Code generated by pluginator on NamespaceTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/namespace"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Change or set the namespace of non-cluster level resources.
|
||||
//
|
||||
//nolint:tagalign
|
||||
type NamespaceTransformerPlugin struct {
|
||||
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
|
||||
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
UnsetOnly bool `json:"unsetOnly" yaml:"unsetOnly"`
|
||||
SetRoleBindingSubjects namespace.RoleBindingSubjectMode `json:"setRoleBindingSubjects" yaml:"setRoleBindingSubjects"`
|
||||
}
|
||||
|
||||
func (p *NamespaceTransformerPlugin) Config(
|
||||
_ *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.Namespace = ""
|
||||
p.FieldSpecs = nil
|
||||
if err := yaml.Unmarshal(c, p); err != nil {
|
||||
return errors.WrapPrefixf(err, "unmarshalling NamespaceTransformer config")
|
||||
}
|
||||
switch p.SetRoleBindingSubjects {
|
||||
case namespace.AllServiceAccountSubjects, namespace.DefaultSubjectsOnly, namespace.NoSubjects:
|
||||
// valid
|
||||
case namespace.SubjectModeUnspecified:
|
||||
p.SetRoleBindingSubjects = namespace.DefaultSubjectsOnly
|
||||
default:
|
||||
return errors.Errorf("invalid value %q for setRoleBindingSubjects: "+
|
||||
"must be one of %q, %q or %q", p.SetRoleBindingSubjects,
|
||||
namespace.DefaultSubjectsOnly, namespace.NoSubjects, namespace.AllServiceAccountSubjects)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *NamespaceTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
if len(p.Namespace) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, r := range m.Resources() {
|
||||
if r.IsNilOrEmpty() {
|
||||
// Don't mutate empty objects?
|
||||
continue
|
||||
}
|
||||
if annotations := r.GetAnnotations(konfig.HelmGeneratedAnnotation); annotations[konfig.HelmGeneratedAnnotation] == "true" {
|
||||
// Don't apply namespace on Helm generated manifest. Helm should take care of it.
|
||||
continue
|
||||
}
|
||||
r.StorePreviousId()
|
||||
if err := r.ApplyFilter(namespace.Filter{
|
||||
Namespace: p.Namespace,
|
||||
FsSlice: p.FieldSpecs,
|
||||
SetRoleBindingSubjects: p.SetRoleBindingSubjects,
|
||||
UnsetOnly: p.UnsetOnly,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
matches := m.GetMatchingResourcesByCurrentId(r.CurId().Equals)
|
||||
if len(matches) != 1 {
|
||||
return fmt.Errorf(
|
||||
"namespace transformation produces ID conflict: %+v", matches)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewNamespaceTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &NamespaceTransformerPlugin{}
|
||||
}
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
// Code generated by pluginator on PatchJson6902Transformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
jsonpatch "gopkg.in/evanphx/json-patch.v4"
|
||||
"sigs.k8s.io/kustomize/api/filters/patchjson6902"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type PatchJson6902TransformerPlugin struct {
|
||||
ldr ifc.Loader
|
||||
decodedPatch jsonpatch.Patch
|
||||
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||
JsonOp string `json:"jsonOp,omitempty" yaml:"jsonOp,omitempty"`
|
||||
}
|
||||
|
||||
func (p *PatchJson6902TransformerPlugin) Config(
|
||||
h *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.ldr = h.Loader()
|
||||
err = yaml.Unmarshal(c, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if p.Target.Name == "" {
|
||||
return fmt.Errorf("must specify the target name")
|
||||
}
|
||||
if p.Path == "" && p.JsonOp == "" {
|
||||
return fmt.Errorf("empty file path and empty jsonOp")
|
||||
}
|
||||
if p.Path != "" {
|
||||
if p.JsonOp != "" {
|
||||
return fmt.Errorf("must specify a file path or jsonOp, not both")
|
||||
}
|
||||
rawOp, err := p.ldr.Load(p.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.JsonOp = string(rawOp)
|
||||
if p.JsonOp == "" {
|
||||
return fmt.Errorf("patch file '%s' empty seems to be empty", p.Path)
|
||||
}
|
||||
}
|
||||
if p.JsonOp[0] != '[' {
|
||||
// if it doesn't seem to be JSON, imagine
|
||||
// it is YAML, and convert to JSON.
|
||||
op, err := yaml.YAMLToJSON([]byte(p.JsonOp))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.JsonOp = string(op)
|
||||
}
|
||||
p.decodedPatch, err = jsonpatch.DecodePatch([]byte(p.JsonOp))
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "decoding %s", p.JsonOp)
|
||||
}
|
||||
if len(p.decodedPatch) == 0 {
|
||||
return fmt.Errorf(
|
||||
"patch appears to be empty; file=%s, JsonOp=%s", p.Path, p.JsonOp)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PatchJson6902TransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
if p.Target == nil {
|
||||
return fmt.Errorf("must specify a target for patch %s", p.JsonOp)
|
||||
}
|
||||
resources, err := m.Select(*p.Target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, res := range resources {
|
||||
internalAnnotations := kioutil.GetInternalAnnotations(&res.RNode)
|
||||
|
||||
err = res.ApplyFilter(patchjson6902.Filter{
|
||||
Patch: p.JsonOp,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
annotations := res.GetAnnotations()
|
||||
for key, value := range internalAnnotations {
|
||||
annotations[key] = value
|
||||
}
|
||||
err = res.SetAnnotations(annotations)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewPatchJson6902TransformerPlugin() resmap.TransformerPlugin {
|
||||
return &PatchJson6902TransformerPlugin{}
|
||||
}
|
||||
Generated
Vendored
+87
@@ -0,0 +1,87 @@
|
||||
// Code generated by pluginator on PatchStrategicMergeTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type PatchStrategicMergeTransformerPlugin struct {
|
||||
loadedPatches []*resource.Resource
|
||||
Paths []types.PatchStrategicMerge `json:"paths,omitempty" yaml:"paths,omitempty"`
|
||||
Patches string `json:"patches,omitempty" yaml:"patches,omitempty"`
|
||||
}
|
||||
|
||||
func (p *PatchStrategicMergeTransformerPlugin) Config(
|
||||
h *resmap.PluginHelpers, c []byte) (err error) {
|
||||
err = yaml.Unmarshal(c, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(p.Paths) == 0 && p.Patches == "" {
|
||||
return fmt.Errorf("empty file path and empty patch content")
|
||||
}
|
||||
if len(p.Paths) != 0 {
|
||||
patches, err := loadFromPaths(h, p.Paths)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.loadedPatches = append(p.loadedPatches, patches...)
|
||||
}
|
||||
if p.Patches != "" {
|
||||
patches, err := h.ResmapFactory().RF().SliceFromBytes([]byte(p.Patches))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.loadedPatches = append(p.loadedPatches, patches...)
|
||||
}
|
||||
if len(p.loadedPatches) == 0 {
|
||||
return fmt.Errorf(
|
||||
"patch appears to be empty; files=%v, Patch=%s", p.Paths, p.Patches)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadFromPaths(
|
||||
h *resmap.PluginHelpers,
|
||||
paths []types.PatchStrategicMerge) (
|
||||
result []*resource.Resource, err error) {
|
||||
var patches []*resource.Resource
|
||||
for _, path := range paths {
|
||||
// For legacy reasons, attempt to treat the path string as
|
||||
// actual patch content.
|
||||
patches, err = h.ResmapFactory().RF().SliceFromBytes([]byte(path))
|
||||
if err != nil {
|
||||
// Failing that, treat it as a file path.
|
||||
patches, err = h.ResmapFactory().RF().SliceFromPatches(
|
||||
h.Loader(), []types.PatchStrategicMerge{path})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
result = append(result, patches...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *PatchStrategicMergeTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
for _, patch := range p.loadedPatches {
|
||||
target, err := m.GetById(patch.OrgId())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = m.ApplySmPatch(
|
||||
resource.MakeIdSet([]*resource.Resource{target}), patch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewPatchStrategicMergeTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &PatchStrategicMergeTransformerPlugin{}
|
||||
}
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
// Code generated by pluginator on PatchTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
jsonpatch "gopkg.in/evanphx/json-patch.v4"
|
||||
"sigs.k8s.io/kustomize/api/filters/patchjson6902"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type PatchTransformerPlugin struct {
|
||||
smPatches []*resource.Resource // strategic-merge patches
|
||||
jsonPatches jsonpatch.Patch // json6902 patch
|
||||
// patchText is pure patch text created by Path or Patch
|
||||
patchText string
|
||||
// patchSource is patch source message
|
||||
patchSource string
|
||||
Path string `json:"path,omitempty" yaml:"path,omitempty"`
|
||||
Patch string `json:"patch,omitempty" yaml:"patch,omitempty"`
|
||||
Target *types.Selector `json:"target,omitempty" yaml:"target,omitempty"`
|
||||
Options *types.PatchArgs `json:"options,omitempty" yaml:"options,omitempty"`
|
||||
}
|
||||
|
||||
func (p *PatchTransformerPlugin) Config(h *resmap.PluginHelpers, c []byte) error {
|
||||
if err := yaml.Unmarshal(c, p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.Patch = strings.TrimSpace(p.Patch)
|
||||
switch {
|
||||
case p.Patch == "" && p.Path == "":
|
||||
return fmt.Errorf("must specify one of patch and path in\n%s", string(c))
|
||||
case p.Patch != "" && p.Path != "":
|
||||
return fmt.Errorf("patch and path can't be set at the same time\n%s", string(c))
|
||||
case p.Patch != "":
|
||||
p.patchText = p.Patch
|
||||
p.patchSource = fmt.Sprintf("[patch: %q]", p.patchText)
|
||||
case p.Path != "":
|
||||
loaded, err := h.Loader().Load(p.Path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get the patch file from path(%s): %w", p.Path, err)
|
||||
}
|
||||
p.patchText = string(loaded)
|
||||
p.patchSource = fmt.Sprintf("[path: %q]", p.Path)
|
||||
}
|
||||
|
||||
patchesSM, errSM := h.ResmapFactory().RF().SliceFromBytes([]byte(p.patchText))
|
||||
patchesJson, errJson := jsonPatchFromBytes([]byte(p.patchText))
|
||||
|
||||
if ((errSM == nil && errJson == nil) ||
|
||||
(patchesSM != nil && patchesJson != nil)) &&
|
||||
(len(patchesSM) > 0 && len(patchesJson) > 0) {
|
||||
return fmt.Errorf(
|
||||
"illegally qualifies as both an SM and JSON patch: %s",
|
||||
p.patchSource)
|
||||
}
|
||||
if errSM != nil && errJson != nil {
|
||||
return fmt.Errorf(
|
||||
"unable to parse SM or JSON patch from %s", p.patchSource)
|
||||
}
|
||||
if errSM == nil {
|
||||
p.smPatches = patchesSM
|
||||
for _, loadedPatch := range p.smPatches {
|
||||
if p.Options == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if p.Options.AllowNameChange {
|
||||
loadedPatch.AllowNameChange()
|
||||
}
|
||||
if p.Options.AllowKindChange {
|
||||
loadedPatch.AllowKindChange()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
p.jsonPatches = patchesJson
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PatchTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
if p.smPatches != nil {
|
||||
return p.transformStrategicMerge(m)
|
||||
}
|
||||
if p.jsonPatches != nil {
|
||||
return p.transformJson6902(m)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// transformStrategicMerge applies each loaded strategic merge patch
|
||||
// to the resource in the ResMap that matches the identifier of the patch.
|
||||
// If only one patch is specified, the Target can be used instead.
|
||||
func (p *PatchTransformerPlugin) transformStrategicMerge(m resmap.ResMap) error {
|
||||
if p.Target != nil {
|
||||
if len(p.smPatches) > 1 {
|
||||
// detail: https://github.com/kubernetes-sigs/kustomize/issues/5049#issuecomment-1440604403
|
||||
return fmt.Errorf("Multiple Strategic-Merge Patches in one `patches` entry is not allowed to set `patches.target` field: %s", p.patchSource)
|
||||
}
|
||||
|
||||
// single patch
|
||||
patch := p.smPatches[0]
|
||||
selected, err := m.Select(*p.Target)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to find patch target %q in `resources`: %w", p.Target, err)
|
||||
}
|
||||
return errors.Wrap(m.ApplySmPatch(resource.MakeIdSet(selected), patch))
|
||||
}
|
||||
|
||||
for _, patch := range p.smPatches {
|
||||
target, err := m.GetById(patch.OrgId())
|
||||
if err != nil {
|
||||
return fmt.Errorf("no resource matches strategic merge patch %q: %w", patch.OrgId(), err)
|
||||
}
|
||||
if err := target.ApplySmPatch(patch); err != nil {
|
||||
return errors.Wrap(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// transformJson6902 applies json6902 Patch to all the resources in the ResMap that match Target.
|
||||
func (p *PatchTransformerPlugin) transformJson6902(m resmap.ResMap) error {
|
||||
if p.Target == nil {
|
||||
return fmt.Errorf("must specify a target for JSON patch %s", p.patchSource)
|
||||
}
|
||||
resources, err := m.Select(*p.Target)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, res := range resources {
|
||||
res.StorePreviousId()
|
||||
internalAnnotations := kioutil.GetInternalAnnotations(&res.RNode)
|
||||
err = res.ApplyFilter(patchjson6902.Filter{
|
||||
Patch: p.patchText,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
annotations := res.GetAnnotations()
|
||||
for key, value := range internalAnnotations {
|
||||
annotations[key] = value
|
||||
}
|
||||
err = res.SetAnnotations(annotations)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// jsonPatchFromBytes loads a Json 6902 patch from a bytes input
|
||||
func jsonPatchFromBytes(in []byte) (jsonpatch.Patch, error) {
|
||||
ops := string(in)
|
||||
if ops == "" {
|
||||
return nil, fmt.Errorf("empty json patch operations")
|
||||
}
|
||||
|
||||
if ops[0] != '[' {
|
||||
// TODO(5049):
|
||||
// In the case of multiple yaml documents, return error instead of ignoring all but first.
|
||||
// Details: https://github.com/kubernetes-sigs/kustomize/pull/5194#discussion_r1256686728
|
||||
jsonOps, err := yaml.YAMLToJSON(in)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ops = string(jsonOps)
|
||||
}
|
||||
return jsonpatch.DecodePatch([]byte(ops))
|
||||
}
|
||||
|
||||
func NewPatchTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &PatchTransformerPlugin{}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
// Code generated by pluginator on PrefixTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/prefix"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Add the given prefix to the field
|
||||
type PrefixTransformerPlugin struct {
|
||||
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
|
||||
FieldSpecs types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
}
|
||||
|
||||
// TODO: Make this gvk skip list part of the config.
|
||||
var prefixFieldSpecsToSkip = types.FsSlice{
|
||||
{Gvk: resid.Gvk{Kind: "CustomResourceDefinition"}},
|
||||
{Gvk: resid.Gvk{Group: "apiregistration.k8s.io", Kind: "APIService"}},
|
||||
{Gvk: resid.Gvk{Kind: "Namespace"}},
|
||||
}
|
||||
|
||||
func (p *PrefixTransformerPlugin) Config(
|
||||
_ *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.Prefix = ""
|
||||
p.FieldSpecs = nil
|
||||
err = yaml.Unmarshal(c, p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if p.FieldSpecs == nil {
|
||||
return errors.New("fieldSpecs is not expected to be nil")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *PrefixTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
// Even if the Prefix is empty we want to proceed with the
|
||||
// transformation. This allows to add contextual information
|
||||
// to the resources (AddNamePrefix).
|
||||
for _, r := range m.Resources() {
|
||||
// TODO: move this test into the filter (i.e. make a better filter)
|
||||
if p.shouldSkip(r.OrgId()) {
|
||||
continue
|
||||
}
|
||||
id := r.OrgId()
|
||||
// current default configuration contains
|
||||
// only one entry: "metadata/name" with no GVK
|
||||
for _, fs := range p.FieldSpecs {
|
||||
// TODO: this is redundant to filter (but needed for now)
|
||||
if !id.IsSelected(&fs.Gvk) {
|
||||
continue
|
||||
}
|
||||
// TODO: move this test into the filter.
|
||||
if fs.Path == "metadata/name" {
|
||||
// "metadata/name" is the only field.
|
||||
// this will add a prefix to the resource
|
||||
// even if it is empty
|
||||
|
||||
r.AddNamePrefix(p.Prefix)
|
||||
if p.Prefix != "" {
|
||||
// TODO: There are multiple transformers that can change a resource's name, and each makes a call to
|
||||
// StorePreviousID(). We should make it so that we only call StorePreviousID once per kustomization layer
|
||||
// to avoid storing intermediate names between transformations, to prevent intermediate name conflicts.
|
||||
r.StorePreviousId()
|
||||
}
|
||||
}
|
||||
if err := r.ApplyFilter(prefix.Filter{
|
||||
Prefix: p.Prefix,
|
||||
FieldSpec: fs,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PrefixTransformerPlugin) shouldSkip(id resid.ResId) bool {
|
||||
for _, path := range prefixFieldSpecsToSkip {
|
||||
if id.IsSelected(&path.Gvk) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func NewPrefixTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &PrefixTransformerPlugin{}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
// Code generated by pluginator on ReplacementTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/replacement"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Replace values in targets with values from a source
|
||||
type ReplacementTransformerPlugin struct {
|
||||
ReplacementList []types.ReplacementField `json:"replacements,omitempty" yaml:"replacements,omitempty"`
|
||||
replacements []types.Replacement
|
||||
}
|
||||
|
||||
func (p *ReplacementTransformerPlugin) Config(
|
||||
h *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.ReplacementList = []types.ReplacementField{}
|
||||
if err := yaml.Unmarshal(c, p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, r := range p.ReplacementList {
|
||||
if r.Path != "" && (r.Source != nil || len(r.Targets) != 0) {
|
||||
return fmt.Errorf("cannot specify both path and inline replacement")
|
||||
}
|
||||
if r.Path != "" {
|
||||
// load the replacement from the path
|
||||
content, err := h.Loader().Load(r.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// find if the path contains a a list of replacements or a single replacement
|
||||
var replacement interface{}
|
||||
err = yaml.Unmarshal(content, &replacement)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items := reflect.ValueOf(replacement)
|
||||
switch items.Kind() {
|
||||
case reflect.Slice:
|
||||
repl := []types.Replacement{}
|
||||
if err := yaml.Unmarshal(content, &repl); err != nil {
|
||||
return err
|
||||
}
|
||||
p.replacements = append(p.replacements, repl...)
|
||||
case reflect.Map:
|
||||
repl := types.Replacement{}
|
||||
if err := yaml.Unmarshal(content, &repl); err != nil {
|
||||
return err
|
||||
}
|
||||
p.replacements = append(p.replacements, repl)
|
||||
default:
|
||||
return fmt.Errorf("unsupported replacement type encountered within replacement path: %v", items.Kind())
|
||||
}
|
||||
} else {
|
||||
// replacement information is already loaded
|
||||
p.replacements = append(p.replacements, r.Replacement)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ReplacementTransformerPlugin) Transform(m resmap.ResMap) (err error) {
|
||||
return m.ApplyFilter(replacement.Filter{
|
||||
Replacements: p.replacements,
|
||||
})
|
||||
}
|
||||
|
||||
func NewReplacementTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &ReplacementTransformerPlugin{}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
// Code generated by pluginator on ReplicaCountTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/replicacount"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Find matching replicas declarations and replace the count.
|
||||
// Eases the kustomization configuration of replica changes.
|
||||
type ReplicaCountTransformerPlugin struct {
|
||||
Replica types.Replica `json:"replica,omitempty" yaml:"replica,omitempty"`
|
||||
FieldSpecs []types.FieldSpec `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
}
|
||||
|
||||
func (p *ReplicaCountTransformerPlugin) Config(
|
||||
_ *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.Replica = types.Replica{}
|
||||
p.FieldSpecs = nil
|
||||
return yaml.Unmarshal(c, p)
|
||||
}
|
||||
|
||||
func (p *ReplicaCountTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
found := false
|
||||
for _, fs := range p.FieldSpecs {
|
||||
matcher := p.createMatcher(fs)
|
||||
resList := m.GetMatchingResourcesByAnyId(matcher)
|
||||
if len(resList) > 0 {
|
||||
found = true
|
||||
for _, r := range resList {
|
||||
// There are redundant checks in the filter
|
||||
// that we'll live with until resolution of
|
||||
// https://github.com/kubernetes-sigs/kustomize/issues/2506
|
||||
err := r.ApplyFilter(replicacount.Filter{
|
||||
Replica: p.Replica,
|
||||
FieldSpec: fs,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
gvks := make([]string, len(p.FieldSpecs))
|
||||
for i, replicaSpec := range p.FieldSpecs {
|
||||
gvks[i] = replicaSpec.Gvk.String()
|
||||
}
|
||||
return fmt.Errorf("resource with name %s does not match a config with the following GVK %v",
|
||||
p.Replica.Name, gvks)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Match Replica.Name and FieldSpec
|
||||
func (p *ReplicaCountTransformerPlugin) createMatcher(fs types.FieldSpec) resmap.IdMatcher {
|
||||
return func(r resid.ResId) bool {
|
||||
return r.Name == p.Replica.Name && r.Gvk.IsSelected(&fs.Gvk)
|
||||
}
|
||||
}
|
||||
|
||||
func NewReplicaCountTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &ReplicaCountTransformerPlugin{}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
// Code generated by pluginator on SecretGenerator; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/kv"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
type SecretGeneratorPlugin struct {
|
||||
h *resmap.PluginHelpers
|
||||
types.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
|
||||
types.SecretArgs
|
||||
}
|
||||
|
||||
func (p *SecretGeneratorPlugin) Config(h *resmap.PluginHelpers, config []byte) (err error) {
|
||||
p.SecretArgs = types.SecretArgs{}
|
||||
err = yaml.Unmarshal(config, p)
|
||||
if p.SecretArgs.Name == "" {
|
||||
p.SecretArgs.Name = p.Name
|
||||
}
|
||||
if p.SecretArgs.Namespace == "" {
|
||||
p.SecretArgs.Namespace = p.Namespace
|
||||
}
|
||||
p.h = h
|
||||
return
|
||||
}
|
||||
|
||||
func (p *SecretGeneratorPlugin) Generate() (resmap.ResMap, error) {
|
||||
return p.h.ResmapFactory().FromSecretArgs(
|
||||
kv.NewLoader(p.h.Loader(), p.h.Validator()), p.SecretArgs)
|
||||
}
|
||||
|
||||
func NewSecretGeneratorPlugin() resmap.GeneratorPlugin {
|
||||
return &SecretGeneratorPlugin{}
|
||||
}
|
||||
+236
@@ -0,0 +1,236 @@
|
||||
// Code generated by pluginator on SortOrderTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// Sort the resources using a customizable ordering based of Kind.
|
||||
// Defaults to the ordering of the GVK struct, which puts cluster-wide basic
|
||||
// resources with no dependencies (like Namespace, StorageClass, etc.) first,
|
||||
// and resources with a high number of dependencies
|
||||
// (like ValidatingWebhookConfiguration) last.
|
||||
type SortOrderTransformerPlugin struct {
|
||||
SortOptions *types.SortOptions `json:"sortOptions,omitempty" yaml:"sortOptions,omitempty"`
|
||||
}
|
||||
|
||||
func (p *SortOrderTransformerPlugin) Config(
|
||||
_ *resmap.PluginHelpers, c []byte) error {
|
||||
return errors.WrapPrefixf(yaml.Unmarshal(c, p), "Failed to unmarshal SortOrderTransformer config")
|
||||
}
|
||||
|
||||
func (p *SortOrderTransformerPlugin) applyDefaults() {
|
||||
// Default to FIFO sort, aka no-op.
|
||||
if p.SortOptions == nil {
|
||||
p.SortOptions = &types.SortOptions{
|
||||
Order: types.FIFOSortOrder,
|
||||
}
|
||||
}
|
||||
|
||||
// If legacy sort is selected and no options are given, default to
|
||||
// hardcoded order.
|
||||
if p.SortOptions.Order == types.LegacySortOrder && p.SortOptions.LegacySortOptions == nil {
|
||||
p.SortOptions.LegacySortOptions = &types.LegacySortOptions{
|
||||
OrderFirst: defaultOrderFirst,
|
||||
OrderLast: defaultOrderLast,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *SortOrderTransformerPlugin) validate() error {
|
||||
// Check valid values for SortOrder
|
||||
if p.SortOptions.Order != types.FIFOSortOrder && p.SortOptions.Order != types.LegacySortOrder {
|
||||
return errors.Errorf("the field 'sortOptions.order' must be one of [%s, %s]",
|
||||
types.FIFOSortOrder, types.LegacySortOrder)
|
||||
}
|
||||
|
||||
// Validate that the only options set are the ones corresponding to the
|
||||
// selected sort order.
|
||||
if p.SortOptions.Order == types.FIFOSortOrder &&
|
||||
p.SortOptions.LegacySortOptions != nil {
|
||||
return errors.Errorf("the field 'sortOptions.legacySortOptions' is"+
|
||||
" set but the selected sort order is '%v', not 'legacy'",
|
||||
p.SortOptions.Order)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SortOrderTransformerPlugin) Transform(m resmap.ResMap) (err error) {
|
||||
p.applyDefaults()
|
||||
err = p.validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Sort
|
||||
if p.SortOptions.Order == types.LegacySortOrder {
|
||||
s := newLegacyIDSorter(m.Resources(), p.SortOptions.LegacySortOptions)
|
||||
sort.Sort(s)
|
||||
|
||||
// Clear the map and re-add the resources in the sorted order.
|
||||
m.Clear()
|
||||
for _, r := range s.resources {
|
||||
err := m.Append(r)
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "SortOrderTransformer: Failed to append to resources")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Code for legacy sorting.
|
||||
// Legacy sorting is a "fixed" order sorting maintained for backwards
|
||||
// compatibility.
|
||||
|
||||
// legacyIDSorter sorts resources based on two priority lists:
|
||||
// - orderFirst: Resources that should be placed in the start, in the given order.
|
||||
// - orderLast: Resources that should be placed in the end, in the given order.
|
||||
type legacyIDSorter struct {
|
||||
// resids only stores the metadata of the object. This is an optimization as
|
||||
// it's expensive to compute these again and again during ordering.
|
||||
resids []resid.ResId
|
||||
// Initially, we sorted the metadata (ResId) of each object and then called GetByCurrentId on each to construct the final list.
|
||||
// The problem is that GetByCurrentId is inefficient and does a linear scan in a list every time we do that.
|
||||
// So instead, we sort resources alongside the ResIds.
|
||||
resources []*resource.Resource
|
||||
|
||||
typeOrders map[string]int
|
||||
}
|
||||
|
||||
func newLegacyIDSorter(
|
||||
resources []*resource.Resource,
|
||||
options *types.LegacySortOptions) *legacyIDSorter {
|
||||
// Precalculate a resource ranking based on the priority lists.
|
||||
var typeOrders = func() map[string]int {
|
||||
m := map[string]int{}
|
||||
for i, n := range options.OrderFirst {
|
||||
m[n] = -len(options.OrderFirst) + i
|
||||
}
|
||||
for i, n := range options.OrderLast {
|
||||
m[n] = 1 + i
|
||||
}
|
||||
return m
|
||||
}()
|
||||
|
||||
ret := &legacyIDSorter{typeOrders: typeOrders}
|
||||
for _, res := range resources {
|
||||
ret.resids = append(ret.resids, res.CurId())
|
||||
ret.resources = append(ret.resources, res)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ sort.Interface = legacyIDSorter{}
|
||||
|
||||
func (a legacyIDSorter) Len() int { return len(a.resids) }
|
||||
func (a legacyIDSorter) Swap(i, j int) {
|
||||
a.resids[i], a.resids[j] = a.resids[j], a.resids[i]
|
||||
a.resources[i], a.resources[j] = a.resources[j], a.resources[i]
|
||||
}
|
||||
func (a legacyIDSorter) Less(i, j int) bool {
|
||||
if !a.resids[i].Gvk.Equals(a.resids[j].Gvk) {
|
||||
return gvkLessThan(a.resids[i].Gvk, a.resids[j].Gvk, a.typeOrders)
|
||||
}
|
||||
return legacyResIDSortString(a.resids[i]) < legacyResIDSortString(a.resids[j])
|
||||
}
|
||||
|
||||
func gvkLessThan(gvk1, gvk2 resid.Gvk, typeOrders map[string]int) bool {
|
||||
index1 := typeOrders[gvk1.Kind]
|
||||
index2 := typeOrders[gvk2.Kind]
|
||||
if index1 != index2 {
|
||||
return index1 < index2
|
||||
}
|
||||
if (gvk1.Kind == types.NamespaceKind && gvk2.Kind == types.NamespaceKind) && (gvk1.Group == "" || gvk2.Group == "") {
|
||||
return legacyGVKSortString(gvk1) > legacyGVKSortString(gvk2)
|
||||
}
|
||||
return legacyGVKSortString(gvk1) < legacyGVKSortString(gvk2)
|
||||
}
|
||||
|
||||
// legacyGVKSortString returns a string representation of given GVK used for
|
||||
// stable sorting.
|
||||
func legacyGVKSortString(x resid.Gvk) string {
|
||||
legacyNoGroup := "~G"
|
||||
legacyNoVersion := "~V"
|
||||
legacyNoKind := "~K"
|
||||
legacyFieldSeparator := "_"
|
||||
|
||||
g := x.Group
|
||||
if g == "" {
|
||||
g = legacyNoGroup
|
||||
}
|
||||
v := x.Version
|
||||
if v == "" {
|
||||
v = legacyNoVersion
|
||||
}
|
||||
k := x.Kind
|
||||
if k == "" {
|
||||
k = legacyNoKind
|
||||
}
|
||||
return strings.Join([]string{g, v, k}, legacyFieldSeparator)
|
||||
}
|
||||
|
||||
// legacyResIDSortString returns a string representation of given ResID used for
|
||||
// stable sorting.
|
||||
func legacyResIDSortString(id resid.ResId) string {
|
||||
legacyNoNamespace := "~X"
|
||||
legacyNoName := "~N"
|
||||
legacySeparator := "|"
|
||||
|
||||
ns := id.Namespace
|
||||
if ns == "" {
|
||||
ns = legacyNoNamespace
|
||||
}
|
||||
nm := id.Name
|
||||
if nm == "" {
|
||||
nm = legacyNoName
|
||||
}
|
||||
return strings.Join(
|
||||
[]string{id.Gvk.String(), ns, nm}, legacySeparator)
|
||||
}
|
||||
|
||||
// DO NOT CHANGE!
|
||||
// Final legacy ordering provided as a default by kustomize.
|
||||
// Originally an attempt to apply resources in the correct order, an effort
|
||||
// which later proved impossible as not all types are known beforehand.
|
||||
// See: https://github.com/kubernetes-sigs/kustomize/issues/3913
|
||||
var defaultOrderFirst = []string{ //nolint:gochecknoglobals
|
||||
"Namespace",
|
||||
"ResourceQuota",
|
||||
"StorageClass",
|
||||
"CustomResourceDefinition",
|
||||
"ServiceAccount",
|
||||
"PodSecurityPolicy",
|
||||
"Role",
|
||||
"ClusterRole",
|
||||
"RoleBinding",
|
||||
"ClusterRoleBinding",
|
||||
"ConfigMap",
|
||||
"Secret",
|
||||
"Endpoints",
|
||||
"Service",
|
||||
"LimitRange",
|
||||
"PriorityClass",
|
||||
"PersistentVolume",
|
||||
"PersistentVolumeClaim",
|
||||
"Deployment",
|
||||
"StatefulSet",
|
||||
"CronJob",
|
||||
"PodDisruptionBudget",
|
||||
}
|
||||
var defaultOrderLast = []string{ //nolint:gochecknoglobals
|
||||
"MutatingWebhookConfiguration",
|
||||
"ValidatingWebhookConfiguration",
|
||||
}
|
||||
|
||||
func NewSortOrderTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &SortOrderTransformerPlugin{}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
// Code generated by pluginator on SuffixTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/suffix"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// Add the given suffix to the field
|
||||
type SuffixTransformerPlugin struct {
|
||||
Suffix string `json:"suffix,omitempty" yaml:"suffix,omitempty"`
|
||||
FieldSpecs types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
}
|
||||
|
||||
// TODO: Make this gvk skip list part of the config.
|
||||
var suffixFieldSpecsToSkip = types.FsSlice{
|
||||
{Gvk: resid.Gvk{Kind: "CustomResourceDefinition"}},
|
||||
{Gvk: resid.Gvk{Group: "apiregistration.k8s.io", Kind: "APIService"}},
|
||||
{Gvk: resid.Gvk{Kind: "Namespace"}},
|
||||
}
|
||||
|
||||
func (p *SuffixTransformerPlugin) Config(
|
||||
_ *resmap.PluginHelpers, c []byte) (err error) {
|
||||
p.Suffix = ""
|
||||
p.FieldSpecs = nil
|
||||
err = yaml.Unmarshal(c, p)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if p.FieldSpecs == nil {
|
||||
return errors.New("fieldSpecs is not expected to be nil")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (p *SuffixTransformerPlugin) Transform(m resmap.ResMap) error {
|
||||
// Even if the Suffix is empty we want to proceed with the
|
||||
// transformation. This allows to add contextual information
|
||||
// to the resources (AddNameSuffix).
|
||||
for _, r := range m.Resources() {
|
||||
// TODO: move this test into the filter (i.e. make a better filter)
|
||||
if p.shouldSkip(r.OrgId()) {
|
||||
continue
|
||||
}
|
||||
id := r.OrgId()
|
||||
// current default configuration contains
|
||||
// only one entry: "metadata/name" with no GVK
|
||||
for _, fs := range p.FieldSpecs {
|
||||
// TODO: this is redundant to filter (but needed for now)
|
||||
if !id.IsSelected(&fs.Gvk) {
|
||||
continue
|
||||
}
|
||||
// TODO: move this test into the filter.
|
||||
if fs.Path == "metadata/name" {
|
||||
// "metadata/name" is the only field.
|
||||
// this will add a suffix to the resource
|
||||
// even if it is empty
|
||||
|
||||
r.AddNameSuffix(p.Suffix)
|
||||
if p.Suffix != "" {
|
||||
// TODO: There are multiple transformers that can change a resource's name, and each makes a call to
|
||||
// StorePreviousID(). We should make it so that we only call StorePreviousID once per kustomization layer
|
||||
// to avoid storing intermediate names between transformations, to prevent intermediate name conflicts.
|
||||
r.StorePreviousId()
|
||||
}
|
||||
}
|
||||
if err := r.ApplyFilter(suffix.Filter{
|
||||
Suffix: p.Suffix,
|
||||
FieldSpec: fs,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *SuffixTransformerPlugin) shouldSkip(id resid.ResId) bool {
|
||||
for _, path := range suffixFieldSpecsToSkip {
|
||||
if id.IsSelected(&path.Gvk) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func NewSuffixTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &SuffixTransformerPlugin{}
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
// Code generated by pluginator on ValueAddTransformer; DO NOT EDIT.
|
||||
package builtins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/filters/namespace"
|
||||
"sigs.k8s.io/kustomize/api/filters/valueadd"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// An 'Add' transformer inspired by the IETF RFC 6902 JSON spec Add operation.
|
||||
type ValueAddTransformerPlugin struct {
|
||||
// Value is the value to add.
|
||||
// Defaults to base name of encompassing kustomization root.
|
||||
Value string `json:"value,omitempty" yaml:"value,omitempty"`
|
||||
|
||||
// Targets is a slice of targets that should have the value added.
|
||||
Targets []Target `json:"targets,omitempty" yaml:"targets,omitempty"`
|
||||
|
||||
// TargetFilePath is a file path. If specified, the file will be parsed into
|
||||
// a slice of Target, and appended to anything that was specified in the
|
||||
// Targets field. This is just a means to share common target specifications.
|
||||
TargetFilePath string `json:"targetFilePath,omitempty" yaml:"targetFilePath,omitempty"`
|
||||
}
|
||||
|
||||
// Target describes where to put the value.
|
||||
type Target struct {
|
||||
// Selector selects the resources to modify.
|
||||
Selector *types.Selector `json:"selector,omitempty" yaml:"selector,omitempty"`
|
||||
|
||||
// NotSelector selects the resources to exclude
|
||||
// from those included by overly broad selectors.
|
||||
// TODO: implement this?
|
||||
// NotSelector *types.Selector `json:"notSelector,omitempty" yaml:"notSelector,omitempty"`
|
||||
|
||||
// FieldPath is a JSON-style path to the field intended to hold the value.
|
||||
FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"`
|
||||
|
||||
// FilePathPosition is passed to the filter directly. Look there for doc.
|
||||
FilePathPosition int `json:"filePathPosition,omitempty" yaml:"filePathPosition,omitempty"`
|
||||
}
|
||||
|
||||
func (p *ValueAddTransformerPlugin) Config(h *resmap.PluginHelpers, c []byte) error {
|
||||
err := yaml.Unmarshal(c, p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Value = strings.TrimSpace(p.Value)
|
||||
if p.Value == "" {
|
||||
p.Value = filepath.Base(h.Loader().Root())
|
||||
}
|
||||
if p.TargetFilePath != "" {
|
||||
bytes, err := h.Loader().Load(p.TargetFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var targets struct {
|
||||
Targets []Target `json:"targets,omitempty" yaml:"targets,omitempty"`
|
||||
}
|
||||
err = yaml.Unmarshal(bytes, &targets)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p.Targets = append(p.Targets, targets.Targets...)
|
||||
}
|
||||
if len(p.Targets) == 0 {
|
||||
return fmt.Errorf("must specify at least one target")
|
||||
}
|
||||
for _, target := range p.Targets {
|
||||
if err = validateSelector(target.Selector); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: call validateSelector(target.NotSelector) if field added.
|
||||
if err = validateJsonFieldPath(target.FieldPath); err != nil {
|
||||
return err
|
||||
}
|
||||
if target.FilePathPosition < 0 {
|
||||
return fmt.Errorf(
|
||||
"value of FilePathPosition (%d) cannot be negative",
|
||||
target.FilePathPosition)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: implement
|
||||
func validateSelector(_ *types.Selector) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: Enforce RFC 6902?
|
||||
func validateJsonFieldPath(p string) error {
|
||||
if len(p) == 0 {
|
||||
return fmt.Errorf("fieldPath cannot be empty")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ValueAddTransformerPlugin) Transform(m resmap.ResMap) (err error) {
|
||||
for _, t := range p.Targets {
|
||||
var resources []*resource.Resource
|
||||
if t.Selector == nil {
|
||||
resources = m.Resources()
|
||||
} else {
|
||||
resources, err = m.Select(*t.Selector)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// TODO: consider t.NotSelector if implemented
|
||||
for _, res := range resources {
|
||||
if t.FieldPath == types.MetadataNamespacePath {
|
||||
err = res.ApplyFilter(namespace.Filter{
|
||||
Namespace: p.Value,
|
||||
})
|
||||
} else {
|
||||
err = res.ApplyFilter(valueadd.Filter{
|
||||
Value: p.Value,
|
||||
FieldPath: t.FieldPath,
|
||||
FilePathPosition: t.FilePathPosition,
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewValueAddTransformerPlugin() resmap.TransformerPlugin {
|
||||
return &ValueAddTransformerPlugin{}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package builtins holds code generated from the builtin plugins.
|
||||
// The "builtin" plugins are written as normal plugins and can
|
||||
// be used as such, but they are also used to generate the code
|
||||
// in this package so they can be statically linked to client code.
|
||||
package builtins
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package generators
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// MakeConfigMap makes a configmap.
|
||||
//
|
||||
// ConfigMap: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#configmap-v1-core
|
||||
//
|
||||
// ConfigMaps and Secrets are similar.
|
||||
//
|
||||
// Both objects have a `data` field, which contains a map from keys to
|
||||
// values that must be UTF-8 valid strings. Such data might be simple text,
|
||||
// or whoever made the data may have done so by performing a base64 encoding
|
||||
// on binary data. Regardless, k8s has no means to know this, so it treats
|
||||
// the data field as a string.
|
||||
//
|
||||
// The ConfigMap has an additional field `binaryData`, also a map, but its
|
||||
// values are _intended_ to be interpreted as a base64 encoding of []byte,
|
||||
// by whatever makes use of the ConfigMap.
|
||||
//
|
||||
// In a ConfigMap, any key used in `data` cannot also be used in `binaryData`
|
||||
// and vice-versa. A key must be unique across both maps.
|
||||
func MakeConfigMap(
|
||||
ldr ifc.KvLoader, args *types.ConfigMapArgs) (rn *yaml.RNode, err error) {
|
||||
rn, err = makeBaseNode("ConfigMap", args.Name, args.Namespace)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m, err := makeValidatedDataMap(ldr, args.Name, args.KvPairSources)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = rn.LoadMapIntoConfigMapData(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = copyLabelsAndAnnotations(rn, args.Options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = setImmutable(rn, args.Options)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rn, nil
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package generators
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// MakeSecret makes a kubernetes Secret.
|
||||
//
|
||||
// Secret: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#secret-v1-core
|
||||
//
|
||||
// ConfigMaps and Secrets are similar.
|
||||
//
|
||||
// Like a ConfigMap, a Secret has a `data` field, but unlike a ConfigMap it has
|
||||
// no `binaryData` field.
|
||||
//
|
||||
// All of a Secret's data is assumed to be opaque in nature, and assumed to be
|
||||
// base64 encoded from its original representation, regardless of whether the
|
||||
// original data was UTF-8 text or binary.
|
||||
//
|
||||
// This encoding provides no secrecy. It's just a neutral, common means to
|
||||
// represent opaque text and binary data. Beneath the base64 encoding
|
||||
// is presumably further encoding under control of the Secret's consumer.
|
||||
//
|
||||
// A Secret has string field `type` which holds an identifier, used by the
|
||||
// client, to choose the algorithm to interpret the `data` field. Kubernetes
|
||||
// cannot make use of this data; it's up to a controller or some pod's service
|
||||
// to interpret the value, using `type` as a clue as to how to do this.
|
||||
func MakeSecret(
|
||||
ldr ifc.KvLoader, args *types.SecretArgs) (rn *yaml.RNode, err error) {
|
||||
rn, err = makeBaseNode("Secret", args.Name, args.Namespace)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t := "Opaque"
|
||||
if args.Type != "" {
|
||||
t = args.Type
|
||||
}
|
||||
if _, err := rn.Pipe(
|
||||
yaml.FieldSetter{
|
||||
Name: "type",
|
||||
Value: yaml.NewStringRNode(t)}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m, err := makeValidatedDataMap(ldr, args.Name, args.KvPairSources)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = rn.LoadMapIntoSecretData(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
copyLabelsAndAnnotations(rn, args.Options)
|
||||
setImmutable(rn, args.Options)
|
||||
return rn, nil
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package generators
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/go-errors/errors"
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
func makeBaseNode(kind, name, namespace string) (*yaml.RNode, error) {
|
||||
rn, err := yaml.Parse(fmt.Sprintf(`
|
||||
apiVersion: v1
|
||||
kind: %s
|
||||
`, kind))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if name == "" {
|
||||
return nil, errors.Errorf("a configmap must have a name")
|
||||
}
|
||||
if _, err := rn.Pipe(yaml.SetK8sName(name)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if namespace != "" {
|
||||
if _, err := rn.Pipe(yaml.SetK8sNamespace(namespace)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return rn, nil
|
||||
}
|
||||
|
||||
func makeValidatedDataMap(
|
||||
ldr ifc.KvLoader, name string, sources types.KvPairSources) (map[string]string, error) {
|
||||
pairs, err := ldr.Load(sources)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefix(err, "loading KV pairs", 0)
|
||||
}
|
||||
knownKeys := make(map[string]string)
|
||||
for _, p := range pairs {
|
||||
// legal key: alphanumeric characters, '-', '_' or '.'
|
||||
if err := ldr.Validator().ErrIfInvalidKey(p.Key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, ok := knownKeys[p.Key]; ok {
|
||||
return nil, errors.Errorf(
|
||||
"configmap %s illegally repeats the key `%s`", name, p.Key)
|
||||
}
|
||||
knownKeys[p.Key] = p.Value
|
||||
}
|
||||
return knownKeys, nil
|
||||
}
|
||||
|
||||
// copyLabelsAndAnnotations copies labels and annotations from
|
||||
// GeneratorOptions into the given object.
|
||||
func copyLabelsAndAnnotations(
|
||||
rn *yaml.RNode, opts *types.GeneratorOptions) error {
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
for _, k := range yaml.SortedMapKeys(opts.Labels) {
|
||||
v := opts.Labels[k]
|
||||
if _, err := rn.Pipe(yaml.SetLabel(k, v)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, k := range yaml.SortedMapKeys(opts.Annotations) {
|
||||
v := opts.Annotations[k]
|
||||
if _, err := rn.Pipe(yaml.SetAnnotation(k, v)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func setImmutable(
|
||||
rn *yaml.RNode, opts *types.GeneratorOptions) error {
|
||||
if opts == nil {
|
||||
return nil
|
||||
}
|
||||
if opts.Immutable {
|
||||
n := &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Value: "true",
|
||||
Tag: yaml.NodeTagBool,
|
||||
}
|
||||
if _, err := rn.Pipe(yaml.FieldSetter{Name: "immutable", Value: yaml.NewRNode(n)}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseFileSource parses the source given.
|
||||
//
|
||||
// Acceptable formats include:
|
||||
// 1. source-path: the basename will become the key name
|
||||
// 2. source-name=source-path: the source-name will become the key name and
|
||||
// source-path is the path to the key file.
|
||||
//
|
||||
// Key names cannot include '='.
|
||||
func ParseFileSource(source string) (keyName, filePath string, err error) {
|
||||
numSeparators := strings.Count(source, "=")
|
||||
switch {
|
||||
case numSeparators == 0:
|
||||
return path.Base(source), source, nil
|
||||
case numSeparators == 1 && strings.HasPrefix(source, "="):
|
||||
return "", "", errors.Errorf("missing key name for file path %q in source %q", strings.TrimPrefix(source, "="), source)
|
||||
case numSeparators == 1 && strings.HasSuffix(source, "="):
|
||||
return "", "", errors.Errorf("missing file path for key name %q in source %q", strings.TrimSuffix(source, "="), source)
|
||||
case numSeparators > 1:
|
||||
return "", "", errors.Errorf("source %q key name or file path contains '='", source)
|
||||
default:
|
||||
components := strings.Split(source, "=")
|
||||
return components[0], components[1], nil
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package git
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
// Cloner is a function that can clone a git repo.
|
||||
type Cloner func(repoSpec *RepoSpec) error
|
||||
|
||||
// ClonerUsingGitExec uses a local git install, as opposed
|
||||
// to say, some remote API, to obtain a local clone of
|
||||
// a remote repo.
|
||||
func ClonerUsingGitExec(repoSpec *RepoSpec) error {
|
||||
r, err := newCmdRunner(repoSpec.Timeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repoSpec.Dir = r.dir
|
||||
if err = r.run("init"); err != nil {
|
||||
return err
|
||||
}
|
||||
// git relative submodule need origin, see https://github.com/kubernetes-sigs/kustomize/issues/5131
|
||||
if err = r.run("remote", "add", "origin", repoSpec.CloneSpec()); err != nil {
|
||||
return err
|
||||
}
|
||||
ref := "HEAD"
|
||||
if repoSpec.Ref != "" {
|
||||
ref = repoSpec.Ref
|
||||
}
|
||||
// we use repoSpec.CloneSpec() instead of origin because on error,
|
||||
// the prior prints the actual repo url for the user.
|
||||
if err = r.run("fetch", "--depth=1", repoSpec.CloneSpec(), ref); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = r.run("checkout", "FETCH_HEAD"); err != nil {
|
||||
return err
|
||||
}
|
||||
if repoSpec.Submodules {
|
||||
return r.run("submodule", "update", "--init", "--recursive")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DoNothingCloner returns a cloner that only sets
|
||||
// cloneDir field in the repoSpec. It's assumed that
|
||||
// the cloneDir is associated with some fake filesystem
|
||||
// used in a test.
|
||||
func DoNothingCloner(dir filesys.ConfirmedDir) Cloner {
|
||||
return func(rs *RepoSpec) error {
|
||||
rs.Dir = dir
|
||||
return nil
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package git
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/internal/utils"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
// gitRunner runs the external git binary.
|
||||
type gitRunner struct {
|
||||
gitProgram string
|
||||
duration time.Duration
|
||||
dir filesys.ConfirmedDir
|
||||
}
|
||||
|
||||
// newCmdRunner returns a gitRunner if it can find the binary.
|
||||
// It also creats a temp directory for cloning repos.
|
||||
func newCmdRunner(timeout time.Duration) (*gitRunner, error) {
|
||||
gitProgram, err := exec.LookPath("git")
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "no 'git' program on path")
|
||||
}
|
||||
dir, err := filesys.NewTmpConfirmedDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &gitRunner{
|
||||
gitProgram: gitProgram,
|
||||
duration: timeout,
|
||||
dir: dir,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// run a command with a timeout.
|
||||
func (r gitRunner) run(args ...string) error {
|
||||
//nolint: gosec
|
||||
cmd := exec.Command(r.gitProgram, args...)
|
||||
cmd.Dir = r.dir.String()
|
||||
return utils.TimedCall(
|
||||
cmd.String(),
|
||||
r.duration,
|
||||
func() error {
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return errors.WrapPrefixf(err, "failed to run '%s': %s", cmd.String(), string(out))
|
||||
}
|
||||
return err
|
||||
})
|
||||
}
|
||||
+387
@@ -0,0 +1,387 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
// Used as a temporary non-empty occupant of the cloneDir
|
||||
// field, as something distinguishable from the empty string
|
||||
// in various outputs (especially tests). Not using an
|
||||
// actual directory name here, as that's a temporary directory
|
||||
// with a unique name that isn't created until clone time.
|
||||
const notCloned = filesys.ConfirmedDir("/notCloned")
|
||||
|
||||
// RepoSpec specifies a git repository and a branch and path therein.
|
||||
type RepoSpec struct {
|
||||
// Raw, original spec, used to look for cycles.
|
||||
// TODO(monopole): Drop raw, use processed fields instead.
|
||||
raw string
|
||||
|
||||
// Host, e.g. https://github.com/
|
||||
Host string
|
||||
|
||||
// RepoPath name (Path to repository),
|
||||
// e.g. kubernetes-sigs/kustomize
|
||||
RepoPath string
|
||||
|
||||
// Dir is where the repository is cloned to.
|
||||
Dir filesys.ConfirmedDir
|
||||
|
||||
// Relative path in the repository, and in the cloneDir,
|
||||
// to a Kustomization.
|
||||
KustRootPath string
|
||||
|
||||
// Branch or tag reference.
|
||||
Ref string
|
||||
|
||||
// Submodules indicates whether or not to clone git submodules.
|
||||
Submodules bool
|
||||
|
||||
// Timeout is the maximum duration allowed for execing git commands.
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
// CloneSpec returns a string suitable for "git clone {spec}".
|
||||
func (x *RepoSpec) CloneSpec() string {
|
||||
return x.Host + x.RepoPath
|
||||
}
|
||||
|
||||
func (x *RepoSpec) CloneDir() filesys.ConfirmedDir {
|
||||
return x.Dir
|
||||
}
|
||||
|
||||
func (x *RepoSpec) Raw() string {
|
||||
return x.raw
|
||||
}
|
||||
|
||||
func (x *RepoSpec) AbsPath() string {
|
||||
return x.Dir.Join(x.KustRootPath)
|
||||
}
|
||||
|
||||
func (x *RepoSpec) Cleaner(fSys filesys.FileSystem) func() error {
|
||||
return func() error { return fSys.RemoveAll(x.Dir.String()) }
|
||||
}
|
||||
|
||||
const (
|
||||
refQuery = "?ref="
|
||||
gitSuffix = ".git"
|
||||
gitRootDelimiter = "_git/"
|
||||
pathSeparator = "/" // do not use filepath.Separator, as this is a URL
|
||||
)
|
||||
|
||||
// NewRepoSpecFromURL parses git-like urls.
|
||||
// From strings like git@github.com:someOrg/someRepo.git or
|
||||
// https://github.com/someOrg/someRepo?ref=someHash, extract
|
||||
// the different parts of URL, set into a RepoSpec object and return RepoSpec object.
|
||||
// It MUST return an error if the input is not a git-like URL, as this is used by some code paths
|
||||
// to distinguish between local and remote paths.
|
||||
//
|
||||
// In particular, NewRepoSpecFromURL separates the URL used to clone the repo from the
|
||||
// elements Kustomize uses for other purposes (e.g. query params that turn into args, and
|
||||
// the path to the kustomization root within the repo).
|
||||
func NewRepoSpecFromURL(n string) (*RepoSpec, error) {
|
||||
repoSpec := &RepoSpec{raw: n, Dir: notCloned, Timeout: defaultTimeout, Submodules: defaultSubmodules}
|
||||
if filepath.IsAbs(n) {
|
||||
return nil, fmt.Errorf("uri looks like abs path: %s", n)
|
||||
}
|
||||
|
||||
// Parse the query first. This is safe because according to rfc3986 "?" is only allowed in the
|
||||
// query and is not recognized %-encoded.
|
||||
// Note that parseQuery returns default values for empty parameters.
|
||||
n, query, _ := strings.Cut(n, "?")
|
||||
repoSpec.Ref, repoSpec.Timeout, repoSpec.Submodules = parseQuery(query)
|
||||
|
||||
var err error
|
||||
|
||||
// Parse the host (e.g. scheme, username, domain) segment.
|
||||
repoSpec.Host, n, err = extractHost(n)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// In some cases, we're given a path to a git repo + a path to the kustomization root within
|
||||
// that repo. We need to split them so that we can ultimately give the repo only to the cloner.
|
||||
repoSpec.RepoPath, repoSpec.KustRootPath, err = parsePathParts(n, defaultRepoPathLength(repoSpec.Host))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return repoSpec, nil
|
||||
}
|
||||
|
||||
const allSegments = -999999
|
||||
const orgRepoSegments = 2
|
||||
|
||||
func defaultRepoPathLength(host string) int {
|
||||
if strings.HasPrefix(host, fileScheme) {
|
||||
return allSegments
|
||||
}
|
||||
return orgRepoSegments
|
||||
}
|
||||
|
||||
// parsePathParts splits the repo path that will ultimately be passed to git to clone the
|
||||
// repo from the kustomization root path, which Kustomize will execute the build in after the repo
|
||||
// is cloned.
|
||||
//
|
||||
// We first try to do this based on explicit markers in the URL (e.g. _git, .git or //).
|
||||
// If none are present, we try to apply a historical default repo path length that is derived from
|
||||
// Github URLs. If there aren't enough segments, we have historically considered the URL invalid.
|
||||
func parsePathParts(n string, defaultSegmentLength int) (string, string, error) {
|
||||
repoPath, kustRootPath, success := tryExplicitMarkerSplit(n)
|
||||
if !success {
|
||||
repoPath, kustRootPath, success = tryDefaultLengthSplit(n, defaultSegmentLength)
|
||||
}
|
||||
|
||||
// Validate the result
|
||||
if !success || len(repoPath) == 0 {
|
||||
return "", "", fmt.Errorf("failed to parse repo path segment")
|
||||
}
|
||||
if kustRootPathExitsRepo(kustRootPath) {
|
||||
return "", "", fmt.Errorf("url path exits repo: %s", n)
|
||||
}
|
||||
|
||||
return repoPath, strings.TrimPrefix(kustRootPath, pathSeparator), nil
|
||||
}
|
||||
|
||||
func tryExplicitMarkerSplit(n string) (string, string, bool) {
|
||||
// Look for the _git delimiter, which by convention is expected to be ONE directory above the repo root.
|
||||
// If found, split on the NEXT path element, which is the repo root.
|
||||
// Example: https://username@dev.azure.com/org/project/_git/repo/path/to/kustomization/root
|
||||
if gitRootIdx := strings.Index(n, gitRootDelimiter); gitRootIdx >= 0 {
|
||||
gitRootPath := n[:gitRootIdx+len(gitRootDelimiter)]
|
||||
subpathSegments := strings.Split(n[gitRootIdx+len(gitRootDelimiter):], pathSeparator)
|
||||
return gitRootPath + subpathSegments[0], strings.Join(subpathSegments[1:], pathSeparator), true
|
||||
|
||||
// Look for a double-slash in the path, which if present separates the repo root from the kust path.
|
||||
// It is a convention, not a real path element, so do not preserve it in the returned value.
|
||||
// Example: https://github.com/org/repo//path/to/kustomozation/root
|
||||
} else if repoRootIdx := strings.Index(n, "//"); repoRootIdx >= 0 {
|
||||
return n[:repoRootIdx], n[repoRootIdx+2:], true
|
||||
|
||||
// Look for .git in the path, which if present is part of the directory name of the git repo.
|
||||
// This means we want to grab everything up to and including that suffix
|
||||
// Example: https://github.com/org/repo.git/path/to/kustomozation/root
|
||||
} else if gitSuffixIdx := strings.Index(n, gitSuffix); gitSuffixIdx >= 0 {
|
||||
upToGitSuffix := n[:gitSuffixIdx+len(gitSuffix)]
|
||||
afterGitSuffix := n[gitSuffixIdx+len(gitSuffix):]
|
||||
return upToGitSuffix, afterGitSuffix, true
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
func tryDefaultLengthSplit(n string, defaultSegmentLength int) (string, string, bool) {
|
||||
// If the default is to take all segments, do so.
|
||||
if defaultSegmentLength == allSegments {
|
||||
return n, "", true
|
||||
|
||||
// If the default is N segments, make sure we have at least that many and take them if so.
|
||||
// If we have less than N, we have historically considered the URL invalid.
|
||||
} else if segments := strings.Split(n, pathSeparator); len(segments) >= defaultSegmentLength {
|
||||
firstNSegments := strings.Join(segments[:defaultSegmentLength], pathSeparator)
|
||||
rest := strings.Join(segments[defaultSegmentLength:], pathSeparator)
|
||||
return firstNSegments, rest, true
|
||||
}
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
func kustRootPathExitsRepo(kustRootPath string) bool {
|
||||
cleanedPath := filepath.Clean(strings.TrimPrefix(kustRootPath, string(filepath.Separator)))
|
||||
pathElements := strings.Split(cleanedPath, string(filepath.Separator))
|
||||
return len(pathElements) > 0 &&
|
||||
pathElements[0] == filesys.ParentDir
|
||||
}
|
||||
|
||||
// Clone git submodules by default.
|
||||
const defaultSubmodules = true
|
||||
|
||||
// Arbitrary, but non-infinite, timeout for running commands.
|
||||
const defaultTimeout = 27 * time.Second
|
||||
|
||||
func parseQuery(query string) (string, time.Duration, bool) {
|
||||
values, err := url.ParseQuery(query)
|
||||
// in event of parse failure, return defaults
|
||||
if err != nil {
|
||||
return "", defaultTimeout, defaultSubmodules
|
||||
}
|
||||
|
||||
// ref is the desired git ref to target. Can be specified by in a git URL
|
||||
// with ?ref=<string> or ?version=<string>, although ref takes precedence.
|
||||
ref := values.Get("version")
|
||||
if queryValue := values.Get("ref"); queryValue != "" {
|
||||
ref = queryValue
|
||||
}
|
||||
|
||||
// depth is the desired git exec timeout. Can be specified by in a git URL
|
||||
// with ?timeout=<duration>.
|
||||
duration := defaultTimeout
|
||||
if queryValue := values.Get("timeout"); queryValue != "" {
|
||||
// Attempt to first parse as a number of integer seconds (like "61"),
|
||||
// and then attempt to parse as a suffixed duration (like "61s").
|
||||
if intValue, err := strconv.Atoi(queryValue); err == nil && intValue > 0 {
|
||||
duration = time.Duration(intValue) * time.Second
|
||||
} else if durationValue, err := time.ParseDuration(queryValue); err == nil && durationValue > 0 {
|
||||
duration = durationValue
|
||||
}
|
||||
}
|
||||
|
||||
// submodules indicates if git submodule cloning is desired. Can be
|
||||
// specified by in a git URL with ?submodules=<bool>.
|
||||
submodules := defaultSubmodules
|
||||
if queryValue := values.Get("submodules"); queryValue != "" {
|
||||
if boolValue, err := strconv.ParseBool(queryValue); err == nil {
|
||||
submodules = boolValue
|
||||
}
|
||||
}
|
||||
|
||||
return ref, duration, submodules
|
||||
}
|
||||
|
||||
func extractHost(n string) (string, string, error) {
|
||||
n = ignoreForcedGitProtocol(n)
|
||||
scheme, n := extractScheme(n)
|
||||
username, n := extractUsername(n)
|
||||
stdGithub := isStandardGithubHost(n)
|
||||
acceptSCP := acceptSCPStyle(scheme, username, stdGithub)
|
||||
|
||||
// Validate the username and scheme before attempting host/path parsing, because if the parsing
|
||||
// so far has not succeeded, we will not be able to extract the host and path correctly.
|
||||
if err := validateScheme(scheme, acceptSCP); err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// Now that we have extracted a valid scheme+username, we can parse host itself.
|
||||
|
||||
// The file protocol specifies an absolute path to a local git repo.
|
||||
// Everything after the scheme (including any 'username' we found) is actually part of that path.
|
||||
if scheme == fileScheme {
|
||||
return scheme, username + n, nil
|
||||
}
|
||||
var host, rest = n, ""
|
||||
if sepIndex := findPathSeparator(n, acceptSCP); sepIndex >= 0 {
|
||||
host, rest = n[:sepIndex+1], n[sepIndex+1:]
|
||||
}
|
||||
|
||||
// Github URLs are strictly normalized in a way that may discard scheme and username components.
|
||||
if stdGithub {
|
||||
scheme, username, host = normalizeGithubHostParts(scheme, username)
|
||||
}
|
||||
|
||||
// Host is required, so do not concat the scheme and username if we didn't find one.
|
||||
if host == "" {
|
||||
return "", "", errors.Errorf("failed to parse host segment")
|
||||
}
|
||||
return scheme + username + host, rest, nil
|
||||
}
|
||||
|
||||
// ignoreForcedGitProtocol strips the "git::" prefix from URLs.
|
||||
// We used to use go-getter to handle our urls: https://github.com/hashicorp/go-getter.
|
||||
// The git:: prefix signaled go-getter to use the git protocol to fetch the url's contents.
|
||||
// We silently strip this prefix to allow these go-getter-style urls to continue to work,
|
||||
// although the git protocol (which is insecure and unsupported on many platforms, including Github)
|
||||
// will not actually be used as intended.
|
||||
func ignoreForcedGitProtocol(n string) string {
|
||||
n, found := trimPrefixIgnoreCase(n, "git::")
|
||||
if found {
|
||||
log.Println("Warning: Forcing the git protocol using the 'git::' URL prefix is not supported. " +
|
||||
"Kustomize currently strips this invalid prefix, but will stop doing so in a future release. " +
|
||||
"Please remove the 'git::' prefix from your configuration.")
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// acceptSCPStyle returns true if the scheme and username indicate potential use of an SCP-style URL.
|
||||
// With this style, the scheme is not explicit and the path is delimited by a colon.
|
||||
// Strictly speaking the username is optional in SCP-like syntax, but Kustomize has always
|
||||
// required it for non-Github URLs.
|
||||
// Example: user@host.xz:path/to/repo.git/
|
||||
func acceptSCPStyle(scheme, username string, isGithubURL bool) bool {
|
||||
return scheme == "" && (username != "" || isGithubURL)
|
||||
}
|
||||
|
||||
func validateScheme(scheme string, acceptSCPStyle bool) error {
|
||||
// see https://git-scm.com/docs/git-fetch#_git_urls for info relevant to these validations
|
||||
switch scheme {
|
||||
case "":
|
||||
// Empty scheme is only ok if it's a Github URL or if it looks like SCP-style syntax
|
||||
if !acceptSCPStyle {
|
||||
return fmt.Errorf("failed to parse scheme")
|
||||
}
|
||||
case sshScheme, fileScheme, httpsScheme, httpScheme:
|
||||
// These are all supported schemes
|
||||
default:
|
||||
// At time of writing, we should never end up here because we do not parse out
|
||||
// unsupported schemes to begin with.
|
||||
return fmt.Errorf("unsupported scheme %q", scheme)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const fileScheme = "file://"
|
||||
const httpScheme = "http://"
|
||||
const httpsScheme = "https://"
|
||||
const sshScheme = "ssh://"
|
||||
|
||||
func extractScheme(s string) (string, string) {
|
||||
for _, prefix := range []string{sshScheme, httpsScheme, httpScheme, fileScheme} {
|
||||
if rest, found := trimPrefixIgnoreCase(s, prefix); found {
|
||||
return prefix, rest
|
||||
}
|
||||
}
|
||||
return "", s
|
||||
}
|
||||
|
||||
func extractUsername(s string) (string, string) {
|
||||
var userRegexp = regexp.MustCompile(`^([a-zA-Z][a-zA-Z0-9-]*)@`)
|
||||
if m := userRegexp.FindStringSubmatch(s); m != nil {
|
||||
username := m[1] + "@"
|
||||
return username, s[len(username):]
|
||||
}
|
||||
return "", s
|
||||
}
|
||||
|
||||
func isStandardGithubHost(s string) bool {
|
||||
lowerCased := strings.ToLower(s)
|
||||
return strings.HasPrefix(lowerCased, "github.com/") ||
|
||||
strings.HasPrefix(lowerCased, "github.com:")
|
||||
}
|
||||
|
||||
// trimPrefixIgnoreCase returns the rest of s and true if prefix, ignoring case, prefixes s.
|
||||
// Otherwise, trimPrefixIgnoreCase returns s and false.
|
||||
func trimPrefixIgnoreCase(s, prefix string) (string, bool) {
|
||||
if len(prefix) <= len(s) && strings.ToLower(s[:len(prefix)]) == prefix {
|
||||
return s[len(prefix):], true
|
||||
}
|
||||
return s, false
|
||||
}
|
||||
|
||||
func findPathSeparator(hostPath string, acceptSCP bool) int {
|
||||
sepIndex := strings.Index(hostPath, pathSeparator)
|
||||
if acceptSCP {
|
||||
colonIndex := strings.Index(hostPath, ":")
|
||||
// The colon acts as a delimiter in scp-style ssh URLs only if not prefixed by '/'.
|
||||
if sepIndex == -1 || (colonIndex > 0 && colonIndex < sepIndex) {
|
||||
sepIndex = colonIndex
|
||||
}
|
||||
}
|
||||
return sepIndex
|
||||
}
|
||||
|
||||
func normalizeGithubHostParts(scheme, username string) (string, string, string) {
|
||||
if strings.HasPrefix(scheme, sshScheme) || username != "" {
|
||||
return "", username, "github.com:"
|
||||
}
|
||||
return httpsScheme, "", "github.com/"
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package image
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsImageMatched returns true if the value of t is identical to the
|
||||
// image name in the full image name and tag as given by s.
|
||||
func IsImageMatched(s, t string) bool {
|
||||
// Tag values are limited to [a-zA-Z0-9_.{}-].
|
||||
// Some tools like Bazel rules_k8s allow tag patterns with {} characters.
|
||||
// More info: https://github.com/bazelbuild/rules_k8s/pull/423
|
||||
pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.{}-]*)?(@sha256:[a-zA-Z0-9_.{}-]*)?$")
|
||||
return pattern.MatchString(s)
|
||||
}
|
||||
|
||||
// Split separates and returns the name and tag parts
|
||||
// from the image string using either colon `:` or at `@` separators.
|
||||
// image reference pattern: [[host[:port]/]component/]component[:tag][@digest]
|
||||
func Split(imageName string) (name string, tag string, digest string) {
|
||||
// check if image name contains a domain
|
||||
// if domain is present, ignore domain and check for `:`
|
||||
searchName := imageName
|
||||
slashIndex := strings.Index(imageName, "/")
|
||||
if slashIndex > 0 {
|
||||
searchName = imageName[slashIndex:]
|
||||
} else {
|
||||
slashIndex = 0
|
||||
}
|
||||
|
||||
id := strings.Index(searchName, "@")
|
||||
ic := strings.Index(searchName, ":")
|
||||
|
||||
// no tag or digest
|
||||
if ic < 0 && id < 0 {
|
||||
return imageName, "", ""
|
||||
}
|
||||
|
||||
// digest only
|
||||
if id >= 0 && (id < ic || ic < 0) {
|
||||
id += slashIndex
|
||||
name = imageName[:id]
|
||||
digest = strings.TrimPrefix(imageName[id:], "@")
|
||||
return name, "", digest
|
||||
}
|
||||
|
||||
// tag and digest
|
||||
if id >= 0 && ic >= 0 {
|
||||
id += slashIndex
|
||||
ic += slashIndex
|
||||
name = imageName[:ic]
|
||||
tag = strings.TrimPrefix(imageName[ic:id], ":")
|
||||
digest = strings.TrimPrefix(imageName[id:], "@")
|
||||
return name, tag, digest
|
||||
}
|
||||
|
||||
// tag only
|
||||
ic += slashIndex
|
||||
name = imageName[:ic]
|
||||
tag = strings.TrimPrefix(imageName[ic:], ":")
|
||||
return name, tag, ""
|
||||
}
|
||||
Generated
Vendored
+47
@@ -0,0 +1,47 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const commonAnnotationFieldSpecs = `
|
||||
commonAnnotations:
|
||||
- path: metadata/annotations
|
||||
create: true
|
||||
|
||||
- path: spec/template/metadata/annotations
|
||||
create: true
|
||||
version: v1
|
||||
kind: ReplicationController
|
||||
|
||||
- path: spec/template/metadata/annotations
|
||||
create: true
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/metadata/annotations
|
||||
create: true
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/metadata/annotations
|
||||
create: true
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/metadata/annotations
|
||||
create: true
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/metadata/annotations
|
||||
create: true
|
||||
group: batch
|
||||
kind: Job
|
||||
|
||||
- path: spec/jobTemplate/metadata/annotations
|
||||
create: true
|
||||
group: batch
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/metadata/annotations
|
||||
create: true
|
||||
group: batch
|
||||
kind: CronJob
|
||||
|
||||
`
|
||||
Generated
Vendored
+113
@@ -0,0 +1,113 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const commonLabelFieldSpecs = `
|
||||
commonLabels:
|
||||
- path: spec/selector
|
||||
create: true
|
||||
version: v1
|
||||
kind: Service
|
||||
|
||||
- path: spec/selector
|
||||
create: true
|
||||
version: v1
|
||||
kind: ReplicationController
|
||||
- path: spec/selector/matchLabels
|
||||
create: true
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/affinity/podAffinity/preferredDuringSchedulingIgnoredDuringExecution/podAffinityTerm/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/affinity/podAffinity/requiredDuringSchedulingIgnoredDuringExecution/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/affinity/podAntiAffinity/preferredDuringSchedulingIgnoredDuringExecution/podAffinityTerm/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/affinity/podAntiAffinity/requiredDuringSchedulingIgnoredDuringExecution/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/topologySpreadConstraints/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/selector/matchLabels
|
||||
create: true
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/selector/matchLabels
|
||||
create: true
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/selector/matchLabels
|
||||
create: true
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/affinity/podAffinity/preferredDuringSchedulingIgnoredDuringExecution/podAffinityTerm/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/affinity/podAffinity/requiredDuringSchedulingIgnoredDuringExecution/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/affinity/podAntiAffinity/preferredDuringSchedulingIgnoredDuringExecution/podAffinityTerm/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/affinity/podAntiAffinity/requiredDuringSchedulingIgnoredDuringExecution/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/topologySpreadConstraints/labelSelector/matchLabels
|
||||
create: false
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/selector/matchLabels
|
||||
create: false
|
||||
group: batch
|
||||
kind: Job
|
||||
|
||||
- path: spec/jobTemplate/spec/selector/matchLabels
|
||||
create: false
|
||||
group: batch
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/selector/matchLabels
|
||||
create: false
|
||||
group: policy
|
||||
kind: PodDisruptionBudget
|
||||
|
||||
- path: spec/podSelector/matchLabels
|
||||
create: false
|
||||
group: networking.k8s.io
|
||||
kind: NetworkPolicy
|
||||
|
||||
- path: spec/ingress/from/podSelector/matchLabels
|
||||
create: false
|
||||
group: networking.k8s.io
|
||||
kind: NetworkPolicy
|
||||
|
||||
- path: spec/egress/to/podSelector/matchLabels
|
||||
create: false
|
||||
group: networking.k8s.io
|
||||
kind: NetworkPolicy
|
||||
` + metadataLabelsFieldSpecs
|
||||
Generated
Vendored
+42
@@ -0,0 +1,42 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
// GetDefaultFieldSpecs returns default fieldSpecs.
|
||||
func GetDefaultFieldSpecs() []byte {
|
||||
configData := [][]byte{
|
||||
[]byte(namePrefixFieldSpecs),
|
||||
[]byte(nameSuffixFieldSpecs),
|
||||
[]byte(commonLabelFieldSpecs),
|
||||
[]byte(templateLabelFieldSpecs),
|
||||
[]byte(commonAnnotationFieldSpecs),
|
||||
[]byte(namespaceFieldSpecs),
|
||||
[]byte(varReferenceFieldSpecs),
|
||||
[]byte(nameReferenceFieldSpecs),
|
||||
[]byte(imagesFieldSpecs),
|
||||
[]byte(replicasFieldSpecs),
|
||||
}
|
||||
return bytes.Join(configData, []byte("\n"))
|
||||
}
|
||||
|
||||
// GetDefaultFieldSpecsAsMap returns default fieldSpecs
|
||||
// as a string->string map.
|
||||
func GetDefaultFieldSpecsAsMap() map[string]string {
|
||||
result := make(map[string]string)
|
||||
result["nameprefix"] = namePrefixFieldSpecs
|
||||
result["namesuffix"] = nameSuffixFieldSpecs
|
||||
result["commonlabels"] = commonLabelFieldSpecs
|
||||
result["templatelabels"] = templateLabelFieldSpecs
|
||||
result["commonannotations"] = commonAnnotationFieldSpecs
|
||||
result["namespace"] = namespaceFieldSpecs
|
||||
result["varreference"] = varReferenceFieldSpecs
|
||||
result["namereference"] = nameReferenceFieldSpecs
|
||||
result["images"] = imagesFieldSpecs
|
||||
result["replicas"] = replicasFieldSpecs
|
||||
return result
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package builtinpluginconsts provides builtin plugin
|
||||
// configuration data. Builtin plugins can also be
|
||||
// configured individually with plugin config files,
|
||||
// in which case the constants in this package are ignored.
|
||||
package builtinpluginconsts
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const (
|
||||
imagesFieldSpecs = `
|
||||
images:
|
||||
- path: spec/containers[]/image
|
||||
create: true
|
||||
- path: spec/initContainers[]/image
|
||||
create: true
|
||||
- path: spec/volumes[]/image/reference
|
||||
create: true
|
||||
- path: spec/template/spec/containers[]/image
|
||||
create: true
|
||||
- path: spec/template/spec/initContainers[]/image
|
||||
create: true
|
||||
- path: spec/template/spec/volumes[]/image/reference
|
||||
create: true
|
||||
`
|
||||
)
|
||||
Generated
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const metadataLabelsFieldSpecs = `
|
||||
- path: metadata/labels
|
||||
create: true
|
||||
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
version: v1
|
||||
kind: ReplicationController
|
||||
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/volumeClaimTemplates[]/metadata/labels
|
||||
create: true
|
||||
group: apps
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/metadata/labels
|
||||
create: true
|
||||
group: batch
|
||||
kind: Job
|
||||
|
||||
- path: spec/jobTemplate/metadata/labels
|
||||
create: true
|
||||
group: batch
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/metadata/labels
|
||||
create: true
|
||||
group: batch
|
||||
kind: CronJob
|
||||
`
|
||||
Generated
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const (
|
||||
namePrefixFieldSpecs = `
|
||||
namePrefix:
|
||||
- path: metadata/name
|
||||
`
|
||||
)
|
||||
Generated
Vendored
+434
@@ -0,0 +1,434 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
// LINT.IfChange
|
||||
const (
|
||||
nameReferenceFieldSpecs = `
|
||||
nameReference:
|
||||
- kind: Deployment
|
||||
fieldSpecs:
|
||||
- path: spec/scaleTargetRef/name
|
||||
kind: HorizontalPodAutoscaler
|
||||
|
||||
- kind: ReplicationController
|
||||
fieldSpecs:
|
||||
- path: spec/scaleTargetRef/name
|
||||
kind: HorizontalPodAutoscaler
|
||||
|
||||
- kind: ReplicaSet
|
||||
fieldSpecs:
|
||||
- path: spec/scaleTargetRef/name
|
||||
kind: HorizontalPodAutoscaler
|
||||
|
||||
- kind: StatefulSet
|
||||
fieldSpecs:
|
||||
- path: spec/scaleTargetRef/name
|
||||
kind: HorizontalPodAutoscaler
|
||||
|
||||
- kind: ConfigMap
|
||||
version: v1
|
||||
fieldSpecs:
|
||||
- path: spec/volumes/configMap/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/initContainers/env/valueFrom/configMapKeyRef/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/containers/envFrom/configMapRef/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/initContainers/envFrom/configMapRef/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/volumes/projected/sources/configMap/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: template/spec/volumes/configMap/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/initContainers/env/valueFrom/configMapKeyRef/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/containers/envFrom/configMapRef/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/initContainers/envFrom/configMapRef/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/volumes/projected/sources/configMap/name
|
||||
kind: PodTemplate
|
||||
- path: spec/template/spec/volumes/configMap/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/configMapKeyRef/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/containers/envFrom/configMapRef/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/initContainers/envFrom/configMapRef/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/volumes/projected/sources/configMap/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/volumes/configMap/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/configMapKeyRef/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/containers/envFrom/configMapRef/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/initContainers/envFrom/configMapRef/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/volumes/projected/sources/configMap/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/volumes/configMap/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/configMapKeyRef/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/containers/envFrom/configMapRef/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/initContainers/envFrom/configMapRef/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/volumes/projected/sources/configMap/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/volumes/configMap/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/configMapKeyRef/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/containers/envFrom/configMapRef/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/initContainers/envFrom/configMapRef/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/volumes/projected/sources/configMap/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/volumes/configMap/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/configMapKeyRef/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/containers/envFrom/configMapRef/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/initContainers/envFrom/configMapRef/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/volumes/projected/sources/configMap/name
|
||||
kind: Job
|
||||
- path: spec/jobTemplate/spec/template/spec/volumes/configMap/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/volumes/projected/sources/configMap/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/containers/env/valueFrom/configMapKeyRef/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/initContainers/env/valueFrom/configMapKeyRef/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/containers/envFrom/configMapRef/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/initContainers/envFrom/configMapRef/name
|
||||
kind: CronJob
|
||||
- path: spec/configSource/configMap
|
||||
kind: Node
|
||||
- path: rules/resourceNames
|
||||
kind: Role
|
||||
- path: rules/resourceNames
|
||||
kind: ClusterRole
|
||||
- path: metadata/annotations/nginx.ingress.kubernetes.io\/fastcgi-params-configmap
|
||||
kind: Ingress
|
||||
|
||||
- kind: Secret
|
||||
version: v1
|
||||
fieldSpecs:
|
||||
- path: spec/volumes/secret/secretName
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/containers/env/valueFrom/secretKeyRef/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/initContainers/env/valueFrom/secretKeyRef/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/containers/envFrom/secretRef/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/initContainers/envFrom/secretRef/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/imagePullSecrets/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: spec/volumes/projected/sources/secret/name
|
||||
version: v1
|
||||
kind: Pod
|
||||
- path: template/spec/volumes/secret/secretName
|
||||
kind: PodTemplate
|
||||
- path: template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/initContainers/env/valueFrom/secretKeyRef/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/containers/envFrom/secretRef/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/initContainers/envFrom/secretRef/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/imagePullSecrets/name
|
||||
kind: PodTemplate
|
||||
- path: template/spec/volumes/projected/sources/secret/name
|
||||
kind: PodTemplate
|
||||
- path: spec/template/spec/volumes/secret/secretName
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/secretKeyRef/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/containers/envFrom/secretRef/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/initContainers/envFrom/secretRef/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/imagePullSecrets/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/volumes/projected/sources/secret/name
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/volumes/secret/secretName
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/secretKeyRef/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/containers/envFrom/secretRef/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/initContainers/envFrom/secretRef/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/imagePullSecrets/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/volumes/projected/sources/secret/name
|
||||
kind: ReplicaSet
|
||||
- path: spec/template/spec/volumes/secret/secretName
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/secretKeyRef/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/containers/envFrom/secretRef/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/initContainers/envFrom/secretRef/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/imagePullSecrets/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/volumes/projected/sources/secret/name
|
||||
kind: DaemonSet
|
||||
- path: spec/template/spec/volumes/secret/secretName
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/secretKeyRef/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/containers/envFrom/secretRef/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/initContainers/envFrom/secretRef/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/imagePullSecrets/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/volumes/projected/sources/secret/name
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/volumes/secret/secretName
|
||||
kind: Job
|
||||
- path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/initContainers/env/valueFrom/secretKeyRef/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/containers/envFrom/secretRef/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/initContainers/envFrom/secretRef/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/imagePullSecrets/name
|
||||
kind: Job
|
||||
- path: spec/template/spec/volumes/projected/sources/secret/name
|
||||
kind: Job
|
||||
- path: spec/jobTemplate/spec/template/spec/volumes/secret/secretName
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/volumes/projected/sources/secret/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/initContainers/env/valueFrom/secretKeyRef/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/containers/envFrom/secretRef/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/initContainers/envFrom/secretRef/name
|
||||
kind: CronJob
|
||||
- path: spec/jobTemplate/spec/template/spec/imagePullSecrets/name
|
||||
kind: CronJob
|
||||
- path: spec/tls/secretName
|
||||
kind: Ingress
|
||||
- path: metadata/annotations/ingress.kubernetes.io\/auth-secret
|
||||
kind: Ingress
|
||||
- path: metadata/annotations/nginx.ingress.kubernetes.io\/auth-secret
|
||||
kind: Ingress
|
||||
- path: metadata/annotations/nginx.ingress.kubernetes.io\/auth-tls-secret
|
||||
kind: Ingress
|
||||
- path: spec/tls/secretName
|
||||
kind: Ingress
|
||||
- path: imagePullSecrets/name
|
||||
kind: ServiceAccount
|
||||
- path: parameters/secretName
|
||||
kind: StorageClass
|
||||
- path: parameters/adminSecretName
|
||||
kind: StorageClass
|
||||
- path: parameters/userSecretName
|
||||
kind: StorageClass
|
||||
- path: parameters/secretRef
|
||||
kind: StorageClass
|
||||
- path: rules/resourceNames
|
||||
kind: Role
|
||||
- path: rules/resourceNames
|
||||
kind: ClusterRole
|
||||
- path: spec/template/spec/containers/env/valueFrom/secretKeyRef/name
|
||||
kind: Service
|
||||
group: serving.knative.dev
|
||||
version: v1
|
||||
- path: spec/azureFile/secretName
|
||||
kind: PersistentVolume
|
||||
|
||||
- kind: Service
|
||||
version: v1
|
||||
fieldSpecs:
|
||||
- path: spec/serviceName
|
||||
kind: StatefulSet
|
||||
group: apps
|
||||
- path: spec/rules/http/paths/backend/serviceName
|
||||
kind: Ingress
|
||||
- path: spec/backend/serviceName
|
||||
kind: Ingress
|
||||
- path: spec/rules/http/paths/backend/service/name
|
||||
kind: Ingress
|
||||
- path: spec/defaultBackend/service/name
|
||||
kind: Ingress
|
||||
- path: spec/service/name
|
||||
kind: APIService
|
||||
group: apiregistration.k8s.io
|
||||
- path: webhooks/clientConfig/service
|
||||
kind: ValidatingWebhookConfiguration
|
||||
group: admissionregistration.k8s.io
|
||||
- path: webhooks/clientConfig/service
|
||||
kind: MutatingWebhookConfiguration
|
||||
group: admissionregistration.k8s.io
|
||||
|
||||
- kind: Role
|
||||
group: rbac.authorization.k8s.io
|
||||
fieldSpecs:
|
||||
- path: roleRef/name
|
||||
kind: RoleBinding
|
||||
group: rbac.authorization.k8s.io
|
||||
|
||||
- kind: ClusterRole
|
||||
group: rbac.authorization.k8s.io
|
||||
fieldSpecs:
|
||||
- path: roleRef/name
|
||||
kind: RoleBinding
|
||||
group: rbac.authorization.k8s.io
|
||||
- path: roleRef/name
|
||||
kind: ClusterRoleBinding
|
||||
group: rbac.authorization.k8s.io
|
||||
|
||||
- kind: ServiceAccount
|
||||
version: v1
|
||||
fieldSpecs:
|
||||
- path: subjects
|
||||
kind: RoleBinding
|
||||
group: rbac.authorization.k8s.io
|
||||
- path: subjects
|
||||
kind: ClusterRoleBinding
|
||||
group: rbac.authorization.k8s.io
|
||||
- path: spec/serviceAccountName
|
||||
kind: Pod
|
||||
- path: spec/template/spec/serviceAccountName
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/serviceAccountName
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/serviceAccountName
|
||||
kind: ReplicationController
|
||||
- path: spec/jobTemplate/spec/template/spec/serviceAccountName
|
||||
kind: CronJob
|
||||
- path: spec/template/spec/serviceAccountName
|
||||
kind: Job
|
||||
- path: spec/template/spec/serviceAccountName
|
||||
kind: DaemonSet
|
||||
|
||||
- kind: PersistentVolumeClaim
|
||||
version: v1
|
||||
fieldSpecs:
|
||||
- path: spec/volumes/persistentVolumeClaim/claimName
|
||||
kind: Pod
|
||||
- path: spec/template/spec/volumes/persistentVolumeClaim/claimName
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/volumes/persistentVolumeClaim/claimName
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/volumes/persistentVolumeClaim/claimName
|
||||
kind: ReplicationController
|
||||
- path: spec/jobTemplate/spec/template/spec/volumes/persistentVolumeClaim/claimName
|
||||
kind: CronJob
|
||||
- path: spec/template/spec/volumes/persistentVolumeClaim/claimName
|
||||
kind: Job
|
||||
- path: spec/template/spec/volumes/persistentVolumeClaim/claimName
|
||||
kind: DaemonSet
|
||||
|
||||
- kind: PersistentVolume
|
||||
version: v1
|
||||
fieldSpecs:
|
||||
- path: spec/volumeName
|
||||
kind: PersistentVolumeClaim
|
||||
- path: rules/resourceNames
|
||||
kind: ClusterRole
|
||||
|
||||
- kind: StorageClass
|
||||
version: v1
|
||||
group: storage.k8s.io
|
||||
fieldSpecs:
|
||||
- path: spec/storageClassName
|
||||
kind: PersistentVolume
|
||||
- path: spec/storageClassName
|
||||
kind: PersistentVolumeClaim
|
||||
- path: spec/volumeClaimTemplates/spec/storageClassName
|
||||
kind: StatefulSet
|
||||
|
||||
- kind: PriorityClass
|
||||
version: v1
|
||||
group: scheduling.k8s.io
|
||||
fieldSpecs:
|
||||
- path: spec/priorityClassName
|
||||
kind: Pod
|
||||
- path: spec/template/spec/priorityClassName
|
||||
kind: StatefulSet
|
||||
- path: spec/template/spec/priorityClassName
|
||||
kind: Deployment
|
||||
- path: spec/template/spec/priorityClassName
|
||||
kind: ReplicationController
|
||||
- path: spec/jobTemplate/spec/template/spec/priorityClassName
|
||||
kind: CronJob
|
||||
- path: spec/template/spec/priorityClassName
|
||||
kind: Job
|
||||
- path: spec/template/spec/priorityClassName
|
||||
kind: DaemonSet
|
||||
|
||||
- kind: IngressClass
|
||||
version: v1
|
||||
group: networking.k8s.io/v1
|
||||
fieldSpecs:
|
||||
- path: spec/ingressClassName
|
||||
kind: Ingress
|
||||
|
||||
- kind: ValidatingAdmissionPolicy
|
||||
group: admissionregistration.k8s.io
|
||||
fieldSpecs:
|
||||
- path: spec/policyName
|
||||
kind: ValidatingAdmissionPolicyBinding
|
||||
group: admissionregistration.k8s.io
|
||||
`
|
||||
)
|
||||
|
||||
// LINT.ThenChange(/examples/transformerconfigs/README.md)
|
||||
Generated
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const (
|
||||
namespaceFieldSpecs = `
|
||||
namespace:
|
||||
- path: metadata/name
|
||||
kind: Namespace
|
||||
create: true
|
||||
- path: spec/service/namespace
|
||||
group: apiregistration.k8s.io
|
||||
kind: APIService
|
||||
create: true
|
||||
- path: spec/conversion/webhook/clientConfig/service/namespace
|
||||
group: apiextensions.k8s.io
|
||||
kind: CustomResourceDefinition
|
||||
`
|
||||
)
|
||||
Generated
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const (
|
||||
nameSuffixFieldSpecs = `
|
||||
nameSuffix:
|
||||
- path: metadata/name
|
||||
`
|
||||
)
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const replicasFieldSpecs = `
|
||||
replicas:
|
||||
- path: spec/replicas
|
||||
create: true
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/replicas
|
||||
create: true
|
||||
kind: ReplicationController
|
||||
|
||||
- path: spec/replicas
|
||||
create: true
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/replicas
|
||||
create: true
|
||||
kind: StatefulSet
|
||||
`
|
||||
Generated
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const templateLabelFieldSpecs = `
|
||||
templateLabels:
|
||||
` + metadataLabelsFieldSpecs
|
||||
Generated
Vendored
+223
@@ -0,0 +1,223 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinpluginconsts
|
||||
|
||||
const (
|
||||
varReferenceFieldSpecs = `
|
||||
varReference:
|
||||
- path: spec/jobTemplate/spec/template/spec/containers/args
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/spec/containers/command
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/spec/containers/env/value
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/spec/containers/volumeMounts/mountPath
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/spec/initContainers/args
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/spec/initContainers/command
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/spec/initContainers/env/value
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/spec/initContainers/volumeMounts/mountPath
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/jobTemplate/spec/template/volumes/nfs/server
|
||||
kind: CronJob
|
||||
|
||||
- path: spec/template/spec/containers/args
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/containers/command
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/containers/env/value
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/containers/volumeMounts/mountPath
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/initContainers/args
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/initContainers/command
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/initContainers/env/value
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/initContainers/volumeMounts/mountPath
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/volumes/nfs/server
|
||||
kind: DaemonSet
|
||||
|
||||
- path: spec/template/spec/containers/args
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/containers/command
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/containers/env/value
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/containers/volumeMounts/mountPath
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/initContainers/args
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/initContainers/command
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/initContainers/env/value
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/initContainers/volumeMounts/mountPath
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/spec/volumes/nfs/server
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/template/metadata/annotations
|
||||
kind: Deployment
|
||||
|
||||
- path: spec/rules/host
|
||||
kind: Ingress
|
||||
|
||||
- path: spec/tls/hosts
|
||||
kind: Ingress
|
||||
|
||||
- path: spec/tls/secretName
|
||||
kind: Ingress
|
||||
|
||||
- path: spec/template/spec/containers/args
|
||||
kind: Job
|
||||
|
||||
- path: spec/template/spec/containers/command
|
||||
kind: Job
|
||||
|
||||
- path: spec/template/spec/containers/env/value
|
||||
kind: Job
|
||||
|
||||
- path: spec/template/spec/containers/volumeMounts/mountPath
|
||||
kind: Job
|
||||
|
||||
- path: spec/template/spec/initContainers/args
|
||||
kind: Job
|
||||
|
||||
- path: spec/template/spec/initContainers/command
|
||||
kind: Job
|
||||
|
||||
- path: spec/template/spec/initContainers/env/value
|
||||
kind: Job
|
||||
|
||||
- path: spec/template/spec/initContainers/volumeMounts/mountPath
|
||||
kind: Job
|
||||
|
||||
- path: spec/template/spec/volumes/nfs/server
|
||||
kind: Job
|
||||
|
||||
- path: spec/containers/args
|
||||
kind: Pod
|
||||
|
||||
- path: spec/containers/command
|
||||
kind: Pod
|
||||
|
||||
- path: spec/containers/env/value
|
||||
kind: Pod
|
||||
|
||||
- path: spec/containers/volumeMounts/mountPath
|
||||
kind: Pod
|
||||
|
||||
- path: spec/initContainers/args
|
||||
kind: Pod
|
||||
|
||||
- path: spec/initContainers/command
|
||||
kind: Pod
|
||||
|
||||
- path: spec/initContainers/env/value
|
||||
kind: Pod
|
||||
|
||||
- path: spec/initContainers/volumeMounts/mountPath
|
||||
kind: Pod
|
||||
|
||||
- path: spec/volumes/nfs/server
|
||||
kind: Pod
|
||||
|
||||
- path: spec/template/spec/containers/args
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/spec/containers/command
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/spec/containers/env/value
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/spec/containers/volumeMounts/mountPath
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/spec/initContainers/args
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/spec/initContainers/command
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/spec/initContainers/env/value
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/spec/initContainers/volumeMounts/mountPath
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/template/spec/volumes/nfs/server
|
||||
kind: ReplicaSet
|
||||
|
||||
- path: spec/ports/port
|
||||
kind: Service
|
||||
|
||||
- path: spec/ports/targetPort
|
||||
kind: Service
|
||||
|
||||
- path: spec/template/spec/containers/args
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/containers/command
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/containers/env/value
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/containers/volumeMounts/mountPath
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/initContainers/args
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/initContainers/command
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/initContainers/env/value
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/template/spec/initContainers/volumeMounts/mountPath
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/volumeClaimTemplates/spec/nfs/server
|
||||
kind: StatefulSet
|
||||
|
||||
- path: spec/nfs/server
|
||||
kind: PersistentVolume
|
||||
|
||||
- path: metadata/labels
|
||||
|
||||
- path: metadata/annotations
|
||||
`
|
||||
)
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package error has contextual error types.
|
||||
package kusterr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// YamlFormatError represents error with yaml file name where json/yaml format error happens.
|
||||
type YamlFormatError struct {
|
||||
Path string
|
||||
ErrorMsg string
|
||||
}
|
||||
|
||||
func (e YamlFormatError) Error() string {
|
||||
return fmt.Sprintf("YAML file [%s] encounters a format error.\n%s\n", e.Path, e.ErrorMsg)
|
||||
}
|
||||
|
||||
// MalformedYamlError represents an error that occurred while trying to decode a given YAML.
|
||||
type MalformedYamlError struct {
|
||||
Path string
|
||||
ErrorMsg string
|
||||
}
|
||||
|
||||
func (e MalformedYamlError) Error() string {
|
||||
return fmt.Sprintf("%s in File: %s", e.ErrorMsg, e.Path)
|
||||
}
|
||||
|
||||
// Handler handles YamlFormatError
|
||||
func Handler(e error, path string) error {
|
||||
if isYAMLSyntaxError(e) {
|
||||
return YamlFormatError{
|
||||
Path: path,
|
||||
ErrorMsg: e.Error(),
|
||||
}
|
||||
}
|
||||
if IsMalformedYAMLError(e) {
|
||||
return MalformedYamlError{
|
||||
Path: path,
|
||||
ErrorMsg: e.Error(),
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
func isYAMLSyntaxError(e error) bool {
|
||||
return strings.Contains(e.Error(), "error converting YAML to JSON") || strings.Contains(e.Error(), "error unmarshaling JSON")
|
||||
}
|
||||
|
||||
func IsMalformedYAMLError(e error) bool {
|
||||
return strings.Contains(e.Error(), "MalformedYAMLError")
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// Copyright 2022 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package loader
|
||||
|
||||
import "sigs.k8s.io/kustomize/kyaml/errors"
|
||||
|
||||
var (
|
||||
ErrHTTP = errors.Errorf("HTTP Error")
|
||||
ErrRtNotDir = errors.Errorf("must build at directory")
|
||||
)
|
||||
+338
@@ -0,0 +1,338 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package loader
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/git"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
// IsRemoteFile returns whether path has a url scheme that kustomize allows for
|
||||
// remote files. See https://github.com/kubernetes-sigs/kustomize/blob/master/examples/remoteBuild.md
|
||||
func IsRemoteFile(path string) bool {
|
||||
u, err := url.Parse(path)
|
||||
return err == nil && (u.Scheme == "http" || u.Scheme == "https")
|
||||
}
|
||||
|
||||
// FileLoader is a kustomization's interface to files.
|
||||
//
|
||||
// The directory in which a kustomization file sits
|
||||
// is referred to below as the kustomization's _root_.
|
||||
//
|
||||
// An instance of fileLoader has an immutable root,
|
||||
// and offers a `New` method returning a new loader
|
||||
// with a new root.
|
||||
//
|
||||
// A kustomization file refers to two kinds of files:
|
||||
//
|
||||
// * supplemental data paths
|
||||
//
|
||||
// `Load` is used to visit these paths.
|
||||
//
|
||||
// These paths refer to resources, patches,
|
||||
// data for ConfigMaps and Secrets, etc.
|
||||
//
|
||||
// The loadRestrictor may disallow certain paths
|
||||
// or classes of paths.
|
||||
//
|
||||
// * bases (other kustomizations)
|
||||
//
|
||||
// `New` is used to load bases.
|
||||
//
|
||||
// A base can be either a remote git repo URL, or
|
||||
// a directory specified relative to the current
|
||||
// root. In the former case, the repo is locally
|
||||
// cloned, and the new loader is rooted on a path
|
||||
// in that clone.
|
||||
//
|
||||
// As loaders create new loaders, a root history
|
||||
// is established, and used to disallow:
|
||||
//
|
||||
// - A base that is a repository that, in turn,
|
||||
// specifies a base repository seen previously
|
||||
// in the loading stack (a cycle).
|
||||
//
|
||||
// - An overlay depending on a base positioned at
|
||||
// or above it. I.e. '../foo' is OK, but '.',
|
||||
// '..', '../..', etc. are disallowed. Allowing
|
||||
// such a base has no advantages and encourages
|
||||
// cycles, particularly if some future change
|
||||
// were to introduce globbing to file
|
||||
// specifications in the kustomization file.
|
||||
//
|
||||
// These restrictions assure that kustomizations
|
||||
// are self-contained and relocatable, and impose
|
||||
// some safety when relying on remote kustomizations,
|
||||
// e.g. a remotely loaded ConfigMap generator specified
|
||||
// to read from /etc/passwd will fail.
|
||||
type FileLoader struct {
|
||||
// Loader that spawned this loader.
|
||||
// Used to avoid cycles.
|
||||
referrer *FileLoader
|
||||
|
||||
// An absolute, cleaned path to a directory.
|
||||
// The Load function will read non-absolute
|
||||
// paths relative to this directory.
|
||||
root filesys.ConfirmedDir
|
||||
|
||||
// Restricts behavior of Load function.
|
||||
loadRestrictor LoadRestrictorFunc
|
||||
|
||||
// If this is non-nil, the files were
|
||||
// obtained from the given repository.
|
||||
repoSpec *git.RepoSpec
|
||||
|
||||
// File system utilities.
|
||||
fSys filesys.FileSystem
|
||||
|
||||
// Used to load from HTTP
|
||||
http *http.Client
|
||||
|
||||
// Used to clone repositories.
|
||||
cloner git.Cloner
|
||||
|
||||
// Used to clean up, as needed.
|
||||
cleaner func() error
|
||||
}
|
||||
|
||||
// Repo returns the absolute path to the repo that contains Root if this fileLoader was created from a url
|
||||
// or the empty string otherwise.
|
||||
func (fl *FileLoader) Repo() string {
|
||||
if fl.repoSpec != nil {
|
||||
return fl.repoSpec.Dir.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Root returns the absolute path that is prepended to any
|
||||
// relative paths used in Load.
|
||||
func (fl *FileLoader) Root() string {
|
||||
return fl.root.String()
|
||||
}
|
||||
|
||||
func NewLoaderOrDie(
|
||||
lr LoadRestrictorFunc,
|
||||
fSys filesys.FileSystem, path string) *FileLoader {
|
||||
root, err := filesys.ConfirmDir(fSys, path)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to make loader at '%s'; %v", path, err)
|
||||
}
|
||||
return newLoaderAtConfirmedDir(
|
||||
lr, root, fSys, nil, git.ClonerUsingGitExec)
|
||||
}
|
||||
|
||||
// newLoaderAtConfirmedDir returns a new FileLoader with given root.
|
||||
func newLoaderAtConfirmedDir(
|
||||
lr LoadRestrictorFunc,
|
||||
root filesys.ConfirmedDir, fSys filesys.FileSystem,
|
||||
referrer *FileLoader, cloner git.Cloner) *FileLoader {
|
||||
return &FileLoader{
|
||||
loadRestrictor: lr,
|
||||
root: root,
|
||||
referrer: referrer,
|
||||
fSys: fSys,
|
||||
cloner: cloner,
|
||||
cleaner: func() error { return nil },
|
||||
}
|
||||
}
|
||||
|
||||
// New returns a new Loader, rooted relative to current loader,
|
||||
// or rooted in a temp directory holding a git repo clone.
|
||||
func (fl *FileLoader) New(path string) (ifc.Loader, error) {
|
||||
if path == "" {
|
||||
return nil, errors.Errorf("new root cannot be empty")
|
||||
}
|
||||
|
||||
repoSpec, err := git.NewRepoSpecFromURL(path)
|
||||
if err == nil {
|
||||
// Treat this as git repo clone request.
|
||||
if err = fl.errIfRepoCycle(repoSpec); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newLoaderAtGitClone(
|
||||
repoSpec, fl.fSys, fl, fl.cloner)
|
||||
}
|
||||
|
||||
if filepath.IsAbs(path) {
|
||||
return nil, fmt.Errorf("new root '%s' cannot be absolute", path)
|
||||
}
|
||||
root, err := filesys.ConfirmDir(fl.fSys, fl.root.Join(path))
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "%s", ErrRtNotDir.Error())
|
||||
}
|
||||
if err = fl.errIfGitContainmentViolation(root); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = fl.errIfArgEqualOrHigher(root); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newLoaderAtConfirmedDir(
|
||||
fl.loadRestrictor, root, fl.fSys, fl, fl.cloner), nil
|
||||
}
|
||||
|
||||
// newLoaderAtGitClone returns a new Loader pinned to a temporary
|
||||
// directory holding a cloned git repo.
|
||||
func newLoaderAtGitClone(
|
||||
repoSpec *git.RepoSpec, fSys filesys.FileSystem,
|
||||
referrer *FileLoader, cloner git.Cloner) (ifc.Loader, error) {
|
||||
cleaner := repoSpec.Cleaner(fSys)
|
||||
err := cloner(repoSpec)
|
||||
if err != nil {
|
||||
cleaner()
|
||||
return nil, err
|
||||
}
|
||||
root, f, err := fSys.CleanedAbs(repoSpec.AbsPath())
|
||||
if err != nil {
|
||||
cleaner()
|
||||
return nil, err
|
||||
}
|
||||
// We don't know that the path requested in repoSpec
|
||||
// is a directory until we actually clone it and look
|
||||
// inside. That just happened, hence the error check
|
||||
// is here.
|
||||
if f != "" {
|
||||
cleaner()
|
||||
return nil, fmt.Errorf(
|
||||
"'%s' refers to file '%s'; expecting directory",
|
||||
repoSpec.AbsPath(), f)
|
||||
}
|
||||
// Path in repo can contain symlinks that exit repo. We can only
|
||||
// check for this after cloning repo.
|
||||
if !root.HasPrefix(repoSpec.CloneDir()) {
|
||||
_ = cleaner()
|
||||
return nil, fmt.Errorf("%q refers to directory outside of repo %q", repoSpec.AbsPath(),
|
||||
repoSpec.CloneDir())
|
||||
}
|
||||
return &FileLoader{
|
||||
// Clones never allowed to escape root.
|
||||
loadRestrictor: RestrictionRootOnly,
|
||||
root: root,
|
||||
referrer: referrer,
|
||||
repoSpec: repoSpec,
|
||||
fSys: fSys,
|
||||
cloner: cloner,
|
||||
cleaner: cleaner,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fl *FileLoader) errIfGitContainmentViolation(
|
||||
base filesys.ConfirmedDir) error {
|
||||
containingRepo := fl.containingRepo()
|
||||
if containingRepo == nil {
|
||||
return nil
|
||||
}
|
||||
if !base.HasPrefix(containingRepo.CloneDir()) {
|
||||
return fmt.Errorf(
|
||||
"security; bases in kustomizations found in "+
|
||||
"cloned git repos must be within the repo, "+
|
||||
"but base '%s' is outside '%s'",
|
||||
base, containingRepo.CloneDir())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Looks back through referrers for a git repo, returning nil
|
||||
// if none found.
|
||||
func (fl *FileLoader) containingRepo() *git.RepoSpec {
|
||||
if fl.repoSpec != nil {
|
||||
return fl.repoSpec
|
||||
}
|
||||
if fl.referrer == nil {
|
||||
return nil
|
||||
}
|
||||
return fl.referrer.containingRepo()
|
||||
}
|
||||
|
||||
// errIfArgEqualOrHigher tests whether the argument,
|
||||
// is equal to or above the root of any ancestor.
|
||||
func (fl *FileLoader) errIfArgEqualOrHigher(
|
||||
candidateRoot filesys.ConfirmedDir) error {
|
||||
if fl.root.HasPrefix(candidateRoot) {
|
||||
return fmt.Errorf(
|
||||
"cycle detected: candidate root '%s' contains visited root '%s'",
|
||||
candidateRoot, fl.root)
|
||||
}
|
||||
if fl.referrer == nil {
|
||||
return nil
|
||||
}
|
||||
return fl.referrer.errIfArgEqualOrHigher(candidateRoot)
|
||||
}
|
||||
|
||||
// TODO(monopole): Distinguish branches?
|
||||
// I.e. Allow a distinction between git URI with
|
||||
// path foo and tag bar and a git URI with the same
|
||||
// path but a different tag?
|
||||
func (fl *FileLoader) errIfRepoCycle(newRepoSpec *git.RepoSpec) error {
|
||||
// TODO(monopole): Use parsed data instead of Raw().
|
||||
if fl.repoSpec != nil &&
|
||||
strings.HasPrefix(fl.repoSpec.Raw(), newRepoSpec.Raw()) {
|
||||
return fmt.Errorf(
|
||||
"cycle detected: URI '%s' referenced by previous URI '%s'",
|
||||
newRepoSpec.Raw(), fl.repoSpec.Raw())
|
||||
}
|
||||
if fl.referrer == nil {
|
||||
return nil
|
||||
}
|
||||
return fl.referrer.errIfRepoCycle(newRepoSpec)
|
||||
}
|
||||
|
||||
// Load returns the content of file at the given path,
|
||||
// else an error. Relative paths are taken relative
|
||||
// to the root.
|
||||
func (fl *FileLoader) Load(path string) ([]byte, error) {
|
||||
if IsRemoteFile(path) {
|
||||
return fl.httpClientGetContent(path)
|
||||
}
|
||||
if !filepath.IsAbs(path) {
|
||||
path = fl.root.Join(path)
|
||||
}
|
||||
path, err := fl.loadRestrictor(fl.fSys, fl.root, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return fl.fSys.ReadFile(path)
|
||||
}
|
||||
|
||||
func (fl *FileLoader) httpClientGetContent(path string) ([]byte, error) {
|
||||
var hc *http.Client
|
||||
if fl.http != nil {
|
||||
hc = fl.http
|
||||
} else {
|
||||
hc = &http.Client{}
|
||||
}
|
||||
parsedURL, err := url.ParseRequestURI(path)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
resp, err := hc.Get(parsedURL.String())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
// response unsuccessful
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
_, err = git.NewRepoSpecFromURL(path)
|
||||
if err == nil {
|
||||
return nil, errors.Errorf("URL is a git repository")
|
||||
}
|
||||
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
}
|
||||
content, err := io.ReadAll(resp.Body)
|
||||
return content, errors.Wrap(err)
|
||||
}
|
||||
|
||||
// Cleanup runs the cleaner.
|
||||
func (fl *FileLoader) Cleanup() error {
|
||||
return fl.cleaner()
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package loader has a data loading interface and various implementations.
|
||||
package loader
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/git"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
// NewLoader returns a Loader pointed at the given target.
|
||||
// If the target is remote, the loader will be restricted
|
||||
// to the root and below only. If the target is local, the
|
||||
// loader will have the restrictions passed in. Regardless,
|
||||
// if a local target attempts to transitively load remote bases,
|
||||
// the remote bases will all be root-only restricted.
|
||||
func NewLoader(
|
||||
lr LoadRestrictorFunc,
|
||||
target string, fSys filesys.FileSystem) (ifc.Loader, error) {
|
||||
repoSpec, err := git.NewRepoSpecFromURL(target)
|
||||
if err == nil {
|
||||
// The target qualifies as a remote git target.
|
||||
return newLoaderAtGitClone(
|
||||
repoSpec, fSys, nil, git.ClonerUsingGitExec)
|
||||
}
|
||||
root, err := filesys.ConfirmDir(fSys, target)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "%s", ErrRtNotDir.Error())
|
||||
}
|
||||
return newLoaderAtConfirmedDir(
|
||||
lr, root, fSys, nil, git.ClonerUsingGitExec), nil
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package loader
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
type LoadRestrictorFunc func(
|
||||
filesys.FileSystem, filesys.ConfirmedDir, string) (string, error)
|
||||
|
||||
func RestrictionRootOnly(
|
||||
fSys filesys.FileSystem, root filesys.ConfirmedDir, path string) (string, error) {
|
||||
d, f, err := fSys.CleanedAbs(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if f == "" {
|
||||
return "", fmt.Errorf("'%s' must resolve to a file", path)
|
||||
}
|
||||
if !d.HasPrefix(root) {
|
||||
return "", fmt.Errorf(
|
||||
"security; file '%s' is not in or below '%s'",
|
||||
path, root)
|
||||
}
|
||||
return d.Join(f), nil
|
||||
}
|
||||
|
||||
func RestrictionNone(
|
||||
_ filesys.FileSystem, _ filesys.ConfirmedDir, path string) (string, error) {
|
||||
return path, nil
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Package builtinconfig provides legacy methods for
|
||||
// configuring builtin plugins from a common config file.
|
||||
// As a user, its best to configure plugins individually
|
||||
// with plugin config files specified in the `transformers:`
|
||||
// or `generators:` field, than to use this legacy
|
||||
// configuration technique.
|
||||
package builtinconfig
|
||||
Generated
Vendored
+42
@@ -0,0 +1,42 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinconfig
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
// loadDefaultConfig returns a TranformerConfig
|
||||
// object from a list of files.
|
||||
func loadDefaultConfig(
|
||||
ldr ifc.Loader, paths []string) (*TransformerConfig, error) {
|
||||
result := &TransformerConfig{}
|
||||
for _, path := range paths {
|
||||
data, err := ldr.Load(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, err := makeTransformerConfigFromBytes(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err = result.Merge(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// makeTransformerConfigFromBytes returns a TransformerConfig object from bytes
|
||||
func makeTransformerConfigFromBytes(data []byte) (*TransformerConfig, error) {
|
||||
var t TransformerConfig
|
||||
err := yaml.UnmarshalStrict(data, &t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.sortFields()
|
||||
return &t, nil
|
||||
}
|
||||
Generated
Vendored
+112
@@ -0,0 +1,112 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinconfig
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
)
|
||||
|
||||
// NameBackReferences is an association between a gvk.GVK (a ReferralTarget)
|
||||
// and a list of Referrers that could refer to it.
|
||||
//
|
||||
// It is used to handle name changes, and can be thought of as a
|
||||
// a contact list. If you change your own contact info (name,
|
||||
// phone number, etc.), you must tell your contacts or they won't
|
||||
// know about the change.
|
||||
//
|
||||
// For example, ConfigMaps can be used by Pods and everything that
|
||||
// contains a Pod; Deployment, Job, StatefulSet, etc.
|
||||
// The ConfigMap is the ReferralTarget, the others are Referrers.
|
||||
//
|
||||
// If the name of a ConfigMap instance changed from 'alice' to 'bob',
|
||||
// one must
|
||||
// - visit all objects that could refer to the ConfigMap (the Referrers)
|
||||
// - see if they mention 'alice',
|
||||
// - if so, change the Referrer's name reference to 'bob'.
|
||||
//
|
||||
// The NameBackReferences instance to aid in this could look like
|
||||
// {
|
||||
// kind: ConfigMap
|
||||
// version: v1
|
||||
// fieldSpecs:
|
||||
// - kind: Pod
|
||||
// version: v1
|
||||
// path: spec/volumes/configMap/name
|
||||
// - kind: Deployment
|
||||
// path: spec/template/spec/volumes/configMap/name
|
||||
// - kind: Job
|
||||
// path: spec/template/spec/volumes/configMap/name
|
||||
// (etc.)
|
||||
// }
|
||||
type NameBackReferences struct {
|
||||
resid.Gvk `json:",inline,omitempty" yaml:",inline,omitempty"`
|
||||
// TODO: rename json 'fieldSpecs' to 'referrers' for clarity.
|
||||
// This will, however, break anyone using a custom config.
|
||||
Referrers types.FsSlice `json:"fieldSpecs,omitempty" yaml:"fieldSpecs,omitempty"`
|
||||
|
||||
// Note: If any new pointer based members are added, DeepCopy needs to be updated
|
||||
}
|
||||
|
||||
func (n NameBackReferences) String() string {
|
||||
var r []string
|
||||
for _, f := range n.Referrers {
|
||||
r = append(r, f.String())
|
||||
}
|
||||
return n.Gvk.String() + ": (\n" +
|
||||
strings.Join(r, "\n") + "\n)"
|
||||
}
|
||||
|
||||
type nbrSlice []NameBackReferences
|
||||
|
||||
func (s nbrSlice) Len() int { return len(s) }
|
||||
func (s nbrSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s nbrSlice) Less(i, j int) bool {
|
||||
return s[i].Gvk.IsLessThan(s[j].Gvk)
|
||||
}
|
||||
|
||||
// DeepCopy returns a new copy of nbrSlice
|
||||
func (s nbrSlice) DeepCopy() nbrSlice {
|
||||
ret := make(nbrSlice, len(s))
|
||||
copy(ret, s)
|
||||
for i, slice := range ret {
|
||||
ret[i].Referrers = slice.Referrers.DeepCopy()
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (s nbrSlice) mergeAll(o nbrSlice) (result nbrSlice, err error) {
|
||||
result = s
|
||||
for _, r := range o {
|
||||
result, err = result.mergeOne(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s nbrSlice) mergeOne(other NameBackReferences) (nbrSlice, error) {
|
||||
var result nbrSlice
|
||||
var err error
|
||||
found := false
|
||||
for _, c := range s {
|
||||
if c.Gvk.Equals(other.Gvk) {
|
||||
c.Referrers, err = c.Referrers.MergeAll(other.Referrers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
found = true
|
||||
}
|
||||
result = append(result, c)
|
||||
}
|
||||
|
||||
if !found {
|
||||
result = append(result, other)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
Generated
Vendored
+202
@@ -0,0 +1,202 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinconfig
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/konfig/builtinpluginconsts"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
)
|
||||
|
||||
// TransformerConfig holds the data needed to perform transformations.
|
||||
//
|
||||
//nolint:tagalign
|
||||
type TransformerConfig struct {
|
||||
// if any fields are added, update the DeepCopy implementation
|
||||
NamePrefix types.FsSlice `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"`
|
||||
NameSuffix types.FsSlice `json:"nameSuffix,omitempty" yaml:"nameSuffix,omitempty"`
|
||||
NameSpace types.FsSlice `json:"namespace,omitempty" yaml:"namespace,omitempty"`
|
||||
CommonLabels types.FsSlice `json:"commonLabels,omitempty" yaml:"commonLabels,omitempty"`
|
||||
Labels types.FsSlice `json:"labels,omitempty" yaml:"labels,omitempty"`
|
||||
TemplateLabels types.FsSlice `json:"templateLabels,omitempty" yaml:"templateLabels,omitempty"`
|
||||
CommonAnnotations types.FsSlice `json:"commonAnnotations,omitempty" yaml:"commonAnnotations,omitempty"`
|
||||
NameReference nbrSlice `json:"nameReference,omitempty" yaml:"nameReference,omitempty"`
|
||||
VarReference types.FsSlice `json:"varReference,omitempty" yaml:"varReference,omitempty"`
|
||||
Images types.FsSlice `json:"images,omitempty" yaml:"images,omitempty"`
|
||||
Replicas types.FsSlice `json:"replicas,omitempty" yaml:"replicas,omitempty"`
|
||||
}
|
||||
|
||||
// MakeEmptyConfig returns an empty TransformerConfig object
|
||||
func MakeEmptyConfig() *TransformerConfig {
|
||||
return &TransformerConfig{}
|
||||
}
|
||||
|
||||
// DeepCopy returns a new copy of TransformerConfig
|
||||
func (t *TransformerConfig) DeepCopy() *TransformerConfig {
|
||||
return &TransformerConfig{
|
||||
NamePrefix: t.NamePrefix.DeepCopy(),
|
||||
NameSuffix: t.NameSuffix.DeepCopy(),
|
||||
NameSpace: t.NameSpace.DeepCopy(),
|
||||
CommonLabels: t.CommonLabels.DeepCopy(),
|
||||
Labels: t.Labels.DeepCopy(),
|
||||
TemplateLabels: t.TemplateLabels.DeepCopy(),
|
||||
CommonAnnotations: t.CommonAnnotations.DeepCopy(),
|
||||
NameReference: t.NameReference.DeepCopy(),
|
||||
VarReference: t.VarReference.DeepCopy(),
|
||||
Images: t.Images.DeepCopy(),
|
||||
Replicas: t.Replicas.DeepCopy(),
|
||||
}
|
||||
}
|
||||
|
||||
// the default transformer config is initialized by MakeDefaultConfig,
|
||||
// and must only be accessed via that function.
|
||||
var (
|
||||
initDefaultConfig sync.Once //nolint:gochecknoglobals
|
||||
defaultConfig *TransformerConfig //nolint:gochecknoglobals
|
||||
)
|
||||
|
||||
// MakeDefaultConfig returns a default TransformerConfig.
|
||||
func MakeDefaultConfig() *TransformerConfig {
|
||||
// parsing is expensive when having a large tree with many kustomization modules, so only do it once
|
||||
initDefaultConfig.Do(func() {
|
||||
var err error
|
||||
defaultConfig, err = makeTransformerConfigFromBytes(
|
||||
builtinpluginconsts.GetDefaultFieldSpecs())
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to make default transformconfig: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
// return a copy to avoid any mutations to protect the reference copy
|
||||
return defaultConfig.DeepCopy()
|
||||
}
|
||||
|
||||
// MakeTransformerConfig returns a merger of custom config,
|
||||
// if any, with default config.
|
||||
func MakeTransformerConfig(
|
||||
ldr ifc.Loader, paths []string) (*TransformerConfig, error) {
|
||||
t1 := MakeDefaultConfig()
|
||||
if len(paths) == 0 {
|
||||
return t1, nil
|
||||
}
|
||||
t2, err := loadDefaultConfig(ldr, paths)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return t1.Merge(t2)
|
||||
}
|
||||
|
||||
// sortFields provides determinism in logging, tests, etc.
|
||||
func (t *TransformerConfig) sortFields() {
|
||||
sort.Sort(t.NamePrefix)
|
||||
sort.Sort(t.NameSuffix)
|
||||
sort.Sort(t.NameSpace)
|
||||
sort.Sort(t.CommonLabels)
|
||||
sort.Sort(t.Labels)
|
||||
sort.Sort(t.TemplateLabels)
|
||||
sort.Sort(t.CommonAnnotations)
|
||||
sort.Sort(t.NameReference)
|
||||
sort.Sort(t.VarReference)
|
||||
sort.Sort(t.Images)
|
||||
sort.Sort(t.Replicas)
|
||||
}
|
||||
|
||||
// AddPrefixFieldSpec adds a FieldSpec to NamePrefix
|
||||
func (t *TransformerConfig) AddPrefixFieldSpec(fs types.FieldSpec) (err error) {
|
||||
t.NamePrefix, err = t.NamePrefix.MergeOne(fs)
|
||||
return err
|
||||
}
|
||||
|
||||
// AddSuffixFieldSpec adds a FieldSpec to NameSuffix
|
||||
func (t *TransformerConfig) AddSuffixFieldSpec(fs types.FieldSpec) (err error) {
|
||||
t.NameSuffix, err = t.NameSuffix.MergeOne(fs)
|
||||
return err
|
||||
}
|
||||
|
||||
// AddCommonLabelsFieldSpec adds a FieldSpec to CommonLabels
|
||||
func (t *TransformerConfig) AddCommonLabelsFieldSpec(fs types.FieldSpec) (err error) {
|
||||
t.CommonLabels, err = t.CommonLabels.MergeOne(fs)
|
||||
return err
|
||||
}
|
||||
|
||||
// AddLabelsFieldSpec adds a FieldSpec to Labels
|
||||
func (t *TransformerConfig) AddLabelsFieldSpec(fs types.FieldSpec) (err error) {
|
||||
t.Labels, err = t.Labels.MergeOne(fs)
|
||||
return err //nolint:wrapcheck
|
||||
}
|
||||
|
||||
// AddAnnotationFieldSpec adds a FieldSpec to CommonAnnotations
|
||||
func (t *TransformerConfig) AddAnnotationFieldSpec(fs types.FieldSpec) (err error) {
|
||||
t.CommonAnnotations, err = t.CommonAnnotations.MergeOne(fs)
|
||||
return err
|
||||
}
|
||||
|
||||
// AddNamereferenceFieldSpec adds a NameBackReferences to NameReference
|
||||
func (t *TransformerConfig) AddNamereferenceFieldSpec(
|
||||
nbrs NameBackReferences) (err error) {
|
||||
t.NameReference, err = t.NameReference.mergeOne(nbrs)
|
||||
return err
|
||||
}
|
||||
|
||||
// Merge merges two TransformerConfigs objects into
|
||||
// a new TransformerConfig object
|
||||
func (t *TransformerConfig) Merge(input *TransformerConfig) (
|
||||
merged *TransformerConfig, err error) {
|
||||
if input == nil {
|
||||
return t, nil
|
||||
}
|
||||
merged = &TransformerConfig{}
|
||||
merged.NamePrefix, err = t.NamePrefix.MergeAll(input.NamePrefix)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge NamePrefix fieldSpec")
|
||||
}
|
||||
merged.NameSuffix, err = t.NameSuffix.MergeAll(input.NameSuffix)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge NameSuffix fieldSpec")
|
||||
}
|
||||
merged.NameSpace, err = t.NameSpace.MergeAll(input.NameSpace)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge NameSpace fieldSpec")
|
||||
}
|
||||
merged.CommonAnnotations, err = t.CommonAnnotations.MergeAll(
|
||||
input.CommonAnnotations)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge CommonAnnotations fieldSpec")
|
||||
}
|
||||
merged.CommonLabels, err = t.CommonLabels.MergeAll(input.CommonLabels)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge CommonLabels fieldSpec")
|
||||
}
|
||||
merged.Labels, err = t.Labels.MergeAll(input.Labels)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge Labels fieldSpec")
|
||||
}
|
||||
merged.TemplateLabels, err = t.TemplateLabels.MergeAll(input.TemplateLabels)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge TemplateLabels fieldSpec")
|
||||
}
|
||||
merged.VarReference, err = t.VarReference.MergeAll(input.VarReference)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge VarReference fieldSpec")
|
||||
}
|
||||
merged.NameReference, err = t.NameReference.mergeAll(input.NameReference)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge NameReference fieldSpec")
|
||||
}
|
||||
merged.Images, err = t.Images.MergeAll(input.Images)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge Images fieldSpec")
|
||||
}
|
||||
merged.Replicas, err = t.Replicas.MergeAll(input.Replicas)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "failed to merge Replicas fieldSpec")
|
||||
}
|
||||
merged.sortFields()
|
||||
return merged, nil
|
||||
}
|
||||
Generated
Vendored
+41
@@ -0,0 +1,41 @@
|
||||
// Code generated by "stringer -type=BuiltinPluginType"; DO NOT EDIT.
|
||||
|
||||
package builtinhelpers
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[Unknown-0]
|
||||
_ = x[AnnotationsTransformer-1]
|
||||
_ = x[ConfigMapGenerator-2]
|
||||
_ = x[IAMPolicyGenerator-3]
|
||||
_ = x[HashTransformer-4]
|
||||
_ = x[ImageTagTransformer-5]
|
||||
_ = x[LabelTransformer-6]
|
||||
_ = x[NamespaceTransformer-7]
|
||||
_ = x[PatchJson6902Transformer-8]
|
||||
_ = x[PatchStrategicMergeTransformer-9]
|
||||
_ = x[PatchTransformer-10]
|
||||
_ = x[PrefixSuffixTransformer-11]
|
||||
_ = x[PrefixTransformer-12]
|
||||
_ = x[SuffixTransformer-13]
|
||||
_ = x[ReplicaCountTransformer-14]
|
||||
_ = x[SecretGenerator-15]
|
||||
_ = x[ValueAddTransformer-16]
|
||||
_ = x[HelmChartInflationGenerator-17]
|
||||
_ = x[ReplacementTransformer-18]
|
||||
}
|
||||
|
||||
const _BuiltinPluginType_name = "UnknownAnnotationsTransformerConfigMapGeneratorIAMPolicyGeneratorHashTransformerImageTagTransformerLabelTransformerNamespaceTransformerPatchJson6902TransformerPatchStrategicMergeTransformerPatchTransformerPrefixSuffixTransformerPrefixTransformerSuffixTransformerReplicaCountTransformerSecretGeneratorValueAddTransformerHelmChartInflationGeneratorReplacementTransformer"
|
||||
|
||||
var _BuiltinPluginType_index = [...]uint16{0, 7, 29, 47, 65, 80, 99, 115, 135, 159, 189, 205, 228, 245, 262, 285, 300, 319, 346, 368}
|
||||
|
||||
func (i BuiltinPluginType) String() string {
|
||||
if i < 0 || i >= BuiltinPluginType(len(_BuiltinPluginType_index)-1) {
|
||||
return "BuiltinPluginType(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _BuiltinPluginType_name[_BuiltinPluginType_index[i]:_BuiltinPluginType_index[i+1]]
|
||||
}
|
||||
+115
@@ -0,0 +1,115 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package builtinhelpers
|
||||
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/internal/builtins"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
)
|
||||
|
||||
//go:generate stringer -type=BuiltinPluginType
|
||||
type BuiltinPluginType int
|
||||
|
||||
const (
|
||||
Unknown BuiltinPluginType = iota
|
||||
AnnotationsTransformer
|
||||
ConfigMapGenerator
|
||||
IAMPolicyGenerator
|
||||
HashTransformer
|
||||
ImageTagTransformer
|
||||
LabelTransformer
|
||||
NamespaceTransformer
|
||||
PatchJson6902Transformer
|
||||
PatchStrategicMergeTransformer
|
||||
PatchTransformer
|
||||
PrefixSuffixTransformer
|
||||
PrefixTransformer
|
||||
SuffixTransformer
|
||||
ReplicaCountTransformer
|
||||
SecretGenerator
|
||||
ValueAddTransformer
|
||||
HelmChartInflationGenerator
|
||||
ReplacementTransformer
|
||||
)
|
||||
|
||||
var stringToBuiltinPluginTypeMap map[string]BuiltinPluginType
|
||||
|
||||
func init() { //nolint:gochecknoinits
|
||||
stringToBuiltinPluginTypeMap = makeStringToBuiltinPluginTypeMap()
|
||||
}
|
||||
|
||||
func makeStringToBuiltinPluginTypeMap() (result map[string]BuiltinPluginType) {
|
||||
result = make(map[string]BuiltinPluginType, 23)
|
||||
for k := range GeneratorFactories {
|
||||
result[k.String()] = k
|
||||
}
|
||||
for k := range TransformerFactories {
|
||||
result[k.String()] = k
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetBuiltinPluginType(n string) BuiltinPluginType {
|
||||
result, ok := stringToBuiltinPluginTypeMap[n]
|
||||
if ok {
|
||||
return result
|
||||
}
|
||||
return Unknown
|
||||
}
|
||||
|
||||
var GeneratorFactories = map[BuiltinPluginType]func() resmap.GeneratorPlugin{
|
||||
ConfigMapGenerator: builtins.NewConfigMapGeneratorPlugin,
|
||||
IAMPolicyGenerator: builtins.NewIAMPolicyGeneratorPlugin,
|
||||
SecretGenerator: builtins.NewSecretGeneratorPlugin,
|
||||
HelmChartInflationGenerator: builtins.NewHelmChartInflationGeneratorPlugin,
|
||||
}
|
||||
|
||||
type MultiTransformer struct {
|
||||
transformers []resmap.TransformerPlugin
|
||||
}
|
||||
|
||||
func (t *MultiTransformer) Transform(m resmap.ResMap) error {
|
||||
for _, transformer := range t.transformers {
|
||||
if err := transformer.Transform(m); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *MultiTransformer) Config(h *resmap.PluginHelpers, b []byte) error {
|
||||
for _, transformer := range t.transformers {
|
||||
if err := transformer.Config(h, b); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewMultiTransformer() resmap.TransformerPlugin {
|
||||
return &MultiTransformer{[]resmap.TransformerPlugin{
|
||||
builtins.NewPrefixTransformerPlugin(),
|
||||
builtins.NewSuffixTransformerPlugin(),
|
||||
}}
|
||||
}
|
||||
|
||||
var TransformerFactories = map[BuiltinPluginType]func() resmap.TransformerPlugin{
|
||||
AnnotationsTransformer: builtins.NewAnnotationsTransformerPlugin,
|
||||
HashTransformer: builtins.NewHashTransformerPlugin,
|
||||
ImageTagTransformer: builtins.NewImageTagTransformerPlugin,
|
||||
LabelTransformer: builtins.NewLabelTransformerPlugin,
|
||||
NamespaceTransformer: builtins.NewNamespaceTransformerPlugin,
|
||||
PatchJson6902Transformer: builtins.NewPatchJson6902TransformerPlugin,
|
||||
PatchStrategicMergeTransformer: builtins.NewPatchStrategicMergeTransformerPlugin,
|
||||
PatchTransformer: builtins.NewPatchTransformerPlugin,
|
||||
PrefixSuffixTransformer: NewMultiTransformer,
|
||||
PrefixTransformer: builtins.NewPrefixTransformerPlugin,
|
||||
SuffixTransformer: builtins.NewSuffixTransformerPlugin,
|
||||
ReplacementTransformer: builtins.NewReplacementTransformerPlugin,
|
||||
ReplicaCountTransformer: builtins.NewReplicaCountTransformerPlugin,
|
||||
ValueAddTransformer: builtins.NewValueAddTransformerPlugin,
|
||||
// Do not wired SortOrderTransformer as a builtin plugin.
|
||||
// We only want it to be available in the top-level kustomization.
|
||||
// See: https://github.com/kubernetes-sigs/kustomize/issues/3913
|
||||
}
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package execplugin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
const (
|
||||
tmpConfigFilePrefix = "kust-plugin-config-"
|
||||
maxArgStringLength = 131071
|
||||
)
|
||||
|
||||
// ExecPlugin record the name and args of an executable
|
||||
// It triggers the executable generator and transformer
|
||||
type ExecPlugin struct {
|
||||
// absolute path of the executable
|
||||
path string
|
||||
|
||||
// Optional command line arguments to the executable
|
||||
// pulled from specially named fields in cfg.
|
||||
// This is for executables that don't want to parse YAML.
|
||||
args []string
|
||||
|
||||
// Plugin configuration data.
|
||||
cfg []byte
|
||||
|
||||
// PluginHelpers
|
||||
h *resmap.PluginHelpers
|
||||
}
|
||||
|
||||
func NewExecPlugin(p string) *ExecPlugin {
|
||||
return &ExecPlugin{path: p}
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) ErrIfNotExecutable() error {
|
||||
f, err := os.Stat(p.path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// In Windows, it is not possible to determine whether a
|
||||
// file is executable through file mode.
|
||||
// TODO: provide for setting the executable FileMode bit on Windows
|
||||
// The (fs *fileStat) Mode() (m FileMode) {} function in
|
||||
// https://golang.org/src/os/types_windows.go
|
||||
// lacks the ability to set the FileMode executable bit in response
|
||||
// to file data on Windows.
|
||||
if f.Mode()&0111 == 0000 && runtime.GOOS != "windows" {
|
||||
return fmt.Errorf("unexecutable plugin at: %s", p.path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Path() string {
|
||||
return p.path
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Args() []string {
|
||||
return p.args
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Cfg() []byte {
|
||||
return p.cfg
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Config(h *resmap.PluginHelpers, config []byte) error {
|
||||
p.h = h
|
||||
p.cfg = config
|
||||
return p.processOptionalArgsFields()
|
||||
}
|
||||
|
||||
type argsConfig struct {
|
||||
ArgsOneLiner string `json:"argsOneLiner,omitempty" yaml:"argsOneLiner,omitempty"`
|
||||
ArgsFromFile string `json:"argsFromFile,omitempty" yaml:"argsFromFile,omitempty"`
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) processOptionalArgsFields() error {
|
||||
var c argsConfig
|
||||
err := yaml.Unmarshal(p.cfg, &c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if c.ArgsOneLiner != "" {
|
||||
argsTolenSlice, err := ShlexSplit(c.ArgsOneLiner)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse argsOneLiner: %w", err)
|
||||
}
|
||||
p.args = argsTolenSlice
|
||||
}
|
||||
if c.ArgsFromFile != "" {
|
||||
content, err := p.h.Loader().Load(c.ArgsFromFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, x := range strings.Split(string(content), "\n") {
|
||||
x := strings.TrimLeft(x, " ")
|
||||
if x != "" {
|
||||
p.args = append(p.args, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Generate() (resmap.ResMap, error) {
|
||||
output, err := p.invokePlugin(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rm, err := p.h.ResmapFactory().NewResMapFromBytes(output)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return utils.UpdateResourceOptions(rm)
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) Transform(rm resmap.ResMap) error {
|
||||
// add ResIds as annotations to all objects so that we can add them back
|
||||
inputRM, err := utils.GetResMapWithIDAnnotation(rm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// encode the ResMap so it can be fed to the plugin
|
||||
resources, err := inputRM.AsYaml()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// invoke the plugin with resources as the input
|
||||
output, err := p.invokePlugin(resources)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v %s", err, string(output))
|
||||
}
|
||||
|
||||
// update the original ResMap based on the output
|
||||
return utils.UpdateResMapValues(p.path, p.h, output, rm)
|
||||
}
|
||||
|
||||
// invokePlugin writes plugin config to a temp file, then
|
||||
// passes the full temp file path as the first arg to a process
|
||||
// running the plugin binary. Process output is returned.
|
||||
func (p *ExecPlugin) invokePlugin(input []byte) ([]byte, error) {
|
||||
f, err := os.CreateTemp("", tmpConfigFilePrefix)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(
|
||||
err, "creating tmp plugin config file")
|
||||
}
|
||||
_, err = f.Write(p.cfg)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(
|
||||
err, "writing plugin config to %s", f.Name())
|
||||
}
|
||||
err = f.Close()
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(
|
||||
err, "closing plugin config file %s", f.Name())
|
||||
}
|
||||
cmd := exec.Command(
|
||||
p.path, append([]string{f.Name()}, p.args...)...)
|
||||
cmd.Env = p.getEnv()
|
||||
cmd.Stdin = bytes.NewReader(input)
|
||||
var stdErr bytes.Buffer
|
||||
cmd.Stderr = &stdErr
|
||||
if _, err := os.Stat(p.h.Loader().Root()); err == nil {
|
||||
cmd.Dir = p.h.Loader().Root()
|
||||
}
|
||||
result, err := cmd.Output()
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(
|
||||
fmt.Errorf("failure in plugin configured via %s; %w", f.Name(), err),
|
||||
"%s", stdErr.String())
|
||||
}
|
||||
return result, os.Remove(f.Name())
|
||||
}
|
||||
|
||||
func (p *ExecPlugin) getEnv() []string {
|
||||
env := os.Environ()
|
||||
pluginConfigString := "KUSTOMIZE_PLUGIN_CONFIG_STRING=" + string(p.cfg)
|
||||
if len(pluginConfigString) <= maxArgStringLength {
|
||||
env = append(env, pluginConfigString)
|
||||
} else {
|
||||
log.Printf("KUSTOMIZE_PLUGIN_CONFIG_STRING exceeds hard limit of %d characters, the environment variable "+
|
||||
"will be omitted", maxArgStringLength)
|
||||
}
|
||||
pluginConfigRoot := "KUSTOMIZE_PLUGIN_CONFIG_ROOT=" + p.h.Loader().Root()
|
||||
if len(pluginConfigRoot) <= maxArgStringLength {
|
||||
env = append(env, pluginConfigRoot)
|
||||
} else {
|
||||
log.Printf("KUSTOMIZE_PLUGIN_CONFIG_ROOT exceeds hard limit of %d characters, the environment variable "+
|
||||
"will be omitted", maxArgStringLength)
|
||||
}
|
||||
return env
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package execplugin
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// ShlexSplit splits a string into a slice of strings using shell-style rules for quoting and commenting
|
||||
// Similar to Python's shlex.split with comments enabled
|
||||
func ShlexSplit(s string) ([]string, error) {
|
||||
return shlexSplit(s)
|
||||
}
|
||||
|
||||
func shlexSplit(s string) ([]string, error) {
|
||||
result := []string{}
|
||||
|
||||
// noQuote is used to track if we are not in a quoted
|
||||
const noQuote = 0
|
||||
|
||||
var current strings.Builder
|
||||
var quote rune = noQuote
|
||||
var escaped bool
|
||||
|
||||
for _, r := range s {
|
||||
switch {
|
||||
case escaped:
|
||||
current.WriteRune(r)
|
||||
escaped = false
|
||||
case r == '\\' && quote != '\'':
|
||||
escaped = true
|
||||
case (r == '\'' || r == '"') && quote == noQuote:
|
||||
quote = r
|
||||
case r == quote:
|
||||
quote = noQuote
|
||||
case r == '#' && quote == noQuote:
|
||||
// Comment starts, ignore the rest of the line
|
||||
if current.Len() > 0 {
|
||||
result = append(result, current.String())
|
||||
}
|
||||
return result, nil
|
||||
case unicode.IsSpace(r) && quote == noQuote:
|
||||
if current.Len() > 0 {
|
||||
result = append(result, current.String())
|
||||
current.Reset()
|
||||
}
|
||||
default:
|
||||
current.WriteRune(r)
|
||||
}
|
||||
}
|
||||
|
||||
if quote != noQuote {
|
||||
return nil, fmt.Errorf("unclosed quote in string")
|
||||
}
|
||||
if current.Len() > 0 {
|
||||
result = append(result, current.String())
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
+201
@@ -0,0 +1,201 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package fnplugin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
|
||||
"sigs.k8s.io/kustomize/kyaml/runfn"
|
||||
"sigs.k8s.io/kustomize/kyaml/yaml"
|
||||
)
|
||||
|
||||
// FnPlugin is the struct to hold function information
|
||||
type FnPlugin struct {
|
||||
// Function runner
|
||||
runFns runfn.RunFns
|
||||
|
||||
// Plugin configuration data.
|
||||
cfg []byte
|
||||
|
||||
// Plugin name cache for error output
|
||||
pluginName string
|
||||
|
||||
// PluginHelpers
|
||||
h *resmap.PluginHelpers
|
||||
}
|
||||
|
||||
func bytesToRNode(yml []byte) (*yaml.RNode, error) {
|
||||
rnode, err := yaml.Parse(string(yml))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rnode, nil
|
||||
}
|
||||
|
||||
func resourceToRNode(res *resource.Resource) (*yaml.RNode, error) {
|
||||
yml, err := res.AsYAML()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bytesToRNode(yml)
|
||||
}
|
||||
|
||||
// GetFunctionSpec return function spec is there is. Otherwise return nil
|
||||
func GetFunctionSpec(res *resource.Resource) (*runtimeutil.FunctionSpec, error) {
|
||||
rnode, err := resourceToRNode(res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not convert resource to RNode: %w", err)
|
||||
}
|
||||
functionSpec, err := runtimeutil.GetFunctionSpec(rnode)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get FunctionSpec: %w", err)
|
||||
}
|
||||
return functionSpec, nil
|
||||
}
|
||||
|
||||
func toStorageMounts(mounts []string) []runtimeutil.StorageMount {
|
||||
var sms []runtimeutil.StorageMount
|
||||
for _, mount := range mounts {
|
||||
sms = append(sms, runtimeutil.StringToStorageMount(mount))
|
||||
}
|
||||
return sms
|
||||
}
|
||||
|
||||
// NewFnPlugin creates a FnPlugin struct
|
||||
func NewFnPlugin(o *types.FnPluginLoadingOptions) *FnPlugin {
|
||||
return &FnPlugin{
|
||||
runFns: runfn.RunFns{
|
||||
Functions: []*yaml.RNode{},
|
||||
Network: o.Network,
|
||||
EnableExec: o.EnableExec,
|
||||
StorageMounts: toStorageMounts(o.Mounts),
|
||||
Env: o.Env,
|
||||
AsCurrentUser: o.AsCurrentUser,
|
||||
WorkingDir: o.WorkingDir,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Cfg returns function config
|
||||
func (p *FnPlugin) Cfg() []byte {
|
||||
return p.cfg
|
||||
}
|
||||
|
||||
// Config is called by kustomize to pass-in config information
|
||||
func (p *FnPlugin) Config(h *resmap.PluginHelpers, config []byte) error {
|
||||
p.h = h
|
||||
p.cfg = config
|
||||
|
||||
fn, err := bytesToRNode(p.cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
meta, err := fn.GetMeta()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.pluginName = fmt.Sprintf("api: %s, kind: %s, name: %s",
|
||||
meta.APIVersion, meta.Kind, meta.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Generate is called when run as generator
|
||||
func (p *FnPlugin) Generate() (resmap.ResMap, error) {
|
||||
output, err := p.invokePlugin(nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rm, err := p.h.ResmapFactory().NewResMapFromBytes(output)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return utils.UpdateResourceOptions(rm)
|
||||
}
|
||||
|
||||
// Transform is called when run as transformer
|
||||
func (p *FnPlugin) Transform(rm resmap.ResMap) error {
|
||||
// add ResIds as annotations to all objects so that we can add them back
|
||||
inputRM, err := utils.GetResMapWithIDAnnotation(rm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// encode the ResMap so it can be fed to the plugin
|
||||
resources, err := inputRM.AsYaml()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// invoke the plugin with resources as the input
|
||||
output, err := p.invokePlugin(resources)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%v %s", err, string(output))
|
||||
}
|
||||
|
||||
// update the original ResMap based on the output
|
||||
return utils.UpdateResMapValues(p.pluginName, p.h, output, rm)
|
||||
}
|
||||
|
||||
func injectAnnotation(input *yaml.RNode, k, v string) error {
|
||||
err := input.PipeE(yaml.SetAnnotation(k, v))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// invokePlugin uses Function runner to run function as plugin
|
||||
func (p *FnPlugin) invokePlugin(input []byte) ([]byte, error) {
|
||||
// get function config rnode
|
||||
functionConfig, err := bytesToRNode(p.cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// This annotation will let kustomize ingnore this item in output
|
||||
err = injectAnnotation(functionConfig, "config.kubernetes.io/local-config", "true")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// we need to add config as input for generators. Some of them don't work with FunctionConfig
|
||||
// and in addition kio.Pipeline won't create anything if there are no objects
|
||||
// see https://github.com/kubernetes-sigs/kustomize/blob/master/kyaml/kio/kio.go#L93
|
||||
// Since we added `local-config` annotation so it will be ignored in generator output
|
||||
// TODO(donnyxia): This is actually not used by generator and only used to bypass a kio limitation.
|
||||
// Need better solution.
|
||||
if input == nil {
|
||||
yml, err := functionConfig.String()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
input = []byte(yml)
|
||||
}
|
||||
|
||||
// Configure and Execute Fn. We don't need to convert resources to ResourceList here
|
||||
// because function runtime will do that. See kyaml/fn/runtime/runtimeutil/runtimeutil.go
|
||||
var ouputBuffer bytes.Buffer
|
||||
p.runFns.Input = bytes.NewReader(input)
|
||||
p.runFns.Functions = append(p.runFns.Functions, functionConfig)
|
||||
p.runFns.Output = &ouputBuffer
|
||||
|
||||
err = p.runFns.Execute()
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(
|
||||
err, "couldn't execute function")
|
||||
}
|
||||
|
||||
return ouputBuffer.Bytes(), nil
|
||||
}
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
// Copyright 2024 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//go:build !kustomize_disable_go_plugin_support
|
||||
|
||||
package loader
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"plugin"
|
||||
"reflect"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/utils"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
)
|
||||
|
||||
// registry is a means to avoid trying to load the same .so file
|
||||
// into memory more than once, which results in an error.
|
||||
// Each test makes its own loader, and tries to load its own plugins,
|
||||
// but the loaded .so files are in shared memory, so one will get
|
||||
// "this plugin already loaded" errors if the registry is maintained
|
||||
// as a Loader instance variable. So make it a package variable.
|
||||
var registry = make(map[string]resmap.Configurable) //nolint:gochecknoglobals
|
||||
|
||||
func copyPlugin(c resmap.Configurable) resmap.Configurable {
|
||||
indirect := reflect.Indirect(reflect.ValueOf(c))
|
||||
newIndirect := reflect.New(indirect.Type())
|
||||
newIndirect.Elem().Set(reflect.ValueOf(indirect.Interface()))
|
||||
newNamed := newIndirect.Interface()
|
||||
return newNamed.(resmap.Configurable) //nolint:forcetypeassert
|
||||
}
|
||||
|
||||
func (l *Loader) loadGoPlugin(id resid.ResId, absPath string) (resmap.Configurable, error) {
|
||||
regId := relativePluginPath(id)
|
||||
if c, ok := registry[regId]; ok {
|
||||
return copyPlugin(c), nil
|
||||
}
|
||||
if !utils.FileExists(absPath) {
|
||||
return nil, fmt.Errorf(
|
||||
"expected file with Go object code at: %s", absPath)
|
||||
}
|
||||
log.Printf("Attempting plugin load from %q", absPath)
|
||||
p, err := plugin.Open(absPath)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "plugin %s fails to load", absPath)
|
||||
}
|
||||
symbol, err := p.Lookup(konfig.PluginSymbol)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(
|
||||
err, "plugin %s doesn't have symbol %s",
|
||||
regId, konfig.PluginSymbol)
|
||||
}
|
||||
c, ok := symbol.(resmap.Configurable)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %q not configurable", regId)
|
||||
}
|
||||
registry[regId] = c
|
||||
return copyPlugin(c), nil
|
||||
}
|
||||
Generated
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// Copyright 2024 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// The build tag "kustomize_disable_go_plugin_support" is used to deactivate the
|
||||
// kustomize API's dependency on the "plugins" package. This is beneficial for
|
||||
// applications that need to embed it but do not have requirements for dynamic
|
||||
// Go plugins.
|
||||
// Including plugins as a dependency can lead to an increase in binary size due
|
||||
// to the population of ELF's sections such as .dynsym and .dynstr.
|
||||
// By utilizing this flag, applications have the flexibility to exclude the
|
||||
// import if they do not require support for dynamic Go plugins.
|
||||
//go:build kustomize_disable_go_plugin_support
|
||||
|
||||
package loader
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
)
|
||||
|
||||
func (l *Loader) loadGoPlugin(_ resid.ResId, _ string) (resmap.Configurable, error) {
|
||||
return nil, fmt.Errorf("plugin load is disabled")
|
||||
}
|
||||
+290
@@ -0,0 +1,290 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package loader
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/builtinhelpers"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/execplugin"
|
||||
"sigs.k8s.io/kustomize/api/internal/plugins/fnplugin"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
"sigs.k8s.io/kustomize/kyaml/resid"
|
||||
)
|
||||
|
||||
// Loader loads plugins using a file loader (a different loader).
|
||||
type Loader struct {
|
||||
pc *types.PluginConfig
|
||||
rf *resmap.Factory
|
||||
fs filesys.FileSystem
|
||||
|
||||
// absolutePluginHome caches the location of a valid plugin root directory.
|
||||
// It should only be set once the directory's existence has been confirmed.
|
||||
absolutePluginHome string
|
||||
}
|
||||
|
||||
func NewLoader(
|
||||
pc *types.PluginConfig, rf *resmap.Factory, fs filesys.FileSystem,
|
||||
) *Loader {
|
||||
return &Loader{pc: pc, rf: rf, fs: fs}
|
||||
}
|
||||
|
||||
// LoaderWithWorkingDir returns loader after setting its working directory.
|
||||
// NOTE: This is not really a new loader since some of the Loader struct fields are pointers.
|
||||
func (l *Loader) LoaderWithWorkingDir(wd string) *Loader {
|
||||
lpc := &types.PluginConfig{
|
||||
PluginRestrictions: l.pc.PluginRestrictions,
|
||||
BpLoadingOptions: l.pc.BpLoadingOptions,
|
||||
FnpLoadingOptions: l.pc.FnpLoadingOptions,
|
||||
HelmConfig: l.pc.HelmConfig,
|
||||
}
|
||||
lpc.FnpLoadingOptions.WorkingDir = wd
|
||||
return &Loader{pc: lpc, rf: l.rf, fs: l.fs}
|
||||
}
|
||||
|
||||
// Config provides the global (not plugin specific) PluginConfig data.
|
||||
func (l *Loader) Config() *types.PluginConfig {
|
||||
return l.pc
|
||||
}
|
||||
|
||||
func (l *Loader) LoadGenerators(
|
||||
ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap) (
|
||||
result []*resmap.GeneratorWithProperties, err error,
|
||||
) {
|
||||
for _, res := range rm.Resources() {
|
||||
g, err := l.LoadGenerator(ldr, v, res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load generator: %w", err)
|
||||
}
|
||||
generatorOrigin, err := resource.OriginFromCustomPlugin(res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get origin from CustomPlugin: %w", err)
|
||||
}
|
||||
result = append(result, &resmap.GeneratorWithProperties{Generator: g, Origin: generatorOrigin})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *Loader) LoadGenerator(
|
||||
ldr ifc.Loader, v ifc.Validator, res *resource.Resource,
|
||||
) (resmap.Generator, error) {
|
||||
c, err := l.loadAndConfigurePlugin(ldr, v, res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g, ok := c.(resmap.Generator)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not a generator", res.OrgId())
|
||||
}
|
||||
return g, nil
|
||||
}
|
||||
|
||||
func (l *Loader) LoadTransformers(
|
||||
ldr ifc.Loader, v ifc.Validator, rm resmap.ResMap,
|
||||
) ([]*resmap.TransformerWithProperties, error) {
|
||||
var result []*resmap.TransformerWithProperties
|
||||
for _, res := range rm.Resources() {
|
||||
t, err := l.LoadTransformer(ldr, v, res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transformerOrigin, err := resource.OriginFromCustomPlugin(res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, &resmap.TransformerWithProperties{Transformer: t, Origin: transformerOrigin})
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (l *Loader) LoadTransformer(
|
||||
ldr ifc.Loader, v ifc.Validator, res *resource.Resource,
|
||||
) (*resmap.TransformerWithProperties, error) {
|
||||
c, err := l.loadAndConfigurePlugin(ldr, v, res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t, ok := c.(resmap.Transformer)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("plugin %s not a transformer", res.OrgId())
|
||||
}
|
||||
return &resmap.TransformerWithProperties{Transformer: t}, nil
|
||||
}
|
||||
|
||||
func relativePluginPath(id resid.ResId) string {
|
||||
return filepath.Join(
|
||||
id.Group,
|
||||
id.Version,
|
||||
strings.ToLower(id.Kind))
|
||||
}
|
||||
|
||||
func (l *Loader) AbsolutePluginPath(id resid.ResId) (string, error) {
|
||||
pluginHome, err := l.absPluginHome()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(pluginHome, relativePluginPath(id), id.Kind), nil
|
||||
}
|
||||
|
||||
// absPluginHome is the home of kustomize Exec and Go plugins.
|
||||
// Kustomize plugin configuration files are k8s-style objects
|
||||
// containing the fields 'apiVersion' and 'kind', e.g.
|
||||
//
|
||||
// apiVersion: apps/v1
|
||||
// kind: Deployment
|
||||
//
|
||||
// kustomize reads plugin configuration data from a file path
|
||||
// specified in the 'generators:' or 'transformers:' field of a
|
||||
// kustomization file. For Exec and Go plugins, kustomize
|
||||
// uses this data to both locate the plugin and configure it.
|
||||
// Each Exec or Go plugin (its code, its tests, its supporting data
|
||||
// files, etc.) must be housed in its own directory at
|
||||
//
|
||||
// ${absPluginHome}/${pluginApiVersion}/LOWERCASE(${pluginKind})
|
||||
//
|
||||
// where
|
||||
// - ${absPluginHome} is an absolute path, defined below.
|
||||
// - ${pluginApiVersion} is taken from the plugin config file.
|
||||
// - ${pluginKind} is taken from the plugin config file.
|
||||
func (l *Loader) absPluginHome() (string, error) {
|
||||
// External plugins are disabled--return the dummy plugin root.
|
||||
if l.pc.PluginRestrictions != types.PluginRestrictionsNone {
|
||||
return konfig.NoPluginHomeSentinal, nil
|
||||
}
|
||||
// We've already determined plugin home--use the cached value.
|
||||
if l.absolutePluginHome != "" {
|
||||
return l.absolutePluginHome, nil
|
||||
}
|
||||
|
||||
// Check default locations for a valid plugin root, and cache it if found.
|
||||
dir, err := konfig.DefaultAbsPluginHome(l.fs)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
l.absolutePluginHome = dir
|
||||
return l.absolutePluginHome, nil
|
||||
}
|
||||
|
||||
func isBuiltinPlugin(res *resource.Resource) bool {
|
||||
// TODO: the special string should appear in Group, not Version.
|
||||
return res.GetGvk().Group == "" &&
|
||||
res.GetGvk().Version == konfig.BuiltinPluginApiVersion
|
||||
}
|
||||
|
||||
func (l *Loader) loadAndConfigurePlugin(
|
||||
ldr ifc.Loader,
|
||||
v ifc.Validator,
|
||||
res *resource.Resource,
|
||||
) (c resmap.Configurable, err error) {
|
||||
if isBuiltinPlugin(res) {
|
||||
switch l.pc.BpLoadingOptions {
|
||||
case types.BploLoadFromFileSys:
|
||||
c, err = l.loadPlugin(res)
|
||||
case types.BploUseStaticallyLinked:
|
||||
// Instead of looking for and loading a .so file,
|
||||
// instantiate the plugin from a generated factory
|
||||
// function (see "pluginator"). Being able to do this
|
||||
// is what makes a plugin "builtin".
|
||||
c, err = l.makeBuiltinPlugin(res.GetGvk())
|
||||
default:
|
||||
err = fmt.Errorf(
|
||||
"unknown plugin loader behavior specified: %s %v", res.GetGvk().String(),
|
||||
l.pc.BpLoadingOptions)
|
||||
}
|
||||
} else {
|
||||
switch l.pc.PluginRestrictions {
|
||||
case types.PluginRestrictionsNone:
|
||||
c, err = l.loadPlugin(res)
|
||||
case types.PluginRestrictionsBuiltinsOnly:
|
||||
err = types.NewErrOnlyBuiltinPluginsAllowed(res.OrgId().Kind)
|
||||
default:
|
||||
err = fmt.Errorf(
|
||||
"unknown plugin restriction specified: %v",
|
||||
l.pc.PluginRestrictions)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
yaml, err := res.AsYAML()
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(err, "marshalling yaml from res %s", res.OrgId())
|
||||
}
|
||||
err = c.Config(resmap.NewPluginHelpers(ldr, v, l.rf, l.pc), yaml)
|
||||
if err != nil {
|
||||
return nil, errors.WrapPrefixf(
|
||||
err, "plugin %s fails configuration", res.OrgId())
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (l *Loader) makeBuiltinPlugin(r resid.Gvk) (resmap.Configurable, error) {
|
||||
bpt := builtinhelpers.GetBuiltinPluginType(r.Kind)
|
||||
if f, ok := builtinhelpers.GeneratorFactories[bpt]; ok {
|
||||
return f(), nil
|
||||
}
|
||||
if f, ok := builtinhelpers.TransformerFactories[bpt]; ok {
|
||||
return f(), nil
|
||||
}
|
||||
return nil, errors.Errorf("unable to load builtin %s", r)
|
||||
}
|
||||
|
||||
func (l *Loader) loadPlugin(res *resource.Resource) (resmap.Configurable, error) {
|
||||
spec, err := fnplugin.GetFunctionSpec(res)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loader: %w", err)
|
||||
}
|
||||
if spec != nil {
|
||||
// validation check that function mounts are under the current kustomization directory
|
||||
for _, mount := range spec.Container.StorageMounts {
|
||||
if filepath.IsAbs(mount.Src) {
|
||||
return nil, errors.Errorf("plugin %s with mount path '%s' is not permitted; "+
|
||||
"mount paths must be relative to the current kustomization directory", res.OrgId(), mount.Src)
|
||||
}
|
||||
if strings.HasPrefix(filepath.Clean(mount.Src), "..") {
|
||||
return nil, errors.Errorf("plugin %s with mount path '%s' is not permitted; "+
|
||||
"mount paths must be under the current kustomization directory", res.OrgId(), mount.Src)
|
||||
}
|
||||
}
|
||||
return fnplugin.NewFnPlugin(&l.pc.FnpLoadingOptions), nil
|
||||
}
|
||||
return l.loadExecOrGoPlugin(res.OrgId())
|
||||
}
|
||||
|
||||
func (l *Loader) loadExecOrGoPlugin(resId resid.ResId) (resmap.Configurable, error) {
|
||||
absPluginPath, err := l.AbsolutePluginPath(resId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// First try to load the plugin as an executable.
|
||||
p := execplugin.NewExecPlugin(absPluginPath)
|
||||
if err = p.ErrIfNotExecutable(); err == nil {
|
||||
return p, nil
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
// The file exists, but something else is wrong,
|
||||
// likely it's not executable.
|
||||
// Assume the user forgot to set the exec bit,
|
||||
// and return an error, rather than adding ".so"
|
||||
// to the name and attempting to load it as a Go
|
||||
// plugin, which will likely fail and result
|
||||
// in an obscure message.
|
||||
return nil, err
|
||||
}
|
||||
// Failing the above, try loading it as a Go plugin.
|
||||
c, err := l.loadGoPlugin(resId, absPluginPath+".so")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
// Copyright 2020 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
"sigs.k8s.io/kustomize/api/resmap"
|
||||
"sigs.k8s.io/kustomize/api/resource"
|
||||
"sigs.k8s.io/kustomize/api/types"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
const (
|
||||
idAnnotation = "kustomize.config.k8s.io/id"
|
||||
HashAnnotation = "kustomize.config.k8s.io/needs-hash"
|
||||
BehaviorAnnotation = "kustomize.config.k8s.io/behavior"
|
||||
)
|
||||
|
||||
func GoBin() string {
|
||||
return filepath.Join(runtime.GOROOT(), "bin", "go")
|
||||
}
|
||||
|
||||
// DeterminePluginSrcRoot guesses where the user
|
||||
// has her ${g}/${v}/$lower(${k})/${k}.go files.
|
||||
func DeterminePluginSrcRoot(fSys filesys.FileSystem) (string, error) {
|
||||
return konfig.FirstDirThatExistsElseError(
|
||||
"plugin src root", fSys, []konfig.NotedFunc{
|
||||
{
|
||||
Note: "relative to unit test",
|
||||
F: func() string {
|
||||
return filepath.Clean(
|
||||
filepath.Join(
|
||||
os.Getenv("PWD"),
|
||||
"..", "..",
|
||||
konfig.RelPluginHome))
|
||||
},
|
||||
},
|
||||
{
|
||||
Note: "relative to unit test (internal pkg)",
|
||||
F: func() string {
|
||||
return filepath.Clean(
|
||||
filepath.Join(
|
||||
os.Getenv("PWD"),
|
||||
"..", "..", "..", "..",
|
||||
konfig.RelPluginHome))
|
||||
},
|
||||
},
|
||||
{
|
||||
Note: "relative to api package",
|
||||
F: func() string {
|
||||
return filepath.Clean(
|
||||
filepath.Join(
|
||||
os.Getenv("PWD"),
|
||||
"..", "..", "..",
|
||||
konfig.RelPluginHome))
|
||||
},
|
||||
},
|
||||
{
|
||||
Note: "old style $GOPATH",
|
||||
F: func() string {
|
||||
return filepath.Join(
|
||||
os.Getenv("GOPATH"),
|
||||
"src", konfig.DomainName,
|
||||
konfig.ProgramName, konfig.RelPluginHome)
|
||||
},
|
||||
},
|
||||
{
|
||||
Note: "HOME with literal 'gopath'",
|
||||
F: func() string {
|
||||
return filepath.Join(
|
||||
konfig.HomeDir(), "gopath",
|
||||
"src", konfig.DomainName,
|
||||
konfig.ProgramName, konfig.RelPluginHome)
|
||||
},
|
||||
},
|
||||
{
|
||||
Note: "home directory",
|
||||
F: func() string {
|
||||
return filepath.Join(
|
||||
konfig.HomeDir(), konfig.DomainName,
|
||||
konfig.ProgramName, konfig.RelPluginHome)
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// FileYoungerThan returns true if the file both exists and has an
|
||||
// age is <= the Duration argument.
|
||||
func FileYoungerThan(path string, d time.Duration) bool {
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return time.Since(fi.ModTime()) <= d
|
||||
}
|
||||
|
||||
// FileModifiedAfter returns true if the file both exists and was
|
||||
// modified after the given time..
|
||||
func FileModifiedAfter(path string, t time.Time) bool {
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return fi.ModTime().After(t)
|
||||
}
|
||||
|
||||
func FileExists(path string) bool {
|
||||
if _, err := os.Stat(path); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GetResMapWithIDAnnotation returns a new copy of the given ResMap with the ResIds annotated in each Resource
|
||||
func GetResMapWithIDAnnotation(rm resmap.ResMap) (resmap.ResMap, error) {
|
||||
inputRM := rm.DeepCopy()
|
||||
for _, r := range inputRM.Resources() {
|
||||
idString, err := yaml.Marshal(r.CurId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
annotations := r.GetAnnotations()
|
||||
annotations[idAnnotation] = string(idString)
|
||||
if err = r.SetAnnotations(annotations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return inputRM, nil
|
||||
}
|
||||
|
||||
// UpdateResMapValues updates the Resource value in the given ResMap
|
||||
// with the emitted Resource values in output.
|
||||
func UpdateResMapValues(pluginName string, h *resmap.PluginHelpers, output []byte, rm resmap.ResMap) error {
|
||||
mapFactory := h.ResmapFactory()
|
||||
resFactory := mapFactory.RF()
|
||||
resources, err := resFactory.SliceFromBytes(output)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Don't use resources here, or error message will be unfriendly to plugin builders
|
||||
newMap, err := mapFactory.NewResMapFromBytes([]byte{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, r := range resources {
|
||||
// stale--not manipulated by plugin transformers
|
||||
if err = removeIDAnnotation(r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add to the new map, checking for duplicates
|
||||
if err := newMap.Append(r); err != nil {
|
||||
prettyID, err := json.Marshal(r.CurId())
|
||||
if err != nil {
|
||||
prettyID = []byte(r.CurId().String())
|
||||
}
|
||||
return fmt.Errorf("plugin %s generated duplicate resource: %s", pluginName, prettyID)
|
||||
}
|
||||
|
||||
// Add to or update the old map
|
||||
oldIdx, err := rm.GetIndexOfCurrentId(r.CurId())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if oldIdx != -1 {
|
||||
rm.GetByIndex(oldIdx).ResetRNode(r)
|
||||
} else {
|
||||
if err := rm.Append(r); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove items the transformer deleted from the old map
|
||||
for _, id := range rm.AllIds() {
|
||||
newIdx, _ := newMap.GetIndexOfCurrentId(id)
|
||||
if newIdx == -1 {
|
||||
if err = rm.Remove(id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func removeIDAnnotation(r *resource.Resource) error {
|
||||
// remove the annotation set by Kustomize to track the resource
|
||||
annotations := r.GetAnnotations()
|
||||
delete(annotations, idAnnotation)
|
||||
return r.SetAnnotations(annotations)
|
||||
}
|
||||
|
||||
// UpdateResourceOptions updates the generator options for each resource in the
|
||||
// given ResMap based on plugin provided annotations.
|
||||
func UpdateResourceOptions(rm resmap.ResMap) (resmap.ResMap, error) {
|
||||
for _, r := range rm.Resources() {
|
||||
// Disable name hashing by default and require plugin to explicitly
|
||||
// request it for each resource.
|
||||
annotations := r.GetAnnotations()
|
||||
behavior := annotations[BehaviorAnnotation]
|
||||
var needsHash bool
|
||||
if val, ok := annotations[HashAnnotation]; ok {
|
||||
b, err := strconv.ParseBool(val)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf(
|
||||
"the annotation %q contains an invalid value (%q)",
|
||||
HashAnnotation, val)
|
||||
}
|
||||
needsHash = b
|
||||
}
|
||||
delete(annotations, HashAnnotation)
|
||||
delete(annotations, BehaviorAnnotation)
|
||||
if err := r.SetAnnotations(annotations); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if needsHash {
|
||||
r.EnableHashSuffix()
|
||||
}
|
||||
r.SetBehavior(types.NewGenerationBehavior(behavior))
|
||||
}
|
||||
return rm, nil
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user