Skip to content

Commit cefb308

Browse files
committed
propagating context
1 parent 3086566 commit cefb308

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+162
-169
lines changed

cmd/kubehound/dumper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var (
6363
}
6464
// Running the ingestion on KHaaS
6565
if cobraCmd.Flags().Lookup("khaas-server").Value.String() != "" {
66-
return core.CoreClientGRPCIngest(khCfg.Ingestor, khCfg.Dynamic.ClusterName, khCfg.Dynamic.RunID.String())
66+
return core.CoreClientGRPCIngest(cobraCmd.Context(), khCfg.Ingestor, khCfg.Dynamic.ClusterName, khCfg.Dynamic.RunID.String())
6767
}
6868

6969
return err

cmd/kubehound/ingest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ var (
6666
}
6767

6868
if isIngestRemoteDefault() {
69-
return core.CoreClientGRPCRehydrateLatest(khCfg.Ingestor)
69+
return core.CoreClientGRPCRehydrateLatest(cobraCmd.Context(), khCfg.Ingestor)
7070
}
7171

72-
return core.CoreClientGRPCIngest(khCfg.Ingestor, khCfg.Dynamic.ClusterName, runID)
72+
return core.CoreClientGRPCIngest(cobraCmd.Context(), khCfg.Ingestor, khCfg.Dynamic.ClusterName, runID)
7373
},
7474
}
7575
)

cmd/kubehound/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ var (
7171
return nil
7272
},
7373
PersistentPostRunE: func(cobraCmd *cobra.Command, args []string) error {
74-
return cmd.CloseKubehoundConfig()
74+
return cmd.CloseKubehoundConfig(cobraCmd.Context())
7575
},
7676
SilenceUsage: true,
7777
SilenceErrors: true,

cmd/kubehound/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var (
2727
return core.CoreGrpcApi(cobraCmd.Context(), khCfg)
2828
},
2929
PersistentPostRunE: func(cobraCmd *cobra.Command, args []string) error {
30-
return cmd.CloseKubehoundConfig()
30+
return cmd.CloseKubehoundConfig(cobraCmd.Context())
3131
},
3232
}
3333
)

deployments/kubehound/binary/Dockerfile_debug

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ COPY deployments ./deployments
1111

1212
RUN GOOS=linux GOARCH=amd64 go build -o "./bin/build/kubehound" ./cmd/kubehound/
1313

14-
FROM gcr.io/distroless/base-debian12 AS build-release-stage
14+
FROM ubuntu:24.04 AS build-release-stage
1515

1616
WORKDIR /
1717

1818
COPY --from=build-stage /go/bin/build/kubehound /kubehound
1919

2020
EXPOSE 9000
2121

22-
USER nonroot:nonroot
23-
2422
ENTRYPOINT ["/kubehound"]
2523
CMD ["serve"]

pkg/cmd/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func InitializeKubehoundConfig(ctx context.Context, configPath string, generateR
3131
viper.Set(config.DynamicRunID, config.NewRunID())
3232
}
3333

34-
khCfg := config.NewKubehoundConfig(configPath, inline)
34+
khCfg := config.NewKubehoundConfig(ctx, configPath, inline)
3535
// Activate debug mode if needed
3636
if khCfg.Debug {
3737
l.Info("Debug mode activated")
@@ -49,7 +49,7 @@ func InitTelemetry(khCfg *config.KubehoundConfig) {
4949
ctx := context.Background()
5050
l := log.Logger(ctx)
5151
l.Info("Initializing application telemetry")
52-
err := telemetry.Initialize(khCfg)
52+
err := telemetry.Initialize(ctx, khCfg)
5353
if err != nil {
5454
l.Warn("failed telemetry initialization", log.ErrorField(err))
5555
}
@@ -76,13 +76,13 @@ func InitTags(ctx context.Context, khCfg *config.KubehoundConfig) {
7676
// log.AddGlobalTags(khCfg.Telemetry.Tags)
7777
}
7878

79-
func CloseKubehoundConfig() error {
79+
func CloseKubehoundConfig(ctx context.Context) error {
8080
khCfg, err := GetConfig()
8181
if err != nil {
8282
return err
8383
}
8484

85-
telemetry.Shutdown(khCfg.Telemetry.Enabled)
85+
telemetry.Shutdown(ctx, khCfg.Telemetry.Enabled)
8686

8787
return nil
8888
}

pkg/cmd/dump.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ func InitLocalDumpCmd(cmd *cobra.Command) {
3838

3939
func InitRemoteDumpCmd(cmd *cobra.Command) {
4040
cmd.Flags().String("bucket-url", "", "Bucket to use to push k8s resources (e.g.: s3://<your_bucket>)")
41-
viper.BindPFlag(config.CollectorFileBlobBucket, cmd.Flags().Lookup("bucket-url")) //nolint: errcheck
41+
viper.BindPFlag(config.IngestorBlobBucketURL, cmd.Flags().Lookup("bucket-url")) //nolint: errcheck
4242

4343
cmd.Flags().String("region", "", "Region to retrieve the configuration (only for s3) (e.g.: us-east-1)")
44-
viper.BindPFlag(config.CollectorFileBlobRegion, cmd.Flags().Lookup("region")) //nolint: errcheck
44+
viper.BindPFlag(config.IngestorBlobBucketURL, cmd.Flags().Lookup("region")) //nolint: errcheck
4545
}
4646

4747
func InitLocalIngestCmd(cmd *cobra.Command) {

pkg/collector/file.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,14 @@ const (
5454
// FileCollector implements a collector based on local K8s API json files generated outside the KubeHound application via e.g kubectl.
5555
type FileCollector struct {
5656
cfg *config.FileCollectorConfig
57-
log *log.KubehoundLogger
5857
tags collectorTags
5958
clusterName string
6059
}
6160

6261
// NewFileCollector creates a new instance of the file collector from the provided application config.
6362
func NewFileCollector(ctx context.Context, cfg *config.KubehoundConfig) (CollectorClient, error) {
63+
ctx = context.WithValue(ctx, log.ContextFieldComponent, FileCollectorName)
64+
l := log.Trace(ctx)
6465
if cfg.Collector.Type != config.CollectorTypeFile {
6566
return nil, fmt.Errorf("invalid collector type in config: %s", cfg.Collector.Type)
6667
}
@@ -69,12 +70,10 @@ func NewFileCollector(ctx context.Context, cfg *config.KubehoundConfig) (Collect
6970
return nil, errors.New("file collector config not provided")
7071
}
7172

72-
l := log.Trace(ctx)
73-
l.Info("Creating file collector from directory", log.String("path", cfg.Collector.File.Directory))
73+
l.Info("Creating file collector from directory", log.String(log.FieldPathKey, cfg.Collector.File.Directory))
7474

7575
return &FileCollector{
76-
cfg: cfg.Collector.File,
77-
// log: l,
76+
cfg: cfg.Collector.File,
7877
tags: newCollectorTags(),
7978
clusterName: cfg.Dynamic.ClusterName,
8079
}, nil
@@ -139,6 +138,7 @@ func (c *FileCollector) streamPodsNamespace(ctx context.Context, fp string, inge
139138
func (c *FileCollector) StreamPods(ctx context.Context, ingestor PodIngestor) error {
140139
span, ctx := tracer.StartSpanFromContext(ctx, span.CollectorStream, tracer.Measured())
141140
span.SetTag(tag.EntityTag, tag.EntityPods)
141+
l := log.Trace(ctx)
142142
var err error
143143
defer func() { span.Finish(tracer.WithError(err)) }()
144144

@@ -156,7 +156,7 @@ func (c *FileCollector) StreamPods(ctx context.Context, ingestor PodIngestor) er
156156
return nil
157157
}
158158

159-
c.log.Debugf("Streaming pods from file %s", fp)
159+
l.Debug("Streaming pods from file", log.String(log.FieldPathKey, fp), log.String(log.FieldEntityKey, tag.EntityPods))
160160

161161
return c.streamPodsNamespace(ctx, fp, ingestor)
162162
})
@@ -190,6 +190,7 @@ func (c *FileCollector) streamRolesNamespace(ctx context.Context, fp string, ing
190190
func (c *FileCollector) StreamRoles(ctx context.Context, ingestor RoleIngestor) error {
191191
span, ctx := tracer.StartSpanFromContext(ctx, span.CollectorStream, tracer.Measured())
192192
span.SetTag(tag.EntityTag, tag.EntityRoles)
193+
l := log.Trace(ctx)
193194
var err error
194195
defer func() { span.Finish(tracer.WithError(err)) }()
195196

@@ -199,17 +200,17 @@ func (c *FileCollector) StreamRoles(ctx context.Context, ingestor RoleIngestor)
199200
return nil
200201
}
201202

202-
f := filepath.Join(path, RolesPath)
203+
fp := filepath.Join(path, RolesPath)
203204

204205
// Check if the file exists
205-
if _, err := os.Stat(f); os.IsNotExist(err) {
206+
if _, err := os.Stat(fp); os.IsNotExist(err) {
206207
// Skipping streaming as file does not exist (k8s type not necessary required in a namespace, for instance, an namespace can have no roles)
207208
return nil
208209
}
209210

210-
c.log.Debugf("Streaming roles from file %s", f)
211+
l.Debug("Streaming roles from file", log.String(log.FieldPathKey, fp), log.String(log.FieldEntityKey, tag.EntityRoles))
211212

212-
return c.streamRolesNamespace(ctx, f, ingestor)
213+
return c.streamRolesNamespace(ctx, fp, ingestor)
213214
})
214215

215216
if err != nil {
@@ -241,6 +242,7 @@ func (c *FileCollector) streamRoleBindingsNamespace(ctx context.Context, fp stri
241242
func (c *FileCollector) StreamRoleBindings(ctx context.Context, ingestor RoleBindingIngestor) error {
242243
span, ctx := tracer.StartSpanFromContext(ctx, span.CollectorStream, tracer.Measured())
243244
span.SetTag(tag.EntityTag, tag.EntityRolebindings)
245+
l := log.Trace(ctx)
244246
var err error
245247
defer func() { span.Finish(tracer.WithError(err)) }()
246248

@@ -258,7 +260,7 @@ func (c *FileCollector) StreamRoleBindings(ctx context.Context, ingestor RoleBin
258260
return nil
259261
}
260262

261-
c.log.Debugf("Streaming role bindings from file %s", fp)
263+
l.Debug("Streaming role bindings from file", log.String(log.FieldPathKey, fp), log.String(log.FieldEntityKey, tag.EntityRolebindings))
262264

263265
return c.streamRoleBindingsNamespace(ctx, fp, ingestor)
264266
})
@@ -292,6 +294,7 @@ func (c *FileCollector) streamEndpointsNamespace(ctx context.Context, fp string,
292294
func (c *FileCollector) StreamEndpoints(ctx context.Context, ingestor EndpointIngestor) error {
293295
span, ctx := tracer.StartSpanFromContext(ctx, span.CollectorStream, tracer.Measured())
294296
span.SetTag(tag.EntityTag, tag.EntityEndpoints)
297+
l := log.Trace(ctx)
295298
var err error
296299
defer func() { span.Finish(tracer.WithError(err)) }()
297300

@@ -308,8 +311,7 @@ func (c *FileCollector) StreamEndpoints(ctx context.Context, ingestor EndpointIn
308311
// Skipping streaming as file does not exist (k8s type not necessary required in a namespace, for instance, an namespace can have no endpoints)
309312
return nil
310313
}
311-
312-
c.log.Debugf("Streaming endpoint slices from file %s", fp)
314+
l.Debug("Streaming endpoints slices from file", log.String(log.FieldPathKey, fp), log.String(log.FieldEntityKey, tag.EntityEndpoints))
313315

314316
return c.streamEndpointsNamespace(ctx, fp, ingestor)
315317
})
@@ -324,11 +326,12 @@ func (c *FileCollector) StreamEndpoints(ctx context.Context, ingestor EndpointIn
324326
func (c *FileCollector) StreamNodes(ctx context.Context, ingestor NodeIngestor) error {
325327
span, ctx := tracer.StartSpanFromContext(ctx, span.CollectorStream, tracer.Measured())
326328
span.SetTag(tag.EntityTag, tag.EntityNodes)
329+
l := log.Trace(ctx)
327330
var err error
328331
defer func() { span.Finish(tracer.WithError(err)) }()
329332

330333
fp := filepath.Join(c.cfg.Directory, NodePath)
331-
c.log.Debugf("Streaming nodes from file %s", fp)
334+
l.Debug("Streaming nodes from file", log.String(log.FieldPathKey, fp), log.String(log.FieldEntityKey, tag.EntityNodes))
332335

333336
list, err := readList[corev1.NodeList](ctx, fp)
334337
if err != nil {
@@ -350,11 +353,12 @@ func (c *FileCollector) StreamNodes(ctx context.Context, ingestor NodeIngestor)
350353
func (c *FileCollector) StreamClusterRoles(ctx context.Context, ingestor ClusterRoleIngestor) error {
351354
span, ctx := tracer.StartSpanFromContext(ctx, span.CollectorStream, tracer.Measured())
352355
span.SetTag(tag.EntityTag, tag.EntityClusterRoles)
356+
l := log.Trace(ctx)
353357
var err error
354358
defer func() { span.Finish(tracer.WithError(err)) }()
355359

356360
fp := filepath.Join(c.cfg.Directory, ClusterRolesPath)
357-
c.log.Debugf("Streaming cluster roles from file %s", fp)
361+
l.Debug("Streaming cluster role from file", log.String(log.FieldPathKey, fp), log.String(log.FieldEntityKey, tag.EntityClusterRoles))
358362

359363
list, err := readList[rbacv1.ClusterRoleList](ctx, fp)
360364
if err != nil {
@@ -376,11 +380,12 @@ func (c *FileCollector) StreamClusterRoles(ctx context.Context, ingestor Cluster
376380
func (c *FileCollector) StreamClusterRoleBindings(ctx context.Context, ingestor ClusterRoleBindingIngestor) error {
377381
span, ctx := tracer.StartSpanFromContext(ctx, span.CollectorStream, tracer.Measured())
378382
span.SetTag(tag.EntityTag, tag.EntityClusterRolebindings)
383+
l := log.Trace(ctx)
379384
var err error
380385
defer func() { span.Finish(tracer.WithError(err)) }()
381386

382387
fp := filepath.Join(c.cfg.Directory, ClusterRoleBindingsPath)
383-
c.log.Debugf("Streaming cluster role bindings from file %s", fp)
388+
l.Debug("Streaming cluster role bindings from file", log.String(log.FieldPathKey, fp), log.String(log.FieldEntityKey, tag.EntityClusterRolebindings))
384389

385390
list, err := readList[rbacv1.ClusterRoleBindingList](ctx, fp)
386391
if err != nil {

pkg/collector/file_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestFileCollector_Constructor(t *testing.T) {
1616
t.Parallel()
1717

1818
v := viper.New()
19-
cfg, err := config.NewConfig(v, "testdata/kubehound-test.yaml")
19+
cfg, err := config.NewConfig(context.TODO(), v, "testdata/kubehound-test.yaml")
2020
assert.NoError(t, err)
2121

2222
c, err := NewFileCollector(context.Background(), cfg)
@@ -64,7 +64,7 @@ func NewTestFileCollector(t *testing.T) *FileCollector {
6464
t.Helper()
6565

6666
v := viper.New()
67-
cfg, err := config.NewConfig(v, "testdata/kubehound-test.yaml")
67+
cfg, err := config.NewConfig(context.TODO(), v, "testdata/kubehound-test.yaml")
6868
assert.NoError(t, err)
6969

7070
c, err := NewFileCollector(context.Background(), cfg)

pkg/collector/k8s_api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ func NewK8sAPICollector(ctx context.Context, cfg *config.KubehoundConfig) (Colle
8989
return nil, errors.New("user did not confirm")
9090
}
9191
} else {
92-
l.Warnf("Non-interactive mode enabled, proceeding with k8s cluster dump: %s", clusterName)
92+
msg := fmt.Sprintf("Non-interactive mode enabled, proceeding with k8s cluster dump: %s", clusterName)
93+
l.Warn(msg)
9394
}
9495

9596
err = checkK8sAPICollectorConfig(cfg.Collector.Type)

0 commit comments

Comments
 (0)