Files
velero/pkg/test/fake_controller_runtime_client.go
T
d8facf81f8 Detect VolumeGroupSnapshot API version at runtime (v1/v1beta2/v1beta1) (#10601)
* Migrate VolumeGroupSnapshot API from v1beta2 to v1

Signed-off-by: Prasad Joshi <prajoshi@redhat.com>

* Add changelog

Signed-off-by: Prasad Joshi <prajoshi@redhat.com>

* Detect VolumeGroupSnapshot API version at runtime

Velero hardcoded the VolumeGroupSnapshot (VGS) API version, so VGS backup and
restore failed on clusters that serve a different version than Velero expects.
For example OCP 5.0 serves only groupsnapshot.storage.k8s.io/v1 and OCP 4.22
serves only v1beta1, while Velero targeted a single version and failed with
"no matches for kind" on the others.

Detect the served VGS API version at runtime via the discovery client
(preferring v1 > v1beta2 > v1beta1) and perform all VGS, VGSClass and VGSContent
I/O through the dynamic client, converting to and from the canonical v1 typed
structs. The controller-runtime typed client cannot negotiate versions because
it sends the GVK of the Go type, so the dynamic client is used at the I/O
boundary instead. Owner-reference matching is now version-independent (Kind and
UID), since the CSI controller stamps refs with whatever version the cluster
serves.

This builds on the v1 migration and external-snapshotter client bump from the
previous commits.

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>

* Add changelog for VGS runtime version detection

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>

* docs: VGS supports v1/v1beta2/v1beta1 via runtime detection

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>

* Remove superseded changelog for #10600 (covered by #10601)

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>

* Use crClient + unstructured for VGS access instead of a dynamic client

Address review feedback: rather than a dedicated VGSClient wrapping
dynamic.Interface + discovery.DiscoveryInterface, perform all VolumeGroupSnapshot
I/O through the existing controller-runtime client with *unstructured.Unstructured.
The object's GVK is stamped to the served version and controller-runtime routes it
via its RESTMapper, giving the same multi-version behavior (v1/v1beta2/v1beta1)
that the typed client cannot.

This removes the extra client plumbing: no constructor signature changes to
NewRestoreFinalizerReconciler or server.go, no Factory DynamicClient/DiscoveryClient
mock churn, and no separate fake client package. The version is resolved from the
client's RESTMapper (preferring v1 > v1beta2 > v1beta1), and the helpers convert
to and from the canonical v1 typed structs.

Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>

---------

Signed-off-by: Prasad Joshi <prajoshi@redhat.com>
Signed-off-by: Shubham Pampattiwar <spampatt@redhat.com>
Co-authored-by: Prasad Joshi <prajoshi@redhat.com>
2026-09-29 11:51:42 -04:00

76 lines
2.7 KiB
Go

/*
Copyright the Velero contributors.
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 test
import (
"testing"
volumegroupsnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumegroupsnapshot/v1"
snapshotv1api "github.com/kubernetes-csi/external-snapshotter/client/v8/apis/volumesnapshot/v1"
"github.com/stretchr/testify/require"
appsv1api "k8s.io/api/apps/v1"
batchv1api "k8s.io/api/batch/v1"
corev1api "k8s.io/api/core/v1"
storagev1api "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
k8sfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
velerov2alpha1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v2alpha1"
)
func NewFakeControllerRuntimeClientBuilder(t *testing.T) *k8sfake.ClientBuilder {
t.Helper()
scheme := runtime.NewScheme()
require.NoError(t, velerov1api.AddToScheme(scheme))
require.NoError(t, velerov2alpha1api.AddToScheme(scheme))
require.NoError(t, corev1api.AddToScheme(scheme))
require.NoError(t, appsv1api.AddToScheme(scheme))
require.NoError(t, snapshotv1api.AddToScheme(scheme))
require.NoError(t, storagev1api.AddToScheme(scheme))
require.NoError(t, volumegroupsnapshotv1.AddToScheme(scheme))
return k8sfake.NewClientBuilder().WithScheme(scheme)
}
func NewFakeControllerRuntimeClient(t *testing.T, initObjs ...runtime.Object) client.Client {
t.Helper()
scheme := runtime.NewScheme()
require.NoError(t, velerov1api.AddToScheme(scheme))
require.NoError(t, velerov2alpha1api.AddToScheme(scheme))
require.NoError(t, corev1api.AddToScheme(scheme))
require.NoError(t, appsv1api.AddToScheme(scheme))
require.NoError(t, snapshotv1api.AddToScheme(scheme))
require.NoError(t, storagev1api.AddToScheme(scheme))
require.NoError(t, batchv1api.AddToScheme(scheme))
require.NoError(t, volumegroupsnapshotv1.AddToScheme(scheme))
return k8sfake.NewClientBuilder().WithScheme(scheme).WithRuntimeObjects(initObjs...).Build()
}
func NewFakeControllerRuntimeWatchClient(
t *testing.T,
initObjs ...runtime.Object,
) client.WithWatch {
t.Helper()
return NewFakeControllerRuntimeClientBuilder(t).WithRuntimeObjects(initObjs...).Build()
}