@@ -3,7 +3,9 @@ package main
33import (
44 "context"
55 "crypto/tls"
6+ "crypto/x509"
67 "fmt"
8+ "io/ioutil"
79 "net"
810 "net/http"
911 "os"
@@ -13,11 +15,14 @@ import (
1315 "syscall"
1416 "time"
1517
18+ "github.com/google/uuid"
1619 "github.com/sirupsen/logrus"
1720 "github.com/virtual-kubelet/virtual-kubelet/log"
1821 logruslogger "github.com/virtual-kubelet/virtual-kubelet/log/logrus"
1922 "google.golang.org/grpc"
23+ "google.golang.org/grpc/connectivity"
2024 "google.golang.org/grpc/credentials"
25+ "google.golang.org/grpc/credentials/insecure"
2126
2227 slurm "github.com/intertwin-eu/interlink-slurm-plugin/pkg/slurm"
2328
@@ -32,10 +37,23 @@ import (
3237)
3338
3439func initProvider (ctx context.Context ) (func (context.Context ) error , error ) {
40+ log .G (ctx ).Info ("Tracing is enabled, setting up the TracerProvider" )
41+
42+ // Get the TELEMETRY_UNIQUE_ID from the environment, if it is not set, use the hostname
43+ uniqueID := os .Getenv ("TELEMETRY_UNIQUE_ID" )
44+ if uniqueID == "" {
45+ log .G (ctx ).Info ("No TELEMETRY_UNIQUE_ID set, generating a new one" )
46+ newUUID := uuid .New ()
47+ uniqueID = newUUID .String ()
48+ log .G (ctx ).Info ("Generated unique ID: " , uniqueID , " use Plugin-" + uniqueID + " as service name from Grafana" )
49+ }
50+
51+ serviceName := "Plugin-" + uniqueID
52+
3553 res , err := resource .New (ctx ,
3654 resource .WithAttributes (
3755 // the service name used to display traces in backends
38- semconv .ServiceName ("InterLink-SLURM-plugin" ),
56+ semconv .ServiceName (serviceName ),
3957 ),
4058 )
4159 if err != nil {
@@ -51,11 +69,56 @@ func initProvider(ctx context.Context) (func(context.Context) error, error) {
5169 otlpEndpoint = "localhost:4317"
5270 }
5371
54- fmt .Println ("TELEMETRY_ENDPOINT: " , otlpEndpoint )
72+ log .G (ctx ).Info ("TELEMETRY_ENDPOINT: " , otlpEndpoint )
73+
74+ caCrtFilePath := os .Getenv ("TELEMETRY_CA_CRT_FILEPATH" )
5575
5676 conn := & grpc.ClientConn {}
57- creds := credentials .NewTLS (& tls.Config {InsecureSkipVerify : true })
58- conn , err = grpc .DialContext (ctx , otlpEndpoint , grpc .WithTransportCredentials (creds ), grpc .WithBlock ())
77+ if caCrtFilePath != "" {
78+
79+ // if the CA certificate is provided, set up mutual TLS
80+
81+ log .G (ctx ).Info ("CA certificate provided, setting up mutual TLS" )
82+
83+ caCert , err := ioutil .ReadFile (caCrtFilePath )
84+ if err != nil {
85+ return nil , fmt .Errorf ("failed to load CA certificate: %w" , err )
86+ }
87+
88+ clientKeyFilePath := os .Getenv ("TELEMETRY_CLIENT_KEY_FILEPATH" )
89+ if clientKeyFilePath == "" {
90+ return nil , fmt .Errorf ("client key file path not provided. Since a CA certificate is provided, a client key is required for mutual TLS" )
91+ }
92+
93+ clientCrtFilePath := os .Getenv ("TELEMETRY_CLIENT_CRT_FILEPATH" )
94+ if clientCrtFilePath == "" {
95+ return nil , fmt .Errorf ("client certificate file path not provided. Since a CA certificate is provided, a client certificate is required for mutual TLS" )
96+ }
97+
98+ certPool := x509 .NewCertPool ()
99+ if ! certPool .AppendCertsFromPEM (caCert ) {
100+ return nil , fmt .Errorf ("failed to append CA certificate" )
101+ }
102+
103+ cert , err := tls .LoadX509KeyPair (clientCrtFilePath , clientKeyFilePath )
104+ if err != nil {
105+ return nil , fmt .Errorf ("failed to load client certificate: %w" , err )
106+ }
107+
108+ tlsConfig := & tls.Config {
109+ Certificates : []tls.Certificate {cert },
110+ RootCAs : certPool ,
111+ MinVersion : tls .VersionTLS12 ,
112+ InsecureSkipVerify : true ,
113+ }
114+ creds := credentials .NewTLS (tlsConfig )
115+ conn , err = grpc .NewClient (otlpEndpoint , grpc .WithTransportCredentials (creds ), grpc .WithBlock ())
116+
117+ } else {
118+ conn , err = grpc .NewClient (otlpEndpoint , grpc .WithTransportCredentials (insecure .NewCredentials ()))
119+ }
120+
121+ conn .WaitForStateChange (ctx , connectivity .Ready )
59122
60123 if err != nil {
61124 return nil , fmt .Errorf ("failed to create gRPC connection to collector: %w" , err )
@@ -82,7 +145,6 @@ func initProvider(ctx context.Context) (func(context.Context) error, error) {
82145
83146 return tracerProvider .Shutdown , nil
84147}
85-
86148func main () {
87149 logger := logrus .StandardLogger ()
88150
0 commit comments