mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-05 04:55:22 +00:00
* k8s 1.18 import wip backup, cmd, controller, generated, restic, restore, serverstatusrequest, test and util Signed-off-by: Andrew Lavery <laverya@umich.edu> * go mod tidy Signed-off-by: Andrew Lavery <laverya@umich.edu> * add changelog file Signed-off-by: Andrew Lavery <laverya@umich.edu> * go fmt Signed-off-by: Andrew Lavery <laverya@umich.edu> * update code-generator and controller-gen in CI Signed-off-by: Andrew Lavery <laverya@umich.edu> * checkout proper code-generator version, regen Signed-off-by: Andrew Lavery <laverya@umich.edu> * fix remaining calls Signed-off-by: Andrew Lavery <laverya@umich.edu> * regenerate CRDs with ./hack/update-generated-crd-code.sh Signed-off-by: Andrew Lavery <laverya@umich.edu> * use existing context in restic and server Signed-off-by: Andrew Lavery <laverya@umich.edu> * fix test cases by resetting resource version also use main library go context, not golang.org/x/net/context, in pkg/restore/restore.go Signed-off-by: Andrew Lavery <laverya@umich.edu> * clarify changelog message Signed-off-by: Andrew Lavery <laverya@umich.edu> * use github.com/kubernetes-csi/external-snapshotter/v2@v2.2.0-rc1 Signed-off-by: Andrew Lavery <laverya@umich.edu> * run 'go mod tidy' to remove old external-snapshotter version Signed-off-by: Andrew Lavery <laverya@umich.edu>
99 lines
3.0 KiB
Go
99 lines
3.0 KiB
Go
/*
|
|
Copyright 2019 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 serverstatus
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/pkg/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/watch"
|
|
|
|
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)
|
|
}
|
|
|
|
type DefaultServerStatusGetter struct {
|
|
Namespace string
|
|
Timeout time.Duration
|
|
}
|
|
|
|
func (g *DefaultServerStatusGetter) GetServerStatus(client velerov1client.ServerStatusRequestsGetter) (*velerov1api.ServerStatusRequest, error) {
|
|
req := builder.ForServerStatusRequest(g.Namespace, "").
|
|
ObjectMeta(
|
|
builder.WithGenerateName("velero-cli-"),
|
|
).Result()
|
|
|
|
created, err := client.ServerStatusRequests(g.Namespace).Create(context.TODO(), req, metav1.CreateOptions{})
|
|
if 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.
|
|
// 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()
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return req, nil
|
|
}
|