Files
velero/pkg/cmd/cli/serverstatus/server_status.go
Wenkai Yin(尹文开) 63ebd4e51b
Some checks failed
Run the E2E test on kind / build (push) Failing after 8m20s
Run the E2E test on kind / setup-test-matrix (push) Successful in 5s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / Build (push) Failing after 36s
Close stale issues and PRs / stale (push) Successful in 33s
Trivy Nightly Scan / Trivy nightly scan (velero, main) (push) Failing after 3m59s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-aws, main) (push) Failing after 1m25s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-gcp, main) (push) Failing after 1m40s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-microsoft-azure, main) (push) Failing after 1m58s
Return error if timeout when checking server version (#9111)
Return error if timeout when checking server version

Fixes #8620

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
2025-07-25 12:31:55 -04:00

80 lines
2.3 KiB
Go

/*
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 serverstatus
import (
"context"
"time"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/wait"
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"
veleroclient "github.com/vmware-tanzu/velero/pkg/client"
)
type Getter interface {
GetServerStatus(kbClient kbclient.Client) (*velerov1api.ServerStatusRequest, error)
}
type DefaultServerStatusGetter struct {
Namespace string
Context context.Context
}
func (g *DefaultServerStatusGetter) GetServerStatus(kbClient kbclient.Client) (*velerov1api.ServerStatusRequest, error) {
created := builder.ForServerStatusRequest(g.Namespace, "", "0").ObjectMeta(builder.WithGenerateName("velero-cli-")).Result()
if err := veleroclient.CreateRetryGenerateName(kbClient, context.Background(), created); err != nil {
return nil, errors.WithStack(err)
}
ctx, cancel := context.WithCancel(g.Context)
defer cancel()
key := kbclient.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.
if updated.Name != created.Name {
return
}
if updated.Status.Phase == velerov1api.ServerStatusRequestPhaseProcessed {
created = updated
cancel()
}
}
wait.Until(checkFunc, 250*time.Millisecond, ctx.Done())
err := ctx.Err()
// context.Canceled error means we have received a processed ServerStatusRequest
if err == context.Canceled {
err = nil
}
return created, err
}