...

Source file src/sigs.k8s.io/kustomize/api/resource/origin.go

Documentation: sigs.k8s.io/kustomize/api/resource

     1  // Copyright 2019 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package resource
     5  
     6  import (
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"sigs.k8s.io/kustomize/api/internal/git"
    11  	kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
    12  )
    13  
    14  // Origin retains information about the origin of resources and transformer configs
    15  // that contributed to the output of `kustomize build`
    16  type Origin struct {
    17  	// Path is the path to the resource. If a local resource, this path is
    18  	// rooted from the directory upon which `kustomize build` was invoked. If a
    19  	// remote resource, this path is rooted from the root of the remote repo.
    20  	Path string `json:"path,omitempty" yaml:"path,omitempty"`
    21  
    22  	// Repo is the remote repository that the resource or transformer originated from if it is
    23  	// not from a local file
    24  	Repo string `json:"repo,omitempty" yaml:"repo,omitempty"`
    25  
    26  	// Ref is the ref of the remote repository that the resource or transformer originated from
    27  	// if it is not from a local file
    28  	Ref string `json:"ref,omitempty" yaml:"ref,omitempty"`
    29  
    30  	// The following fields only apply to resources that have been
    31  	// generated by fields other than the `resources` field, or to transformer
    32  	// configs.
    33  
    34  	// ConfiguredIn is the file path to the generator or transformer config that created the
    35  	// resource
    36  	ConfiguredIn string `json:"configuredIn,omitempty" yaml:"configuredIn,omitempty"`
    37  
    38  	// ConfiguredBy is the ObjectReference of the generator or transformer config
    39  	ConfiguredBy kyaml.ResourceIdentifier `json:"configuredBy,omitempty" yaml:"configuredBy,omitempty"`
    40  }
    41  
    42  // Copy returns a copy of origin
    43  func (origin *Origin) Copy() Origin {
    44  	if origin == nil {
    45  		return Origin{}
    46  	}
    47  	return *origin
    48  }
    49  
    50  // Append returns a copy of origin with a path appended to it
    51  func (origin *Origin) Append(path string) *Origin {
    52  	originCopy := origin.Copy()
    53  	repoSpec, err := git.NewRepoSpecFromURL(path)
    54  	if err == nil {
    55  		originCopy.Repo = repoSpec.CloneSpec()
    56  		absPath := repoSpec.AbsPath()
    57  		path = absPath[strings.Index(absPath[1:], "/")+1:][1:]
    58  		originCopy.Path = ""
    59  		originCopy.Ref = repoSpec.Ref
    60  	}
    61  	originCopy.Path = filepath.Join(originCopy.Path, path)
    62  	return &originCopy
    63  }
    64  
    65  // String returns a string version of origin
    66  func (origin *Origin) String() (string, error) {
    67  	anno, err := kyaml.Marshal(origin)
    68  	return string(anno), err
    69  }
    70  
    71  // Transformations is a list of Origin
    72  type Transformations []*Origin
    73  
    74  // String returns a string version of Transformations
    75  func (transformations *Transformations) String() (string, error) {
    76  	anno, err := kyaml.Marshal(transformations)
    77  	return string(anno), err
    78  }
    79  
    80  // OriginFromCustomPlugin takes a custom plugin defined as a resource
    81  // and returns an origin object to describe it
    82  func OriginFromCustomPlugin(res *Resource) (*Origin, error) {
    83  	origin, err := res.GetOrigin()
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	var result *Origin
    88  	if origin != nil {
    89  		result = &Origin{
    90  			Repo:         origin.Repo,
    91  			Ref:          origin.Ref,
    92  			ConfiguredIn: origin.Path,
    93  			ConfiguredBy: kyaml.ResourceIdentifier{
    94  				TypeMeta: kyaml.TypeMeta{
    95  					APIVersion: res.GetApiVersion(),
    96  					Kind:       res.GetKind(),
    97  				},
    98  				NameMeta: kyaml.NameMeta{
    99  					Name:      res.GetName(),
   100  					Namespace: res.GetNamespace(),
   101  				},
   102  			},
   103  		}
   104  	}
   105  	return result, nil
   106  }
   107  

View as plain text