This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
183 lines (159 loc) · 5.24 KB
/
handler.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package servicecontrol
import (
"context"
"errors"
"fmt"
"io"
"sync"
sc "google.golang.org/api/servicecontrol/v1"
"istio.io/istio/mixer/adapter/servicecontrol/config"
"istio.io/istio/mixer/adapter/servicecontrol/template/servicecontrolreport"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/status"
"istio.io/istio/mixer/template/apikey"
"istio.io/istio/mixer/template/quota"
"istio.io/istio/pkg/cache"
)
type (
serviceControlClient interface {
Check(googleServiceName string, request *sc.CheckRequest) (*sc.CheckResponse, error)
Report(googleServiceName string, request *sc.ReportRequest) (*sc.ReportResponse, error)
AllocateQuota(googleServiceName string, request *sc.AllocateQuotaRequest) (*sc.AllocateQuotaResponse, error)
}
checkProcessor interface {
ProcessCheck(ctx context.Context, instance *apikey.Instance) (adapter.CheckResult, error)
}
reportProcessor interface {
io.Closer
ProcessReport(ctx context.Context, instances []*servicecontrolreport.Instance) error
}
quotaProcessor interface {
ProcessQuota(ctx context.Context, instances *quota.Instance, args adapter.QuotaArgs) (adapter.QuotaResult, error)
}
serviceProcessor struct {
checkProcessor
reportProcessor
quotaProcessor
}
handlerContext struct {
env adapter.Env
config *config.Params
// A map keyed by mesh service name to service config in adapter config
serviceConfigIndex map[string]*config.GcpServiceSetting
checkDataShape map[string]*apikey.Type
reportDataShape map[string]*servicecontrolreport.Type
checkResponseCache cache.ExpiringCache // A LRU cache for check response
client serviceControlClient
}
handler struct {
ctx *handlerContext
// lock protects svcProcMap.
lock sync.Mutex
// Istio mesh service name to serviceProcessor map. Each serviceProcessor instance handles a single
// service.
svcProcMap map[string]*serviceProcessor
}
)
func newServiceProcessor(meshServiceName string, ctx *handlerContext) (*serviceProcessor, error) {
checkProc, err := newCheckProcessor(meshServiceName, ctx)
if err != nil {
return nil, err
}
reportProc, err := newReportProcessor(meshServiceName, ctx, checkProc)
if err != nil {
return nil, err
}
quotaProc, err := newQuotaProcessor(meshServiceName, ctx)
if err != nil {
return nil, err
}
return &serviceProcessor{
checkProcessor: checkProc,
reportProcessor: reportProc,
quotaProcessor: quotaProc,
}, nil
}
// HandleApiKey handles apikey check.
// nolint:golint
// Disable lint warning of HandleApiKey name
func (h *handler) HandleApiKey(ctx context.Context, instance *apikey.Instance) (adapter.CheckResult, error) {
svcProc, err := h.getServiceProcessor(ctx)
if err != nil {
return adapter.CheckResult{
Status: status.WithPermissionDenied(err.Error()),
}, nil
}
return svcProc.ProcessCheck(ctx, instance)
}
// HandleServicecontrolReport handles reporting metrics and logs.
func (h *handler) HandleServicecontrolReport(ctx context.Context, instances []*servicecontrolreport.Instance) error {
svcProc, err := h.getServiceProcessor(ctx)
if err != nil {
return err
}
return svcProc.ProcessReport(ctx, instances)
}
// HandleQuota handles rate limiting quota.
func (h *handler) HandleQuota(ctx context.Context, instance *quota.Instance,
args adapter.QuotaArgs) (adapter.QuotaResult, error) {
svcProc, err := h.getServiceProcessor(ctx)
if err != nil {
return adapter.QuotaResult{
// This map to rpc.INTERNAL.
Status: status.WithError(err),
}, nil
}
return svcProc.ProcessQuota(ctx, instance, args)
}
// Close closes a handler.
// TODO(manlinl): Run svcProc.Close in goroutine after reportProcessor implements buffering.
func (h *handler) Close() error {
h.lock.Lock()
defer h.lock.Lock()
for _, svcProc := range h.svcProcMap {
// TODO: handle Close errors
_ = svcProc.Close()
}
return nil
}
func (h *handler) getServiceProcessor(ctx context.Context) (*serviceProcessor, error) {
requestData, ok := adapter.RequestDataFromContext(ctx)
if !ok {
return nil, errors.New(`no RequestData found in context`)
}
serviceFullName := requestData.DestinationService.FullName
if _, ok := h.ctx.serviceConfigIndex[serviceFullName]; !ok {
return nil, fmt.Errorf("unknown service %v", serviceFullName)
}
h.lock.Lock()
defer h.lock.Unlock()
svcProc, found := h.svcProcMap[serviceFullName]
if found {
return svcProc, nil
}
svcProc, err := newServiceProcessor(serviceFullName, h.ctx)
if err != nil {
return nil, err
}
h.svcProcMap[serviceFullName] = svcProc
return svcProc, nil
}
func newHandler(ctx *handlerContext) (*handler, error) {
return &handler{
ctx: ctx,
svcProcMap: make(map[string]*serviceProcessor, len(ctx.config.ServiceConfigs)),
}, nil
}