2020-04-01 09:55:42 -05:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
|
|
|
|
https://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.
|
|
|
|
*/
|
|
|
|
|
2019-09-30 12:05:34 -07:00
|
|
|
package document
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2020-03-27 11:49:24 -05:00
|
|
|
"strings"
|
|
|
|
|
2021-02-08 22:14:57 -06:00
|
|
|
kustfs "sigs.k8s.io/kustomize/api/filesys"
|
2021-04-28 19:22:43 +00:00
|
|
|
"sigs.k8s.io/kustomize/api/hasher"
|
2020-03-27 11:49:24 -05:00
|
|
|
"sigs.k8s.io/kustomize/api/krusty"
|
|
|
|
"sigs.k8s.io/kustomize/api/resmap"
|
2020-07-01 14:04:54 -05:00
|
|
|
"sigs.k8s.io/kustomize/api/resource"
|
2020-03-27 11:49:24 -05:00
|
|
|
"sigs.k8s.io/kustomize/api/types"
|
2019-09-30 12:05:34 -07:00
|
|
|
|
2020-11-16 19:24:06 -06:00
|
|
|
"opendev.org/airship/airshipctl/pkg/fs"
|
2019-10-11 17:13:59 -05:00
|
|
|
utilyaml "opendev.org/airship/airshipctl/pkg/util/yaml"
|
2019-09-30 12:05:34 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// KustomizeBuildOptions contain the options for running a Kustomize build on a bundle
|
|
|
|
type KustomizeBuildOptions struct {
|
|
|
|
KustomizationPath string
|
2020-03-27 11:49:24 -05:00
|
|
|
LoadRestrictions types.LoadRestrictions
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// BundleFactory contains the objects within a bundle
|
|
|
|
type BundleFactory struct {
|
|
|
|
KustomizeBuildOptions
|
|
|
|
resmap.ResMap
|
2020-11-16 19:24:06 -06:00
|
|
|
fs.FileSystem
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bundle interface provides the specification for a bundle implementation
|
|
|
|
type Bundle interface {
|
|
|
|
Write(out io.Writer) error
|
2020-11-16 19:24:06 -06:00
|
|
|
SetFileSystem(fs.FileSystem) error
|
|
|
|
GetFileSystem() fs.FileSystem
|
2020-02-21 23:14:27 +00:00
|
|
|
Select(selector Selector) ([]Document, error)
|
2020-03-27 14:09:32 -07:00
|
|
|
SelectOne(selector Selector) (Document, error)
|
2020-02-19 12:10:38 +04:00
|
|
|
SelectBundle(selector Selector) (Bundle, error)
|
2020-01-24 12:56:59 +04:00
|
|
|
SelectByFieldValue(string, func(interface{}) bool) (Bundle, error)
|
2019-09-30 12:05:34 -07:00
|
|
|
GetByGvk(string, string, string) ([]Document, error)
|
|
|
|
GetByName(string) (Document, error)
|
2020-02-21 23:14:27 +00:00
|
|
|
GetByAnnotation(annotationSelector string) ([]Document, error)
|
|
|
|
GetByLabel(labelSelector string) ([]Document, error)
|
2019-09-30 12:05:34 -07:00
|
|
|
GetAllDocuments() ([]Document, error)
|
2020-07-01 14:04:54 -05:00
|
|
|
Append(Document) error
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:32:31 -05:00
|
|
|
// DocFactoryFunc is a type of function which returns (Document, error) and can be used on demand
|
|
|
|
type DocFactoryFunc func() (Document, error)
|
|
|
|
|
2020-09-21 12:46:23 -05:00
|
|
|
// BundleFactoryFunc is a function that returns bundle, can be used to build bundle on demand
|
|
|
|
// instead of inplace, useful, when you don't know if bundle will be needed or not, see phase for detail
|
|
|
|
type BundleFactoryFunc func() (Bundle, error)
|
|
|
|
|
2021-03-15 18:32:31 -05:00
|
|
|
// BundleFactoryFromBytes is a function which returns BundleFactoryFunc based on new bundle from bytes
|
|
|
|
func BundleFactoryFromBytes(data []byte) BundleFactoryFunc {
|
|
|
|
return func() (Bundle, error) {
|
|
|
|
return NewBundleFromBytes(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BundleFactoryFromDocRoot is a function which returns BundleFactoryFunc based on new bundle from DocumentRoot path
|
|
|
|
func BundleFactoryFromDocRoot(docRootFunc func() (string, error)) BundleFactoryFunc {
|
|
|
|
return func() (Bundle, error) {
|
|
|
|
path, err := docRootFunc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewBundleByPath(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 22:26:54 -05:00
|
|
|
// NewBundleByPath is a function which builds new document.Bundle from kustomize rootPath using default FS object
|
|
|
|
// example: document.NewBundleByPath("path/to/phase-root")
|
Split document model, add entrypoints for repos
Add NewBundleByPath function, that would return bundle built from
the specified path argument
Add CurrentContextEntryPoint method of the config
object, that would allow easily get kustomize root path based on
clusterType and phase. You can also leave phase arg empty string,
which would try to return bundle for all phases
Introduce changes to config pakage objects:
- Manifest:
SubPath: this is relative path to the root of the repository that
contains directories with sites (SiteNames)
PrimaryRepositoryName: this is a string that must correspond to a key
of the Repositories map of manifest object, which is used to derive
primary repository
Repositories object is a map, map keys correspond to names of the
directories where `document pull` command will download repositories
defined in manifest prepended by manifest.TargetPath.
Introduce new config method CurrentContextEntryPoint(), method takes
TargetPath, PrimaryRepo.URL, SubPath, and clusterType and phase
constructs a path to the entry point out of which the DocumentBundle
should be build, and returns it to the caller. After that caller can
build a bundle out of it, the bundle will contain documents relevant to
particular cluster-type and phase.
All objects that depend on bundle interface are updated to use the
CurrentContextEntryPoint() method of the config object
Relates-To: #99
Closes: #99
Change-Id: I99320c4cb626841d46f4c298b583e9af90b1dce4
2020-03-06 00:48:50 +00:00
|
|
|
func NewBundleByPath(rootPath string) (Bundle, error) {
|
2020-11-16 19:24:06 -06:00
|
|
|
return NewBundle(fs.NewDocumentFs(), rootPath)
|
Split document model, add entrypoints for repos
Add NewBundleByPath function, that would return bundle built from
the specified path argument
Add CurrentContextEntryPoint method of the config
object, that would allow easily get kustomize root path based on
clusterType and phase. You can also leave phase arg empty string,
which would try to return bundle for all phases
Introduce changes to config pakage objects:
- Manifest:
SubPath: this is relative path to the root of the repository that
contains directories with sites (SiteNames)
PrimaryRepositoryName: this is a string that must correspond to a key
of the Repositories map of manifest object, which is used to derive
primary repository
Repositories object is a map, map keys correspond to names of the
directories where `document pull` command will download repositories
defined in manifest prepended by manifest.TargetPath.
Introduce new config method CurrentContextEntryPoint(), method takes
TargetPath, PrimaryRepo.URL, SubPath, and clusterType and phase
constructs a path to the entry point out of which the DocumentBundle
should be build, and returns it to the caller. After that caller can
build a bundle out of it, the bundle will contain documents relevant to
particular cluster-type and phase.
All objects that depend on bundle interface are updated to use the
CurrentContextEntryPoint() method of the config object
Relates-To: #99
Closes: #99
Change-Id: I99320c4cb626841d46f4c298b583e9af90b1dce4
2020-03-06 00:48:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 22:14:57 -06:00
|
|
|
// NewBundleFromBytes is a function which builds new document.Bundle from raw []bytes
|
|
|
|
func NewBundleFromBytes(data []byte) (Bundle, error) {
|
|
|
|
fSys := fs.Fs{
|
|
|
|
FileSystem: kustfs.MakeFsInMemory(),
|
|
|
|
}
|
|
|
|
if err := fSys.WriteFile("/kustomization.yaml", []byte(`resources:
|
|
|
|
- data.yaml`)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := fSys.WriteFile("/data.yaml", data); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewBundle(fSys, "/")
|
|
|
|
}
|
|
|
|
|
2019-09-30 12:05:34 -07:00
|
|
|
// NewBundle is a convenience function to create a new bundle
|
|
|
|
// Over time, it will evolve to support allowing more control
|
|
|
|
// for kustomize plugins
|
2020-11-16 19:24:06 -06:00
|
|
|
func NewBundle(fSys fs.FileSystem, kustomizePath string) (Bundle, error) {
|
2019-09-30 12:05:34 -07:00
|
|
|
var options = KustomizeBuildOptions{
|
|
|
|
KustomizationPath: kustomizePath,
|
2020-03-27 11:49:24 -05:00
|
|
|
LoadRestrictions: types.LoadRestrictionsRootOnly,
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// init an empty bundle factory
|
2020-03-16 14:26:58 -07:00
|
|
|
bundle := &BundleFactory{}
|
2019-09-30 12:05:34 -07:00
|
|
|
|
|
|
|
// set the fs and build options we will use
|
2020-03-16 14:26:58 -07:00
|
|
|
if err := bundle.SetFileSystem(fSys); err != nil {
|
2020-01-08 13:54:13 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-16 14:26:58 -07:00
|
|
|
if err := bundle.SetKustomizeBuildOptions(options); err != nil {
|
2020-01-08 13:54:13 -06:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-30 12:05:34 -07:00
|
|
|
|
2020-03-27 11:49:24 -05:00
|
|
|
var o = krusty.Options{
|
|
|
|
DoLegacyResourceSort: true, // Default and what we want
|
|
|
|
LoadRestrictions: options.LoadRestrictions,
|
2020-04-14 16:21:02 +04:00
|
|
|
DoPrune: false, // Default
|
|
|
|
PluginConfig: &types.PluginConfig{
|
|
|
|
PluginRestrictions: types.PluginRestrictionsNone,
|
2020-10-22 18:50:30 +04:00
|
|
|
BpLoadingOptions: types.BploUseStaticallyLinked,
|
2021-04-28 19:22:43 +00:00
|
|
|
FnpLoadingOptions: types.FnPluginLoadingOptions{
|
|
|
|
Network: true,
|
|
|
|
},
|
2020-04-14 16:21:02 +04:00
|
|
|
},
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
|
2021-04-28 19:22:43 +00:00
|
|
|
kustomizer := krusty.MakeKustomizer(&o)
|
|
|
|
m, err := kustomizer.Run(fSys, kustomizePath)
|
2019-09-30 12:05:34 -07:00
|
|
|
if err != nil {
|
2021-02-08 22:14:57 -06:00
|
|
|
return nil, err
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
2020-01-08 13:54:13 -06:00
|
|
|
err = bundle.SetKustomizeResourceMap(m)
|
|
|
|
return bundle, err
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetKustomizeResourceMap returns a Kustomize Resource Map for this bundle
|
|
|
|
func (b *BundleFactory) GetKustomizeResourceMap() resmap.ResMap {
|
|
|
|
return b.ResMap
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKustomizeResourceMap allows us to set the populated resource map for this bundle. In
|
|
|
|
// the future, it may modify it before saving it.
|
|
|
|
func (b *BundleFactory) SetKustomizeResourceMap(r resmap.ResMap) error {
|
|
|
|
b.ResMap = r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKustomizeBuildOptions returns the build options object used to generate the resource map
|
|
|
|
// for this bundle
|
|
|
|
func (b *BundleFactory) GetKustomizeBuildOptions() KustomizeBuildOptions {
|
|
|
|
return b.KustomizeBuildOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKustomizeBuildOptions sets the build options to be used for this bundle. In
|
|
|
|
// the future, it may perform some basic validations.
|
|
|
|
func (b *BundleFactory) SetKustomizeBuildOptions(k KustomizeBuildOptions) error {
|
|
|
|
b.KustomizeBuildOptions = k
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFileSystem sets the filesystem that will be used by this bundle
|
2020-11-16 19:24:06 -06:00
|
|
|
func (b *BundleFactory) SetFileSystem(fSys fs.FileSystem) error {
|
2019-09-30 12:05:34 -07:00
|
|
|
b.FileSystem = fSys
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFileSystem gets the filesystem that will be used by this bundle
|
2020-11-16 19:24:06 -06:00
|
|
|
func (b *BundleFactory) GetFileSystem() fs.FileSystem {
|
2019-09-30 12:05:34 -07:00
|
|
|
return b.FileSystem
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllDocuments returns all documents in this bundle
|
|
|
|
func (b *BundleFactory) GetAllDocuments() ([]Document, error) {
|
2020-02-05 16:26:19 -06:00
|
|
|
docSet := make([]Document, len(b.ResMap.Resources()))
|
|
|
|
for i, res := range b.ResMap.Resources() {
|
2019-09-30 12:05:34 -07:00
|
|
|
// Construct Bundle document for each resource returned
|
|
|
|
doc, err := NewDocument(res)
|
|
|
|
if err != nil {
|
|
|
|
return docSet, err
|
|
|
|
}
|
2020-02-05 16:26:19 -06:00
|
|
|
docSet[i] = doc
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
return docSet, nil
|
|
|
|
}
|
|
|
|
|
2020-04-02 15:38:40 -07:00
|
|
|
// GetByName finds a document by name
|
2019-09-30 12:05:34 -07:00
|
|
|
func (b *BundleFactory) GetByName(name string) (Document, error) {
|
2020-04-02 15:38:40 -07:00
|
|
|
return b.SelectOne(NewSelector().ByName(name))
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
|
2020-02-21 23:14:27 +00:00
|
|
|
// Select offers an interface to pass a Selector, built on top of kustomize Selector
|
|
|
|
// to the bundle returning Documents that match the criteria
|
|
|
|
func (b *BundleFactory) Select(selector Selector) ([]Document, error) {
|
2019-09-30 12:05:34 -07:00
|
|
|
// use the kustomize select method
|
2020-02-21 23:14:27 +00:00
|
|
|
resources, err := b.ResMap.Select(selector.Selector)
|
2019-09-30 12:05:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return []Document{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct Bundle document for each resource returned
|
2020-02-05 16:26:19 -06:00
|
|
|
docSet := make([]Document, len(resources))
|
|
|
|
for i, res := range resources {
|
2020-01-08 13:54:13 -06:00
|
|
|
var doc Document
|
|
|
|
doc, err = NewDocument(res)
|
2019-09-30 12:05:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return docSet, err
|
|
|
|
}
|
2020-02-05 16:26:19 -06:00
|
|
|
docSet[i] = doc
|
2019-09-30 12:05:34 -07:00
|
|
|
}
|
|
|
|
return docSet, err
|
|
|
|
}
|
|
|
|
|
2020-03-27 14:09:32 -07:00
|
|
|
// SelectOne serves the common use case where you expect one match
|
|
|
|
// and only one match to your selector -- in other words, you want to
|
|
|
|
// error if you didn't find any documents, and error if you found
|
|
|
|
// more than one. This reduces code repetition that would otherwise
|
|
|
|
// be scattered around that evaluates the length of the doc set returned
|
|
|
|
// for this common case
|
|
|
|
func (b *BundleFactory) SelectOne(selector Selector) (Document, error) {
|
|
|
|
docSet, err := b.Select(selector)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// evaluate docSet for at least one document, and no more than
|
|
|
|
// one document and raise errors as appropriate
|
|
|
|
switch numDocsFound := len(docSet); {
|
|
|
|
case numDocsFound == 0:
|
|
|
|
return nil, ErrDocNotFound{Selector: selector}
|
|
|
|
case numDocsFound > 1:
|
2020-04-02 15:38:40 -07:00
|
|
|
return nil, ErrMultiDocsFound{Selector: selector}
|
2020-03-27 14:09:32 -07:00
|
|
|
}
|
|
|
|
return docSet[0], nil
|
|
|
|
}
|
|
|
|
|
2020-02-19 12:10:38 +04:00
|
|
|
// SelectBundle offers an interface to pass a Selector, built on top of kustomize Selector
|
|
|
|
// to the bundle returning a new Bundle that matches the criteria. This is useful
|
|
|
|
// where you want to actually prune the underlying bundle you are working with
|
|
|
|
// rather then getting back the matching documents for scenarios like
|
|
|
|
// test cases where you want to pass in custom "filtered" bundles
|
|
|
|
// specific to the test case
|
|
|
|
func (b *BundleFactory) SelectBundle(selector Selector) (Bundle, error) {
|
|
|
|
// use the kustomize select method
|
|
|
|
resources, err := b.ResMap.Select(selector.Selector)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a blank resourcemap and append the found resources
|
|
|
|
// into the new resource map
|
|
|
|
resourceMap := resmap.New()
|
|
|
|
for _, res := range resources {
|
|
|
|
if err = resourceMap.Append(res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a new bundle with the same options and filesystem
|
|
|
|
// as this one but with a reduced resourceMap
|
|
|
|
return &BundleFactory{
|
|
|
|
KustomizeBuildOptions: b.KustomizeBuildOptions,
|
|
|
|
ResMap: resourceMap,
|
|
|
|
FileSystem: b.FileSystem,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:56:59 +04:00
|
|
|
// SelectByFieldValue returns new Bundle with filtered resource documents.
|
|
|
|
// Method iterates over all resources in the bundle. If resource has field
|
|
|
|
// (i.e. key) specified in JSON path, and the comparison function returns
|
|
|
|
// 'true' for value referenced by JSON path, then resource is added to
|
|
|
|
// resulting bundle.
|
|
|
|
// Example:
|
|
|
|
// The bundle contains 3 documents
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// apiVersion: v1
|
|
|
|
// kind: DocKind1
|
|
|
|
// metadata:
|
|
|
|
// name: doc1
|
|
|
|
// spec:
|
|
|
|
// somekey:
|
|
|
|
// somefield: "someValue"
|
|
|
|
// ---
|
|
|
|
// apiVersion: v1
|
|
|
|
// kind: DocKind2
|
|
|
|
// metadata:
|
|
|
|
// name: doc2
|
|
|
|
// spec:
|
|
|
|
// somekey:
|
|
|
|
// somefield: "someValue"
|
|
|
|
// ---
|
|
|
|
// apiVersion: v1
|
|
|
|
// kind: DocKind1
|
|
|
|
// metadata:
|
|
|
|
// name: doc3
|
|
|
|
// spec:
|
|
|
|
// somekey:
|
|
|
|
// somefield: "someOtherValue"
|
|
|
|
//
|
|
|
|
// Execution of bundleInstance.SelectByFieldValue(
|
|
|
|
// "spec.somekey.somefield",
|
|
|
|
// func(v interface{}) { return v == "someValue" })
|
|
|
|
// will return a new Bundle instance containing 2 documents:
|
|
|
|
// ---
|
|
|
|
// apiVersion: v1
|
|
|
|
// kind: DocKind1
|
|
|
|
// metadata:
|
|
|
|
// name: doc1
|
|
|
|
// spec:
|
|
|
|
// somekey:
|
|
|
|
// somefield: "someValue"
|
|
|
|
// ---
|
|
|
|
// apiVersion: v1
|
|
|
|
// kind: DocKind2
|
|
|
|
// metadata:
|
|
|
|
// name: doc2
|
|
|
|
// spec:
|
|
|
|
// somekey:
|
|
|
|
// somefield: "someValue"
|
|
|
|
func (b *BundleFactory) SelectByFieldValue(path string, condition func(interface{}) bool) (Bundle, error) {
|
|
|
|
result := &BundleFactory{
|
|
|
|
KustomizeBuildOptions: b.KustomizeBuildOptions,
|
|
|
|
FileSystem: b.FileSystem,
|
|
|
|
}
|
|
|
|
resourceMap := resmap.New()
|
|
|
|
for _, res := range b.Resources() {
|
|
|
|
val, err := res.GetFieldValue(path)
|
|
|
|
if err != nil {
|
2020-03-27 11:49:24 -05:00
|
|
|
if strings.Contains(err.Error(), "no field named") {
|
2020-01-24 12:56:59 +04:00
|
|
|
// this resource doesn't have the specified field - skip it
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if condition(val) {
|
|
|
|
if err = resourceMap.Append(res); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := result.SetKustomizeResourceMap(resourceMap); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-09-30 12:05:34 -07:00
|
|
|
// GetByAnnotation is a convenience method to get documents for a particular annotation
|
2020-02-21 23:14:27 +00:00
|
|
|
func (b *BundleFactory) GetByAnnotation(annotationSelector string) ([]Document, error) {
|
2019-09-30 12:05:34 -07:00
|
|
|
// Construct kustomize annotation selector
|
2020-02-21 23:14:27 +00:00
|
|
|
selector := NewSelector().ByAnnotation(annotationSelector)
|
2019-09-30 12:05:34 -07:00
|
|
|
// pass it to the selector
|
|
|
|
return b.Select(selector)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByLabel is a convenience method to get documents for a particular label
|
2020-02-21 23:14:27 +00:00
|
|
|
func (b *BundleFactory) GetByLabel(labelSelector string) ([]Document, error) {
|
|
|
|
// Construct kustomize label selector
|
|
|
|
selector := NewSelector().ByLabel(labelSelector)
|
2019-09-30 12:05:34 -07:00
|
|
|
// pass it to the selector
|
|
|
|
return b.Select(selector)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByGvk is a convenience method to get documents for a particular Gvk tuple
|
|
|
|
func (b *BundleFactory) GetByGvk(group, version, kind string) ([]Document, error) {
|
|
|
|
// Construct kustomize gvk object
|
2020-02-21 23:14:27 +00:00
|
|
|
selector := NewSelector().ByGvk(group, version, kind)
|
2019-09-30 12:05:34 -07:00
|
|
|
// pass it to the selector
|
|
|
|
return b.Select(selector)
|
|
|
|
}
|
|
|
|
|
2020-07-01 14:04:54 -05:00
|
|
|
// Append bundle with the document, this only works with document interface implementation
|
|
|
|
// that is provided by this package
|
|
|
|
func (b *BundleFactory) Append(doc Document) error {
|
|
|
|
yaml, err := doc.AsYAML()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-28 19:22:43 +00:00
|
|
|
res, err := resource.NewFactory(&hasher.Hasher{}).FromBytes(yaml)
|
2020-07-01 14:04:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return b.ResMap.Append(res)
|
|
|
|
}
|
|
|
|
|
2019-09-30 12:05:34 -07:00
|
|
|
// Write will write out the entire bundle resource map
|
|
|
|
func (b *BundleFactory) Write(out io.Writer) error {
|
|
|
|
for _, res := range b.ResMap.Resources() {
|
2019-10-11 17:13:59 -05:00
|
|
|
err := utilyaml.WriteOut(out, res)
|
2019-09-30 12:05:34 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|