Skip to content

Commit

Permalink
Add experimental-field-mask flag
Browse files Browse the repository at this point in the history
Follow-up to change in cilium exposing new experimental field in
requests: cilium/cilium#23198

Usage example:
  hubble observe --experimental-field-mask time,verdict

Signed-off-by: Aleksander Mistewicz <amistewicz@google.com>
  • Loading branch information
AwesomePatrol committed Jun 22, 2023
1 parent 212f54b commit 2e43cdf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/observe/flows.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/spf13/viper"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/timestamppb"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -701,6 +702,16 @@ func getFlowsRequest(ofilter *flowFilter, allowlist []string, denylist []string)
First: first,
}

if len(experimentalOpts.fieldMask) > 0 {
fm, err := fieldmaskpb.New(&flowpb.Flow{}, experimentalOpts.fieldMask...)
if err != nil {
return nil, fmt.Errorf("failed to construct field mask: %w", err)
}
req.Experimental = &observerpb.GetFlowsRequest_Experimental{
FieldMask: fm,
}
}

return req, nil
}

Expand Down
22 changes: 22 additions & 0 deletions cmd/observe/flows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cilium/hubble/pkg/defaults"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/fieldmaskpb"
"google.golang.org/protobuf/types/known/timestamppb"
)

Expand Down Expand Up @@ -125,3 +126,24 @@ denylist:
assert.NoError(t, err)
assert.Equal(t, expected, out)
}

func Test_getFlowsRequest_ExperimentalFieldMask_valid(t *testing.T) {
selectorOpts.until = ""
experimentalOpts.fieldMask = []string{"time", "verdict"}
filter := newFlowFilter()
req, err := getFlowsRequest(filter, nil, nil)
assert.NoError(t, err)
assert.Equal(t, &observerpb.GetFlowsRequest{
Number: 20,
Experimental: &observerpb.GetFlowsRequest_Experimental{
FieldMask: &fieldmaskpb.FieldMask{Paths: []string{"time", "verdict"}},
},
}, req)
}

func Test_getFlowsRequest_ExperimentalFieldMask_invalid(t *testing.T) {
experimentalOpts.fieldMask = []string{"time", "verdict", "invalid-field"}
filter := newFlowFilter()
_, err := getFlowsRequest(filter, nil, nil)
assert.ErrorContains(t, err, "invalid-field")
}
7 changes: 7 additions & 0 deletions cmd/observe/observe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ var (
inputFile string
}

experimentalOpts struct {
fieldMask []string
}

printer *hubprinter.Printer

// selector flags
Expand Down Expand Up @@ -150,6 +154,9 @@ func init() {

otherFlags.StringVar(&otherOpts.inputFile, "input-file", "",
"Query flows from this file instead of the server. Use '-' to read from stdin.")

otherFlags.StringSliceVar(&experimentalOpts.fieldMask, "experimental-field-mask", nil,
"Experimental: Comma-separated list of fields for mask. Fields not in the mask will be removed from server response.")
}

// New observer command.
Expand Down

0 comments on commit 2e43cdf

Please sign in to comment.