Skip to content

Commit

Permalink
Merge branch 'master' into Issue-1314
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvp8510 authored May 26, 2021
2 parents c7c57c4 + 050e95a commit 0712cb9
Show file tree
Hide file tree
Showing 30 changed files with 230 additions and 422 deletions.
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,28 @@ go 1.14

require (
github.com/Masterminds/semver v1.5.0
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 // indirect
github.com/go-openapi/spec v0.19.8
github.com/googleapis/gnostic v0.3.1
github.com/kr/pretty v0.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/openshift/api v0.0.0-20200701144905-de5b010b2b38
github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e
github.com/opentracing/opentracing-go v1.1.0
github.com/operator-framework/operator-sdk v0.18.2
github.com/pkg/errors v0.9.1
github.com/prometheus/common v0.9.1
github.com/sirupsen/logrus v1.5.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.7.0
github.com/uber/jaeger-client-go v2.20.1+incompatible
go.opentelemetry.io/otel v0.1.2
go.opentelemetry.io/otel/exporter/trace/jaeger v0.1.2
go.opentelemetry.io/otel v0.20.0
go.opentelemetry.io/otel/exporters/trace/jaeger v0.20.0
go.opentelemetry.io/otel/sdk v0.20.0
go.opentelemetry.io/otel/trace v0.20.0
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 // indirect
google.golang.org/grpc v1.32.0
gopkg.in/yaml.v2 v2.3.0
google.golang.org/grpc v1.32.0 // indirect
k8s.io/api v0.18.6
k8s.io/apimachinery v0.18.6
k8s.io/client-go v12.0.0+incompatible
Expand Down
230 changes: 20 additions & 210 deletions go.sum

Large diffs are not rendered by default.

113 changes: 36 additions & 77 deletions pkg/cmd/start/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ import (
"github.com/operator-framework/operator-sdk/pkg/metrics"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/exporter/trace/jaeger"
"go.opentelemetry.io/otel/global"
"google.golang.org/grpc/codes"
"go.opentelemetry.io/otel"
otelattribute "go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -37,21 +35,18 @@ import (
)

func bootstrap(ctx context.Context) manager.Manager {
tracing.Bootstrap()

tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
namespace := getNamespace(ctx)
tracing.Bootstrap(ctx, namespace)

tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "bootstrap")
defer span.End()

setLogLevel(ctx)

namespace := getNamespace(ctx)

buildIdentity(ctx, namespace)

if viper.GetBool("tracing-enabled") {
buildJaegerExporter(ctx)
}
tracing.SetInstanceID(ctx, namespace)

log.WithFields(log.Fields{
"os": runtime.GOOS,
Expand All @@ -69,16 +64,14 @@ func bootstrap(ctx context.Context) manager.Manager {

cfg, err := config.GetConfig()
if err != nil {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.Fatal(err)
}

span.SetAttribute(key.String("Platform", viper.GetString("platform")))
span.SetAttributes(otelattribute.String("Platform", viper.GetString("platform")))
watchNamespace, err := k8sutil.GetWatchNamespace()
if err != nil {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.WithError(err).Fatal("failed to get watch namespace")
}

Expand All @@ -97,7 +90,7 @@ func bootstrap(ctx context.Context) manager.Manager {
}

func detectOAuthProxyImageStream(ctx context.Context, mgr manager.Manager) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "detectOAuthProxyImageStream")
defer span.End()

Expand Down Expand Up @@ -163,7 +156,7 @@ func detectOAuthProxyImageStream(ctx context.Context, mgr manager.Manager) {
}

func detectNamespacePermissions(ctx context.Context, mgr manager.Manager) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "detectNamespacePermissions")
defer span.End()

Expand All @@ -172,16 +165,16 @@ func detectNamespacePermissions(ctx context.Context, mgr manager.Manager) {
if err := mgr.GetAPIReader().List(ctx, namespaces, opts...); err != nil {
log.WithError(err).Trace("could not get a list of namespaces, disabling namespace controller")
tracing.HandleError(err, span)
span.SetAttribute(key.Bool(v1.ConfigEnableNamespaceController, false))
span.SetAttributes(otelattribute.Bool(v1.ConfigEnableNamespaceController, false))
viper.Set(v1.ConfigEnableNamespaceController, false)
} else {
span.SetAttribute(key.Bool(v1.ConfigEnableNamespaceController, true))
span.SetAttributes(otelattribute.Bool(v1.ConfigEnableNamespaceController, true))
viper.Set(v1.ConfigEnableNamespaceController, true)
}
}

func setOperatorScope(ctx context.Context, namespace string) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "setOperatorScope")
defer span.End()

Expand All @@ -190,17 +183,17 @@ func setOperatorScope(ctx context.Context, namespace string) {

// for now, the logic is simple: if we are watching all namespaces, then we are cluster-wide
if viper.GetString(v1.ConfigWatchNamespace) == v1.WatchAllNamespaces {
span.SetAttribute(key.String(v1.ConfigOperatorScope, v1.OperatorScopeCluster))
span.SetAttributes(otelattribute.String(v1.ConfigOperatorScope, v1.OperatorScopeCluster))
viper.Set(v1.ConfigOperatorScope, v1.OperatorScopeCluster)
} else {
log.Info("Consider running the operator in a cluster-wide scope for extra features")
span.SetAttribute(key.String(v1.ConfigOperatorScope, v1.OperatorScopeNamespace))
span.SetAttributes(otelattribute.String(v1.ConfigOperatorScope, v1.OperatorScopeNamespace))
viper.Set(v1.ConfigOperatorScope, v1.OperatorScopeNamespace)
}
}

func setLogLevel(ctx context.Context) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "setLogLevel")
defer span.End()

Expand All @@ -213,7 +206,7 @@ func setLogLevel(ctx context.Context) {
}

func buildIdentity(ctx context.Context, podNamespace string) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "buildIdentity")
defer span.End()

Expand All @@ -229,39 +222,12 @@ func buildIdentity(ctx context.Context, podNamespace string) {
identity = fmt.Sprintf("%s", operatorName)
}

span.SetAttribute(key.String(v1.ConfigIdentity, identity))
span.SetAttributes(otelattribute.String(v1.ConfigIdentity, identity))
viper.Set(v1.ConfigIdentity, identity)
}

func buildJaegerExporter(ctx context.Context) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "buildJaegerExporter")
defer span.End()

agentHostPort := viper.GetString("jaeger-agent-hostport")
jaegerExporter, err := jaeger.NewExporter(
jaeger.WithAgentEndpoint(agentHostPort),
jaeger.WithProcess(jaeger.Process{
ServiceName: "jaeger-operator",
Tags: []core.KeyValue{
key.String("operator.identity", viper.GetString(v1.ConfigIdentity)),
key.String("operator.version", version.Get().Operator),
},
}),
jaeger.WithOnError(func(err error) {
log.WithError(err).Warn("failed to setup the Jaeger exporter")
}),
)
if err == nil {
tracing.AddJaegerExporter(jaegerExporter)
} else {
span.SetStatus(codes.Internal)
log.WithError(err).Warn("could not configure a Jaeger tracer for the operator")
}
}

func createManager(ctx context.Context, cfg *rest.Config) manager.Manager {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "createManager")
defer span.End()

Expand All @@ -283,15 +249,13 @@ func createManager(ctx context.Context, cfg *rest.Config) manager.Manager {
// Create a new manager to provide shared dependencies and start components
mgr, err := manager.New(cfg, options)
if err != nil {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.Fatal(err)
}

// Setup Scheme for all resources
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.Fatal(err)
}

Expand All @@ -301,7 +265,7 @@ func createManager(ctx context.Context, cfg *rest.Config) manager.Manager {
// serveCRMetrics gets the Operator/CustomResource GVKs and generates metrics based on those types.
// It serves those metrics on "http://metricsHost:operatorMetricsPort".
func serveCRMetrics(ctx context.Context, cfg *rest.Config, operatorNs string) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "serveCRMetrics")
defer span.End()

Expand All @@ -310,7 +274,7 @@ func serveCRMetrics(ctx context.Context, cfg *rest.Config, operatorNs string) {
// customize this implementation to avoid permissions issues.
filteredGVK, err := k8sutil.GetGVKsFromAddToScheme(v1.SchemeBuilder.AddToScheme)
if err != nil {
span.SetStatus(codes.Internal)
span.SetStatus(codes.Error, err.Error())
log.WithError(err).Warn("could not retrieve group/version/kind managed by this operator")
return
}
Expand All @@ -319,22 +283,20 @@ func serveCRMetrics(ctx context.Context, cfg *rest.Config, operatorNs string) {
// NOTE that passing nil or an empty list of namespaces in GenerateAndServeCRMetrics will result in an error.
ns, err := kubemetrics.GetNamespacesForMetrics(operatorNs)
if err != nil {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.WithError(err).Warn("could not obtain the namespaces for metrics")
return
}

err = kubemetrics.GenerateAndServeCRMetrics(cfg, ns, filteredGVK, viper.GetString("metrics-host"), viper.GetInt32("cr-metrics-port"))
if err != nil {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.WithError(err).Warn("could not generate and serve custom resource metrics")
}
}

func performUpgrades(ctx context.Context, mgr manager.Manager) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "performUpgrades")
defer span.End()

Expand All @@ -345,7 +307,7 @@ func performUpgrades(ctx context.Context, mgr manager.Manager) {
}

func setupControllers(ctx context.Context, mgr manager.Manager) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "setupControllers")
defer span.End()

Expand All @@ -355,7 +317,7 @@ func setupControllers(ctx context.Context, mgr manager.Manager) {
}

func getNamespace(ctx context.Context) string {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "getNamespace")
defer span.End()

Expand All @@ -365,8 +327,7 @@ func getNamespace(ctx context.Context) string {

var err error
if podNamespace, err = k8sutil.GetOperatorNamespace(); err != nil {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.WithError(err).Warn("could not read the namespace from the service account")
}
}
Expand All @@ -375,7 +336,7 @@ func getNamespace(ctx context.Context) string {
}

func createMetricsService(ctx context.Context, cfg *rest.Config, namespace string) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "createMetricsService")
defer span.End()

Expand All @@ -390,16 +351,15 @@ func createMetricsService(ctx context.Context, cfg *rest.Config, namespace strin
// Create Service object to expose the metrics port(s).
service, err := metrics.CreateMetricsService(ctx, cfg, servicePorts)
if err != nil {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.WithError(err).Warn("could not create metrics Service")
}

createServiceMonitor(ctx, cfg, namespace, service)
}

func createServiceMonitor(ctx context.Context, cfg *rest.Config, namespace string, service *corev1.Service) {
tracer := global.TraceProvider().GetTracer(v1.BootstrapTracer)
tracer := otel.GetTracerProvider().Tracer(v1.BootstrapTracer)
ctx, span := tracer.Start(ctx, "createServiceMonitor")
defer span.End()

Expand All @@ -411,8 +371,7 @@ func createServiceMonitor(ctx context.Context, cfg *rest.Config, namespace strin
if err == metrics.ErrServiceMonitorNotPresent {
log.WithError(err).Info("Install prometheus-operator in your cluster to create ServiceMonitor objects")
} else {
span.SetStatus(codes.Internal)
span.SetAttribute(key.String("error", err.Error()))
span.SetStatus(codes.Error, err.Error())
log.WithError(err).Warn("could not create ServiceMonitor object")
}
}
Expand Down
Loading

0 comments on commit 0712cb9

Please sign in to comment.