-
Notifications
You must be signed in to change notification settings - Fork 329
/
rthook.go
64 lines (54 loc) · 1.66 KB
/
rthook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package rthooks
import (
"context"
"fmt"
"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/cmd/tetra/common"
"github.com/spf13/cobra"
)
type addContainerConf struct {
containerID string
rootDir string
annotations map[string]string
}
func New() *cobra.Command {
ret := &cobra.Command{
Use: "rthooks",
Short: "trigger runtime hooks (for testing/debugging)",
Hidden: true,
SilenceUsage: true,
}
cnf := addContainerConf{}
add := &cobra.Command{
Use: "create-container --container-id=<containerID> --root-dir=<rootDir>",
Short: "trigger create-container hook",
Run: func(_ *cobra.Command, _ []string) {
common.CliRun(func(ctx context.Context, cli tetragon.FineGuidanceSensorsClient) {
createContainer(ctx, cli, &cnf)
})
},
}
flags := add.Flags()
flags.StringVar(&cnf.containerID, "container-id", "", "container directory")
flags.StringVar(&cnf.rootDir, "root-dir", "", "container root directory")
flags.StringToStringVar(&cnf.annotations, "annotations", map[string]string{}, "container annotations")
ret.AddCommand(add)
return ret
}
func createContainer(ctx context.Context, client tetragon.FineGuidanceSensorsClient, cnf *addContainerConf) {
req := &tetragon.RuntimeHookRequest{
Event: &tetragon.RuntimeHookRequest_CreateContainer{
CreateContainer: &tetragon.CreateContainer{
CgroupsPath: cnf.containerID,
RootDir: cnf.rootDir,
Annotations: cnf.annotations,
},
},
}
_, err := client.RuntimeHook(ctx, req)
if err != nil {
fmt.Printf("triggering create-container hook failed: %s", err)
}
}