From fb926c4bc1051a336c2ac8df8d090e7af25bdf5e Mon Sep 17 00:00:00 2001 From: Mikhail Fedosin Date: Thu, 29 Jun 2023 17:55:15 +0200 Subject: [PATCH] chore: start using clusterctl context in external functions --- hack/tools/tilt-prepare/main.go | 4 ++-- test/framework/autoscaler_helpers.go | 6 +++-- test/framework/clusterctl/client.go | 28 ++++++++++++------------ test/framework/ownerreference_helpers.go | 6 +++-- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/hack/tools/tilt-prepare/main.go b/hack/tools/tilt-prepare/main.go index 195d8cb392f0..c841c061c407 100644 --- a/hack/tools/tilt-prepare/main.go +++ b/hack/tools/tilt-prepare/main.go @@ -550,14 +550,14 @@ func preLoadImageTask(image string) taskFunction { // certManagerTask generates a task for installing cert-manager if not already present. func certManagerTask() taskFunction { return func(ctx context.Context, prefix string, errCh chan error) { - config, err := config.New("") + config, err := config.New(ctx, "") if err != nil { errCh <- errors.Wrapf(err, "[%s] failed create clusterctl config", prefix) return } cluster := cluster.New(cluster.Kubeconfig{}, config) - if err := cluster.CertManager().EnsureInstalled(); err != nil { + if err := cluster.CertManager().EnsureInstalled(ctx); err != nil { errCh <- errors.Wrapf(err, "[%s] failed to install cert-manger", prefix) } } diff --git a/test/framework/autoscaler_helpers.go b/test/framework/autoscaler_helpers.go index 8601ee3f723c..8af2725d715a 100644 --- a/test/framework/autoscaler_helpers.go +++ b/test/framework/autoscaler_helpers.go @@ -220,11 +220,13 @@ type ProcessYAMLInput struct { } func ProcessYAML(input *ProcessYAMLInput) ([]byte, error) { + ctx := context.Background() + for n, v := range input.Env { _ = os.Setenv(n, v) } - c, err := clusterctlclient.New(input.ClusterctlConfigPath) + c, err := clusterctlclient.New(ctx, input.ClusterctlConfigPath) if err != nil { return nil, err } @@ -234,7 +236,7 @@ func ProcessYAML(input *ProcessYAMLInput) ([]byte, error) { }, } - printer, err := c.ProcessYAML(options) + printer, err := c.ProcessYAML(ctx, options) if err != nil { return nil, err } diff --git a/test/framework/clusterctl/client.go b/test/framework/clusterctl/client.go index 2fd4993dee37..4c11de9cc50a 100644 --- a/test/framework/clusterctl/client.go +++ b/test/framework/clusterctl/client.go @@ -61,7 +61,7 @@ type InitInput struct { } // Init calls clusterctl init with the list of providers defined in the local repository. -func Init(_ context.Context, input InitInput) { +func Init(ctx context.Context, input InitInput) { args := calculateClusterCtlInitArgs(input) log.Logf("clusterctl %s", strings.Join(args, " ")) @@ -81,10 +81,10 @@ func Init(_ context.Context, input InitInput) { WaitProviders: true, } - clusterctlClient, log := getClusterctlClientWithLogger(input.ClusterctlConfigPath, "clusterctl-init.log", input.LogFolder) + clusterctlClient, log := getClusterctlClientWithLogger(ctx, input.ClusterctlConfigPath, "clusterctl-init.log", input.LogFolder) defer log.Close() - _, err := clusterctlClient.Init(initOpt) + _, err := clusterctlClient.Init(ctx, initOpt) Expect(err).ToNot(HaveOccurred(), "failed to run clusterctl init") } @@ -209,10 +209,10 @@ func Upgrade(ctx context.Context, input UpgradeInput) { WaitProviders: true, } - clusterctlClient, log := getClusterctlClientWithLogger(input.ClusterctlConfigPath, "clusterctl-upgrade.log", input.LogFolder) + clusterctlClient, log := getClusterctlClientWithLogger(ctx, input.ClusterctlConfigPath, "clusterctl-upgrade.log", input.LogFolder) defer log.Close() - err := clusterctlClient.ApplyUpgrade(upgradeOpt) + err := clusterctlClient.ApplyUpgrade(ctx, upgradeOpt) Expect(err).ToNot(HaveOccurred(), "failed to run clusterctl upgrade") } @@ -224,7 +224,7 @@ type DeleteInput struct { } // Delete calls clusterctl delete --all. -func Delete(_ context.Context, input DeleteInput) { +func Delete(ctx context.Context, input DeleteInput) { log.Logf("clusterctl delete --all") deleteOpts := clusterctlclient.DeleteOptions{ @@ -235,10 +235,10 @@ func Delete(_ context.Context, input DeleteInput) { DeleteAll: true, } - clusterctlClient, log := getClusterctlClientWithLogger(input.ClusterctlConfigPath, "clusterctl-delete.log", input.LogFolder) + clusterctlClient, log := getClusterctlClientWithLogger(ctx, input.ClusterctlConfigPath, "clusterctl-delete.log", input.LogFolder) defer log.Close() - err := clusterctlClient.Delete(deleteOpts) + err := clusterctlClient.Delete(ctx, deleteOpts) Expect(err).ToNot(HaveOccurred(), "failed to run clusterctl upgrade") } @@ -294,10 +294,10 @@ func ConfigCluster(ctx context.Context, input ConfigClusterInput) []byte { input.ClusterctlConfigPath = outputPath } - clusterctlClient, log := getClusterctlClientWithLogger(input.ClusterctlConfigPath, fmt.Sprintf("%s-cluster-template.yaml", input.ClusterName), input.LogFolder) + clusterctlClient, log := getClusterctlClientWithLogger(ctx, input.ClusterctlConfigPath, fmt.Sprintf("%s-cluster-template.yaml", input.ClusterName), input.LogFolder) defer log.Close() - template, err := clusterctlClient.GetClusterTemplate(templateOptions) + template, err := clusterctlClient.GetClusterTemplate(ctx, templateOptions) Expect(err).ToNot(HaveOccurred(), "Failed to run clusterctl config cluster") yaml, err := template.Yaml() @@ -403,7 +403,7 @@ func Move(ctx context.Context, input MoveInput) { input.Namespace, ) - clusterctlClient, log := getClusterctlClientWithLogger(input.ClusterctlConfigPath, "clusterctl-move.log", logDir) + clusterctlClient, log := getClusterctlClientWithLogger(ctx, input.ClusterctlConfigPath, "clusterctl-move.log", logDir) defer log.Close() options := clusterctlclient.MoveOptions{ FromKubeconfig: clusterctlclient.Kubeconfig{Path: input.FromKubeconfigPath, Context: ""}, @@ -411,17 +411,17 @@ func Move(ctx context.Context, input MoveInput) { Namespace: input.Namespace, } - Expect(clusterctlClient.Move(options)).To(Succeed(), "Failed to run clusterctl move") + Expect(clusterctlClient.Move(ctx, options)).To(Succeed(), "Failed to run clusterctl move") } -func getClusterctlClientWithLogger(configPath, logName, logFolder string) (clusterctlclient.Client, *logger.LogFile) { +func getClusterctlClientWithLogger(ctx context.Context, configPath, logName, logFolder string) (clusterctlclient.Client, *logger.LogFile) { log := logger.OpenLogFile(logger.OpenLogFileInput{ LogFolder: logFolder, Name: logName, }) clusterctllog.SetLogger(log.Logger()) - c, err := clusterctlclient.New(configPath) + c, err := clusterctlclient.New(ctx, configPath) Expect(err).ToNot(HaveOccurred(), "Failed to create the clusterctl client library") return c, log } diff --git a/test/framework/ownerreference_helpers.go b/test/framework/ownerreference_helpers.go index 3628acc61aed..5c5e26d2c882 100644 --- a/test/framework/ownerreference_helpers.go +++ b/test/framework/ownerreference_helpers.go @@ -84,7 +84,9 @@ func AssertOwnerReferences(namespace, kubeconfigPath string, assertFuncs ...map[ } Eventually(func() error { allErrs := []error{} - graph, err := clusterctlcluster.GetOwnerGraph(namespace, kubeconfigPath) + ctx := context.Background() + + graph, err := clusterctlcluster.GetOwnerGraph(ctx, namespace, kubeconfigPath) Expect(err).ToNot(HaveOccurred()) for _, v := range graph { if _, ok := allAssertFuncs[v.Object.Kind]; !ok { @@ -340,7 +342,7 @@ func forceClusterClassReconcile(ctx context.Context, cli client.Client, clusterK } func removeOwnerReferences(ctx context.Context, proxy ClusterProxy, namespace string) { - graph, err := clusterctlcluster.GetOwnerGraph(namespace, proxy.GetKubeconfigPath()) + graph, err := clusterctlcluster.GetOwnerGraph(ctx, namespace, proxy.GetKubeconfigPath()) Expect(err).ToNot(HaveOccurred()) for _, object := range graph { ref := object.Object