Skip to content

Commit 2580da4

Browse files
vadimshtCommit Bot
authored andcommitted
[cloud] Add a more lightweight version of Config.
It initializes ONLY core services (currently datastore and portion of 'info' service used by the datastore). Logging, auth, request state, etc is untouched. The existing Config seems to be pretty tangled up with Cloud Logging which we do not want to use. [email protected] BUG=959427 Change-Id: I90cae7beb2f8a5a7f1f5dfce48928b030afad546 Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/gae/+/1669864 Commit-Queue: Vadim Shtayura <[email protected]> Reviewed-by: Andrii Shyshkalov <[email protected]>
1 parent abcc820 commit 2580da4

File tree

4 files changed

+158
-21
lines changed

4 files changed

+158
-21
lines changed

impl/cloud/context.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,16 @@ func (cfg *Config) Use(c context.Context, req *Request) context.Context {
232232
}
233233

234234
// Setup and install the "info" service.
235-
gi := serviceInstanceGlobalInfo{
236-
Config: cfg,
237-
Request: req,
238-
}
239-
c = useInfo(c, &gi)
235+
c = useInfo(c, &serviceInstanceGlobalInfo{
236+
IsDev: cfg.IsDev,
237+
ProjectID: cfg.ProjectID,
238+
ServiceName: cfg.ServiceName,
239+
VersionName: cfg.VersionName,
240+
InstanceID: cfg.InstanceID,
241+
RequestID: req.TraceID,
242+
ServiceAccountName: cfg.ServiceAccountName,
243+
ServiceProvider: cfg.ServiceProvider,
244+
})
240245

241246
// datastore service
242247
if cfg.DS != nil {

impl/cloud/context_lite.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2019 The LUCI Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cloud
16+
17+
import (
18+
"cloud.google.com/go/datastore"
19+
"github.com/bradfitz/gomemcache/memcache"
20+
"golang.org/x/net/context"
21+
22+
"go.chromium.org/gae/impl/dummy"
23+
ds "go.chromium.org/gae/service/datastore"
24+
"go.chromium.org/gae/service/mail"
25+
mc "go.chromium.org/gae/service/memcache"
26+
"go.chromium.org/gae/service/module"
27+
"go.chromium.org/gae/service/taskqueue"
28+
"go.chromium.org/gae/service/user"
29+
)
30+
31+
// ConfigLite can be used to configure a context to have supported Cloud
32+
// Services clients and ONLY them.
33+
//
34+
// Currently supports only datastore and its required dependencies.
35+
//
36+
// Unlike Config, it doesn't try to setup logging, intercept HTTP requests,
37+
// provide auth, etc.
38+
type ConfigLite struct {
39+
// IsDev is true if this is a development execution.
40+
IsDev bool
41+
42+
// ProjectID, if not empty, is the project ID returned by the "info" service.
43+
//
44+
// If empty, the service will treat requests for this field as not
45+
// implemented.
46+
ProjectID string
47+
48+
// ServiceName, if not empty, is the service (module) name returned by the
49+
// "info" service.
50+
//
51+
// If empty, the service will treat requests for this field as not
52+
// implemented.
53+
ServiceName string
54+
55+
// VersionName, if not empty, is the version name returned by the "info"
56+
// service.
57+
//
58+
// If empty, the service will treat requests for this field as not
59+
// implemented.
60+
VersionName string
61+
62+
// InstanceID, if not empty, is the instance ID returned by the "info"
63+
// service.
64+
//
65+
// If empty, the service will treat requests for this field as not
66+
// implemented.
67+
InstanceID string
68+
69+
// RequestID, if not empty, is the request ID returned by the "info"
70+
// service.
71+
//
72+
// If empty, the service will treat requests for this field as not
73+
// implemented.
74+
RequestID string
75+
76+
// ServiceAccountName, if not empty, is the service account name returned by
77+
// the "info" service.
78+
//
79+
// If empty, the service will treat requests for this field as not
80+
// implemented.
81+
ServiceAccountName string
82+
83+
// DS is the Cloud Datastore client.
84+
//
85+
// If populated, the datastore service will be installed.
86+
DS *datastore.Client
87+
88+
// MC is the memcache service client.
89+
//
90+
// If populated, the memcache service will be installed.
91+
MC *memcache.Client
92+
}
93+
94+
// Use configures the context with implementation of Cloud Services.
95+
//
96+
// Any services that are missing will have "impl/dummy" stubs installed. These
97+
// stubs will panic if called.
98+
func (cfg *ConfigLite) Use(c context.Context) context.Context {
99+
// Dummy services that we don't support.
100+
c = mail.Set(c, dummy.Mail())
101+
c = module.Set(c, dummy.Module())
102+
c = taskqueue.SetRaw(c, dummy.TaskQueue())
103+
c = user.Set(c, dummy.User())
104+
105+
c = useInfo(c, &serviceInstanceGlobalInfo{
106+
IsDev: cfg.IsDev,
107+
ProjectID: cfg.ProjectID,
108+
ServiceName: cfg.ServiceName,
109+
VersionName: cfg.VersionName,
110+
InstanceID: cfg.InstanceID,
111+
RequestID: cfg.RequestID,
112+
ServiceAccountName: cfg.ServiceAccountName,
113+
})
114+
115+
if cfg.DS != nil {
116+
cds := cloudDatastore{client: cfg.DS}
117+
c = cds.use(c)
118+
} else {
119+
c = ds.SetRaw(c, dummy.Datastore())
120+
}
121+
122+
if cfg.MC != nil {
123+
mc := memcacheClient{client: cfg.MC}
124+
c = mc.use(c)
125+
} else {
126+
c = mc.SetRaw(c, dummy.Memcache())
127+
}
128+
129+
return c
130+
}

impl/cloud/info.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ import (
2727
// serviceInstanceGlobalInfo is the set of base, immutable info service values.
2828
// These are initialized when the service instance is instantiated.
2929
type serviceInstanceGlobalInfo struct {
30-
*Config
31-
*Request
30+
IsDev bool
31+
ProjectID string
32+
ServiceName string
33+
VersionName string
34+
InstanceID string
35+
RequestID string
36+
ServiceAccountName string
37+
ServiceProvider ServiceProvider
3238
}
3339

3440
// infoState is the state of the "service/info" service in the current Context.
@@ -90,14 +96,14 @@ func (i *infoService) IsDevAppServer() bool { return i.IsDev }
9096

9197
func (*infoService) Datacenter() string { panic(ErrNotImplemented) }
9298
func (*infoService) DefaultVersionHostname() string { panic(ErrNotImplemented) }
93-
func (i *infoService) InstanceID() string { return maybe(i.Config.InstanceID) }
99+
func (i *infoService) InstanceID() string { return maybe(i.infoState.InstanceID) }
94100
func (*infoService) IsOverQuota(err error) bool { return false }
95101
func (*infoService) IsTimeoutError(err error) bool { return false }
96102
func (*infoService) ModuleHostname(module, version, instance string) (string, error) {
97103
return "", ErrNotImplemented
98104
}
99105
func (i *infoService) ModuleName() string { return maybe(i.ServiceName) }
100-
func (i *infoService) RequestID() string { return maybe(i.TraceID) }
106+
func (i *infoService) RequestID() string { return maybe(i.infoState.RequestID) }
101107
func (*infoService) ServerSoftware() string { panic(ErrNotImplemented) }
102108
func (i *infoService) VersionID() string { return maybe(i.VersionName) }
103109

impl/cloud/info_test.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,14 @@ func TestInfo(t *testing.T) {
3434
const maxNamespaceLen = 100
3535

3636
gi := serviceInstanceGlobalInfo{
37-
Config: &Config{
38-
IsDev: false,
39-
ProjectID: "project-id",
40-
ServiceName: "service-name",
41-
VersionName: "version-name",
42-
InstanceID: "instance-id",
43-
ServiceAccountName: "[email protected]",
44-
ServiceProvider: nil,
45-
},
46-
Request: &Request{
47-
TraceID: "trace",
48-
},
37+
IsDev: false,
38+
ProjectID: "project-id",
39+
ServiceName: "service-name",
40+
VersionName: "version-name",
41+
InstanceID: "instance-id",
42+
ServiceAccountName: "[email protected]",
43+
ServiceProvider: nil,
44+
RequestID: "trace",
4945
}
5046
c := useInfo(context.Background(), &gi)
5147

0 commit comments

Comments
 (0)