-
Notifications
You must be signed in to change notification settings - Fork 9
/
componentplan.go
274 lines (240 loc) · 9.48 KB
/
componentplan.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
/*
Copyright 2023 The Kubebb 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 v1alpha1
import (
"context"
"sort"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kubebb/core/pkg/utils"
)
const (
ComponentPlanReleaseNameLabel = Group + "/componentplan-release"
ComponentPlanRetryTimesAnnotation = Group + "/componentplan-retry"
ComponentPlanRollBackLabel = Group + "/rollback"
)
// ConditionType for ComponentPlan
const (
ComponentPlanTypeSucceeded ConditionType = "Succeeded"
ComponentPlanTypeApproved ConditionType = "Approved"
ComponentPlanTypeActioned ConditionType = "Actioned"
)
// Condition resons for ComponentPlan
const (
ComponentPlanReasonWaitDo ConditionReason = "WaitDo"
ComponentPlanReasonInstalling ConditionReason = "Installing"
ComponentPlanReasonUpgrading ConditionReason = "Upgrading"
ComponentPlanReasonUninstalling ConditionReason = "Uninstalling"
ComponentPlanReasonRollingBack ConditionReason = "RollingBack"
ComponentPlanReasonInstallSuccess ConditionReason = "InstallSuccess"
ComponentPlanReasonInstallFailed ConditionReason = "InstallFailed"
ComponentPlanReasonUninstallSuccess ConditionReason = "UninstallSuccess"
ComponentPlanReasonUninstallFailed ConditionReason = "UninstallFailed"
ComponentPlanReasonUpgradeSuccess ConditionReason = "UpgradeSuccess"
ComponentPlanReasonUpgradeFailed ConditionReason = "UpgradeFailed"
ComponentPlanReasonRollBackSuccess ConditionReason = "RollBackSuccess"
ComponentPlanReasonRollBackFailed ConditionReason = "RollBackFailed"
)
// GenerateComponentPlanName generates the name of the component plan for a given subscription
func GenerateComponentPlanName(sub *Subscription, version string) string {
return "sub." + sub.Name + "." + version
}
// GenerateComponentPlanManifestConfigMapName generates the name of the configmap of the component plan
func GenerateComponentPlanManifestConfigMapName(plan *ComponentPlan) string {
return "manifest." + plan.Name
}
func ComponentPlanSucceeded() Condition {
return componentPlanCondition(ComponentPlanTypeSucceeded, "", corev1.ConditionTrue, nil)
}
func ComponentPlanInitSucceeded() Condition {
return componentPlanCondition(ComponentPlanTypeSucceeded, "", corev1.ConditionFalse, nil)
}
func ComponentPlanFailed(err error) Condition {
return componentPlanCondition(ComponentPlanTypeSucceeded, "", corev1.ConditionFalse, err)
}
func ComponentPlanApproved() Condition {
return componentPlanCondition(ComponentPlanTypeApproved, "", corev1.ConditionTrue, nil)
}
func ComponentPlanUnapproved() Condition {
return componentPlanCondition(ComponentPlanTypeApproved, "", corev1.ConditionFalse, nil)
}
func ComponentPlanInstallSuccess() Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonInstallSuccess, corev1.ConditionTrue, nil)
}
func ComponentPlanInstallFailed(err error) Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonInstallFailed, corev1.ConditionFalse, err)
}
func ComponentPlanInstalling() Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonInstalling, corev1.ConditionFalse, nil)
}
func ComponentPlanUninstallSuccess() Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonUninstallSuccess, corev1.ConditionTrue, nil)
}
func ComponentPlanUninstallFailed(err error) Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonUninstallFailed, corev1.ConditionFalse, err)
}
func ComponentPlanUninstalling() Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonUninstalling, corev1.ConditionFalse, nil)
}
func ComponentPlanUpgradeSuccess() Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonUpgradeSuccess, corev1.ConditionTrue, nil)
}
func ComponentPlanUpgradeFailed(err error) Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonUpgradeFailed, corev1.ConditionFalse, err)
}
func ComponentPlanUpgrading() Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonUpgrading, corev1.ConditionFalse, nil)
}
func ComponentPlanWaitDo(err error) Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonWaitDo, corev1.ConditionFalse, err)
}
func ComponentPlanRollBackSuccess() Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonRollBackSuccess, corev1.ConditionTrue, nil)
}
func ComponentPlanRollBackFailed(err error) Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonRollBackFailed, corev1.ConditionFalse, err)
}
func ComponentPlanRollingBack() Condition {
return componentPlanCondition(ComponentPlanTypeActioned, ComponentPlanReasonRollingBack, corev1.ConditionFalse, nil)
}
func componentPlanCondition(ct ConditionType, reason ConditionReason, status corev1.ConditionStatus, err error) Condition {
if status == "" {
status = corev1.ConditionUnknown
}
c := Condition{
Type: ct,
Status: status,
LastTransitionTime: metav1.Now(),
Reason: reason,
}
if err != nil {
c.Message = err.Error()
}
return c
}
func (c *ComponentPlan) InitCondition() []Condition {
if c.Spec.Approved {
return []Condition{ComponentPlanApproved(), ComponentPlanWaitDo(nil), ComponentPlanInitSucceeded()}
} else {
return []Condition{ComponentPlanUnapproved(), ComponentPlanWaitDo(nil), ComponentPlanInitSucceeded()}
}
}
func (c *ComponentPlan) IsActionedReason(cr ConditionReason) bool {
return c.Status.GetCondition(ComponentPlanTypeActioned).Reason == cr
}
func (c *ComponentPlan) GetReleaseName() string {
return c.Spec.Name
}
// ComponentPlanDiffIgnorePaths is the list of paths to ignore when comparing
// These fields will almost certainly change when componentplan is updated, and displaying these
// changes will only result in more invalid information, so they need to be ignored
var ComponentPlanDiffIgnorePaths = []string{
"metadata.generation",
"metadata.resourceVersion",
"metadata.labels.helm.sh/chart",
"spec.template.metadata.labels.helm.sh/chart",
}
// GetResourcesAndImages get resource slices, image lists from manifests
func GetResourcesAndImages(ctx context.Context, logger logr.Logger, c client.Client, data, namespace string) (resources []Resource, images []string, err error) {
manifests, err := utils.SplitYAML([]byte(data))
if err != nil {
return nil, nil, err
}
resources = make([]Resource, len(manifests))
for i, manifest := range manifests {
obj := manifest
if len(obj.GetNamespace()) == 0 {
if rs, err := c.RESTMapper().RESTMapping(obj.GroupVersionKind().GroupKind()); err != nil {
logger.Error(err, "get RESTMapping err, just ignore and continue", "obj", klog.KObj(obj))
continue
} else {
if rs.Scope.Name() == meta.RESTScopeNameNamespace {
obj.SetNamespace(namespace)
} else {
obj.SetNamespace("")
}
}
}
has := &unstructured.Unstructured{}
has.SetKind(obj.GetKind())
has.SetAPIVersion(obj.GetAPIVersion())
err = c.Get(ctx, client.ObjectKeyFromObject(obj), has)
var isNew bool
if err != nil && apierrors.IsNotFound(err) || meta.IsNoMatchError(err) {
isNew = true
} else if err != nil {
logger.Error(err, "Resource get error, no notFound", "manifest", manifest, "obj", klog.KObj(obj))
return nil, nil, err
}
r := Resource{
Kind: obj.GetKind(),
Name: obj.GetName(),
APIVersion: obj.GetAPIVersion(),
}
if isNew {
r.NewCreated = &isNew
} else {
diff, err := utils.ResourceDiffStr(ctx, obj, has, ComponentPlanDiffIgnorePaths, c)
switch {
case err != nil:
logger.Error(err, "failed to get diff", "obj", klog.KObj(obj))
diffMsg := "diff with exist"
r.SpecDiffwithExist = &diffMsg
case diff == "":
ignore := "no spec diff, but some fields like resourceVersion will update"
r.SpecDiffwithExist = &ignore
default:
r.SpecDiffwithExist = &diff
}
}
resources[i] = r
gvk := obj.GroupVersionKind()
switch gvk.Group {
case "":
switch gvk.Kind { // nolint
case "Pod":
images = append(images, utils.GetPodImage(obj)...)
}
case "apps":
switch gvk.Kind {
case "Deployment":
images = append(images, utils.GetDeploymentImage(obj)...)
case "StatefulSet":
images = append(images, utils.GetStatefulSetImage(obj)...)
}
case "batch":
switch gvk.Kind {
case "Job":
images = append(images, utils.GetJobImage(obj)...)
case "CronJob":
images = append(images, utils.GetCronJobImage(obj)...)
}
}
}
imageMap := make(map[string]bool)
for _, i := range images {
imageMap[i] = true
}
images = make([]string, 0, len(imageMap))
for k := range imageMap {
images = append(images, k)
}
sort.Strings(images)
return resources, images, nil
}