Skip to content

Commit f873e5e

Browse files
m25nwarshawdJoe GeeGlenn Oppegard
authored
Load cron scripts from multiple sources (#1326)
Summary: For our usage of Pixie, it's convenient to exclude Pixie's dependency on the control plane. This commit allows the query broker: 1. to load cron scripts from config maps in the same namespace 2. to turn on and off loading cron scripts from the cloud or config maps Type of change: /kind feature Changes: This commit implements the functionality described above. * added a `scriptrunner.Source` type that encapsulates how cron scripts are initialized/updated. * moved existing logic for fetching cron scripts to `scriptrunner.CloudSource` * added `scriptrunner.ConfigMapSource` * Modified `scriptrunner.ScriptRunner` to use `scriptrunner.Source`s. * Added a configuration flag (`--cron_script_sources` or or `PL_CRON_SCRIPT_SOURCES`) that allows a customer to choose which sources cron scripts can be loaded from. By default, it only loads cron scripts from the control plane. If you wanted to use both, for example, you could set `PL_CRON_SCRIPT_SOURCES="cloud configmaps"`. * Moved many of the fakes/mocks/test helpers to `helper_test.go` Test Plan: All of these changes have been tested at the unit level in addition to manual testing in all relevant configurations. We did our manual testing on GKE, EKS, and Kind with Kubernetes version 1.25. We don't anticipate other Kubernetes platforms or versions to be a problem since the config map API is stable and consistent across those dimensions. Moving the logic to load scripts from the control plane was probably the riskiest change and might warrant some extra attention. --------- Signed-off-by: Matthew Conger-Eldeen <matthewco@vmware.com> Co-authored-by: Devon Warshaw <warshawd@vmware.com> Co-authored-by: Joe Gee <jgee@vmware.com> Co-authored-by: Glenn Oppegard <oppegard@vmware.com>
1 parent 4945285 commit f873e5e

File tree

17 files changed

+2258
-987
lines changed

17 files changed

+2258
-987
lines changed

k8s/vizier/base/default_role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: RoleBinding
4+
metadata:
5+
name: pl-vizier-crd-binding
6+
roleRef:
7+
apiGroup: rbac.authorization.k8s.io
8+
kind: Role
9+
name: pl-vizier-crd-role
10+
subjects:
11+
- kind: ServiceAccount
12+
name: default

k8s/vizier/base/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ patches:
2121
kind: StatefulSet
2222
resources:
2323
- ../bootstrap
24+
- default_role.yaml
2425
- kelvin_deployment.yaml
2526
- kelvin_service.yaml
2627
- metadata_role.yaml

k8s/vizier/base/query_broker_deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ spec:
105105
- ALL
106106
seccompProfile:
107107
type: RuntimeDefault
108+
serviceAccountName: query-broker-service-account
108109
securityContext:
109110
runAsUser: 10100
110111
runAsGroup: 10100
Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
11
---
2+
apiVersion: v1
3+
kind: ServiceAccount
4+
metadata:
5+
name: query-broker-service-account
6+
---
7+
apiVersion: rbac.authorization.k8s.io/v1
8+
kind: Role
9+
metadata:
10+
creationTimestamp: null
11+
name: pl-vizier-query-broker-role
12+
rules:
13+
- apiGroups:
14+
- ""
15+
resources:
16+
- configmaps
17+
verbs:
18+
- get
19+
- list
20+
- watch
21+
---
222
apiVersion: rbac.authorization.k8s.io/v1
323
kind: RoleBinding
424
metadata:
5-
name: pl-vizier-crd-binding
25+
name: pl-vizier-query-broker-crd-binding
626
roleRef:
727
apiGroup: rbac.authorization.k8s.io
828
kind: Role
929
name: pl-vizier-crd-role
1030
subjects:
1131
- kind: ServiceAccount
12-
name: default
13-
namespace: pl
32+
name: query-broker-service-account
33+
---
34+
apiVersion: rbac.authorization.k8s.io/v1
35+
kind: RoleBinding
36+
metadata:
37+
name: pl-vizier-query-broker-binding
38+
roleRef:
39+
apiGroup: rbac.authorization.k8s.io
40+
kind: Role
41+
name: pl-vizier-query-broker-role
42+
subjects:
43+
- kind: ServiceAccount
44+
name: query-broker-service-account

src/utils/template_generator/vizier_yamls/vizier_yamls.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,18 @@ func generateVzYAMLs(yamlMap map[string]string) ([]*yamls.YAMLFile, error) {
418418
Placeholder: "__PX_SUBJECT_NAMESPACE__",
419419
TemplateValue: nsTmpl,
420420
},
421+
{
422+
TemplateMatcher: yamls.GenerateResourceNameMatcherFn("pl-vizier-query-broker-crd-binding"),
423+
Patch: `{ "subjects": [{ "name": "query-broker-service-account", "namespace": "__PX_SUBJECT_NAMESPACE__", "kind": "ServiceAccount" }] }`,
424+
Placeholder: "__PX_SUBJECT_NAMESPACE__",
425+
TemplateValue: nsTmpl,
426+
},
427+
{
428+
TemplateMatcher: yamls.GenerateResourceNameMatcherFn("pl-vizier-query-broker-binding"),
429+
Patch: `{ "subjects": [{ "name": "query-broker-service-account", "namespace": "__PX_SUBJECT_NAMESPACE__", "kind": "ServiceAccount" }] }`,
430+
Placeholder: "__PX_SUBJECT_NAMESPACE__",
431+
TemplateValue: nsTmpl,
432+
},
421433
{
422434
TemplateMatcher: yamls.GenerateResourceNameMatcherFn("pl-vizier-metadata-node-view-cluster-binding"),
423435
Patch: `{ "subjects": [{ "name": "metadata-service-account", "namespace": "__PX_SUBJECT_NAMESPACE__", "kind": "ServiceAccount" }] }`,

src/vizier/services/query_broker/controllers/query_executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ func (q *QueryExecutorImpl) prepareScript(ctx context.Context, resultCh chan<- *
508508
func (q *QueryExecutorImpl) runScript(ctx context.Context, resultCh chan<- *vizierpb.ExecuteScriptResponse, req *vizierpb.ExecuteScriptRequest) error {
509509
defer close(resultCh)
510510
q.startTime = time.Now()
511-
log.WithField("query_id", q.queryID).Infof("Running script")
511+
log.WithField("query_id", q.queryID).WithField("query_name", q.queryName).Infof("Running script")
512512

513513
if req.QueryID == "" {
514514
if err := q.prepareScript(ctx, resultCh, req); err != nil {

src/vizier/services/query_broker/query_broker_server.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func init() {
5959
pflag.String("mds_service", "vizier-metadata-svc", "The metadata service name")
6060
pflag.String("mds_port", "50400", "The querybroker service port")
6161
pflag.String("pod_namespace", "pl", "The namespace this pod runs in.")
62+
pflag.StringArray("cron_script_sources", scriptrunner.DefaultSources, "Where to find cron scripts (cloud, configmaps)")
6263
}
6364

6465
// NewVizierServiceClient creates a new vz RPC client stub.
@@ -206,10 +207,14 @@ func main() {
206207
defer ptProxy.Close()
207208

208209
// Start cron script runner.
209-
sr, err := scriptrunner.New(natsConn, csClient, vzServiceClient, viper.GetString("jwt_signing_key"))
210-
if err != nil {
211-
log.WithError(err).Fatal("Failed to start script runner")
212-
}
210+
sources := scriptrunner.Sources(
211+
natsConn,
212+
csClient,
213+
viper.GetString("jwt_signing_key"),
214+
viper.GetString("pod_namespace"),
215+
viper.GetStringSlice("cron_script_sources"),
216+
)
217+
sr := scriptrunner.New(csClient, vzServiceClient, viper.GetString("jwt_signing_key"), sources...)
213218

214219
// Load the scripts and start the background sync.
215220
go func() {
@@ -218,6 +223,7 @@ func main() {
218223
log.WithError(err).Error("Failed to sync cron scripts")
219224
}
220225
}()
226+
221227
s.Start()
222228
s.StopOnInterrupt()
223229
}

src/vizier/services/query_broker/script_runner/BUILD.bazel

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ load("//bazel:pl_build_system.bzl", "pl_go_test")
1919

2020
go_library(
2121
name = "script_runner",
22-
srcs = ["script_runner.go"],
22+
srcs = [
23+
"cloud_source.go",
24+
"config_map_source.go",
25+
"script_runner.go",
26+
"source.go",
27+
"sources.go",
28+
],
2329
importpath = "px.dev/pixie/src/vizier/services/query_broker/script_runner",
2430
visibility = ["//visibility:public"],
2531
deps = [
@@ -31,6 +37,7 @@ go_library(
3137
"//src/shared/scripts",
3238
"//src/shared/services/utils",
3339
"//src/utils",
40+
"//src/utils/shared/k8s",
3441
"//src/vizier/services/metadata/metadatapb:service_pl_go_proto",
3542
"//src/vizier/utils/messagebus",
3643
"@com_github_gofrs_uuid//:uuid",
@@ -39,6 +46,15 @@ go_library(
3946
"@com_github_nats_io_nats_go//:nats_go",
4047
"@com_github_sirupsen_logrus//:logrus",
4148
"@in_gopkg_yaml_v2//:yaml_v2",
49+
"@io_k8s_api//core/v1:core",
50+
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
51+
"@io_k8s_apimachinery//pkg/labels",
52+
"@io_k8s_client_go//informers",
53+
"@io_k8s_client_go//informers/core/v1:core",
54+
"@io_k8s_client_go//kubernetes",
55+
"@io_k8s_client_go//listers/core/v1:core",
56+
"@io_k8s_client_go//rest",
57+
"@io_k8s_client_go//tools/cache",
4258
"@org_golang_google_grpc//codes",
4359
"@org_golang_google_grpc//metadata",
4460
"@org_golang_google_grpc//status",
@@ -47,10 +63,16 @@ go_library(
4763

4864
pl_go_test(
4965
name = "script_runner_test",
50-
srcs = ["script_runner_test.go"],
66+
srcs = [
67+
"cloud_source_test.go",
68+
"config_map_source_test.go",
69+
"helper_test.go",
70+
"script_runner_test.go",
71+
],
5172
embed = [":script_runner"],
5273
deps = [
5374
"//src/api/proto/vizierpb:vizier_pl_go_proto",
75+
"//src/api/proto/vizierpb/mock",
5476
"//src/carnot/planner/compilerpb:compiler_status_pl_go_proto",
5577
"//src/common/base/statuspb:status_pl_go_proto",
5678
"//src/shared/cvmsgspb:cvmsgs_pl_go_proto",
@@ -61,9 +83,12 @@ pl_go_test(
6183
"@com_github_gofrs_uuid//:uuid",
6284
"@com_github_gogo_protobuf//proto",
6385
"@com_github_gogo_protobuf//types",
86+
"@com_github_golang_mock//gomock",
6487
"@com_github_nats_io_nats_go//:nats_go",
65-
"@com_github_stretchr_testify//assert",
6688
"@com_github_stretchr_testify//require",
89+
"@io_k8s_api//core/v1:core",
90+
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
91+
"@io_k8s_client_go//kubernetes/fake",
6792
"@org_golang_google_grpc//:go_default_library",
6893
"@org_golang_google_grpc//codes",
6994
"@org_golang_google_grpc//status",

0 commit comments

Comments
 (0)