-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
141 lines (116 loc) · 2.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/infisical/infisical-csi-provider/internal/server"
"github.com/infisical/infisical-csi-provider/internal/version"
"google.golang.org/grpc"
pb "sigs.k8s.io/secrets-store-csi-driver/provider/v1alpha1"
)
var (
hostUrl = flag.String("host-url", "https://app.infisical.com", "infisical instance URL")
endpoint = flag.String("endpoint", "/tmp/infisical.socket", "Path to socket on which to listen for driver gRPC calls.")
healthPort = flag.String("health-port", "8080", "Port for HTTP health check.")
)
// ListenAndServe accepts incoming connections on a Unix socket. It is a blocking method.
// Returns non-nil error unless Close or Shutdown is called.
func listenAndServe(gs *grpc.Server) error {
if !strings.HasPrefix(*endpoint, "@") {
err := os.Remove(*endpoint)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to delete the socket file, error: %w", err)
}
}
ln, err := net.Listen("unix", *endpoint)
if err != nil {
return err
}
defer ln.Close()
s := &server.Server{
HostUrl: *hostUrl,
}
pb.RegisterCSIDriverProviderServer(gs, s)
log.Printf("Listening on socket: %s\n", *endpoint)
return gs.Serve(ln)
}
func shutdown(gs *grpc.Server) {
if gs != nil {
gs.GracefulStop()
}
}
func startHealthCheck() chan error {
mux := http.NewServeMux()
ms := http.Server{
Addr: fmt.Sprintf(":%s", *healthPort),
Handler: mux,
}
errorCh := make(chan error)
mux.HandleFunc("/health/ready", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
log.Printf("Initializing health check %+v", *healthPort)
go func() {
defer func() {
err := ms.Shutdown(context.Background())
if err != nil {
log.Printf("error shutting down health handler: %+v", err)
}
}()
select {
case errorCh <- ms.ListenAndServe():
default:
}
}()
return errorCh
}
func startProviderServer() chan error {
gs := grpc.NewServer(
grpc.ConnectionTimeout(20 * time.Second),
)
errorCh := make(chan error)
log.Printf("Starting up provider server with build version: %s\n", version.BuildVersion)
go func() {
defer func() {
shutdown(gs)
close(errorCh)
}()
select {
case errorCh <- listenAndServe(gs):
default:
}
}()
return errorCh
}
func realMain() error {
signalsChan := make(chan os.Signal, 1)
signal.Notify(signalsChan, syscall.SIGINT, syscall.SIGTERM)
healthErrorChan := startHealthCheck()
providerErrorChan := startProviderServer()
for {
select {
case sig := <-signalsChan:
return fmt.Errorf("captured %v, shutting down provider", sig)
case providerErr := <-providerErrorChan:
return providerErr
case healthErr := <-healthErrorChan:
return healthErr
}
}
}
func main() {
flag.Parse()
err := realMain()
if err != nil {
log.Printf("error running provider: %+v", err)
os.Exit(1)
}
}