...
1
15
16 package chart
17
18 import (
19 "path/filepath"
20 "regexp"
21 "strings"
22 )
23
24
25 const APIVersionV1 = "v1"
26
27
28 const APIVersionV2 = "v2"
29
30
31 var aliasNameFormat = regexp.MustCompile("^[a-zA-Z0-9_-]+$")
32
33
34
35 type Chart struct {
36
37
38
39
40 Raw []*File `json:"-"`
41
42 Metadata *Metadata `json:"metadata"`
43
44 Lock *Lock `json:"lock"`
45
46 Templates []*File `json:"templates"`
47
48 Values map[string]interface{} `json:"values"`
49
50 Schema []byte `json:"schema"`
51
52
53 Files []*File `json:"files"`
54
55 parent *Chart
56 dependencies []*Chart
57 }
58
59 type CRD struct {
60
61 Name string
62
63 Filename string
64
65 File *File
66 }
67
68
69 func (ch *Chart) SetDependencies(charts ...*Chart) {
70 ch.dependencies = nil
71 ch.AddDependency(charts...)
72 }
73
74
75 func (ch *Chart) Name() string {
76 if ch.Metadata == nil {
77 return ""
78 }
79 return ch.Metadata.Name
80 }
81
82
83 func (ch *Chart) AddDependency(charts ...*Chart) {
84 for i, x := range charts {
85 charts[i].parent = ch
86 ch.dependencies = append(ch.dependencies, x)
87 }
88 }
89
90
91 func (ch *Chart) Root() *Chart {
92 if ch.IsRoot() {
93 return ch
94 }
95 return ch.Parent().Root()
96 }
97
98
99 func (ch *Chart) Dependencies() []*Chart { return ch.dependencies }
100
101
102 func (ch *Chart) IsRoot() bool { return ch.parent == nil }
103
104
105 func (ch *Chart) Parent() *Chart { return ch.parent }
106
107
108 func (ch *Chart) ChartPath() string {
109 if !ch.IsRoot() {
110 return ch.Parent().ChartPath() + "." + ch.Name()
111 }
112 return ch.Name()
113 }
114
115
116 func (ch *Chart) ChartFullPath() string {
117 if !ch.IsRoot() {
118 return ch.Parent().ChartFullPath() + "/charts/" + ch.Name()
119 }
120 return ch.Name()
121 }
122
123
124 func (ch *Chart) Validate() error {
125 return ch.Metadata.Validate()
126 }
127
128
129 func (ch *Chart) AppVersion() string {
130 if ch.Metadata == nil {
131 return ""
132 }
133 return ch.Metadata.AppVersion
134 }
135
136
137
138 func (ch *Chart) CRDs() []*File {
139 files := []*File{}
140
141 for _, f := range ch.Files {
142 if strings.HasPrefix(f.Name, "crds/") && hasManifestExtension(f.Name) {
143 files = append(files, f)
144 }
145 }
146
147 for _, dep := range ch.Dependencies() {
148 files = append(files, dep.CRDs()...)
149 }
150 return files
151 }
152
153
154 func (ch *Chart) CRDObjects() []CRD {
155 crds := []CRD{}
156
157 for _, f := range ch.Files {
158 if strings.HasPrefix(f.Name, "crds/") && hasManifestExtension(f.Name) {
159 mycrd := CRD{Name: f.Name, Filename: filepath.Join(ch.ChartFullPath(), f.Name), File: f}
160 crds = append(crds, mycrd)
161 }
162 }
163
164 for _, dep := range ch.Dependencies() {
165 crds = append(crds, dep.CRDObjects()...)
166 }
167 return crds
168 }
169
170 func hasManifestExtension(fname string) bool {
171 ext := filepath.Ext(fname)
172 return strings.EqualFold(ext, ".yaml") || strings.EqualFold(ext, ".yml") || strings.EqualFold(ext, ".json")
173 }
174
View as plain text