Skip to content

Commit 56408b5

Browse files
authored
Kubehound with no Docker lib dependency (#315)
* adding go tag for docker dependencies * adding tag to makefile
1 parent dd25721 commit 56408b5

File tree

10 files changed

+99
-38
lines changed

10 files changed

+99
-38
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ generate: ## Generate code for the application
6666

6767
.PHONY: build
6868
build: ## Build the application
69-
go build $(BUILD_FLAGS) -o "$(or $(DESTDIR),./bin/build)/kubehound$(BINARY_EXT)" ./cmd/kubehound/
69+
go build $(BUILD_FLAGS) -o "$(or $(DESTDIR),./bin/build)/kubehound$(BINARY_EXT)" -tags no_backend ./cmd/kubehound/
7070

7171
.PHONY: binary
7272
binary:
73-
$(BUILDX_CMD) bake binary-with-coverage
73+
$(BUILDX_CMD) bake binary
7474

7575
.PHONY: lint
7676
lint:

cmd/kubehound/backend.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
//go:build no_backend
2+
13
package main
24

35
import (
4-
docker "github.com/DataDog/KubeHound/pkg/backend"
6+
"github.com/DataDog/KubeHound/pkg/backend"
57
"github.com/spf13/cobra"
68
)
79

810
var (
9-
Backend *docker.Backend
11+
Backend *backend.Backend
1012
hard bool
1113
composePath []string
1214

13-
uiProfile = docker.DefaultUIProfile
15+
uiProfile = backend.DefaultUIProfile
1416
uiInvana bool
1517
)
1618

@@ -24,7 +26,7 @@ var (
2426
uiProfile = append(uiProfile, "invana")
2527
}
2628

27-
return docker.NewBackend(cobraCmd.Context(), composePath, uiProfile)
29+
return backend.NewBackend(cobraCmd.Context(), composePath, uiProfile)
2830
},
2931
}
3032

@@ -33,7 +35,7 @@ var (
3335
Short: "Spawn the kubehound stack",
3436
Long: `Spawn the kubehound stack`,
3537
RunE: func(cobraCmd *cobra.Command, args []string) error {
36-
return docker.Up(cobraCmd.Context())
38+
return backend.Up(cobraCmd.Context())
3739
},
3840
}
3941

@@ -42,7 +44,7 @@ var (
4244
Short: "Wipe the persisted backend data",
4345
Long: `Wipe the persisted backend data`,
4446
RunE: func(cobraCmd *cobra.Command, args []string) error {
45-
return docker.Wipe(cobraCmd.Context())
47+
return backend.Wipe(cobraCmd.Context())
4648
},
4749
}
4850

@@ -51,7 +53,7 @@ var (
5153
Short: "Tear down the kubehound stack",
5254
Long: `Tear down the kubehound stack`,
5355
RunE: func(cobraCmd *cobra.Command, args []string) error {
54-
return docker.Down(cobraCmd.Context())
56+
return backend.Down(cobraCmd.Context())
5557
},
5658
}
5759

@@ -60,19 +62,19 @@ var (
6062
Short: "Restart the kubehound stack",
6163
Long: `Restart the kubehound stack`,
6264
RunE: func(cobraCmd *cobra.Command, args []string) error {
63-
err := docker.Down(cobraCmd.Context())
65+
err := backend.Down(cobraCmd.Context())
6466
if err != nil {
6567
return err
6668
}
6769

6870
if hard {
69-
err = docker.Wipe(cobraCmd.Context())
71+
err = backend.Wipe(cobraCmd.Context())
7072
if err != nil {
7173
return err
7274
}
7375
}
7476

75-
return docker.Reset(cobraCmd.Context())
77+
return backend.Reset(cobraCmd.Context())
7678
},
7779
}
7880
)

cmd/kubehound/dev.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
//go:build no_backend
2+
13
package main
24

35
import (
46
"context"
57
"os"
68

79
"github.com/DataDog/KubeHound/pkg/backend"
8-
docker "github.com/DataDog/KubeHound/pkg/backend"
910
"github.com/spf13/cobra"
1011
)
1112

@@ -64,15 +65,15 @@ func runEnv(ctx context.Context, composePaths []string) error {
6465
profiles = append(profiles, backend.DevUIProfile)
6566
}
6667

67-
err := docker.NewBackend(ctx, composePaths, profiles)
68+
err := backend.NewBackend(ctx, composePaths, profiles)
6869
if err != nil {
6970
return err
7071
}
7172
if downTesting {
72-
return docker.Down(ctx)
73+
return backend.Down(ctx)
7374
}
7475

75-
return docker.BuildUp(ctx, noCache)
76+
return backend.BuildUp(ctx, noCache)
7677
}
7778

7879
func init() {

cmd/kubehound/dumper.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66

7-
docker "github.com/DataDog/KubeHound/pkg/backend"
87
"github.com/DataDog/KubeHound/pkg/cmd"
98
"github.com/DataDog/KubeHound/pkg/config"
109
"github.com/DataDog/KubeHound/pkg/kubehound/core"
@@ -91,13 +90,9 @@ var (
9190
}
9291

9392
if startBackend {
94-
err = docker.NewBackend(cobraCmd.Context(), composePath, docker.DefaultUIProfile)
93+
err = runBackendCompose(cobraCmd.Context())
9594
if err != nil {
96-
return fmt.Errorf("new backend: %w", err)
97-
}
98-
err = docker.Up(cobraCmd.Context())
99-
if err != nil {
100-
return fmt.Errorf("docker up: %w", err)
95+
return err
10196
}
10297
}
10398

cmd/kubehound/root.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/DataDog/KubeHound/pkg/backend"
76
"github.com/DataDog/KubeHound/pkg/cmd"
87
"github.com/DataDog/KubeHound/pkg/kubehound/core"
98
"github.com/DataDog/KubeHound/pkg/telemetry/log"
@@ -27,23 +26,10 @@ var (
2726
l := log.Logger(cobraCmd.Context())
2827
// auto spawning the backend stack
2928
if !skipBackend {
30-
// Forcing the embed docker config to be loaded
31-
err := backend.NewBackend(cobraCmd.Context(), []string{""}, backend.DefaultUIProfile)
29+
err := runBackend(cobraCmd.Context())
3230
if err != nil {
3331
return err
3432
}
35-
res, err := backend.IsStackRunning(cobraCmd.Context())
36-
if err != nil {
37-
return err
38-
}
39-
if !res {
40-
err = backend.Up(cobraCmd.Context())
41-
if err != nil {
42-
return err
43-
}
44-
} else {
45-
l.Info("Backend stack is already running")
46-
}
4733
}
4834

4935
// Passing the Kubehound config from viper

cmd/kubehound/util_default.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//go:build !no_backend
2+
3+
package main
4+
5+
import (
6+
"context"
7+
8+
"github.com/DataDog/KubeHound/pkg/telemetry/log"
9+
)
10+
11+
func runBackend(ctx context.Context) error {
12+
l := log.Logger(ctx)
13+
l.Warn("Backend is not supported in this build")
14+
15+
return nil
16+
}
17+
18+
func runBackendCompose(ctx context.Context) error {
19+
l := log.Logger(ctx)
20+
l.Warn("Backend is not supported in this build")
21+
22+
return nil
23+
}

cmd/kubehound/util_no_backend.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//go:build no_backend
2+
3+
package main
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/DataDog/KubeHound/pkg/backend"
10+
"github.com/DataDog/KubeHound/pkg/telemetry/log"
11+
)
12+
13+
func runBackend(ctx context.Context) error {
14+
l := log.Logger(ctx)
15+
16+
// Forcing the embed docker config to be loaded
17+
err := backend.NewBackend(ctx, []string{""}, backend.DefaultUIProfile)
18+
if err != nil {
19+
return err
20+
}
21+
res, err := backend.IsStackRunning(ctx)
22+
if err != nil {
23+
return err
24+
}
25+
if !res {
26+
err = backend.Up(ctx)
27+
if err != nil {
28+
return err
29+
}
30+
} else {
31+
l.Info("Backend stack is already running")
32+
}
33+
34+
return nil
35+
}
36+
37+
func runBackendCompose(ctx context.Context) error {
38+
err := backend.NewBackend(ctx, composePath, backend.DefaultUIProfile)
39+
if err != nil {
40+
return fmt.Errorf("new backend: %w", err)
41+
}
42+
err = backend.Up(ctx)
43+
if err != nil {
44+
return fmt.Errorf("docker up: %w", err)
45+
}
46+
47+
return nil
48+
}

pkg/backend/containers.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build no_backend
2+
13
package backend
24

35
import (

pkg/backend/project.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build no_backend
2+
13
package backend
24

35
import (

pkg/backend/util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build no_backend
2+
13
package backend
24

35
func mergeMaps(currentMap map[interface{}]interface{}, newMap map[interface{}]interface{}) map[interface{}]interface{} {

0 commit comments

Comments
 (0)