Skip to content

Commit

Permalink
Merge pull request #1461 from flux-iac/wait-package-deprecations
Browse files Browse the repository at this point in the history
Fix deprecated usage of the k8s.io/apimachinery/pkg/util/wait package
  • Loading branch information
akselleirv authored Oct 14, 2024
2 parents 9d30779 + 5474bc0 commit f288d0e
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
7 changes: 4 additions & 3 deletions cmd/tfctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"os"
"strings"

"github.com/flux-iac/tofu-controller/tfctl"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"k8s.io/cli-runtime/pkg/genericclioptions"

"github.com/flux-iac/tofu-controller/tfctl"
)

var (
Expand Down Expand Up @@ -385,7 +386,7 @@ func buildReplanCmd(app *tfctl.CLI) *cobra.Command {
Example: strings.Trim(replanExamples, "\n"),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.Replan(os.Stdout, args[0])
return app.Replan(cmd.Context(), os.Stdout, args[0])
},
}
return replan
Expand All @@ -398,7 +399,7 @@ func buildBreakTheGlassCmd(app *tfctl.CLI) *cobra.Command {
Short: "Break the glass",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return app.BreakTheGlass(os.Stdout, args[0])
return app.BreakTheGlass(cmd.Context(), os.Stdout, args[0])
},
}
return breakTheGlass
Expand Down
7 changes: 4 additions & 3 deletions controllers/tf_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"strings"
"time"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
"github.com/flux-iac/tofu-controller/mtls"
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/acl"
Expand Down Expand Up @@ -61,6 +59,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
"github.com/flux-iac/tofu-controller/mtls"
)

// TerraformReconciler reconciles a Terraform object
Expand Down Expand Up @@ -388,7 +389,7 @@ func (r *TerraformReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
timeout = time.Second * 120
)
traceLog.Info("Poll function that will clean up the Runner pod")
err := wait.PollImmediate(interval, timeout, func() (bool, error) {
err := wait.PollUntilContextTimeout(ctx, interval, timeout, true, func(ctx context.Context) (bool, error) {
traceLog.Info("Get the Runner pod")
var runnerPod corev1.Pod
err := r.Get(ctx, getRunnerPodObjectKey(terraform), &runnerPod)
Expand Down
9 changes: 5 additions & 4 deletions controllers/tf_controller_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
"strings"
"time"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
"github.com/flux-iac/tofu-controller/mtls"
"github.com/flux-iac/tofu-controller/runner"
"github.com/fluxcd/pkg/runtime/logger"
"google.golang.org/grpc"
corev1 "k8s.io/api/core/v1"
Expand All @@ -22,6 +19,10 @@ import (
controllerruntime "sigs.k8s.io/controller-runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
"github.com/flux-iac/tofu-controller/mtls"
"github.com/flux-iac/tofu-controller/runner"
)

func getRunnerPodObjectKey(terraform infrav1.Terraform) types.NamespacedName {
Expand Down Expand Up @@ -347,7 +348,7 @@ func (r *TerraformReconciler) reconcileRunnerPod(ctx context.Context, terraform

runnerPod := *runnerPodTemplate.DeepCopy()
runnerPodKey := client.ObjectKeyFromObject(&runnerPod)
err = wait.PollImmediate(interval, timeout, func() (bool, error) {
err = wait.PollUntilContextTimeout(ctx, interval, timeout, true, func(ctx context.Context) (bool, error) {
err := r.Get(ctx, runnerPodKey, &runnerPod)
if err != nil && errors.IsNotFound(err) {
return true, nil
Expand Down
7 changes: 4 additions & 3 deletions internal/server/polling/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"time"

"github.com/flux-iac/tofu-controller/internal/config"
bpconfig "github.com/flux-iac/tofu-controller/internal/config"
"github.com/fluxcd/pkg/runtime/acl"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
corev1 "k8s.io/api/core/v1"
Expand All @@ -17,6 +15,9 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/flux-iac/tofu-controller/internal/config"
bpconfig "github.com/flux-iac/tofu-controller/internal/config"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
)

Expand Down Expand Up @@ -213,7 +214,7 @@ func (s *Server) deleteTerraformAndSource(ctx context.Context, tf *infrav1.Terra
}

// We have to wait for the Terraform object to be deleted before deleting the source
err = wait.PollImmediate(pollInterval, pollTimeout, func() (bool, error) {
err = wait.PollUntilContextTimeout(ctx, pollInterval, pollTimeout, true, func(ctx context.Context) (bool, error) {
if err := s.clusterClient.Get(ctx, client.ObjectKey{Name: tf.Name, Namespace: tf.Namespace}, tf); err != nil {
if errors.IsNotFound(err) {
return true, nil // Terraform object is deleted
Expand Down
17 changes: 9 additions & 8 deletions tfctl/break_glass.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,39 @@ import (
"os/exec"
"time"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
)

func (c *CLI) BreakTheGlass(out io.Writer, resource string) error {
func (c *CLI) BreakTheGlass(ctx context.Context, out io.Writer, resource string) error {
tfObject := types.NamespacedName{
Name: resource,
Namespace: c.namespace,
}

if err := requestBreakingTheGlass(context.TODO(), c.client, tfObject); err != nil {
if err := requestBreakingTheGlass(ctx, c.client, tfObject); err != nil {
return err
}
fmt.Fprintf(out, " Break the glass requested for %s/%s\n", c.namespace, resource)
if err := requestReconciliation(context.TODO(), c.client, tfObject); err != nil {
if err := requestReconciliation(ctx, c.client, tfObject); err != nil {
return err
}

defer func() {
err := removeBreakingTheGlass(context.TODO(), c.client, tfObject)
err := removeBreakingTheGlass(ctx, c.client, tfObject)
if err != nil {
fmt.Fprintf(out, " Failed to remove break the glass annotation for %s/%s\n", c.namespace, resource)
}
}()

terraform := &infrav1.Terraform{}
err := wait.PollImmediate(5*time.Second, 5*time.Minute, func() (bool, error) {
err := c.client.Get(context.TODO(), tfObject, terraform)
err := wait.PollUntilContextTimeout(ctx, 5*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
err := c.client.Get(ctx, tfObject, terraform)
if err != nil {
return false, nil
}
Expand All @@ -63,7 +64,7 @@ func (c *CLI) BreakTheGlass(out io.Writer, resource string) error {
return err
}

shell(context.TODO(), c.kubeconfigArgs, tfObject)
shell(ctx, c.kubeconfigArgs, tfObject)

return nil
}
Expand Down
13 changes: 7 additions & 6 deletions tfctl/replan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"io"
"time"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
"github.com/fluxcd/pkg/apis/meta"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"

infrav1 "github.com/flux-iac/tofu-controller/api/v1alpha2"
)

// clear plan pending
Expand All @@ -22,25 +23,25 @@ import (
// print the plan output

// Replan re-plans the given Terraform resource
func (c *CLI) Replan(out io.Writer, resource string) error {
func (c *CLI) Replan(ctx context.Context, out io.Writer, resource string) error {
key := types.NamespacedName{
Name: resource,
Namespace: c.namespace,
}

if err := replan(context.TODO(), c.client, key); err != nil {
if err := replan(ctx, c.client, key); err != nil {
return err
}

if err := requestReconciliation(context.TODO(), c.client, key); err != nil {
if err := requestReconciliation(ctx, c.client, key); err != nil {
return err
}
fmt.Fprintf(out, " Replan requested for %s/%s\n", c.namespace, resource)

// wait for the plan to be ready, 4 loops, 30 seconds each
if err := wait.Poll(1*time.Second, 120*time.Second, func() (bool, error) {
if err := wait.PollUntilContextTimeout(ctx, 1*time.Second, 120*time.Second, true, func(ctx context.Context) (bool, error) {
terraform := &infrav1.Terraform{}
if err := c.client.Get(context.TODO(), key, terraform); err != nil {
if err := c.client.Get(ctx, key, terraform); err != nil {
return false, err
}

Expand Down

0 comments on commit f288d0e

Please sign in to comment.