Skip to content

Commit 2e801f6

Browse files
committed
Add NoopSuggester for disabling Elasticsearch autocomplete
Signed-off-by: Dom Del Nano <ddelnano@gmail.com>
1 parent ce714e6 commit 2e801f6

File tree

4 files changed

+101
-44
lines changed

4 files changed

+101
-44
lines changed

src/cloud/api/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ go_library(
4040
"//src/shared/services/server",
4141
"//src/utils/script",
4242
"@com_github_gorilla_handlers//:handlers",
43+
"@com_github_olivere_elastic_v7//:elastic",
4344
"@com_github_sirupsen_logrus//:logrus",
4445
"@com_github_spf13_pflag//:pflag",
4546
"@com_github_spf13_viper//:viper",

src/cloud/api/api_server.go

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"time"
2929

3030
"github.com/gorilla/handlers"
31+
"github.com/olivere/elastic/v7"
3132
log "github.com/sirupsen/logrus"
3233
"github.com/spf13/pflag"
3334
"github.com/spf13/viper"
@@ -69,6 +70,7 @@ func init() {
6970
pflag.String("auth_connector_name", "", "If any, the name of the auth connector to be used with Pixie")
7071
pflag.String("auth_connector_callback_url", "", "If any, the callback URL for the auth connector")
7172
pflag.Bool("script_modification_disabled", false, "If script modification should be disallowed to prevent arbitrary script execution")
73+
pflag.Bool("disable_autocomplete", false, "Disable autocomplete functionality (no Elasticsearch required)")
7274
}
7375

7476
func main() {
@@ -136,15 +138,20 @@ func main() {
136138
// Connect to NATS.
137139
nc := msgbus.MustConnectNATS()
138140

139-
esConfig := &esutils.Config{
140-
URL: []string{viper.GetString("elastic_service")},
141-
User: viper.GetString("elastic_username"),
142-
Passwd: viper.GetString("elastic_password"),
143-
CaCertFile: viper.GetString("elastic_ca_cert"),
144-
}
145-
es, err := esutils.NewEsClient(esConfig)
146-
if err != nil {
147-
log.WithError(err).Fatal("Could not connect to elastic")
141+
disableAutocomplete := viper.GetBool("disable_autocomplete")
142+
143+
var es *elastic.Client
144+
if !disableAutocomplete {
145+
esConfig := &esutils.Config{
146+
URL: []string{viper.GetString("elastic_service")},
147+
User: viper.GetString("elastic_username"),
148+
Passwd: viper.GetString("elastic_password"),
149+
CaCertFile: viper.GetString("elastic_ca_cert"),
150+
}
151+
es, err = esutils.NewEsClient(esConfig)
152+
if err != nil {
153+
log.WithError(err).Fatal("Could not connect to elastic")
154+
}
148155
}
149156

150157
mux := http.NewServeMux()
@@ -228,45 +235,52 @@ func main() {
228235
vizierpb.RegisterVizierServiceServer(s.GRPCServer(), vpt)
229236
vizierpb.RegisterVizierDebugServiceServer(s.GRPCServer(), vpt)
230237

231-
mdIndexName := viper.GetString("md_index_name")
232-
if mdIndexName == "" {
233-
log.Fatal("Must specify a name for the elastic index.")
234-
}
235-
esSuggester, err := autocomplete.NewElasticSuggester(es, mdIndexName, "scripts", pc)
236-
if err != nil {
237-
log.WithError(err).Fatal("Failed to start elastic suggester")
238-
}
239-
240-
var br *script.BundleManager
241-
var bundleErr error
242-
updateBundle := func() {
243-
// Requiring the bundle manager in the API service is temporary until we
244-
// start indexing scripts.
245-
br, bundleErr = script.NewBundleManagerWithOrg([]string{defaultBundleFile, ossBundleFile}, "", "")
246-
if bundleErr != nil {
247-
log.WithError(bundleErr).Error("Failed to init bundle manager")
248-
br = nil
238+
var suggester autocomplete.Suggester
239+
if disableAutocomplete {
240+
log.Info("Autocomplete disabled - using NoopSuggester")
241+
suggester = autocomplete.NewNoopSuggester()
242+
} else {
243+
mdIndexName := viper.GetString("md_index_name")
244+
if mdIndexName == "" {
245+
log.Fatal("Must specify a name for the elastic index.")
249246
}
250-
esSuggester.UpdateScriptBundle(br)
251-
}
252-
253-
quitCh := make(chan bool)
254-
go func() {
255-
updateBundle()
256-
scriptTimer := time.NewTicker(30 * time.Second)
257-
defer scriptTimer.Stop()
258-
for {
259-
select {
260-
case <-quitCh:
261-
return
262-
case <-scriptTimer.C:
263-
updateBundle()
247+
esSuggester, err := autocomplete.NewElasticSuggester(es, mdIndexName, "scripts", pc)
248+
if err != nil {
249+
log.WithError(err).Fatal("Failed to start elastic suggester")
250+
}
251+
suggester = esSuggester
252+
253+
var br *script.BundleManager
254+
var bundleErr error
255+
updateBundle := func() {
256+
// Requiring the bundle manager in the API service is temporary until we
257+
// start indexing scripts.
258+
br, bundleErr = script.NewBundleManagerWithOrg([]string{defaultBundleFile, ossBundleFile}, "", "")
259+
if bundleErr != nil {
260+
log.WithError(bundleErr).Error("Failed to init bundle manager")
261+
br = nil
264262
}
263+
esSuggester.UpdateScriptBundle(br)
265264
}
266-
}()
267-
defer close(quitCh)
268265

269-
as := &controllers.AutocompleteServer{Suggester: esSuggester}
266+
quitCh := make(chan bool)
267+
go func() {
268+
updateBundle()
269+
scriptTimer := time.NewTicker(30 * time.Second)
270+
defer scriptTimer.Stop()
271+
for {
272+
select {
273+
case <-quitCh:
274+
return
275+
case <-scriptTimer.C:
276+
updateBundle()
277+
}
278+
}
279+
}()
280+
defer close(quitCh)
281+
}
282+
283+
as := &controllers.AutocompleteServer{Suggester: suggester}
270284
cloudpb.RegisterAutocompleteServiceServer(s.GRPCServer(), as)
271285

272286
os := &controllers.OrganizationServiceServer{ProfileServiceClient: pc, AuthServiceClient: ac, OrgServiceClient: oc}

src/cloud/autocomplete/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ go_library(
2121
name = "autocomplete",
2222
srcs = [
2323
"autocomplete.go",
24+
"noop_suggester.go",
2425
"suggester.go",
2526
],
2627
importpath = "px.dev/pixie/src/cloud/autocomplete",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2018- The Pixie Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*/
18+
19+
package autocomplete
20+
21+
// NoopSuggester is a suggester that returns empty results.
22+
// It is used when autocomplete is disabled (e.g., when Elasticsearch is not available).
23+
type NoopSuggester struct{}
24+
25+
// NewNoopSuggester creates a new NoopSuggester.
26+
func NewNoopSuggester() *NoopSuggester {
27+
return &NoopSuggester{}
28+
}
29+
30+
// GetSuggestions returns empty results for all requests.
31+
func (n *NoopSuggester) GetSuggestions(reqs []*SuggestionRequest) ([]*SuggestionResult, error) {
32+
results := make([]*SuggestionResult, len(reqs))
33+
for i := range reqs {
34+
results[i] = &SuggestionResult{
35+
Suggestions: []*Suggestion{},
36+
ExactMatch: false,
37+
HasAdditionalMatches: false,
38+
}
39+
}
40+
return results, nil
41+
}

0 commit comments

Comments
 (0)