mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-24 08:54:30 +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:
co-authored by
Ashish Amarnath
parent
aed504a0fd
commit
c952932f1b
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright 2019 the Velero contributors.
|
||||
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.
|
||||
@@ -21,78 +21,53 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
"github.com/vmware-tanzu/velero/pkg/builder"
|
||||
velerov1client "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned/typed/velero/v1"
|
||||
)
|
||||
|
||||
type ServerStatusGetter interface {
|
||||
GetServerStatus(client velerov1client.ServerStatusRequestsGetter) (*velerov1api.ServerStatusRequest, error)
|
||||
GetServerStatus(kbClient kbclient.Client) (*velerov1api.ServerStatusRequest, error)
|
||||
}
|
||||
|
||||
type DefaultServerStatusGetter struct {
|
||||
Namespace string
|
||||
Timeout time.Duration
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
func (g *DefaultServerStatusGetter) GetServerStatus(client velerov1client.ServerStatusRequestsGetter) (*velerov1api.ServerStatusRequest, error) {
|
||||
req := builder.ForServerStatusRequest(g.Namespace, "").
|
||||
ObjectMeta(
|
||||
builder.WithGenerateName("velero-cli-"),
|
||||
).Result()
|
||||
func (g *DefaultServerStatusGetter) GetServerStatus(kbClient kbclient.Client) (*velerov1api.ServerStatusRequest, error) {
|
||||
created := builder.ForServerStatusRequest(g.Namespace, "", "0").ObjectMeta(builder.WithGenerateName("velero-cli-")).Result()
|
||||
|
||||
created, err := client.ServerStatusRequests(g.Namespace).Create(context.TODO(), req, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
if err := kbClient.Create(context.Background(), created, &kbclient.CreateOptions{}); err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
defer client.ServerStatusRequests(g.Namespace).Delete(context.TODO(), created.Name, metav1.DeleteOptions{})
|
||||
|
||||
listOptions := metav1.ListOptions{
|
||||
// TODO: once the minimum supported Kubernetes version is v1.9.0, uncomment the following line.
|
||||
ctx, cancel := context.WithCancel(g.Context)
|
||||
defer cancel()
|
||||
|
||||
key := client.ObjectKey{Name: created.Name, Namespace: g.Namespace}
|
||||
checkFunc := func() {
|
||||
updated := &velerov1api.ServerStatusRequest{}
|
||||
if err := kbClient.Get(ctx, key, updated); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: once the minimum supported Kubernetes version is v1.9.0, remove the following check.
|
||||
// See http://issue.k8s.io/51046 for details.
|
||||
//FieldSelector: "metadata.name=" + req.Name
|
||||
ResourceVersion: created.ResourceVersion,
|
||||
}
|
||||
watcher, err := client.ServerStatusRequests(g.Namespace).Watch(context.TODO(), listOptions)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
defer watcher.Stop()
|
||||
if updated.Name != created.Name {
|
||||
return
|
||||
}
|
||||
|
||||
expired := time.NewTimer(g.Timeout)
|
||||
defer expired.Stop()
|
||||
|
||||
Loop:
|
||||
for {
|
||||
select {
|
||||
case <-expired.C:
|
||||
return nil, errors.New("timed out waiting for server status request to be processed")
|
||||
case e := <-watcher.ResultChan():
|
||||
updated, ok := e.Object.(*velerov1api.ServerStatusRequest)
|
||||
if !ok {
|
||||
return nil, errors.Errorf("unexpected type %T", e.Object)
|
||||
}
|
||||
|
||||
// TODO: once the minimum supported Kubernetes version is v1.9.0, remove the following check.
|
||||
// See http://issue.k8s.io/51046 for details.
|
||||
if updated.Name != created.Name {
|
||||
continue
|
||||
}
|
||||
|
||||
switch e.Type {
|
||||
case watch.Deleted:
|
||||
return nil, errors.New("server status request was unexpectedly deleted")
|
||||
case watch.Modified:
|
||||
if updated.Status.Phase == velerov1api.ServerStatusRequestPhaseProcessed {
|
||||
req = updated
|
||||
break Loop
|
||||
}
|
||||
}
|
||||
if updated.Status.Phase == velerov1api.ServerStatusRequestPhaseProcessed {
|
||||
created = updated
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
return req, nil
|
||||
wait.Until(checkFunc, 250*time.Millisecond, ctx.Done())
|
||||
|
||||
return created, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user