Skip to content

Commit 56efcd9

Browse files
committed
Add wait-kubeconfig-timeout flag to clusterctl
Signed-off-by: Vince Prignano <[email protected]>
1 parent 0b215f4 commit 56efcd9

8 files changed

+38
-20
lines changed

cmd/clusterctl/clusterdeployer/clusterdeployer.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package clusterdeployer
1919
import (
2020
"fmt"
2121
"strings"
22+
"time"
2223

2324
"github.com/pkg/errors"
2425
"k8s.io/klog"
@@ -36,6 +37,7 @@ type ClusterDeployer struct {
3637
addonComponents string
3738
bootstrapComponents string
3839
cleanupBootstrapCluster bool
40+
waitKubeConfigTimeout time.Duration
3941
}
4042

4143
func New(
@@ -44,14 +46,16 @@ func New(
4446
providerComponents string,
4547
addonComponents string,
4648
bootstrapComponents string,
47-
cleanupBootstrapCluster bool) *ClusterDeployer {
49+
cleanupBootstrapCluster bool,
50+
waitKubeConfigTimeout time.Duration) *ClusterDeployer {
4851
return &ClusterDeployer{
4952
bootstrapProvisioner: bootstrapProvisioner,
5053
clientFactory: clientFactory,
5154
providerComponents: providerComponents,
5255
addonComponents: addonComponents,
5356
bootstrapComponents: bootstrapComponents,
5457
cleanupBootstrapCluster: cleanupBootstrapCluster,
58+
waitKubeConfigTimeout: waitKubeConfigTimeout,
5559
}
5660
}
5761

@@ -100,7 +104,8 @@ func (d *ClusterDeployer) Create(cluster *clusterv1.Cluster, machines []*cluster
100104
}
101105

102106
klog.Info("Creating target cluster")
103-
targetKubeconfig, err := phases.GetKubeconfig(bootstrapClient, provider, kubeconfigOutput, cluster.Name, cluster.Namespace)
107+
targetKubeconfig, err := phases.GetKubeconfig(bootstrapClient, provider, kubeconfigOutput,
108+
cluster.Name, cluster.Namespace, d.waitKubeConfigTimeout)
104109
if err != nil {
105110
return fmt.Errorf("unable to create target cluster kubeconfig: %v", err)
106111
}

cmd/clusterctl/clusterdeployer/clusterdeployer_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"io/ioutil"
2222
"os"
2323
"testing"
24+
"time"
2425

2526
"github.com/pkg/errors"
2627
apiv1 "k8s.io/api/core/v1"
@@ -846,7 +847,7 @@ func TestClusterCreate(t *testing.T) {
846847

847848
pcStore := mockProviderComponentsStore{}
848849
pcFactory := mockProviderComponentsStoreFactory{NewFromCoreclientsetPCStore: &pcStore}
849-
d := New(p, f, "", "", bootstrapComponent, testcase.cleanupExternal)
850+
d := New(p, f, "", "", bootstrapComponent, testcase.cleanupExternal, 1*time.Minute)
850851

851852
inputMachines := make(map[string][]*clusterv1.Machine)
852853

@@ -961,7 +962,7 @@ func TestCreateProviderComponentsScenarios(t *testing.T) {
961962
pcFactory := mockProviderComponentsStoreFactory{NewFromCoreclientsetPCStore: &tc.pcStore}
962963
providerComponentsYaml := "---\nyaml: definition"
963964
addonsYaml := "---\nyaml: definition"
964-
d := New(p, f, providerComponentsYaml, addonsYaml, "", false)
965+
d := New(p, f, providerComponentsYaml, addonsYaml, "", false, 1*time.Minute)
965966
err := d.Create(inputCluster, inputMachines, pd, kubeconfigOut, &pcFactory)
966967
if err == nil && tc.expectedError != "" {
967968
t.Fatalf("error mismatch: got '%v', want '%v'", err, tc.expectedError)
@@ -1102,7 +1103,7 @@ func TestDeleteCleanupExternalCluster(t *testing.T) {
11021103
f := newTestClusterClientFactory()
11031104
f.clusterClients[bootstrapKubeconfig] = tc.bootstrapClient
11041105
f.clusterClients[targetKubeconfig] = tc.targetClient
1105-
d := New(p, f, "", "", "", tc.cleanupExternalCluster)
1106+
d := New(p, f, "", "", "", tc.cleanupExternalCluster, 1*time.Minute)
11061107
err := d.Delete(tc.targetClient)
11071108
if err != nil || tc.expectedErrorMessage != "" {
11081109
if err == nil {
@@ -1348,7 +1349,7 @@ func TestClusterDelete(t *testing.T) {
13481349
f := newTestClusterClientFactory()
13491350
f.clusterClients[bootstrapKubeconfig] = testCase.bootstrapClient
13501351
f.ClusterClientErr = testCase.NewCoreClientsetErr
1351-
d := New(p, f, "", "", "", true)
1352+
d := New(p, f, "", "", "", true, 1*time.Minute)
13521353

13531354
err := d.Delete(testCase.targetClient)
13541355
if err != nil || testCase.expectedErrorMessage != "" {

cmd/clusterctl/cmd/alpha_phase_get_kubeconfig.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package cmd
1919
import (
2020
"fmt"
2121
"io/ioutil"
22+
"time"
2223

2324
"github.com/spf13/cobra"
2425
"k8s.io/klog"
@@ -27,11 +28,12 @@ import (
2728
)
2829

2930
type AlphaPhaseGetKubeconfigOptions struct {
30-
ClusterName string
31-
Kubeconfig string
32-
KubeconfigOutput string
33-
Namespace string
34-
Provider string
31+
ClusterName string
32+
Kubeconfig string
33+
KubeconfigOutput string
34+
Namespace string
35+
Provider string
36+
WaitKubeConfigTimeout time.Duration
3537
}
3638

3739
var pgko = &AlphaPhaseGetKubeconfigOptions{}
@@ -76,7 +78,7 @@ func RunAlphaPhaseGetKubeconfig(pgko *AlphaPhaseGetKubeconfigOptions) error {
7678
return err
7779
}
7880

79-
if _, err := phases.GetKubeconfig(client, provider, pgko.KubeconfigOutput, pgko.ClusterName, pgko.Namespace); err != nil {
81+
if _, err := phases.GetKubeconfig(client, provider, pgko.KubeconfigOutput, pgko.ClusterName, pgko.Namespace, pgko.WaitKubeConfigTimeout); err != nil {
8082
return fmt.Errorf("unable to get kubeconfig: %v", err)
8183
}
8284

@@ -93,5 +95,6 @@ func init() {
9395
// Optional flags
9496
alphaPhaseGetKubeconfigCmd.Flags().StringVarP(&pgko.KubeconfigOutput, "kubeconfig-out", "", "kubeconfig", "Where to output the kubeconfig for the provisioned cluster")
9597
alphaPhaseGetKubeconfigCmd.Flags().StringVarP(&pgko.Namespace, "namespace", "n", "", "Namespace")
98+
alphaPhaseGetKubeconfigCmd.Flags().DurationVarP(&pgko.WaitKubeConfigTimeout, "wait-kubeconfig-timeout", "", 20*time.Minute, "The total time to wait for KubeConfig to be available before exiting with an error")
9699
alphaPhasesCmd.AddCommand(alphaPhaseGetKubeconfigCmd)
97100
}

cmd/clusterctl/cmd/create_cluster.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818

1919
import (
2020
"io/ioutil"
21+
"time"
2122

2223
"github.com/pkg/errors"
2324
"github.com/spf13/cobra"
@@ -39,6 +40,7 @@ type CreateOptions struct {
3940
Provider string
4041
KubeconfigOutput string
4142
BootstrapFlags bootstrap.Options
43+
WaitKubeConfigTimeout time.Duration
4244
}
4345

4446
var co = &CreateOptions{}
@@ -107,7 +109,9 @@ func RunCreate(co *CreateOptions) error {
107109
string(pc),
108110
string(ac),
109111
string(bc),
110-
co.BootstrapFlags.Cleanup)
112+
co.BootstrapFlags.Cleanup,
113+
co.WaitKubeConfigTimeout,
114+
)
111115

112116
return d.Create(c, m, pd, co.KubeconfigOutput, pcsFactory)
113117
}
@@ -128,6 +132,7 @@ func init() {
128132
createClusterCmd.Flags().StringVarP(&co.AddonComponents, "addon-components", "a", "", "A yaml file containing cluster addons to apply to the internal cluster")
129133
createClusterCmd.Flags().StringVarP(&co.BootstrapOnlyComponents, "bootstrap-only-components", "", "", "A yaml file containing components to apply only on the bootstrap cluster (before the provider components are applied) but not the provisioned cluster")
130134
createClusterCmd.Flags().StringVarP(&co.KubeconfigOutput, "kubeconfig-out", "", "kubeconfig", "Where to output the kubeconfig for the provisioned cluster")
135+
createClusterCmd.Flags().DurationVarP(&co.WaitKubeConfigTimeout, "wait-kubeconfig-timeout", "", 20*time.Minute, "The total time to wait for KubeConfig to be available before exiting with an error")
131136

132137
co.BootstrapFlags.AddFlags(createClusterCmd.Flags())
133138
createCmd.AddCommand(createClusterCmd)

cmd/clusterctl/cmd/delete_cluster.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package cmd
1818

1919
import (
20+
"time"
21+
2022
"github.com/pkg/errors"
2123
"github.com/spf13/cobra"
2224
v1 "k8s.io/api/core/v1"
@@ -89,7 +91,8 @@ func RunDelete() error {
8991
providerComponents,
9092
"",
9193
"",
92-
do.BootstrapFlags.Cleanup)
94+
do.BootstrapFlags.Cleanup,
95+
1*time.Minute)
9396

9497
return deployer.Delete(clusterClient)
9598
}

cmd/clusterctl/phases/getkubeconfig.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ import (
2929
)
3030

3131
const (
32-
retryKubeConfigReady = 10 * time.Second
33-
timeoutKubeconfigReady = 20 * time.Minute
32+
retryKubeConfigReady = 10 * time.Second
3433
)
3534

3635
// GetKubeconfig returns a kubeconfig for the target cluster
37-
func GetKubeconfig(bootstrapClient clusterclient.Client, provider provider.Deployer, kubeconfigOutput string, clusterName, namespace string) (string, error) {
36+
func GetKubeconfig(bootstrapClient clusterclient.Client, provider provider.Deployer, kubeconfigOutput string, clusterName, namespace string, timeout time.Duration) (string, error) {
3837
klog.V(1).Info("Getting target cluster kubeconfig.")
39-
targetKubeconfig, err := waitForKubeconfigReady(bootstrapClient, provider, clusterName, namespace)
38+
targetKubeconfig, err := waitForKubeconfigReady(bootstrapClient, provider, clusterName, namespace, timeout)
4039
if err != nil {
4140
return "", fmt.Errorf("unable to get target cluster kubeconfig: %v", err)
4241
}
@@ -48,9 +47,9 @@ func GetKubeconfig(bootstrapClient clusterclient.Client, provider provider.Deplo
4847
return targetKubeconfig, nil
4948
}
5049

51-
func waitForKubeconfigReady(bootstrapClient clusterclient.Client, provider provider.Deployer, clusterName, namespace string) (string, error) {
50+
func waitForKubeconfigReady(bootstrapClient clusterclient.Client, provider provider.Deployer, clusterName, namespace string, timeout time.Duration) (string, error) {
5251
kubeconfig := ""
53-
err := util.PollImmediate(retryKubeConfigReady, timeoutKubeconfigReady, func() (bool, error) {
52+
err := util.PollImmediate(retryKubeConfigReady, timeout, func() (bool, error) {
5453
cluster, controlPlane, _, err := clusterclient.GetClusterAPIObject(bootstrapClient, clusterName, namespace)
5554
if err != nil {
5655
return false, err

cmd/clusterctl/testdata/create-cluster-no-args-invalid-flag.golden

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Flags:
1515
-m, --machines string A yaml file containing machine object definition(s). Required.
1616
--provider string Which provider deployment logic to use. Required.
1717
-p, --provider-components string A yaml file containing cluster api provider controllers and supporting objects. Required.
18+
--wait-kubeconfig-timeout duration The total time to wait for KubeConfig to be available before exiting with an error (default 20m0s)
1819

1920
Global Flags:
2021
--alsologtostderr log to standard error as well as files

cmd/clusterctl/testdata/create-cluster-no-args.golden

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Flags:
1515
-m, --machines string A yaml file containing machine object definition(s). Required.
1616
--provider string Which provider deployment logic to use. Required.
1717
-p, --provider-components string A yaml file containing cluster api provider controllers and supporting objects. Required.
18+
--wait-kubeconfig-timeout duration The total time to wait for KubeConfig to be available before exiting with an error (default 20m0s)
1819

1920
Global Flags:
2021
--alsologtostderr log to standard error as well as files

0 commit comments

Comments
 (0)