Skip to content

Commit 8f692d7

Browse files
Merge pull request #2909 from Nordix/add-crdmigrator/adil
✨ Implement CRD migration
2 parents acdfcba + dbe3230 commit 8f692d7

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

config/rbac/role.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ rules:
4444
- patch
4545
- update
4646
- watch
47+
- apiGroups:
48+
- apiextensions.k8s.io
49+
resources:
50+
- customresourcedefinitions
51+
verbs:
52+
- get
53+
- list
54+
- watch
55+
- apiGroups:
56+
- apiextensions.k8s.io
57+
resourceNames:
58+
- metal3clusters.infrastructure.cluster.x-k8s.io
59+
- metal3clustertemplates.infrastructure.cluster.x-k8s.io
60+
- metal3datas.infrastructure.cluster.x-k8s.io
61+
- metal3datatemplates.infrastructure.cluster.x-k8s.io
62+
- metal3machines.infrastructure.cluster.x-k8s.io
63+
- metal3machinetemplates.infrastructure.cluster.x-k8s.io
64+
resources:
65+
- customresourcedefinitions
66+
- customresourcedefinitions/status
67+
verbs:
68+
- patch
69+
- update
4770
- apiGroups:
4871
- authentication.k8s.io
4972
resources:

main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
appsv1 "k8s.io/api/apps/v1"
3737
corev1 "k8s.io/api/core/v1"
3838
storagev1 "k8s.io/api/storage/v1"
39+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3940
"k8s.io/apimachinery/pkg/labels"
4041
"k8s.io/apimachinery/pkg/runtime"
4142
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -55,6 +56,7 @@ import (
5556
capipamv1beta1 "sigs.k8s.io/cluster-api/api/ipam/v1beta1"
5657
capipamv1 "sigs.k8s.io/cluster-api/api/ipam/v1beta2"
5758
"sigs.k8s.io/cluster-api/controllers/clustercache"
59+
"sigs.k8s.io/cluster-api/controllers/crdmigrator"
5860
"sigs.k8s.io/cluster-api/controllers/remote"
5961
"sigs.k8s.io/cluster-api/util/flags"
6062
ctrl "sigs.k8s.io/controller-runtime"
@@ -106,6 +108,7 @@ var (
106108
logOptions = logs.NewOptions()
107109
enableBMHNameBasedPreallocation bool
108110
managerOptions = flags.ManagerOptions{}
111+
skipCRDMigrationPhases []string
109112
)
110113

111114
func init() {
@@ -125,11 +128,17 @@ func init() {
125128

126129
// BMO Operator schemes
127130
_ = bmov1alpha1.AddToScheme(myscheme)
131+
132+
// Add apiextensions scheme
133+
_ = apiextensionsv1.AddToScheme(myscheme)
128134
}
129135

130136
// Add RBAC for the authorized diagnostics endpoint.
131137
// +kubebuilder:rbac:groups=authentication.k8s.io,resources=tokenreviews,verbs=create
132138
// +kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create
139+
// ADD CRD RBAC for CRD Migrator.
140+
// +kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions,verbs=get;list;watch
141+
// +kubebuilder:rbac:groups=apiextensions.k8s.io,resources=customresourcedefinitions;customresourcedefinitions/status,verbs=update;patch,resourceNames=metal3clusters.infrastructure.cluster.x-k8s.io;metal3clustertemplates.infrastructure.cluster.x-k8s.io;metal3machines.infrastructure.cluster.x-k8s.io;metal3machinetemplates.infrastructure.cluster.x-k8s.io;metal3datas.infrastructure.cluster.x-k8s.io;metal3datatemplates.infrastructure.cluster.x-k8s.io
133142

134143
func main() {
135144
initFlags(pflag.CommandLine)
@@ -361,6 +370,9 @@ func initFlags(fs *pflag.FlagSet) {
361370
"Maximum number of queries that should be allowed in one burst from the controller client to the Kubernetes API server. Default 30")
362371

363372
flags.AddManagerOptions(fs, &managerOptions)
373+
374+
fs.StringArrayVar(&skipCRDMigrationPhases, "skip-crd-migration-phases", []string{},
375+
"List of CRD migration phases to skip. Valid values are: StorageVersionMigration, CleanupManagedFields.")
364376
}
365377

366378
func waitForAPIs(cfg *rest.Config) error {
@@ -401,6 +413,43 @@ func setupChecks(mgr ctrl.Manager) {
401413
}
402414

403415
func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {
416+
crdMigratorConfig := map[client.Object]crdmigrator.ByObjectConfig{
417+
&infrav1.Metal3Cluster{}: {
418+
UseCache: true,
419+
},
420+
&infrav1.Metal3ClusterTemplate{}: {
421+
UseCache: false,
422+
},
423+
&infrav1.Metal3Machine{}: {
424+
UseCache: true,
425+
},
426+
&infrav1.Metal3MachineTemplate{}: {
427+
UseCache: true,
428+
},
429+
&infrav1.Metal3Data{}: {
430+
UseCache: true,
431+
},
432+
&infrav1.Metal3DataTemplate{}: {
433+
UseCache: true,
434+
},
435+
}
436+
437+
crdMigratorSkipPhases := []crdmigrator.Phase{}
438+
for _, p := range skipCRDMigrationPhases {
439+
crdMigratorSkipPhases = append(crdMigratorSkipPhases, crdmigrator.Phase(p))
440+
}
441+
if err := (&crdmigrator.CRDMigrator{
442+
Client: mgr.GetClient(),
443+
APIReader: mgr.GetAPIReader(),
444+
SkipCRDMigrationPhases: crdMigratorSkipPhases,
445+
Config: crdMigratorConfig,
446+
// The CRDMigrator is run with only concurrency 1 to ensure we don't overwhelm the apiserver by patching a
447+
// lot of CRs concurrently.
448+
}).SetupWithManager(ctx, mgr, concurrency(1)); err != nil {
449+
setupLog.Error(err, "Unable to create controller", "controller", "CRDMigrator")
450+
os.Exit(1)
451+
}
452+
404453
secretCachingClient, err := client.New(mgr.GetConfig(), client.Options{
405454
HTTPClient: mgr.GetHTTPClient(),
406455
Cache: &client.CacheOptions{

0 commit comments

Comments
 (0)