From 14c5d86039cc9d1fb2aae21a916a6f0cc1289c7f Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 21 Apr 2020 13:02:48 +0200 Subject: [PATCH] tracing configuration in apiserversource Signed-off-by: Francesco Guardiani --- .../apiserversource/apiserversource.go | 36 +++++++++++++++---- pkg/reconciler/apiserversource/controller.go | 7 ++-- .../apiserversource/controller_test.go | 13 +++++-- .../resources/receive_adapter.go | 12 +++++-- .../resources/receive_adapter_test.go | 7 +++- 5 files changed, 61 insertions(+), 14 deletions(-) diff --git a/pkg/reconciler/apiserversource/apiserversource.go b/pkg/reconciler/apiserversource/apiserversource.go index 799b0981b1b..2ed53510d6c 100644 --- a/pkg/reconciler/apiserversource/apiserversource.go +++ b/pkg/reconciler/apiserversource/apiserversource.go @@ -32,18 +32,20 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" - "knative.dev/eventing/pkg/apis/sources/v1alpha2" - apiserversourcereconciler "knative.dev/eventing/pkg/client/injection/reconciler/sources/v1alpha2/apiserversource" - listers "knative.dev/eventing/pkg/client/listers/sources/v1alpha2" - "knative.dev/eventing/pkg/logging" - "knative.dev/eventing/pkg/reconciler/apiserversource/resources" - "knative.dev/eventing/pkg/utils" duckv1 "knative.dev/pkg/apis/duck/v1" "knative.dev/pkg/controller" pkgLogging "knative.dev/pkg/logging" "knative.dev/pkg/metrics" pkgreconciler "knative.dev/pkg/reconciler" "knative.dev/pkg/resolver" + pkgtracingconfig "knative.dev/pkg/tracing/config" + + "knative.dev/eventing/pkg/apis/sources/v1alpha2" + apiserversourcereconciler "knative.dev/eventing/pkg/client/injection/reconciler/sources/v1alpha2/apiserversource" + listers "knative.dev/eventing/pkg/client/listers/sources/v1alpha2" + "knative.dev/eventing/pkg/logging" + "knative.dev/eventing/pkg/reconciler/apiserversource/resources" + "knative.dev/eventing/pkg/utils" ) const ( @@ -80,6 +82,7 @@ type Reconciler struct { loggingContext context.Context loggingConfig *pkgLogging.Config metricsConfig *metrics.ExporterOptions + tracingCfg *pkgtracingconfig.Config } var _ apiserversourcereconciler.Interface = (*Reconciler)(nil) @@ -148,6 +151,11 @@ func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1alpha2.Api logging.FromContext(ctx).Error("error while converting metrics config to json", zap.Any("receiveAdapter", err)) } + tracingCfg, err := pkgtracingconfig.TracingConfigToJson(r.tracingCfg) + if err != nil { + logging.FromContext(ctx).Error("error while converting tracing config to json", zap.Any("receiveAdapter", err)) + } + adapterArgs := resources.ReceiveAdapterArgs{ Image: r.receiveAdapterImage, Source: src, @@ -155,6 +163,7 @@ func (r *Reconciler) createReceiveAdapter(ctx context.Context, src *v1alpha2.Api SinkURI: sinkURI, LoggingConfig: loggingConfig, MetricsConfig: metricsConfig, + TracingConfig: tracingCfg, } expected, err := resources.MakeReceiveAdapter(&adapterArgs) if err != nil { @@ -240,6 +249,21 @@ func (r *Reconciler) UpdateFromMetricsConfigMap(cfg *corev1.ConfigMap) { logging.FromContext(r.loggingContext).Debug("Update from metrics ConfigMap", zap.Any("ConfigMap", cfg)) } +// TODO determine how to push the updated metrics config to existing data plane Pods. +func (r *Reconciler) UpdateFromTracingConfigMap(cfg *corev1.ConfigMap) { + if cfg != nil { + delete(cfg.Data, "_example") + } + + tracingCfg, err := pkgtracingconfig.NewTracingConfigFromMap(cfg.Data) + if err != nil { + logging.FromContext(r.loggingContext).Warn("failed to create tracing config from configmap", zap.String("cfg.Name", cfg.Name)) + return + } + + r.tracingCfg = tracingCfg +} + func (r *Reconciler) runAccessCheck(src *v1alpha2.ApiServerSource) error { if src.Spec.Resources == nil || len(src.Spec.Resources) == 0 { src.Status.MarkSufficientPermissions() diff --git a/pkg/reconciler/apiserversource/controller.go b/pkg/reconciler/apiserversource/controller.go index 93292883255..10c8a639b0e 100644 --- a/pkg/reconciler/apiserversource/controller.go +++ b/pkg/reconciler/apiserversource/controller.go @@ -26,13 +26,15 @@ import ( "knative.dev/pkg/logging" "knative.dev/pkg/metrics" "knative.dev/pkg/resolver" + pkgtracingconfig "knative.dev/pkg/tracing/config" "knative.dev/eventing/pkg/apis/sources/v1alpha1" - apiserversourceinformer "knative.dev/eventing/pkg/client/injection/informers/sources/v1alpha2/apiserversource" - apiserversourcereconciler "knative.dev/eventing/pkg/client/injection/reconciler/sources/v1alpha2/apiserversource" kubeclient "knative.dev/pkg/client/injection/kube/client" deploymentinformer "knative.dev/pkg/client/injection/kube/informers/apps/v1/deployment" + + apiserversourceinformer "knative.dev/eventing/pkg/client/injection/informers/sources/v1alpha2/apiserversource" + apiserversourcereconciler "knative.dev/eventing/pkg/client/injection/reconciler/sources/v1alpha2/apiserversource" ) // envConfig will be used to extract the required environment variables using @@ -79,6 +81,7 @@ func NewController( cmw.Watch(logging.ConfigMapName(), r.UpdateFromLoggingConfigMap) cmw.Watch(metrics.ConfigMapName(), r.UpdateFromMetricsConfigMap) + cmw.Watch(pkgtracingconfig.ConfigName, r.UpdateFromTracingConfigMap) return impl } diff --git a/pkg/reconciler/apiserversource/controller_test.go b/pkg/reconciler/apiserversource/controller_test.go index eaacddb078c..c39e7b6b71a 100644 --- a/pkg/reconciler/apiserversource/controller_test.go +++ b/pkg/reconciler/apiserversource/controller_test.go @@ -26,10 +26,11 @@ import ( "knative.dev/pkg/configmap" . "knative.dev/pkg/reconciler/testing" - // Fake injection informers - _ "knative.dev/eventing/pkg/client/injection/informers/sources/v1alpha2/apiserversource/fake" "knative.dev/pkg/client/injection/ducks/duck/v1/addressable" _ "knative.dev/pkg/client/injection/kube/informers/apps/v1/deployment/fake" + + // Fake injection informers + _ "knative.dev/eventing/pkg/client/injection/informers/sources/v1alpha2/apiserversource/fake" ) func TestNew(t *testing.T) { @@ -56,6 +57,14 @@ func TestNew(t *testing.T) { "loglevel.controller": "info", "loglevel.webhook": "info", }, + }, &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "config-tracing", + Namespace: "knative-eventing", + }, + Data: map[string]string{ + "_example": "test-config", + }, })) if c == nil { diff --git a/pkg/reconciler/apiserversource/resources/receive_adapter.go b/pkg/reconciler/apiserversource/resources/receive_adapter.go index 9b763c61122..eb115bdd03e 100644 --- a/pkg/reconciler/apiserversource/resources/receive_adapter.go +++ b/pkg/reconciler/apiserversource/resources/receive_adapter.go @@ -26,10 +26,11 @@ import ( v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "knative.dev/eventing/pkg/adapter/apiserver" - "knative.dev/eventing/pkg/apis/sources/v1alpha2" "knative.dev/pkg/kmeta" "knative.dev/pkg/system" + + "knative.dev/eventing/pkg/adapter/apiserver" + "knative.dev/eventing/pkg/apis/sources/v1alpha2" ) // ReceiveAdapterArgs are the arguments needed to create a ApiServer Receive Adapter. @@ -41,6 +42,7 @@ type ReceiveAdapterArgs struct { SinkURI string MetricsConfig string LoggingConfig string + TracingConfig string } // MakeReceiveAdapter generates (but does not insert into K8s) the Receive Adapter Deployment for @@ -151,5 +153,9 @@ func makeEnv(args *ReceiveAdapterArgs) ([]corev1.EnvVar, error) { }, { Name: "K_LOGGING_CONFIG", Value: args.LoggingConfig, - }}, nil + }, + { + Name: "K_TRACING_CONFIG", + Value: args.TracingConfig, + }}, nil } diff --git a/pkg/reconciler/apiserversource/resources/receive_adapter_test.go b/pkg/reconciler/apiserversource/resources/receive_adapter_test.go index a091f07a457..380f33d0abd 100644 --- a/pkg/reconciler/apiserversource/resources/receive_adapter_test.go +++ b/pkg/reconciler/apiserversource/resources/receive_adapter_test.go @@ -24,9 +24,10 @@ import ( v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "knative.dev/eventing/pkg/apis/sources/v1alpha2" "knative.dev/pkg/kmeta" + "knative.dev/eventing/pkg/apis/sources/v1alpha2" + _ "knative.dev/pkg/metrics/testing" _ "knative.dev/pkg/system/testing" ) @@ -152,6 +153,10 @@ func TestMakeReceiveAdapter(t *testing.T) { Name: "K_LOGGING_CONFIG", Value: "", }, + { + Name: "K_TRACING_CONFIG", + Value: "", + }, }, }, },