-
Notifications
You must be signed in to change notification settings - Fork 135
/
manifest.go
249 lines (215 loc) · 5.55 KB
/
manifest.go
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2022 The PipeCD Authors.
//
// 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.
package kubernetes
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
"github.com/pipe-cd/pipecd/pkg/model"
)
type Manifest struct {
Key ResourceKey
u *unstructured.Unstructured
}
func MakeManifest(key ResourceKey, u *unstructured.Unstructured) Manifest {
return Manifest{
Key: key,
u: u,
}
}
func (m Manifest) Duplicate(name string) Manifest {
u := m.u.DeepCopy()
u.SetName(name)
key := m.Key
key.Name = name
return Manifest{
Key: key,
u: u,
}
}
func (m Manifest) YamlBytes() ([]byte, error) {
return yaml.Marshal(m.u)
}
func (m Manifest) MarshalJSON() ([]byte, error) {
return m.u.MarshalJSON()
}
func (m Manifest) AddAnnotations(annotations map[string]string) {
if len(annotations) == 0 {
return
}
annos := m.u.GetAnnotations()
if annos == nil {
m.u.SetAnnotations(annotations)
return
}
for k, v := range annotations {
annos[k] = v
}
m.u.SetAnnotations(annos)
}
func (m Manifest) GetAnnotations() map[string]string {
return m.u.GetAnnotations()
}
func (m Manifest) GetNestedStringMap(fields ...string) (map[string]string, error) {
sm, _, err := unstructured.NestedStringMap(m.u.Object, fields...)
if err != nil {
return nil, err
}
return sm, nil
}
func (m Manifest) GetNestedMap(fields ...string) (map[string]interface{}, error) {
sm, _, err := unstructured.NestedMap(m.u.Object, fields...)
if err != nil {
return nil, err
}
return sm, nil
}
// AddStringMapValues adds or overrides the given key-values into the string map
// that can be found at the specified fields.
func (m Manifest) AddStringMapValues(values map[string]string, fields ...string) error {
curMap, _, err := unstructured.NestedStringMap(m.u.Object, fields...)
if err != nil {
return err
}
if curMap == nil {
return unstructured.SetNestedStringMap(m.u.Object, values, fields...)
}
for k, v := range values {
curMap[k] = v
}
return unstructured.SetNestedStringMap(m.u.Object, curMap, fields...)
}
func (m Manifest) GetSpec() (interface{}, error) {
spec, ok, err := unstructured.NestedFieldNoCopy(m.u.Object, "spec")
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("spec was not found")
}
return spec, nil
}
func (m Manifest) SetStructuredSpec(spec interface{}) error {
data, err := yaml.Marshal(spec)
if err != nil {
return err
}
unstructuredSpec := make(map[string]interface{})
if err := yaml.Unmarshal(data, &unstructuredSpec); err != nil {
return err
}
return unstructured.SetNestedField(m.u.Object, unstructuredSpec, "spec")
}
func (m Manifest) ConvertToStructuredObject(o interface{}) error {
data, err := m.MarshalJSON()
if err != nil {
return err
}
return json.Unmarshal(data, o)
}
func ParseFromStructuredObject(s interface{}) (Manifest, error) {
data, err := json.Marshal(s)
if err != nil {
return Manifest{}, err
}
obj := &unstructured.Unstructured{}
if err := obj.UnmarshalJSON(data); err != nil {
return Manifest{}, err
}
return Manifest{
Key: MakeResourceKey(obj),
u: obj,
}, nil
}
func LoadPlainYAMLManifests(dir string, names []string, configFileName string) ([]Manifest, error) {
// If no name was specified we have to walk the app directory to collect the manifest list.
if len(names) == 0 {
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if path == dir {
return nil
}
if f.IsDir() {
return filepath.SkipDir
}
ext := filepath.Ext(f.Name())
if ext != ".yaml" && ext != ".yml" && ext != ".json" {
return nil
}
if model.IsApplicationConfigFile(f.Name()) {
return nil
}
if f.Name() == configFileName {
return nil
}
names = append(names, f.Name())
return nil
})
if err != nil {
return nil, err
}
}
manifests := make([]Manifest, 0, len(names))
for _, name := range names {
path := filepath.Join(dir, name)
ms, err := LoadManifestsFromYAMLFile(path)
if err != nil {
return nil, fmt.Errorf("failed to load manifest at %s (%w)", path, err)
}
manifests = append(manifests, ms...)
}
return manifests, nil
}
func LoadManifestsFromYAMLFile(path string) ([]Manifest, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return ParseManifests(string(data))
}
func ParseManifests(data string) ([]Manifest, error) {
const separator = "\n---"
var (
parts = strings.Split(data, separator)
manifests = make([]Manifest, 0, len(parts))
)
for i, part := range parts {
// Ignore all the cases where no content between separator.
if len(strings.TrimSpace(part)) == 0 {
continue
}
// Append new line which trim by document separator.
if i != len(parts)-1 {
part += "\n"
}
var obj unstructured.Unstructured
if err := yaml.Unmarshal([]byte(part), &obj); err != nil {
return nil, err
}
if len(obj.Object) == 0 {
continue
}
manifests = append(manifests, Manifest{
Key: MakeResourceKey(&obj),
u: &obj,
})
}
return manifests, nil
}