talos
51 строка · 1.2 Кб
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5package conditions
6
7import (
8"context"
9"fmt"
10"os"
11"time"
12
13"k8s.io/client-go/tools/clientcmd"
14)
15
16type kubeconfig string
17
18func (filename kubeconfig) Wait(ctx context.Context) error {
19ticker := time.NewTicker(time.Second)
20defer ticker.Stop()
21
22for {
23_, err := os.Stat(string(filename))
24if err != nil && !os.IsNotExist(err) {
25return err
26}
27
28_, err = clientcmd.BuildConfigFromFlags("", string(filename))
29if err == nil {
30return nil
31}
32
33// TODO: we can't check for specific error here (looking for file not found for client key/cert):
34// https://github.com/kubernetes/kubernetes/pull/105080
35
36select {
37case <-ctx.Done():
38return ctx.Err()
39case <-ticker.C:
40}
41}
42}
43
44func (filename kubeconfig) String() string {
45return fmt.Sprintf("kubeconfig %q to be ready", string(filename))
46}
47
48// WaitForKubeconfigReady is a condition that will wait for the kubeconfig to be ready.
49func WaitForKubeconfigReady(filename string) Condition {
50return kubeconfig(filename)
51}
52