Skip to content

Commit 10d7853

Browse files
committed
CSPL-4513 Addressing Igor's changes
1 parent f807ffc commit 10d7853

5 files changed

Lines changed: 34 additions & 14 deletions

File tree

pkg/logging/slog.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,30 @@
1616
package logging
1717

1818
import (
19+
"context"
1920
"io"
2021
"log/slog"
2122
"os"
2223
"strings"
2324
"time"
2425
)
2526

27+
type slogContextKey struct{}
28+
29+
// WithLogger returns a new context with the given slog.Logger attached.
30+
func WithLogger(ctx context.Context, l *slog.Logger) context.Context {
31+
return context.WithValue(ctx, slogContextKey{}, l)
32+
}
33+
34+
// FromContext extracts the slog.Logger from the context.
35+
// Falls back to slog.Default() if none is set.
36+
func FromContext(ctx context.Context) *slog.Logger {
37+
if l, ok := ctx.Value(slogContextKey{}).(*slog.Logger); ok {
38+
return l
39+
}
40+
return slog.Default()
41+
}
42+
2643
const (
2744
EnvLogLevel = "LOG_LEVEL"
2845
EnvLogFormat = "LOG_FORMAT"

pkg/splunk/client/enterprise_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
"bytes"
2020
"context"
2121
"fmt"
22-
"log/slog"
2322
"net/http"
2423
"net/url"
2524
"strings"
2625
"testing"
2726

27+
"github.com/splunk/splunk-operator/pkg/logging"
2828
splcommon "github.com/splunk/splunk-operator/pkg/splunk/common"
2929

3030
spltest "github.com/splunk/splunk-operator/pkg/splunk/test"
@@ -705,7 +705,7 @@ func TestUpdateConfFile(t *testing.T) {
705705
fileName := "outputs"
706706

707707
ctx := context.TODO()
708-
logger := slog.With("func", "TestUpdateConfFile", "name", "test", "namespace", "test")
708+
logger := logging.FromContext(ctx).With("func", "TestUpdateConfFile", "name", "test", "namespace", "test")
709709

710710
// First request: create the property (object) if it doesn't exist
711711
createBody := strings.NewReader(fmt.Sprintf("name=%s", property))

pkg/splunk/enterprise/indexercluster.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
3030

3131
enterpriseApiV3 "github.com/splunk/splunk-operator/api/v3"
32+
"github.com/splunk/splunk-operator/pkg/logging"
3233
splclient "github.com/splunk/splunk-operator/pkg/splunk/client"
3334
splcommon "github.com/splunk/splunk-operator/pkg/splunk/common"
3435
splctrl "github.com/splunk/splunk-operator/pkg/splunk/splkcontroller"
@@ -53,7 +54,7 @@ func ApplyIndexerClusterManager(ctx context.Context, client splcommon.Controller
5354
RequeueAfter: time.Second * 5,
5455
}
5556

56-
logger := slog.With("func", "ApplyIndexerClusterManager", "name", cr.GetName(), "namespace", cr.GetNamespace())
57+
logger := logging.FromContext(ctx).With("func", "ApplyIndexerClusterManager", "name", cr.GetName(), "namespace", cr.GetNamespace())
5758

5859
eventPublisher := GetEventPublisher(ctx, cr)
5960
ctx = context.WithValue(ctx, splcommon.EventPublisherKey, eventPublisher)
@@ -366,7 +367,7 @@ func ApplyIndexerCluster(ctx context.Context, client splcommon.ControllerClient,
366367
Requeue: true,
367368
RequeueAfter: time.Second * 5,
368369
}
369-
logger := slog.With("func", "ApplyIndexerCluster", "name", cr.GetName(), "namespace", cr.GetNamespace())
370+
logger := logging.FromContext(ctx).With("func", "ApplyIndexerCluster", "name", cr.GetName(), "namespace", cr.GetNamespace())
370371

371372
eventPublisher := GetEventPublisher(ctx, cr)
372373
cr.Kind = "IndexerCluster"
@@ -1360,7 +1361,7 @@ var newSplunkClientForQueuePipeline = splclient.NewSplunkClient
13601361

13611362
// updateIndexerConfFiles checks if Queue or Pipeline inputs are created for the first time and updates the conf file if so
13621363
func (mgr *indexerClusterPodManager) updateIndexerConfFiles(ctx context.Context, newCR *enterpriseApi.IndexerCluster, queue *enterpriseApi.QueueSpec, os *enterpriseApi.ObjectStorageSpec, accessKey, secretKey string, k8s rclient.Client) error {
1363-
logger := slog.With("func", "updateIndexerConfFiles", "name", newCR.GetName(), "namespace", newCR.GetNamespace())
1364+
logger := logging.FromContext(ctx).With("func", "updateIndexerConfFiles", "name", newCR.GetName(), "namespace", newCR.GetNamespace())
13641365

13651366
// Only update config for pods that exist
13661367
readyReplicas := newCR.Status.ReadyReplicas

pkg/splunk/enterprise/indexercluster_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"sigs.k8s.io/controller-runtime/pkg/client"
4646
"sigs.k8s.io/controller-runtime/pkg/client/fake"
4747

48+
"github.com/splunk/splunk-operator/pkg/logging"
4849
splclient "github.com/splunk/splunk-operator/pkg/splunk/client"
4950
splcommon "github.com/splunk/splunk-operator/pkg/splunk/common"
5051
spltest "github.com/splunk/splunk-operator/pkg/splunk/test"
@@ -307,7 +308,7 @@ func TestGetMonitoringConsoleClient(t *testing.T) {
307308
},
308309
},
309310
}
310-
scopedLog := slog.With("func", "TestGetMonitoringConsoleClient", "name", "stack1", "namespace", "test")
311+
scopedLog := logging.FromContext(context.Background()).With("func", "TestGetMonitoringConsoleClient", "name", "stack1", "namespace", "test")
311312

312313
secrets := &corev1.Secret{
313314
ObjectMeta: metav1.ObjectMeta{
@@ -336,7 +337,7 @@ func TestGetClusterManagerClient(t *testing.T) {
336337
os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com")
337338

338339
ctx := context.TODO()
339-
scopedLog := slog.With("func", "TestGetClusterManagerClient", "name", "stack1", "namespace", "test")
340+
scopedLog := logging.FromContext(ctx).With("func", "TestGetClusterManagerClient", "name", "stack1", "namespace", "test")
340341
cr := enterpriseApi.IndexerCluster{
341342
TypeMeta: metav1.TypeMeta{
342343
Kind: "IndexerCluster",
@@ -387,7 +388,7 @@ func TestGetClusterManagerClient(t *testing.T) {
387388

388389
func getIndexerClusterPodManager(method string, mockHandlers []spltest.MockHTTPHandler, mockSplunkClient *spltest.MockHTTPClient, replicas int32) *indexerClusterPodManager {
389390
os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com")
390-
scopedLog := slog.With("func", method, "name", "stack1", "namespace", "test")
391+
scopedLog := logging.FromContext(context.Background()).With("func", method, "name", "stack1", "namespace", "test")
391392
cr := enterpriseApi.IndexerCluster{
392393
TypeMeta: metav1.TypeMeta{
393394
Kind: "IndexerCluster",
@@ -1029,7 +1030,7 @@ func TestSetClusterMaintenanceMode(t *testing.T) {
10291030
func TestApplyIdxcSecret(t *testing.T) {
10301031
os.Setenv("SPLUNK_GENERAL_TERMS", "--accept-sgt-current-at-splunk-com")
10311032
method := "ApplyIdxcSecret"
1032-
scopedLog := slog.With("func", method, "name", "stack1", "namespace", "test")
1033+
scopedLog := logging.FromContext(context.Background()).With("func", method, "name", "stack1", "namespace", "test")
10331034
var initObjectList []client.Object
10341035

10351036
ctx := context.TODO()
@@ -2830,7 +2831,7 @@ func TestPasswordSyncCompleted(t *testing.T) {
28302831
// Initialize a minimal pod manager for ApplyIdxcSecret
28312832
mgr := &indexerClusterPodManager{
28322833
c: client,
2833-
log: slog.With("func", "TestPasswordSyncCompleted", "name", idxc.GetName(), "namespace", idxc.GetNamespace()),
2834+
log: logging.FromContext(ctx).With("func", "TestPasswordSyncCompleted", "name", idxc.GetName(), "namespace", idxc.GetNamespace()),
28342835
cr: &idxc,
28352836
}
28362837

@@ -3362,7 +3363,7 @@ func TestIdxcPasswordSyncFailedEvent(t *testing.T) {
33623363

33633364
mgr := &indexerClusterPodManager{
33643365
c: c,
3365-
log: slog.With("func", "TestIdxcPasswordSyncFailedEvent", "name", idxc.GetName(), "namespace", idxc.GetNamespace()),
3366+
log: logging.FromContext(ctx).With("func", "TestIdxcPasswordSyncFailedEvent", "name", idxc.GetName(), "namespace", idxc.GetNamespace()),
33663367
cr: &idxc,
33673368
newSplunkClient: func(managementURI, username, password string) *splclient.SplunkClient {
33683369
sc := splclient.NewSplunkClient(managementURI, username, password)

pkg/splunk/enterprise/ingestorcluster.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"time"
2424

2525
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
26+
"github.com/splunk/splunk-operator/pkg/logging"
2627
splclient "github.com/splunk/splunk-operator/pkg/splunk/client"
2728
splcommon "github.com/splunk/splunk-operator/pkg/splunk/common"
2829
splctrl "github.com/splunk/splunk-operator/pkg/splunk/splkcontroller"
@@ -44,7 +45,7 @@ func ApplyIngestorCluster(ctx context.Context, client client.Client, cr *enterpr
4445
RequeueAfter: time.Second * 5,
4546
}
4647

47-
logger := slog.With("func", "ApplyIngestorCluster", "name", cr.GetName(), "namespace", cr.GetNamespace())
48+
logger := logging.FromContext(ctx).With("func", "ApplyIngestorCluster", "name", cr.GetName(), "namespace", cr.GetNamespace())
4849

4950
if cr.Status.ResourceRevMap == nil {
5051
cr.Status.ResourceRevMap = make(map[string]string)
@@ -311,7 +312,7 @@ func ApplyIngestorCluster(ctx context.Context, client client.Client, cr *enterpr
311312

312313
// getClient for ingestorClusterPodManager returns a SplunkClient for the member n
313314
func (mgr *ingestorClusterPodManager) getClient(ctx context.Context, n int32) *splclient.SplunkClient {
314-
logger := slog.With("func", "getClient", "name", mgr.cr.GetName(), "namespace", mgr.cr.GetNamespace())
315+
logger := logging.FromContext(ctx).With("func", "getClient", "name", mgr.cr.GetName(), "namespace", mgr.cr.GetNamespace())
315316

316317
// Get Pod Name
317318
memberName := GetSplunkStatefulsetPodName(SplunkIngestor, mgr.cr.GetName(), n)
@@ -361,7 +362,7 @@ func getIngestorStatefulSet(ctx context.Context, client splcommon.ControllerClie
361362

362363
// updateIngestorConfFiles checks if Queue or Pipeline inputs are created for the first time and updates the conf file if so
363364
func (mgr *ingestorClusterPodManager) updateIngestorConfFiles(ctx context.Context, newCR *enterpriseApi.IngestorCluster, queue *enterpriseApi.QueueSpec, os *enterpriseApi.ObjectStorageSpec, accessKey, secretKey string, k8s client.Client) error {
364-
logger := slog.With("func", "updateIngestorConfFiles", "name", newCR.GetName(), "namespace", newCR.GetNamespace())
365+
logger := logging.FromContext(ctx).With("func", "updateIngestorConfFiles", "name", newCR.GetName(), "namespace", newCR.GetNamespace())
365366

366367
// Only update config for pods that exist
367368
readyReplicas := newCR.Status.ReadyReplicas

0 commit comments

Comments
 (0)