File: origin.go

package info (click to toggle)
golang-k8s-sigs-kustomize-api 0.20.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,768 kB
  • sloc: makefile: 206; sh: 67
file content (106 lines) | stat: -rw-r--r-- 3,273 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package resource

import (
	"path/filepath"
	"strings"

	"sigs.k8s.io/kustomize/api/internal/git"
	kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
)

// Origin retains information about the origin of resources and transformer configs
// that contributed to the output of `kustomize build`
type Origin struct {
	// Path is the path to the resource. If a local resource, this path is
	// rooted from the directory upon which `kustomize build` was invoked. If a
	// remote resource, this path is rooted from the root of the remote repo.
	Path string `json:"path,omitempty" yaml:"path,omitempty"`

	// Repo is the remote repository that the resource or transformer originated from if it is
	// not from a local file
	Repo string `json:"repo,omitempty" yaml:"repo,omitempty"`

	// Ref is the ref of the remote repository that the resource or transformer originated from
	// if it is not from a local file
	Ref string `json:"ref,omitempty" yaml:"ref,omitempty"`

	// The following fields only apply to resources that have been
	// generated by fields other than the `resources` field, or to transformer
	// configs.

	// ConfiguredIn is the file path to the generator or transformer config that created the
	// resource
	ConfiguredIn string `json:"configuredIn,omitempty" yaml:"configuredIn,omitempty"`

	// ConfiguredBy is the ObjectReference of the generator or transformer config
	ConfiguredBy kyaml.ResourceIdentifier `json:"configuredBy,omitempty" yaml:"configuredBy,omitempty"`
}

// Copy returns a copy of origin
func (origin *Origin) Copy() Origin {
	if origin == nil {
		return Origin{}
	}
	return *origin
}

// Append returns a copy of origin with a path appended to it
func (origin *Origin) Append(path string) *Origin {
	originCopy := origin.Copy()
	repoSpec, err := git.NewRepoSpecFromURL(path)
	if err == nil {
		originCopy.Repo = repoSpec.CloneSpec()
		absPath := repoSpec.AbsPath()
		path = absPath[strings.Index(absPath[1:], "/")+1:][1:]
		originCopy.Path = ""
		originCopy.Ref = repoSpec.Ref
	}
	originCopy.Path = filepath.Join(originCopy.Path, path)
	return &originCopy
}

// String returns a string version of origin
func (origin *Origin) String() (string, error) {
	anno, err := kyaml.Marshal(origin)
	return string(anno), err
}

// Transformations is a list of Origin
type Transformations []*Origin

// String returns a string version of Transformations
func (transformations *Transformations) String() (string, error) {
	anno, err := kyaml.Marshal(transformations)
	return string(anno), err
}

// OriginFromCustomPlugin takes a custom plugin defined as a resource
// and returns an origin object to describe it
func OriginFromCustomPlugin(res *Resource) (*Origin, error) {
	origin, err := res.GetOrigin()
	if err != nil {
		return nil, err
	}
	var result *Origin
	if origin != nil {
		result = &Origin{
			Repo:         origin.Repo,
			Ref:          origin.Ref,
			ConfiguredIn: origin.Path,
			ConfiguredBy: kyaml.ResourceIdentifier{
				TypeMeta: kyaml.TypeMeta{
					APIVersion: res.GetApiVersion(),
					Kind:       res.GetKind(),
				},
				NameMeta: kyaml.NameMeta{
					Name:      res.GetName(),
					Namespace: res.GetNamespace(),
				},
			},
		}
	}
	return result, nil
}