Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add collection key to differentiate namespaced policies #2337

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions api/v1/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

282 changes: 156 additions & 126 deletions api/v1/tetragon/sensors.pb.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions api/v1/tetragon/sensors.proto
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,19 @@ message AddTracingPolicyResponse {}

message DeleteTracingPolicyRequest {
string name = 1;
string namespace = 2;
}
message DeleteTracingPolicyResponse {}

message EnableTracingPolicyRequest {
string name = 1;
string namespace = 2;
}
message EnableTracingPolicyResponse {}

message DisableTracingPolicyRequest {
string name = 1;
string namespace = 2;
}
message DisableTracingPolicyResponse {}

Expand Down
1 change: 1 addition & 0 deletions cmd/tetra/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
KeyServerAddress = "server-address" // string
KeyTimeout = "timeout" // duration
KeyRetries = "retries" // int
KeyNamespace = "namespace" //string
)

const (
Expand Down
18 changes: 15 additions & 3 deletions cmd/tetra/tracingpolicy/tracingpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func New() *cobra.Command {
},
}

var tpDelNamespaceFlag string
tpDelCmd := &cobra.Command{
Use: "delete <name>",
Short: "delete a tracing policy",
Expand All @@ -56,7 +57,8 @@ func New() *cobra.Command {
defer c.Close()

_, err := c.Client.DeleteTracingPolicy(c.Ctx, &tetragon.DeleteTracingPolicyRequest{
Name: args[0],
Name: args[0],
Namespace: tpDelNamespaceFlag,
})
if err != nil {
return fmt.Errorf("failed to delete tracing policy: %w", err)
Expand All @@ -67,6 +69,7 @@ func New() *cobra.Command {
},
}

var tpEnableNamespaceFlag string
tpEnableCmd := &cobra.Command{
Use: "enable <name>",
Short: "enable a tracing policy",
Expand All @@ -77,7 +80,8 @@ func New() *cobra.Command {
defer c.Close()

_, err := c.Client.EnableTracingPolicy(c.Ctx, &tetragon.EnableTracingPolicyRequest{
Name: args[0],
Name: args[0],
Namespace: tpEnableNamespaceFlag,
})
if err != nil {
return fmt.Errorf("failed to enable tracing policy: %w", err)
Expand All @@ -88,6 +92,7 @@ func New() *cobra.Command {
},
}

var tpDisableNamespaceFlag string
tpDisableCmd := &cobra.Command{
Use: "disable <name>",
Short: "disable a tracing policy",
Expand All @@ -98,7 +103,8 @@ func New() *cobra.Command {
defer c.Close()

_, err := c.Client.DisableTracingPolicy(c.Ctx, &tetragon.DisableTracingPolicyRequest{
Name: args[0],
Name: args[0],
Namespace: tpDisableNamespaceFlag,
})

if err != nil {
Expand Down Expand Up @@ -191,6 +197,12 @@ func New() *cobra.Command {
return nil
},
}
tpDelFlags := tpDelCmd.Flags()
tpDelFlags.StringVarP(&tpDelNamespaceFlag, common.KeyNamespace, "n", "", "Namespace of the tracing policy.")
tpEnableFlags := tpEnableCmd.Flags()
tpEnableFlags.StringVarP(&tpEnableNamespaceFlag, common.KeyNamespace, "n", "", "Namespace of the tracing policy.")
tpDisableFlags := tpDisableCmd.Flags()
tpDisableFlags.StringVarP(&tpDisableNamespaceFlag, common.KeyNamespace, "n", "", "Namespace of the tracing policy.")
tpListFlags := tpListCmd.Flags()
tpListFlags.StringVarP(&tpListOutputFlag, common.KeyOutput, "o", "text", "Output format. text or json")

Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/content/en/docs/reference/grpc-api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ tetragon_tracingpolicy_loaded{state="load_error"} %d
err = testutil.CollectAndCompare(collector, expectedMetrics(0, 1, 0, 0))
assert.NoError(t, err)

err = manager.DisableTracingPolicy(context.TODO(), "pizza")
err = manager.DisableTracingPolicy(context.TODO(), "pizza", "")
assert.NoError(t, err)
err = testutil.CollectAndCompare(collector, expectedMetrics(1, 0, 0, 0))
assert.NoError(t, err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/sensors/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ func (s TracingPolicyState) ToTetragonState() tetragon.TracingPolicyState {
}
}

// collectionKey is the unique key for sensors
// this enables policies with the same name for different namespaces
type collectionKey struct {
name, namespace string
}

func (ck *collectionKey) String() string {
if ck.namespace != "" {
return fmt.Sprintf("%s/%s", ck.namespace, ck.name)
}
return ck.name
}

// collection is a collection of sensors
// This can either be creating from a tracing policy, or by loading sensors indepenently for sensors
// that are not loaded via a tracing policy (e.g., base sensor) and testing.
Expand Down
Loading
Loading