forked from danehans/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_handlers.go
143 lines (118 loc) · 6.22 KB
/
api_handlers.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
142
143
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package cmd
import (
"context"
"net/http"
"github.com/cilium/hive/cell"
"github.com/go-openapi/runtime/middleware"
"github.com/cilium/cilium/api/v1/server/restapi/daemon"
"github.com/cilium/cilium/api/v1/server/restapi/endpoint"
"github.com/cilium/cilium/api/v1/server/restapi/policy"
"github.com/cilium/cilium/pkg/api"
"github.com/cilium/cilium/pkg/option"
"github.com/cilium/cilium/pkg/promise"
)
type handlersOut struct {
cell.Out
DaemonGetDebuginfoHandler daemon.GetDebuginfoHandler
DaemonGetHealthzHandler daemon.GetHealthzHandler
EndpointDeleteEndpointHandler endpoint.DeleteEndpointHandler
EndpointDeleteEndpointIDHandler endpoint.DeleteEndpointIDHandler
EndpointGetEndpointHandler endpoint.GetEndpointHandler
EndpointGetEndpointIDConfigHandler endpoint.GetEndpointIDConfigHandler
EndpointGetEndpointIDHandler endpoint.GetEndpointIDHandler
EndpointGetEndpointIDHealthzHandler endpoint.GetEndpointIDHealthzHandler
EndpointGetEndpointIDLabelsHandler endpoint.GetEndpointIDLabelsHandler
EndpointGetEndpointIDLogHandler endpoint.GetEndpointIDLogHandler
EndpointPatchEndpointIDConfigHandler endpoint.PatchEndpointIDConfigHandler
EndpointPatchEndpointIDHandler endpoint.PatchEndpointIDHandler
EndpointPatchEndpointIDLabelsHandler endpoint.PatchEndpointIDLabelsHandler
EndpointPutEndpointIDHandler endpoint.PutEndpointIDHandler
PolicyDeleteFqdnCacheHandler policy.DeleteFqdnCacheHandler
PolicyDeletePolicyHandler policy.DeletePolicyHandler
PolicyGetFqdnCacheHandler policy.GetFqdnCacheHandler
PolicyGetFqdnCacheIDHandler policy.GetFqdnCacheIDHandler
PolicyGetFqdnNamesHandler policy.GetFqdnNamesHandler
PolicyGetIdentityEndpointsHandler policy.GetIdentityEndpointsHandler
PolicyGetIdentityHandler policy.GetIdentityHandler
PolicyGetIdentityIDHandler policy.GetIdentityIDHandler
PolicyGetIPHandler policy.GetIPHandler
PolicyGetPolicyHandler policy.GetPolicyHandler
PolicyGetPolicySelectorsHandler policy.GetPolicySelectorsHandler
PolicyPutPolicyHandler policy.PutPolicyHandler
}
// apiHandler implements Handle() for the given parameter type.
// It allows expressing the API handlers requiring *Daemon as simply
// as a function of form `func(d *Daemon, p ParamType) middleware.Responder`.
// This wrapper takes care of Await'ing for *Daemon.
type apiHandler[Params any] struct {
dp promise.Promise[*Daemon]
handler func(d *Daemon, p Params) middleware.Responder
}
func (a *apiHandler[Params]) Handle(p Params) middleware.Responder {
// Wait for *Daemon to be ready. While 'p' would have a context, it's hard to get it
// since it's a struct. Could use reflection, but since we'll stop the agent anyway
// if daemon initialization fails it doesn't really matter that much here what context
// to use.
d, err := a.dp.Await(context.Background())
if err != nil {
return api.Error(http.StatusServiceUnavailable, err)
}
return a.handler(d, p)
}
func wrapAPIHandler[Params any](dp promise.Promise[*Daemon], handler func(d *Daemon, p Params) middleware.Responder) *apiHandler[Params] {
return &apiHandler[Params]{dp: dp, handler: handler}
}
// apiHandlers bridges the API handlers still implemented inside Daemon into a set of
// individual handlers. Since NewDaemon() is side-effectful, we can only get a promise for
// *Daemon, and thus the handlers will need to Await() for it to be ready.
//
// This method depends on [deletionQueue] to make sure the deletion lock file is created and locked
// before the API server starts.
//
// This is meant to be a temporary measure until handlers have been moved out from *Daemon
// to daemon/restapi or feature-specific packages. At that point the dependency on *deletionQueue
// should be moved to the cell in daemon/restapi.
func ciliumAPIHandlers(dp promise.Promise[*Daemon], cfg *option.DaemonConfig, _ *deletionQueue) (out handlersOut) {
// /healthz/
out.DaemonGetHealthzHandler = wrapAPIHandler(dp, getHealthzHandler)
// /endpoint/
out.EndpointDeleteEndpointHandler = wrapAPIHandler(dp, deleteEndpointHandler)
out.EndpointGetEndpointHandler = wrapAPIHandler(dp, getEndpointHandler)
// /endpoint/{id}
out.EndpointGetEndpointIDHandler = wrapAPIHandler(dp, getEndpointIDHandler)
out.EndpointPutEndpointIDHandler = wrapAPIHandler(dp, putEndpointIDHandler)
out.EndpointPatchEndpointIDHandler = wrapAPIHandler(dp, patchEndpointIDHandler)
out.EndpointDeleteEndpointIDHandler = wrapAPIHandler(dp, deleteEndpointIDHandler)
// /endpoint/{id}config/
out.EndpointGetEndpointIDConfigHandler = wrapAPIHandler(dp, getEndpointIDConfigHandler)
out.EndpointPatchEndpointIDConfigHandler = wrapAPIHandler(dp, patchEndpointIDConfigHandler)
// /endpoint/{id}/labels/
out.EndpointGetEndpointIDLabelsHandler = wrapAPIHandler(dp, getEndpointIDLabelsHandler)
out.EndpointPatchEndpointIDLabelsHandler = wrapAPIHandler(dp, putEndpointIDLabelsHandler)
// /endpoint/{id}/log/
out.EndpointGetEndpointIDLogHandler = wrapAPIHandler(dp, getEndpointIDLogHandler)
// /endpoint/{id}/healthz
out.EndpointGetEndpointIDHealthzHandler = wrapAPIHandler(dp, getEndpointIDHealthzHandler)
// /identity/
out.PolicyGetIdentityHandler = wrapAPIHandler(dp, getIdentityHandler)
out.PolicyGetIdentityIDHandler = wrapAPIHandler(dp, getIdentityIDHandler)
// /identity/endpoints
out.PolicyGetIdentityEndpointsHandler = wrapAPIHandler(dp, getIdentityEndpointsHandler)
// /policy/
out.PolicyGetPolicyHandler = wrapAPIHandler(dp, getPolicyHandler)
out.PolicyPutPolicyHandler = wrapAPIHandler(dp, putPolicyHandler)
out.PolicyDeletePolicyHandler = wrapAPIHandler(dp, deletePolicyHandler)
out.PolicyGetPolicySelectorsHandler = wrapAPIHandler(dp, getPolicySelectorsHandler)
// /debuginfo
out.DaemonGetDebuginfoHandler = wrapAPIHandler(dp, getDebugInfoHandler)
// /fqdn/cache
out.PolicyGetFqdnCacheHandler = wrapAPIHandler(dp, getFqdnCacheHandler)
out.PolicyDeleteFqdnCacheHandler = wrapAPIHandler(dp, deleteFqdnCacheHandler)
out.PolicyGetFqdnCacheIDHandler = wrapAPIHandler(dp, getFqdnCacheIDHandler)
out.PolicyGetFqdnNamesHandler = wrapAPIHandler(dp, getFqdnNamesHandler)
// /ip/
out.PolicyGetIPHandler = wrapAPIHandler(dp, getIPHandler)
return
}