Skip to content

Commit

Permalink
Add context with timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
alenkacz committed Dec 30, 2020
1 parent 92be7d6 commit e9a3ed2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
12 changes: 10 additions & 2 deletions pkg/cmd/verify/deployments.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package verify

import (
"context"
"fmt"
"github.com/alenkacz/cert-manager-verifier/pkg/verify"
"github.com/spf13/cobra"
Expand All @@ -9,8 +10,11 @@ import (
"k8s.io/client-go/kubernetes"
"os"
"github.com/sirupsen/logrus"
"time"
)

const defaultTimeout = 2 * time.Minute

type Options struct {
ConfigFlags *genericclioptions.ConfigFlags
Streams *genericclioptions.IOStreams
Expand Down Expand Up @@ -47,6 +51,7 @@ func NewCmd() *cobra.Command {
rootCmd.SilenceUsage = true
// TODO add flag to configure cm namespace
// TODO add flag to specify CM version and verify version
// TODO make timeout configurable

return rootCmd
}
Expand All @@ -55,6 +60,9 @@ func (o *Options) Execute() error {
logrus.SetOutput(o.Streams.Out)
logrus.SetFormatter(SimpleFormatter{})

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

if o.ConfigFlags.Namespace == nil {
cmn := "cert-manager"
o.ConfigFlags.Namespace = &cmn
Expand All @@ -74,13 +82,13 @@ func (o *Options) Execute() error {

deployments := verify.DeploymentDefinitionDefault()
logrus.Infof("Waiting for following deployments in namespace %s:\n%s", deployments.Namespace, formatDeploymentNames(deployments.Names))
result := verify.DeploymentsReady(kubeClient, deployments)
result := verify.DeploymentsReady(ctx, kubeClient, deployments)
logrus.Infof(formatDeploymentResult(result))

if !allReady(result) {
return fmt.Errorf("FAILED! Not all deployments are ready.")
}
err = verify.WaitForTestCertificate(dynamicClient)
err = verify.WaitForTestCertificate(ctx, dynamicClient)
if err != nil {
logrus.Infof("error when waiting for certificate to be ready: %v", err)
return err
Expand Down
9 changes: 6 additions & 3 deletions pkg/verify/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ var namespace = &unstructured.Unstructured{

// TODO support also other API versions
// TODO make it possible to execute this inside namespace, not creating one
func WaitForTestCertificate(dynamicClient dynamic.Interface) error {
func WaitForTestCertificate(ctx context.Context, dynamicClient dynamic.Interface) error {
if err := ctx.Err(); err != nil {
return fmt.Errorf("Timeout reached: %v", err)
}
cert := certificate("cert-manager-test", group, version)
resources := []*unstructured.Unstructured{namespace, issuer("cert-manager-test", group, version), cert}
// defer cleanupTestResources(dynamicClient, resources)
defer cleanupTestResources(dynamicClient, resources)

for _, res := range resources {
err := createResource(dynamicClient, res)
Expand All @@ -46,7 +49,7 @@ func WaitForTestCertificate(dynamicClient dynamic.Interface) error {
}
}
poller := &certPoller{dynamicClient, cert}
return wait.PollImmediate(defaultPollInterval, defaultMaxWait, poller.certificateReady)
return wait.PollImmediateUntil(defaultPollInterval, poller.certificateReady, ctx.Done())
}

type certPoller struct {
Expand Down
15 changes: 12 additions & 3 deletions pkg/verify/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ type DeploymentResult struct {
// TODO make this configurable
// TODO have a global timeout for all deployments
const defaultPollInterval = 100 * time.Millisecond
const defaultMaxWait = 10 * time.Second

func DeploymentsReady(kubeClient *kubernetes.Clientset, deployments DeploymentDefinition) []DeploymentResult {
func DeploymentsReady(ctx context.Context, kubeClient *kubernetes.Clientset, deployments DeploymentDefinition) []DeploymentResult {
ctx.Deadline()
result := []DeploymentResult{}
for _, d := range deployments.Names {
if err := ctx.Err(); err != nil {
dr := DeploymentResult{
Name: d,
Error: fmt.Errorf("Timeout reached: %v", err),
}
result = append(result, dr)
continue
}

poller := &poller{kubeClient, d, deployments.Namespace}
err := wait.PollImmediate(defaultPollInterval, defaultMaxWait, poller.deploymentReady)
err := wait.PollImmediateUntil(defaultPollInterval, poller.deploymentReady, ctx.Done())
dr := DeploymentResult{
Name: d,
Ready: true,
Expand Down

0 comments on commit e9a3ed2

Please sign in to comment.