Skip to content

Commit fe23504

Browse files
m25nwarshawd
andcommitted
load cron scripts from multiple sources
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. Co-authored-by: Devon Warshaw <warshawd@vmware.com> Signed-off-by: Matthew Conger-Eldeen <matthewco@vmware.com>
1 parent fa0d741 commit fe23504

File tree

16 files changed

+1986
-962
lines changed

16 files changed

+1986
-962
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
@@ -412,6 +412,18 @@ func generateVzYAMLs(yamlMap map[string]string) ([]*yamls.YAMLFile, error) {
412412
Placeholder: "__PX_SUBJECT_NAMESPACE__",
413413
TemplateValue: nsTmpl,
414414
},
415+
{
416+
TemplateMatcher: yamls.GenerateResourceNameMatcherFn("pl-vizier-query-broker-crd-binding"),
417+
Patch: `{ "subjects": [{ "name": "query-broker-service-account", "namespace": "__PX_SUBJECT_NAMESPACE__", "kind": "ServiceAccount" }] }`,
418+
Placeholder: "__PX_SUBJECT_NAMESPACE__",
419+
TemplateValue: nsTmpl,
420+
},
421+
{
422+
TemplateMatcher: yamls.GenerateResourceNameMatcherFn("pl-vizier-query-broker-binding"),
423+
Patch: `{ "subjects": [{ "name": "query-broker-service-account", "namespace": "__PX_SUBJECT_NAMESPACE__", "kind": "ServiceAccount" }] }`,
424+
Placeholder: "__PX_SUBJECT_NAMESPACE__",
425+
TemplateValue: nsTmpl,
426+
},
415427
{
416428
TemplateMatcher: yamls.GenerateResourceNameMatcherFn("pl-vizier-metadata-node-view-cluster-binding"),
417429
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: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ 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+
"script_source.go",
27+
],
2328
importpath = "px.dev/pixie/src/vizier/services/query_broker/script_runner",
2429
visibility = ["//visibility:public"],
2530
deps = [
@@ -31,6 +36,7 @@ go_library(
3136
"//src/shared/scripts",
3237
"//src/shared/services/utils",
3338
"//src/utils",
39+
"//src/utils/shared/k8s",
3440
"//src/vizier/services/metadata/metadatapb:service_pl_go_proto",
3541
"//src/vizier/utils/messagebus",
3642
"@com_github_gofrs_uuid//:uuid",
@@ -39,6 +45,11 @@ go_library(
3945
"@com_github_nats_io_nats_go//:nats_go",
4046
"@com_github_sirupsen_logrus//:logrus",
4147
"@in_gopkg_yaml_v2//:yaml_v2",
48+
"@io_k8s_api//core/v1:core",
49+
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
50+
"@io_k8s_apimachinery//pkg/watch",
51+
"@io_k8s_client_go//kubernetes/typed/core/v1:core",
52+
"@io_k8s_client_go//rest",
4253
"@org_golang_google_grpc//codes",
4354
"@org_golang_google_grpc//metadata",
4455
"@org_golang_google_grpc//status",
@@ -47,10 +58,16 @@ go_library(
4758

4859
pl_go_test(
4960
name = "script_runner_test",
50-
srcs = ["script_runner_test.go"],
61+
srcs = [
62+
"cloud_source_test.go",
63+
"config_map_source_test.go",
64+
"helper_test.go",
65+
"script_runner_test.go",
66+
],
5167
embed = [":script_runner"],
5268
deps = [
5369
"//src/api/proto/vizierpb:vizier_pl_go_proto",
70+
"//src/api/proto/vizierpb/mock",
5471
"//src/carnot/planner/compilerpb:compiler_status_pl_go_proto",
5572
"//src/common/base/statuspb:status_pl_go_proto",
5673
"//src/shared/cvmsgspb:cvmsgs_pl_go_proto",
@@ -61,9 +78,16 @@ pl_go_test(
6178
"@com_github_gofrs_uuid//:uuid",
6279
"@com_github_gogo_protobuf//proto",
6380
"@com_github_gogo_protobuf//types",
81+
"@com_github_golang_mock//gomock",
6482
"@com_github_nats_io_nats_go//:nats_go",
65-
"@com_github_stretchr_testify//assert",
6683
"@com_github_stretchr_testify//require",
84+
"@io_k8s_api//core/v1:core",
85+
"@io_k8s_apimachinery//pkg/apis/meta/v1:meta",
86+
"@io_k8s_apimachinery//pkg/labels",
87+
"@io_k8s_apimachinery//pkg/runtime",
88+
"@io_k8s_apimachinery//pkg/watch",
89+
"@io_k8s_client_go//kubernetes/fake",
90+
"@io_k8s_client_go//testing",
6791
"@org_golang_google_grpc//:go_default_library",
6892
"@org_golang_google_grpc//codes",
6993
"@org_golang_google_grpc//status",

0 commit comments

Comments
 (0)