Skip to content

Commit

Permalink
added health address flag and health handler func
Browse files Browse the repository at this point in the history
Signed-off-by: Sanskarzz <[email protected]>
  • Loading branch information
Sanskarzz authored and anushkamittal2001 committed Jun 17, 2024
1 parent d991dec commit b68dc3a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import (

var policies []string
var address string
var healthaddress string

func init() {
serveCmd.Flags().StringSliceVar(&policies, "policy", nil, "Path to kyverno-json policies")
serveCmd.Flags().StringVar(&address, "address", ":9000", "Address to listen on")
serveCmd.Flags().StringVar(&healthaddress, "healthaddress", ":8181", "Address to listen on for health checks")
}

var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start the kyverno-envoy-plugin server",
Run: func(cmd *cobra.Command, args []string) {
srv := server.NewServers(policies, address)
srv := server.NewServers(policies, address, healthaddress)
server.StartServers(srv)
},
}
Expand Down
45 changes: 28 additions & 17 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"fmt"
"io"
"log"
"net"
"net/http"
Expand All @@ -28,8 +27,9 @@ import (
)

type extAuthzServerV3 struct {
policies []string
address string
policies []string
address string
healthaddress string
}

type Servers struct {
Expand All @@ -38,11 +38,12 @@ type Servers struct {
grpcV3 *extAuthzServerV3
}

func NewServers(policies []string, address string) *Servers {
func NewServers(policies []string, address string, healthaddress string) *Servers {
return &Servers{
grpcV3: &extAuthzServerV3{
policies: policies,
address: address,
policies: policies,
address: address,
healthaddress: healthaddress,
},
}
}
Expand All @@ -60,11 +61,22 @@ func StartServers(srv *Servers) {
}

func (s *Servers) startHTTPServer(ctx context.Context) {

healthaddress := s.grpcV3.healthaddress
if !strings.Contains(healthaddress, "://") {
healthaddress = "http://" + healthaddress
}

parsedURL, err := url.Parse(healthaddress)
if err != nil {
log.Fatalf("failed to parse address url: %v", err)
}

s.httpServer = &http.Server{
Addr: ":8000",
Handler: http.HandlerFunc(handler),
Addr: parsedURL.Host,
Handler: http.HandlerFunc(healthHandler),
}
fmt.Println("Starting HTTP server on Port 8000")
log.Printf("Starting HTTP health checks on port %s", parsedURL.Host)
go func() {
<-ctx.Done()

Expand All @@ -81,15 +93,14 @@ func (s *Servers) startHTTPServer(ctx context.Context) {
}
}

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Received request from %s %s\n", r.RemoteAddr, r.URL.Path)
body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusInternalServerError)
func healthHandler(w http.ResponseWriter, r *http.Request) {
// Check if the request path is /health
if r.URL.Path == "/health" {
// Return a 200 OK status to indicate the server is healthy
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
return
}
defer r.Body.Close()
fmt.Println("Request payload:", string(body))
}

func (s *Servers) startGRPCServer(ctx context.Context) {
Expand Down Expand Up @@ -125,7 +136,7 @@ func (s *Servers) startGRPCServer(ctx context.Context) {
log.Fatalf("failed to listen: %v", err)
}
s.grpcServer = grpc.NewServer()
log.Printf("Starting GRPC server on %s", s.grpcV3.address)
log.Printf("Starting GRPC server on port %s", parsedURL.Host)

authv3.RegisterAuthorizationServer(s.grpcServer, s.grpcV3)

Expand Down

0 comments on commit b68dc3a

Please sign in to comment.