-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathdetermine.go
297 lines (267 loc) · 10.1 KB
/
determine.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright 2024 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 deployment
import (
"fmt"
"slices"
"strings"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/config"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/provider"
"github.com/pipe-cd/pipecd/pkg/model"
"github.com/pipe-cd/pipecd/pkg/plugin/diff"
)
type containerImage struct {
name string
tag string
}
// parseContainerImage splits the container image into name and tag.
// The image should be in the format of "name:tag".
// If the tag is not specified, it will be empty.
func parseContainerImage(image string) (img containerImage) {
parts := strings.Split(image, ":")
if len(parts) == 2 {
img.tag = parts[1]
}
paths := strings.Split(parts[0], "/")
img.name = paths[len(paths)-1]
return
}
// determineVersions decides artifact versions of an application.
// It finds all container images that are being specified in the workload manifests then returns their names and tags.
func determineVersions(manifests []provider.Manifest) ([]*model.ArtifactVersion, error) {
imageMap := map[string]struct{}{}
for _, m := range manifests {
// TODO: we should consider other fields like spec.jobTempate.spec.template.spec.containers because CronJob uses this format.
containers, ok, err := unstructured.NestedSlice(m.Body.Object, "spec", "template", "spec", "containers")
if err != nil {
// if the containers field is not an array, it will return an error.
// we define this as error because the 'containers' is plural form, so it should be an array.
return nil, err
}
if !ok {
continue
}
// Remove duplicate images on multiple manifests.
for _, c := range containers {
m, ok := c.(map[string]interface{})
if !ok {
// TODO: Add logging.
continue
}
img, ok := m["image"]
if !ok {
continue
}
imgStr, ok := img.(string)
if !ok {
return nil, fmt.Errorf("invalid image format: %T(%v)", img, img)
}
imageMap[imgStr] = struct{}{}
}
}
versions := make([]*model.ArtifactVersion, 0, len(imageMap))
for i := range imageMap {
image := parseContainerImage(i)
versions = append(versions, &model.ArtifactVersion{
Kind: model.ArtifactVersion_CONTAINER_IMAGE,
Version: image.tag,
Name: image.name,
Url: i,
})
}
return versions, nil
}
// findManifests returns the manifests that have the specified kind and name.
func findManifests(kind, name string, manifests []provider.Manifest) []provider.Manifest {
out := make([]provider.Manifest, 0, len(manifests))
for _, m := range manifests {
if m.Body.GetKind() != kind {
continue
}
if name != "" && m.Body.GetName() != name {
continue
}
out = append(out, m)
}
return out
}
// findWorkloadManifests returns the manifests that have the specified references.
// the default kind is Deployment if it is not specified.
func findWorkloadManifests(manifests []provider.Manifest, refs []config.K8sResourceReference) []provider.Manifest {
if len(refs) == 0 {
return findManifests(provider.KindDeployment, "", manifests)
}
workloads := make([]provider.Manifest, 0)
for _, ref := range refs {
kind := provider.KindDeployment
if ref.Kind != "" {
kind = ref.Kind
}
ms := findManifests(kind, ref.Name, manifests)
workloads = append(workloads, ms...)
}
return workloads
}
type workloadPair struct {
old provider.Manifest
new provider.Manifest
}
func findUpdatedWorkloads(olds, news []provider.Manifest) []workloadPair {
pairs := make([]workloadPair, 0)
oldMap := make(map[provider.ResourceKey]provider.Manifest, len(olds))
nomalizeKey := func(k provider.ResourceKey) provider.ResourceKey {
// Ignoring APIVersion because user can upgrade to the new APIVersion for the same workload.
k.APIVersion = ""
if k.Namespace == provider.DefaultNamespace {
k.Namespace = ""
}
return k
}
for _, m := range olds {
key := nomalizeKey(m.Key)
oldMap[key] = m
}
for _, n := range news {
key := nomalizeKey(n.Key)
if o, ok := oldMap[key]; ok {
pairs = append(pairs, workloadPair{
old: o,
new: n,
})
}
}
return pairs
}
// findConfigsAndSecrets returns the manifests that are ConfigMap or Secret.
func findConfigsAndSecrets(manifests []provider.Manifest) map[provider.ResourceKey]provider.Manifest {
configs := make(map[provider.ResourceKey]provider.Manifest)
for _, m := range manifests {
if m.Key.IsConfigMap() {
configs[m.Key] = m
}
if m.Key.IsSecret() {
configs[m.Key] = m
}
}
return configs
}
func checkImageChange(ns diff.Nodes) (string, bool) {
const containerImageQuery = `^spec\.template\.spec\.containers\.\d+.image$`
nodes, _ := ns.Find(containerImageQuery)
if len(nodes) == 0 {
return "", false
}
images := make([]string, 0, len(ns))
for _, n := range nodes {
beforeImg := parseContainerImage(n.StringX())
afterImg := parseContainerImage(n.StringY())
if beforeImg.name == afterImg.name {
images = append(images, fmt.Sprintf("image %s from %s to %s", beforeImg.name, beforeImg.tag, afterImg.tag))
} else {
images = append(images, fmt.Sprintf("image %s:%s to %s:%s", beforeImg.name, beforeImg.tag, afterImg.name, afterImg.tag))
}
}
desc := fmt.Sprintf("Sync progressively because of updating %s", strings.Join(images, ", "))
return desc, true
}
// checkReplicasChange checks if the replicas field is changed.
func checkReplicasChange(ns diff.Nodes) (before, after string, changed bool) {
const replicasQuery = `^spec\.replicas$`
node, err := ns.FindOne(replicasQuery)
if err != nil {
// no difference between the before and after manifests, or unknown error occurred.
return "", "", false
}
if node.TypeX == nil {
// The replicas field is not found in the before manifest.
// There is difference between the before and after manifests, So it means the replicas field is added in the after manifest.
// So the replicas field in the before manifest is nil, we should return "<nil>" as the before value.
return "<nil>", node.StringY(), true
}
if node.TypeY == nil {
// The replicas field is not found in the after manifest.
// There is difference between the before and after manifests, So it means the replicas field is removed in the after manifest.
// So the replicas field in the after manifest is nil, we should return "<nil>" as the after value.
return node.StringX(), "<nil>", true
}
return node.StringX(), node.StringY(), true
}
// First up, checks to see if the workload's `spec.template` has been changed,
// and then checks if the configmap/secret's data.
func determineStrategy(olds, news []provider.Manifest, workloadRefs []config.K8sResourceReference, logger *zap.Logger) (strategy model.SyncStrategy, summary string) {
oldWorkloads := findWorkloadManifests(olds, workloadRefs)
if len(oldWorkloads) == 0 {
return model.SyncStrategy_QUICK_SYNC, "Quick sync by applying all manifests because it was unable to find the currently running workloads"
}
newWorkloads := findWorkloadManifests(news, workloadRefs)
if len(newWorkloads) == 0 {
return model.SyncStrategy_QUICK_SYNC, "Quick sync by applying all manifests because it was unable to find workloads in the new manifests"
}
workloads := findUpdatedWorkloads(oldWorkloads, newWorkloads)
diffs := make(map[provider.ResourceKey]diff.Nodes, len(workloads))
for _, w := range workloads {
// If the workload's pod template was touched
// do progressive deployment with the specified pipeline.
diffResult, err := provider.Diff(w.old, w.new, logger)
if err != nil {
return model.SyncStrategy_PIPELINE, fmt.Sprintf("Sync progressively due to an error while calculating the diff (%v)", err)
}
diffNodes := diffResult.Nodes()
diffs[w.new.Key] = diffNodes
templateDiffs := diffNodes.FindByPrefix("spec.template")
if len(templateDiffs) > 0 {
if msg, changed := checkImageChange(templateDiffs); changed {
return model.SyncStrategy_PIPELINE, msg
}
return model.SyncStrategy_PIPELINE, fmt.Sprintf("Sync progressively because pod template of workload %s was changed", w.new.Key.Name)
}
}
// If the config/secret was touched, we also need to do progressive
// deployment to check run with the new config/secret content.
oldConfigs := findConfigsAndSecrets(olds)
newConfigs := findConfigsAndSecrets(news)
if len(oldConfigs) > len(newConfigs) {
return model.SyncStrategy_PIPELINE, fmt.Sprintf("Sync progressively because %d configmap/secret deleted", len(oldConfigs)-len(newConfigs))
}
if len(oldConfigs) < len(newConfigs) {
return model.SyncStrategy_PIPELINE, fmt.Sprintf("Sync progressively because new %d configmap/secret added", len(newConfigs)-len(oldConfigs))
}
for k, oc := range oldConfigs {
nc, ok := newConfigs[k]
if !ok {
return model.SyncStrategy_PIPELINE, fmt.Sprintf("Sync progressively because %s %s was deleted", oc.Key.Kind, oc.Key.Name)
}
result, err := provider.Diff(oc, nc, logger)
if err != nil {
return model.SyncStrategy_PIPELINE, fmt.Sprintf("Sync progressively due to an error while calculating the diff (%v)", err)
}
if result.HasDiff() {
return model.SyncStrategy_PIPELINE, fmt.Sprintf("Sync progressively because %s %s was updated", oc.Key.Kind, oc.Key.Name)
}
}
// Check if this is a scaling commit.
scales := make([]string, 0, len(diffs))
for k, d := range diffs {
if before, after, changed := checkReplicasChange(d); changed {
scales = append(scales, fmt.Sprintf("%s/%s from %s to %s", k.Kind, k.Name, before, after))
}
}
if len(scales) > 0 {
slices.Sort(scales)
return model.SyncStrategy_QUICK_SYNC, fmt.Sprintf("Quick sync to scale %s", strings.Join(scales, ", "))
}
return model.SyncStrategy_QUICK_SYNC, "Quick sync by applying all manifests"
}