/
crd_upgrade.go
209 lines (183 loc) · 6.18 KB
/
crd_upgrade.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
package remote
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/kyma-project/lifecycle-manager/api/shared"
"github.com/kyma-project/lifecycle-manager/api/v1beta2"
"github.com/kyma-project/lifecycle-manager/internal/crd"
)
type SyncCrdsUseCase struct {
crdCache *crd.Cache
}
func NewSyncCrdsUseCase(cache *crd.Cache) SyncCrdsUseCase {
if cache == nil {
return SyncCrdsUseCase{crdCache: crd.NewCache(nil)}
}
return SyncCrdsUseCase{crdCache: cache}
}
func (s *SyncCrdsUseCase) Execute(ctx context.Context, kyma *v1beta2.Kyma,
runtimeClient Client, controlPlaneClient Client,
) (bool, error) {
kymaCrdUpdated, err := s.fetchCrdsAndUpdateKymaAnnotations(ctx, controlPlaneClient,
runtimeClient, kyma, shared.KymaKind.Plural())
if err != nil {
err = client.IgnoreNotFound(err)
if err != nil {
return false, fmt.Errorf("failed to fetch module template CRDs and update Kyma annotations: %w", err)
}
}
moduleTemplateCrdUpdated, err := s.fetchCrdsAndUpdateKymaAnnotations(ctx, controlPlaneClient,
runtimeClient, kyma, shared.ModuleTemplateKind.Plural())
if err != nil {
err = client.IgnoreNotFound(err)
if err != nil {
return false, fmt.Errorf("failed to fetch kyma CRDs and update Kyma annotations: %w", err)
}
}
return kymaCrdUpdated || moduleTemplateCrdUpdated, nil
}
func PatchCRD(ctx context.Context, clnt client.Client, crd *apiextensionsv1.CustomResourceDefinition) error {
crdToApply := &apiextensionsv1.CustomResourceDefinition{}
crdToApply.SetGroupVersionKind(crd.GroupVersionKind())
crdToApply.SetName(crd.Name)
crdToApply.Spec = crd.Spec
crdToApply.Spec.Conversion.Strategy = apiextensionsv1.NoneConverter
crdToApply.Spec.Conversion.Webhook = nil
err := clnt.Patch(ctx, crdToApply,
client.Apply,
client.ForceOwnership,
client.FieldOwner(shared.OperatorName))
if err != nil {
return fmt.Errorf("failed to patch CRD: %w", err)
}
return nil
}
type CrdType string
const (
KCP CrdType = "KCP"
SKR CrdType = "SKR"
)
func updateRemoteCRD(ctx context.Context, kyma *v1beta2.Kyma, runtimeClient Client,
crdFromRuntime *apiextensionsv1.CustomResourceDefinition, kcpCrd *apiextensionsv1.CustomResourceDefinition,
) (bool, error) {
if ShouldPatchRemoteCRD(crdFromRuntime, kcpCrd, kyma) {
err := PatchCRD(ctx, runtimeClient, kcpCrd)
if err != nil {
return false, err
}
return true, nil
}
return false, nil
}
func ShouldPatchRemoteCRD(
runtimeCrd *apiextensionsv1.CustomResourceDefinition, kcpCrd *apiextensionsv1.CustomResourceDefinition,
kyma *v1beta2.Kyma,
) bool {
kcpAnnotation := getAnnotation(kcpCrd, KCP)
skrAnnotation := getAnnotation(runtimeCrd, SKR)
latestGeneration := strconv.FormatInt(kcpCrd.Generation, 10)
runtimeCRDGeneration := strconv.FormatInt(runtimeCrd.Generation, 10)
return kyma.Annotations[kcpAnnotation] != latestGeneration ||
kyma.Annotations[skrAnnotation] != runtimeCRDGeneration
}
func updateKymaAnnotations(kyma *v1beta2.Kyma, crd *apiextensionsv1.CustomResourceDefinition, crdType CrdType) {
if kyma.Annotations == nil {
kyma.Annotations = make(map[string]string)
}
annotation := getAnnotation(crd, crdType)
kyma.Annotations[annotation] = strconv.FormatInt(crd.Generation, 10)
}
func getAnnotation(crd *apiextensionsv1.CustomResourceDefinition, crdType CrdType) string {
return fmt.Sprintf("%s-%s-crd-generation", strings.ToLower(crd.Spec.Names.Kind), strings.ToLower(string(crdType)))
}
func (s *SyncCrdsUseCase) fetchCrdsAndUpdateKymaAnnotations(ctx context.Context, controlPlaneClient Client,
runtimeClient Client, kyma *v1beta2.Kyma, plural string,
) (bool, error) {
kcpCrd, skrCrd, err := s.fetchCrds(ctx, controlPlaneClient, runtimeClient, plural)
if err != nil {
return false, err
}
crdUpdated, err := updateRemoteCRD(ctx, kyma, runtimeClient, skrCrd, kcpCrd)
if err != nil {
return false, err
}
if crdUpdated {
err = runtimeClient.Get(
ctx, client.ObjectKey{
Name: fmt.Sprintf("%s.%s", plural, v1beta2.GroupVersion.Group),
}, skrCrd,
)
if err != nil {
return false, fmt.Errorf("failed to get SKR CRD: %w", err)
}
updateKymaAnnotations(kyma, kcpCrd, KCP)
updateKymaAnnotations(kyma, skrCrd, SKR)
}
return crdUpdated, nil
}
func (s *SyncCrdsUseCase) fetchCrds(ctx context.Context, controlPlaneClient Client, runtimeClient Client, plural string) (
*apiextensionsv1.CustomResourceDefinition, *apiextensionsv1.CustomResourceDefinition, error,
) {
kcpCrdName := fmt.Sprintf("%s.%s", plural, v1beta2.GroupVersion.Group)
kcpCrd, ok := s.crdCache.Get(kcpCrdName)
if !ok {
kcpCrd = apiextensionsv1.CustomResourceDefinition{}
err := controlPlaneClient.Get(
ctx, client.ObjectKey{Name: kcpCrdName}, &kcpCrd,
)
if err != nil {
return nil, nil, fmt.Errorf("failed to fetch CRDs from kcp: %w", err)
}
s.crdCache.Add(kcpCrdName, kcpCrd)
}
crdFromRuntime := &apiextensionsv1.CustomResourceDefinition{}
err := runtimeClient.Get(
ctx, client.ObjectKey{
Name: fmt.Sprintf("%s.%s", plural, v1beta2.GroupVersion.Group),
}, crdFromRuntime,
)
if err != nil {
return nil, nil, fmt.Errorf("failed to fetch CRDs from runtime: %w", err)
}
return &kcpCrd, crdFromRuntime, nil
}
func ContainsLatestVersion(crdFromRuntime *apiextensionsv1.CustomResourceDefinition, latestVersion string) bool {
for _, version := range crdFromRuntime.Spec.Versions {
if latestVersion == version.Name {
return true
}
}
return false
}
func CRDNotFoundErr(err error) bool {
groupErr := &discovery.ErrGroupDiscoveryFailed{}
if !errors.As(err, &groupErr) {
return false
}
for _, err := range groupErr.Groups {
if cannotFoundResource(err) {
return true
}
}
return false
}
func cannotFoundResource(err error) bool {
var apiStatusErr apierrors.APIStatus
if ok := errors.As(err, &apiStatusErr); ok && apiStatusErr.Status().Details != nil {
for _, cause := range apiStatusErr.Status().Details.Causes {
if cause.Type == apimetav1.CauseTypeUnexpectedServerResponse &&
strings.Contains(cause.Message, "not found") {
return true
}
}
}
return false
}