/
migrator.go
126 lines (100 loc) · 3.69 KB
/
migrator.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
/*
Copyright 2020 The Knative 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 storageversion
import (
"context"
"fmt"
apix "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apixclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/pager"
)
// Migrator will read custom resource definitions and upgrade
// the associated resources to the latest storage version
type Migrator struct {
dynamicClient dynamic.Interface
apixClient apixclient.Interface
}
// NewMigrator will return a new Migrator
func NewMigrator(d dynamic.Interface, a apixclient.Interface) *Migrator {
return &Migrator{
dynamicClient: d,
apixClient: a,
}
}
// Migrate takes a group resource (ie. resource.some.group.dev) and
// updates instances of the resource to the latest storage version
//
// This is done by listing all the resources and performing an empty patch
// which triggers a migration on the K8s API server
//
// Finally the migrator will update the CRD's status and drop older storage
// versions
func (m *Migrator) Migrate(ctx context.Context, gr schema.GroupResource) error {
crdClient := m.apixClient.ApiextensionsV1().CustomResourceDefinitions()
crd, err := crdClient.Get(ctx, gr.String(), metav1.GetOptions{})
if err != nil {
return fmt.Errorf("unable to fetch crd %s - %w", gr, err)
}
version := storageVersion(crd)
if version == "" {
return fmt.Errorf("unable to determine storage version for %s", gr)
}
// don't migrate storage version if CRD has a single valid storage in its status
if len(crd.Status.StoredVersions) == 1 && crd.Status.StoredVersions[0] == version {
return nil
}
if err := m.migrateResources(ctx, gr.WithVersion(version)); err != nil {
return err
}
patch := `{"status":{"storedVersions":["` + version + `"]}}`
_, err = crdClient.Patch(ctx, crd.Name, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}, "status")
if err != nil {
return fmt.Errorf("unable to drop storage version definition %s - %w", gr, err)
}
return nil
}
func (m *Migrator) migrateResources(ctx context.Context, gvr schema.GroupVersionResource) error {
client := m.dynamicClient.Resource(gvr)
listFunc := func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
return client.Namespace(metav1.NamespaceAll).List(ctx, opts)
}
onEach := func(obj runtime.Object) error {
item := obj.(metav1.Object)
_, err := client.Namespace(item.GetNamespace()).
Patch(ctx, item.GetName(), types.MergePatchType, []byte("{}"), metav1.PatchOptions{})
if err != nil && !apierrs.IsNotFound(err) {
return fmt.Errorf("unable to patch resource %s/%s (gvr: %s) - %w",
item.GetNamespace(), item.GetName(),
gvr, err)
}
return nil
}
pager := pager.New(listFunc)
return pager.EachListItem(ctx, metav1.ListOptions{}, onEach)
}
func storageVersion(crd *apix.CustomResourceDefinition) string {
var version string
for _, v := range crd.Spec.Versions {
if v.Storage {
version = v.Name
break
}
}
return version
}