2017-08-02 17:27:17 +00:00
|
|
|
/*
|
2020-09-01 21:15:23 +00:00
|
|
|
Copyright 2020 the Velero contributors.
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2019-06-05 17:41:03 +00:00
|
|
|
package serverstatus
|
2017-08-02 17:27:17 +00:00
|
|
|
|
|
|
|
import (
|
k8s 1.18 import (#2651)
* 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>
2020-07-16 16:21:37 +00:00
|
|
|
"context"
|
2018-12-05 17:04:57 +00:00
|
|
|
"time"
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2018-12-05 17:04:57 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-09-01 21:15:23 +00:00
|
|
|
"k8s.io/apimachinery/pkg/util/wait"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
|
2017-08-02 17:27:17 +00:00
|
|
|
|
2019-09-30 21:26:56 +00:00
|
|
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
|
|
"github.com/vmware-tanzu/velero/pkg/builder"
|
2017-08-02 17:27:17 +00:00
|
|
|
)
|
|
|
|
|
2019-06-05 17:41:03 +00:00
|
|
|
type ServerStatusGetter interface {
|
2020-09-01 21:15:23 +00:00
|
|
|
GetServerStatus(kbClient kbclient.Client) (*velerov1api.ServerStatusRequest, error)
|
2018-12-05 17:04:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:41:03 +00:00
|
|
|
type DefaultServerStatusGetter struct {
|
|
|
|
Namespace string
|
2020-09-01 21:15:23 +00:00
|
|
|
Context context.Context
|
2018-12-05 17:04:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 21:15:23 +00:00
|
|
|
func (g *DefaultServerStatusGetter) GetServerStatus(kbClient kbclient.Client) (*velerov1api.ServerStatusRequest, error) {
|
|
|
|
created := builder.ForServerStatusRequest(g.Namespace, "", "0").ObjectMeta(builder.WithGenerateName("velero-cli-")).Result()
|
2018-12-05 17:04:57 +00:00
|
|
|
|
2020-09-01 21:15:23 +00:00
|
|
|
if err := kbClient.Create(context.Background(), created, &kbclient.CreateOptions{}); err != nil {
|
2018-12-05 17:04:57 +00:00
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2020-09-01 21:15:23 +00:00
|
|
|
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.
|
2018-12-05 17:04:57 +00:00
|
|
|
// See http://issue.k8s.io/51046 for details.
|
2020-09-01 21:15:23 +00:00
|
|
|
if updated.Name != created.Name {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated.Status.Phase == velerov1api.ServerStatusRequestPhaseProcessed {
|
|
|
|
created = updated
|
|
|
|
cancel()
|
2018-12-05 17:04:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 21:15:23 +00:00
|
|
|
wait.Until(checkFunc, 250*time.Millisecond, ctx.Done())
|
|
|
|
|
|
|
|
return created, nil
|
2018-12-05 17:04:57 +00:00
|
|
|
}
|