mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-05 04:55:22 +00:00
Migrate ServerStatusRequest controller and resource to kubebuilder (#2838)
* Convert ServerStatusRequest controller to controller-runtime Signed-off-by: Carlisia <carlisia@vmware.com> * Add select stm Signed-off-by: Carlisia <carlisia@vmware.com> * Fixed status patch bug Signed-off-by: Carlisia <carlisia@vmware.com> * Add mgr start Signed-off-by: Carlisia <carlisia@vmware.com> * Trying to sync Signed-off-by: Carlisia <carlisia@vmware.com> * Clean async now Signed-off-by: Carlisia <carlisia@vmware.com> * Clean up + move context out Signed-off-by: Carlisia <carlisia@vmware.com> * Bug: not closing the channel Signed-off-by: Carlisia <carlisia@vmware.com> * Clean up some tests Signed-off-by: Carlisia <carlisia@vmware.com> * Much better way to fetch an update using a backoff loop Signed-off-by: Carlisia <carlisia@vmware.com> * Even better way to retry: use apimachinery lib Signed-off-by: Carlisia <carlisia@vmware.com> * Refactor controller + add test Signed-off-by: Carlisia <carlisia@vmware.com> * partially fix unit tests Signed-off-by: Ashish Amarnath <ashisham@vmware.com> * Fix and add tests Signed-off-by: Carlisia <carlisia@vmware.com> * Add changelog Signed-off-by: Carlisia <carlisia@vmware.com> * Add ability to disable the controller + cleanups Signed-off-by: Carlisia <carlisia@vmware.com> * Fix bug w/ disabling controllers + fix test + clean up Signed-off-by: Carlisia <carlisia@vmware.com> * Move role.yaml to the correct folder Signed-off-by: Carlisia <carlisia@vmware.com> * Add sample serverstatusrequest.yaml Signed-off-by: Carlisia <carlisia@vmware.com> * Add requeue + better formatting Signed-off-by: Carlisia <carlisia@vmware.com> * Increase # of max concurrent reconciles Signed-off-by: Carlisia <carlisia@vmware.com> Co-authored-by: Ashish Amarnath <ashisham@vmware.com>
This commit is contained in:
270
pkg/controller/server_status_request_controller_test.go
Normal file
270
pkg/controller/server_status_request_controller_test.go
Normal file
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
Copyright 2020 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 controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/clock"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
"github.com/vmware-tanzu/velero/internal/velero"
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
"github.com/vmware-tanzu/velero/pkg/builder"
|
||||
"github.com/vmware-tanzu/velero/pkg/buildinfo"
|
||||
"github.com/vmware-tanzu/velero/pkg/plugin/framework"
|
||||
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
||||
)
|
||||
|
||||
func statusRequestBuilder(resourceVersion string) *builder.ServerStatusRequestBuilder {
|
||||
return builder.ForServerStatusRequest(velerov1api.DefaultNamespace, "sr-1", resourceVersion)
|
||||
}
|
||||
|
||||
var _ = Describe("Server Status Request Reconciler", func() {
|
||||
BeforeEach(func() {})
|
||||
AfterEach(func() {})
|
||||
|
||||
It("Should successfully patch a server status request object status phase", func() {
|
||||
// now will be used to set the fake clock's time; capture
|
||||
// it here so it can be referenced in the test case defs.
|
||||
now, err := time.Parse(time.RFC1123, time.RFC1123)
|
||||
Expect(err).To(BeNil())
|
||||
now = now.Local()
|
||||
|
||||
tests := []struct {
|
||||
req *velerov1api.ServerStatusRequest
|
||||
reqPluginLister *fakePluginLister
|
||||
expected *velerov1api.ServerStatusRequest
|
||||
expectedRequeue ctrl.Result
|
||||
expectedErrMsg string
|
||||
}{
|
||||
{
|
||||
// server status request with phase=empty will be processed
|
||||
req: statusRequestBuilder("1").
|
||||
ServerVersion(buildinfo.Version).
|
||||
ProcessedTimestamp(now).
|
||||
Plugins([]velerov1api.PluginInfo{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
}).
|
||||
Result(),
|
||||
reqPluginLister: &fakePluginLister{
|
||||
plugins: []framework.PluginIdentifier{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: statusRequestBuilder("1").
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now).
|
||||
Plugins([]velerov1api.PluginInfo{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
}).
|
||||
Result(),
|
||||
expectedRequeue: ctrl.Result{Requeue: false, RequeueAfter: statusRequestResyncPeriod},
|
||||
},
|
||||
{
|
||||
// server status request with phase=new will be processed
|
||||
req: statusRequestBuilder("1").
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase(velerov1api.ServerStatusRequestPhaseNew).
|
||||
ProcessedTimestamp(now).
|
||||
Plugins([]velerov1api.PluginInfo{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
}).
|
||||
Result(),
|
||||
reqPluginLister: &fakePluginLister{
|
||||
plugins: []framework.PluginIdentifier{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: statusRequestBuilder("1").
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now).
|
||||
Plugins([]velerov1api.PluginInfo{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
}).
|
||||
Result(),
|
||||
expectedRequeue: ctrl.Result{Requeue: false, RequeueAfter: statusRequestResyncPeriod},
|
||||
},
|
||||
{
|
||||
// server status request with phase=Processed does not get deleted if not expired
|
||||
req: statusRequestBuilder("1").
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now). // not yet expired
|
||||
Plugins([]velerov1api.PluginInfo{
|
||||
{
|
||||
Name: "custom.io/myotherown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
}).
|
||||
Result(),
|
||||
reqPluginLister: &fakePluginLister{
|
||||
plugins: []framework.PluginIdentifier{
|
||||
{
|
||||
Name: "custom.io/myotherown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: statusRequestBuilder("1").
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now).
|
||||
Plugins([]velerov1api.PluginInfo{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
}).
|
||||
Result(),
|
||||
expectedRequeue: ctrl.Result{Requeue: false, RequeueAfter: statusRequestResyncPeriod},
|
||||
},
|
||||
{
|
||||
// server status request with phase=Processed gets deleted if expire
|
||||
req: statusRequestBuilder("1").
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
|
||||
ProcessedTimestamp(now.Add(-61 * time.Second)). // expired
|
||||
Plugins([]velerov1api.PluginInfo{
|
||||
{
|
||||
Name: "custom.io/myotherown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
}).
|
||||
Result(),
|
||||
reqPluginLister: &fakePluginLister{
|
||||
plugins: []framework.PluginIdentifier{
|
||||
{
|
||||
Name: "custom.io/myotherown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: nil,
|
||||
expectedRequeue: ctrl.Result{Requeue: false, RequeueAfter: statusRequestResyncPeriod},
|
||||
},
|
||||
{
|
||||
// server status request with invalid phase returns an error and does not requeue
|
||||
req: statusRequestBuilder("1").
|
||||
ServerVersion(buildinfo.Version).
|
||||
Phase("an-invalid-phase").
|
||||
ProcessedTimestamp(now).
|
||||
Plugins([]velerov1api.PluginInfo{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
}).
|
||||
Result(),
|
||||
reqPluginLister: &fakePluginLister{
|
||||
plugins: []framework.PluginIdentifier{
|
||||
{
|
||||
Name: "custom.io/myown",
|
||||
Kind: "VolumeSnapshotter",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedErrMsg: "unexpected ServerStatusRequest phase",
|
||||
expectedRequeue: ctrl.Result{Requeue: false, RequeueAfter: 0},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
// Setup reconciler
|
||||
Expect(velerov1api.AddToScheme(scheme.Scheme)).To(Succeed())
|
||||
serverStatusInfo := velero.ServerStatus{
|
||||
PluginRegistry: test.reqPluginLister,
|
||||
Clock: clock.NewFakeClock(now),
|
||||
}
|
||||
r := ServerStatusRequestReconciler{
|
||||
Client: fake.NewFakeClientWithScheme(scheme.Scheme, test.req),
|
||||
ServerStatus: serverStatusInfo,
|
||||
Ctx: context.Background(),
|
||||
Log: velerotest.NewLogger(),
|
||||
}
|
||||
|
||||
actualResult, err := r.Reconcile(ctrl.Request{
|
||||
NamespacedName: types.NamespacedName{
|
||||
Namespace: velerov1api.DefaultNamespace,
|
||||
Name: test.req.Name,
|
||||
},
|
||||
})
|
||||
|
||||
Expect(actualResult).To(BeEquivalentTo(test.expectedRequeue))
|
||||
if test.expectedErrMsg == "" {
|
||||
Expect(err).To(BeNil())
|
||||
} else {
|
||||
Expect(err.Error()).To(BeEquivalentTo(test.expectedErrMsg))
|
||||
return
|
||||
}
|
||||
|
||||
instance := &velerov1api.ServerStatusRequest{}
|
||||
err = r.Client.Get(ctx, kbclient.ObjectKey{Name: test.req.Name, Namespace: test.req.Namespace}, instance)
|
||||
|
||||
// Assertions
|
||||
if test.expected == nil {
|
||||
Expect(apierrors.IsNotFound(err)).To(BeTrue())
|
||||
} else {
|
||||
Expect(err).To(BeNil())
|
||||
Eventually(instance.Status.Phase == test.expected.Status.Phase, timeout).Should(BeTrue())
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
type fakePluginLister struct {
|
||||
plugins []framework.PluginIdentifier
|
||||
}
|
||||
|
||||
func (l *fakePluginLister) List(kind framework.PluginKind) []framework.PluginIdentifier {
|
||||
var plugins []framework.PluginIdentifier
|
||||
for _, plugin := range l.plugins {
|
||||
if plugin.Kind == kind {
|
||||
plugins = append(plugins, plugin)
|
||||
}
|
||||
}
|
||||
|
||||
return plugins
|
||||
}
|
||||
Reference in New Issue
Block a user