Skip to content

Commit

Permalink
chore: start using clusterctl context in external functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedosin committed Jul 4, 2023
1 parent 47d5e3c commit c6a948a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
4 changes: 2 additions & 2 deletions hack/tools/tilt-prepare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,14 +542,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)
}
}
Expand Down
6 changes: 4 additions & 2 deletions test/framework/autoscaler_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
28 changes: 14 additions & 14 deletions test/framework/clusterctl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " "))

Expand All @@ -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")
}

Expand Down Expand Up @@ -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")
}

Expand All @@ -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{
Expand All @@ -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")
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -403,25 +403,25 @@ 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: ""},
ToKubeconfig: clusterctlclient.Kubeconfig{Path: input.ToKubeconfigPath, Context: ""},
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
}
Expand Down
6 changes: 4 additions & 2 deletions test/framework/ownerreference_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).To(BeNil())
for _, v := range graph {
if _, ok := allAssertFuncs[v.Object.Kind]; !ok {
Expand Down Expand Up @@ -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).To(BeNil())
for _, object := range graph {
ref := object.Object
Expand Down

0 comments on commit c6a948a

Please sign in to comment.