-
Notifications
You must be signed in to change notification settings - Fork 75
/
configurationpackage_steps.go
143 lines (131 loc) · 4.68 KB
/
configurationpackage_steps.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
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0
package migration
import (
"fmt"
v1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
const (
errSetDeletionPolicyFmt = "failed to put the patch file to set the deletion policy to %q: %s"
errEditConfigurationPackageFmt = `failed to put the edited Configuration package: %s`
)
func (pg *PlanGenerator) convertConfigurationPackage(o UnstructuredWithMetadata) error {
pkg, err := toConfigurationPackageV1(o.Object)
if err != nil {
return err
}
// add step for disabling the dependency resolution
// for the configuration package
s := pg.stepConfiguration(stepConfigurationPackageDisableDepResolution)
p := fmt.Sprintf("%s/%s.yaml", s.Name, getVersionedName(o.Object))
s.Patch.Files = append(s.Patch.Files, p)
if err := pg.target.Put(UnstructuredWithMetadata{
Object: unstructured.Unstructured{
Object: addNameGVK(o.Object, map[string]any{
"spec": map[string]any{
"skipDependencyResolution": true,
},
}),
},
Metadata: Metadata{
Path: p,
},
}); err != nil {
return err
}
// add step for enabling the dependency resolution
// for the configuration package
s = pg.stepConfiguration(stepConfigurationPackageEnableDepResolution)
p = fmt.Sprintf("%s/%s.yaml", s.Name, getVersionedName(o.Object))
s.Patch.Files = append(s.Patch.Files, p)
if err := pg.target.Put(UnstructuredWithMetadata{
Object: unstructured.Unstructured{
Object: addNameGVK(o.Object, map[string]any{
"spec": map[string]any{
"skipDependencyResolution": false,
},
}),
},
Metadata: Metadata{
Path: p,
},
}); err != nil {
return err
}
// add the step for editing the configuration package
for _, pkgConv := range pg.registry.configurationPackageConverters {
if pkgConv.re == nil || pkgConv.converter == nil || !pkgConv.re.MatchString(pkg.Spec.Package) {
continue
}
err := pkgConv.converter.ConfigurationPackageV1(pkg)
if err != nil {
return errors.Wrapf(err, "failed to call converter on Configuration package: %s", pkg.Spec.Package)
}
// TODO: if a converter only converts a specific version,
// (or does not convert the given configuration),
// we will have a false positive. Better to compute and check
// a diff here.
target := &UnstructuredWithMetadata{
Object: ToSanitizedUnstructured(pkg),
Metadata: o.Metadata,
}
if err := pg.stepEditConfigurationPackage(o, target); err != nil {
return err
}
}
return nil
}
func (pg *PlanGenerator) stepEditConfigurationPackage(source UnstructuredWithMetadata, t *UnstructuredWithMetadata) error {
if !pg.stepEnabled(stepEditConfigurationPackage) {
return nil
}
s := pg.stepConfigurationWithSubStep(stepEditConfigurationPackage, true)
t.Metadata.Path = fmt.Sprintf("%s/%s.yaml", s.Name, getVersionedName(t.Object))
s.Patch.Files = append(s.Patch.Files, t.Metadata.Path)
patchMap, err := computeJSONMergePathDoc(source.Object, t.Object)
if err != nil {
return err
}
return errors.Wrapf(pg.target.Put(UnstructuredWithMetadata{
Object: unstructured.Unstructured{
Object: addNameGVK(t.Object, patchMap),
},
Metadata: t.Metadata,
}), errEditConfigurationPackageFmt, t.Object.GetName())
}
func (pg *PlanGenerator) stepOrhanMR(u UnstructuredWithMetadata) (bool, error) {
return pg.stepSetDeletionPolicy(u, stepOrphanMRs, v1.DeletionOrphan, true)
}
func (pg *PlanGenerator) stepRevertOrhanMR(u UnstructuredWithMetadata) (bool, error) {
return pg.stepSetDeletionPolicy(u, stepRevertOrphanMRs, v1.DeletionDelete, false)
}
func (pg *PlanGenerator) stepSetDeletionPolicy(u UnstructuredWithMetadata, step step, policy v1.DeletionPolicy, checkCurrentPolicy bool) (bool, error) {
if !pg.stepEnabled(step) {
return false, nil
}
pv := fieldpath.Pave(u.Object.Object)
p, err := pv.GetString("spec.deletionPolicy")
if err != nil && !fieldpath.IsNotFound(err) {
return false, errors.Wrapf(err, "failed to get the current deletion policy from MR: %s", u.Object.GetName())
}
if checkCurrentPolicy && err == nil && v1.DeletionPolicy(p) == policy {
return false, nil
}
s := pg.stepConfiguration(step)
u.Metadata.Path = fmt.Sprintf("%s/%s.yaml", s.Name, getVersionedName(u.Object))
s.Patch.Files = append(s.Patch.Files, u.Metadata.Path)
return true, errors.Wrapf(pg.target.Put(UnstructuredWithMetadata{
Object: unstructured.Unstructured{
Object: addNameGVK(u.Object, map[string]any{
"spec": map[string]any{
"deletionPolicy": string(policy),
},
}),
},
Metadata: u.Metadata,
}), errSetDeletionPolicyFmt, policy, u.Object.GetName())
}