github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/internal/agent/start.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2021-Present The Jackal Authors
     3  
     4  // Package agent holds the mutating webhook server.
     5  package agent
     6  
     7  import (
     8  	"context"
     9  	"net/http"
    10  	"os"
    11  	"os/signal"
    12  	"syscall"
    13  
    14  	"github.com/Racer159/jackal/src/config/lang"
    15  	agentHttp "github.com/Racer159/jackal/src/internal/agent/http"
    16  	"github.com/Racer159/jackal/src/pkg/message"
    17  )
    18  
    19  // Heavily influenced by https://github.com/douglasmakey/admissioncontroller and
    20  // https://github.com/slackhq/simple-kubernetes-webhook
    21  
    22  // We can hard-code these because we control the entire thing anyway.
    23  const (
    24  	httpPort = "8443"
    25  	tlsCert  = "/etc/certs/tls.crt"
    26  	tlsKey   = "/etc/certs/tls.key"
    27  )
    28  
    29  // StartWebhook launches the Jackal agent mutating webhook in the cluster.
    30  func StartWebhook() {
    31  	message.Debug("agent.StartWebhook()")
    32  
    33  	startServer(agentHttp.NewAdmissionServer(httpPort))
    34  }
    35  
    36  // StartHTTPProxy launches the jackal agent proxy in the cluster.
    37  func StartHTTPProxy() {
    38  	message.Debug("agent.StartHttpProxy()")
    39  
    40  	startServer(agentHttp.NewProxyServer(httpPort))
    41  }
    42  
    43  func startServer(server *http.Server) {
    44  	go func() {
    45  		if err := server.ListenAndServeTLS(tlsCert, tlsKey); err != nil && err != http.ErrServerClosed {
    46  			message.Fatal(err, lang.AgentErrStart)
    47  		}
    48  	}()
    49  
    50  	message.Infof(lang.AgentInfoPort, httpPort)
    51  
    52  	// listen shutdown signal
    53  	signalChan := make(chan os.Signal, 1)
    54  	signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
    55  	<-signalChan
    56  
    57  	message.Infof(lang.AgentInfoShutdown)
    58  	if err := server.Shutdown(context.Background()); err != nil {
    59  		message.Fatal(err, lang.AgentErrShutdown)
    60  	}
    61  }