@@ -10,13 +10,13 @@ import (
10
10
)
11
11
12
12
const (
13
- HTTPServer = "http"
14
- GRPCServer = "grpc"
15
- MetricsEndpointFlag = "metrics"
16
- HealthzEndpointFlag = "healthz"
17
- PProfEndpointFlag = "pprof"
18
- ListenEndpointFlag = "address"
19
- PyroscopeAgentFlag = "pyroscope"
13
+ HTTPServer = "http"
14
+ GRPCServer = "grpc"
15
+ MetricsEndpointFlag = "metrics"
16
+ HealthzEndpointFlag = "healthz"
17
+ PProfEndpointFlag = "pprof"
18
+ ListenEndpointFlag = "address"
19
+ PyroscopeAgentEndpointFlag = "pyroscope"
20
20
)
21
21
22
22
// TODO: this should be extract to be under 'pkg/cmd/flags' once we remove the binary tracee-rules.
@@ -39,60 +39,75 @@ func PrepareServer(serverSlice []string) (*Server, error) {
39
39
enablePProf = false
40
40
enablePyroscope = false
41
41
)
42
- grpcAddr := ""
43
42
for _ , endpoint := range serverSlice {
44
- if strings .Contains (endpoint , HTTPServer ) {
45
- if strings .Contains (endpoint , MetricsEndpointFlag ) {
46
- if strings .Contains (endpoint , "true" ) {
43
+ // split flag http.address or grpc.address for example
44
+ serverParts := strings .SplitN (endpoint , "." , 2 )
45
+ switch serverParts [0 ] {
46
+ //flag http.Xxx
47
+ case HTTPServer :
48
+ httpParts := strings .SplitN (serverParts [1 ], "=" , 1 )
49
+ switch httpParts [0 ] {
50
+ case ListenEndpointFlag :
51
+ server .HTTPServer = http .New (httpParts [1 ])
52
+ case MetricsEndpointFlag :
53
+ if strings .Compare (httpParts [1 ], "true" ) == 0 {
47
54
enableMetrics = true
48
55
}
49
- } else if strings . Contains ( endpoint , HealthzEndpointFlag ) {
50
- if strings .Contains ( endpoint , "true" ) {
56
+ case HealthzEndpointFlag :
57
+ if strings .Compare ( httpParts [ 1 ] , "true" ) == 0 {
51
58
enableHealthz = true
52
59
}
53
- } else if strings . Contains ( endpoint , PProfEndpointFlag ) {
54
- if strings .Contains ( endpoint , "true" ) {
60
+ case PProfEndpointFlag :
61
+ if strings .Compare ( httpParts [ 1 ] , "true" ) == 0 {
55
62
enablePProf = true
56
63
}
57
- } else if strings . Contains ( endpoint , PyroscopeAgentFlag ) {
58
- if strings .Contains (endpoint , "true" ) {
64
+ case PyroscopeAgentEndpointFlag :
65
+ if strings .Compare (endpoint , "true" ) == 0 {
59
66
enablePyroscope = true
60
67
}
61
- } else if strings .Contains (endpoint , ListenEndpointFlag ) {
62
- server .HTTPServer = http .New (endpoint [len (HTTPServer )+ 1 + len (ListenEndpointFlag ):])
63
- } else {
68
+ default :
64
69
server .HTTPServer = http .New ("" )
65
70
}
66
- } else if strings .Contains (endpoint , GRPCServer ) {
67
- if strings .Contains (endpoint , ListenEndpointFlag ) {
68
- grpcAddr = endpoint [len (GRPCServer )+ 1 + len (ListenEndpointFlag ):]
69
-
70
- addrParts := strings .SplitN (grpcAddr , ":" , 2 )
71
- protocol := addrParts [0 ]
72
-
73
- if protocol != "tcp" && protocol != "unix" {
74
- return nil , errfmt .Errorf ("grpc supported protocols are tcp or unix. eg: tcp:4466, unix:/tmp/tracee.sock" )
75
- }
76
-
77
- if len (addrParts ) == 1 {
78
- if protocol == "tcp" {
79
- grpcAddr += ":4466"
80
- } else { // protocol == "unix"
81
- grpcAddr += ":/var/run/tracee.sock"
71
+ //flag grpc.Xxx
72
+ case GRPCServer :
73
+ grpcParts := strings .SplitN (serverParts [1 ], "=" , 1 )
74
+ switch grpcParts [0 ] {
75
+ case ListenEndpointFlag :
76
+ addressParts := strings .SplitN (grpcParts [1 ], ":" , 2 )
77
+ switch addressParts [0 ] {
78
+ case "unix" :
79
+ if len (addressParts ) == 1 {
80
+ addressParts = append (addressParts , "/var/run/tracee.sock" )
82
81
}
83
- }
84
- // cleanup listen address if needed (unix socket), for example if a panic happened
85
- if protocol == "unix" {
86
- path := strings .SplitN (grpcAddr , ":" , 2 )[1 ]
87
- if _ , err = os .Stat (path ); err == nil {
88
- err = os .Remove (path )
82
+ if _ , err = os .Stat (addressParts [1 ]); err == nil {
83
+ err = os .Remove (addressParts [1 ])
89
84
if err != nil {
90
- return nil , errfmt .Errorf ("failed to cleanup gRPC listening address (%s): %v" , path , err )
85
+ return nil , errfmt .Errorf ("failed to cleanup gRPC listening address (%s): %v" , addressParts [ 1 ] , err )
91
86
}
92
87
}
88
+ case "tcp" :
89
+ if len (addressParts ) == 1 {
90
+ addressParts = append (addressParts , "4466" )
91
+ }
92
+ default :
93
+ return nil , errfmt .Errorf ("grpc supported protocols are tcp or unix. eg: tcp:4466, unix:/tmp/tracee.sock" )
94
+ }
95
+ server .GRPCServer , err = grpc .New (addressParts [0 ], addressParts [1 ])
96
+ if err != nil {
97
+ return nil , err
93
98
}
94
- server .GRPCServer , err = grpc .New ("dsa" , "dsa" ) //protocol, grpcAddr)
95
99
100
+ default :
101
+ if _ , err = os .Stat ("/var/run/tracee.sock" ); err == nil {
102
+ err = os .Remove ("/var/run/tracee.sock" )
103
+ if err != nil {
104
+ return nil , errfmt .Errorf ("failed to cleanup gRPC listening address (%s): %v" , "/var/run/tracee.sock" , err )
105
+ }
106
+ }
107
+ server .GRPCServer , err = grpc .New ("unix" , "/var/run/tracee.sock" )
108
+ if err != nil {
109
+ return nil , err
110
+ }
96
111
}
97
112
}
98
113
}
0 commit comments