Skip to content

Commit

Permalink
Merge pull request #52 from boxboat/fix-inifinite-loop-edge-case
Browse files Browse the repository at this point in the history
Exit if unable to retrieve a certificate after 10 attempts
  • Loading branch information
matthewdevenny authored May 31, 2023
2 parents 4b6474f + 9cc83e6 commit 01310aa
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"

dockhand "github.com/boxboat/dockhand-secrets-operator/pkg/apis/dhs.dockhand.dev/v1alpha2"
"github.com/boxboat/dockhand-secrets-operator/pkg/common"
"github.com/boxboat/dockhand-secrets-operator/pkg/k8s"
"github.com/boxboat/dockhand-secrets-operator/pkg/webhook"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/leaderelection"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)

type ServerArgs struct {
Expand Down Expand Up @@ -60,7 +61,7 @@ func runCertManager(ctx context.Context) {
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: onStartedLeading,
OnStoppedLeading: onStoppedLeading,
OnNewLeader: onNewLeader(serverArgs.serviceId),
OnNewLeader: onNewLeader(serverArgs.serviceId),
},
WatchDog: nil,
ReleaseOnCancel: true,
Expand Down Expand Up @@ -130,7 +131,7 @@ func ensureTLSCertificateSecretInCluster(ctx context.Context) {
common.Log.Warnf("Could not update deployment %v", err)
}
} else {
err = k8s.UpdateCABundleForWebhook(ctx, serverArgs.serviceName + ".dhs.dockhand.dev", caPem)
err = k8s.UpdateCABundleForWebhook(ctx, serverArgs.serviceName+".dhs.dockhand.dev", caPem)
common.LogIfError(err)
}

Expand Down Expand Up @@ -161,16 +162,17 @@ func runServer(ctx context.Context) {
break
}
attempt += 1
} else {
common.ExitIfError(fmt.Errorf("unable to retrieve a certificate after 10 attempts - exiting"))
}
}

common.Log.Infof("Starting server")

server := &webhook.Server{
Server: &http.Server{
Addr: fmt.Sprintf(":%v", serverArgs.serverPort),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{tlsPair},
},
Addr: fmt.Sprintf(":%v", serverArgs.serverPort),
TLSConfig: &tls.Config{Certificates: []tls.Certificate{tlsPair}},
},
}

Expand Down

0 comments on commit 01310aa

Please sign in to comment.