Skip to content

Commit

Permalink
feat: some minor changes added address flag and liveness handler
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 bd4237a commit d991dec
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import (
)

var policies []string
var address string

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

var serveCmd = &cobra.Command{
Use: "serve",
Short: "Start the kyverno-envoy-plugin server",
Run: func(cmd *cobra.Command, args []string) {
srv := server.NewServers(policies)
srv := server.NewServers(policies, address)
server.StartServers(srv)
},
}
Expand Down
37 changes: 34 additions & 3 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package server

import (
"context"
"strings"

"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
Expand All @@ -27,6 +29,7 @@ import (

type extAuthzServerV3 struct {
policies []string
address string
}

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

func NewServers(policies []string) *Servers {
func NewServers(policies []string, address string) *Servers {
return &Servers{
grpcV3: &extAuthzServerV3{
policies: policies,
address: address,
},
}
}
Expand Down Expand Up @@ -89,12 +93,39 @@ func handler(w http.ResponseWriter, r *http.Request) {
}

func (s *Servers) startGRPCServer(ctx context.Context) {
lis, err := net.Listen("tcp", ":9000")

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

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

var lis net.Listener

switch parsedURL.Scheme {
case "unix":
socketPath := parsedURL.Host + parsedURL.Path
if strings.HasPrefix(parsedURL.String(), parsedURL.Scheme+"://@") {
socketPath = "@" + socketPath
} else {
os.Remove(socketPath)
}
lis, err = net.Listen("unix", socketPath)
case "grpc":
lis, err = net.Listen("tcp", parsedURL.Host)
default:
err = fmt.Errorf("invalid url schema %q", parsedURL.Scheme)
}

if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s.grpcServer = grpc.NewServer()
fmt.Println("Starting GRPC server on Port 9000")
log.Printf("Starting GRPC server on %s", s.grpcV3.address)

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

Expand Down

0 comments on commit d991dec

Please sign in to comment.