Change acmesolver parameters and catch stopCh for server shutdown
Signed-off-by: JoshVanL <vleeuwenjoshua@gmail.com>
This commit is contained in:
parent
d45847d302
commit
8f924151a3
@ -7,6 +7,8 @@ go_library(
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//pkg/issuer/acme/http/solver:go_default_library",
|
||||
"//pkg/logs:go_default_library",
|
||||
"//pkg/util:go_default_library",
|
||||
"@com_github_spf13_cobra//:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
@ -19,30 +19,32 @@ package app
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jetstack/cert-manager/pkg/issuer/acme/http/solver"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/jetstack/cert-manager/pkg/issuer/acme/http/solver"
|
||||
logf "github.com/jetstack/cert-manager/pkg/logs"
|
||||
"github.com/jetstack/cert-manager/pkg/util"
|
||||
)
|
||||
|
||||
func NewACMESolverCommand(ctx context.Context) *cobra.Command {
|
||||
var (
|
||||
listenPort int
|
||||
domain string
|
||||
token string
|
||||
key string
|
||||
)
|
||||
func NewACMESolverCommand(stopCh <-chan struct{}) *cobra.Command {
|
||||
s := new(solver.HTTP01Solver)
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "acmesolver",
|
||||
Short: "HTTP server used to solver ACME challenges.",
|
||||
Short: "HTTP server used to solve ACME challenges.",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
s := &solver.HTTP01Solver{
|
||||
ListenPort: listenPort,
|
||||
Domain: domain,
|
||||
Token: token,
|
||||
Key: key,
|
||||
}
|
||||
rootCtx := util.ContextWithStopCh(context.Background(), stopCh)
|
||||
rootCtx = logf.NewContext(rootCtx, nil, "acmesolver")
|
||||
log := logf.FromContext(rootCtx)
|
||||
|
||||
if err := s.Listen(ctx); err != nil {
|
||||
go func() {
|
||||
<-stopCh
|
||||
if err := s.Shutdown(rootCtx); err != nil {
|
||||
log.Error(err, "error shutting down acmesolver server")
|
||||
}
|
||||
}()
|
||||
|
||||
if err := s.Listen(log); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -50,10 +52,10 @@ func NewACMESolverCommand(ctx context.Context) *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().IntVar(&listenPort, "listen-port", 8089, "the port number to listen on for connections")
|
||||
cmd.Flags().StringVar(&domain, "domain", "", "the domain name to verify")
|
||||
cmd.Flags().StringVar(&token, "token", "", "the challenge token to verify against")
|
||||
cmd.Flags().StringVar(&key, "key", "", "the challenge key to respond with")
|
||||
cmd.Flags().IntVar(&s.ListenPort, "listen-port", 8089, "the port number to listen on for connections")
|
||||
cmd.Flags().StringVar(&s.Domain, "domain", "", "the domain name to verify")
|
||||
cmd.Flags().StringVar(&s.Token, "token", "", "the challenge token to verify against")
|
||||
cmd.Flags().StringVar(&s.Key, "key", "", "the challenge key to respond with")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
@ -17,7 +17,6 @@ limitations under the License.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
@ -31,16 +30,7 @@ import (
|
||||
|
||||
func main() {
|
||||
stopCh := utilcmd.SetupSignalHandler()
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
cmd := app.NewACMESolverCommand(ctx)
|
||||
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-stopCh:
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
cmd := app.NewACMESolverCommand(stopCh)
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", err)
|
||||
|
||||
@ -8,7 +8,7 @@ go_library(
|
||||
],
|
||||
importpath = "github.com/jetstack/cert-manager/pkg/issuer/acme/http/solver",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = ["//pkg/logs:go_default_library"],
|
||||
deps = ["@com_github_go_logr_logr//:go_default_library"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
|
||||
@ -17,13 +17,12 @@ limitations under the License.
|
||||
package solver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
logf "github.com/jetstack/cert-manager/pkg/logs"
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
type HTTP01Solver struct {
|
||||
@ -32,10 +31,11 @@ type HTTP01Solver struct {
|
||||
Domain string
|
||||
Token string
|
||||
Key string
|
||||
|
||||
http.Server
|
||||
}
|
||||
|
||||
func (h *HTTP01Solver) Listen(ctx context.Context) error {
|
||||
log := logf.FromContext(ctx)
|
||||
func (h *HTTP01Solver) Listen(log logr.Logger) error {
|
||||
log.Info("starting listener",
|
||||
"expected_domain", h.Domain,
|
||||
"expected_token", h.Token,
|
||||
@ -89,5 +89,11 @@ func (h *HTTP01Solver) Listen(ctx context.Context) error {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprint(w, h.Key)
|
||||
})
|
||||
return http.ListenAndServe(fmt.Sprintf(":%d", h.ListenPort), handler)
|
||||
|
||||
h.Server = http.Server{
|
||||
Addr: fmt.Sprintf(":%d", h.ListenPort),
|
||||
Handler: handler,
|
||||
}
|
||||
|
||||
return h.Server.ListenAndServe()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user