-
Notifications
You must be signed in to change notification settings - Fork 6
Instance API #271
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
Open
kaworu
wants to merge
4
commits into
main
Choose a base branch
from
pr/kaworu/instance-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Instance API #271
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ var apps = []string{ | |
| "wordpress", | ||
| "zookeeper", | ||
| } | ||
|
|
||
| var labels = []string{ | ||
| "io.cilium/app", | ||
| "k8s-app", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright Authors of Cilium | ||
|
|
||
| package fake | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "io" | ||
| randv2 "math/rand/v2" | ||
| "strings" | ||
| ) | ||
|
|
||
| // Faker is the main interface exposed to generate fake data. | ||
| type Faker interface { //nolint:interfacebloat | ||
| // Adjective generates a random adjective. | ||
| Adjective() string | ||
| // AlphaNum generates a random alphanumeric string of the given length. | ||
| AlphaNum(length int) string | ||
| // App generates a random software application name. | ||
| App() string | ||
| // Noun generates a random noun. | ||
| Noun() string | ||
| // Name generates a random name. | ||
| Name() string | ||
| // Names generates a random set of names. It panics if n < 0. | ||
| Names(n int) []string | ||
| // DeploymentTier generates a random software deployment tier such as prod, | ||
| // staging, etc. | ||
| DeploymentTier() string | ||
|
|
||
| // K8sLabels generates a random set of Kubernetes labels. | ||
| K8sLabels() []string | ||
| // K8sNamespace generates a random Kubernetes namespace name. | ||
| K8sNamespace() string | ||
| // K8sNodeName generates a random Kubernetes node name. | ||
| K8sNodeName() string | ||
| // K8sPodName generates a random Kubernetes pod name. | ||
| K8sPodName() string | ||
|
|
||
| // MAC generates a random MAC address. | ||
| MAC() string | ||
| // IP generates a random IP address. Options may be provided to specify a | ||
| // network for the address or if it should be IPv4 or IPv6. | ||
| IP(options ...IPOption) string | ||
| // Port generates a random port number between 1 and 65535 or in the range | ||
| // specified by the given option. | ||
| Port(options ...PortOption) uint32 | ||
| } | ||
|
|
||
| // New creates a new Faker using a random seed. | ||
| func New() Faker { | ||
| // NOTE: We seed from the global math/rand/v2 source rather than | ||
| // crypto/rand to avoid errors handling; the faker should not be used for | ||
| // security-sensitive purposes. We use ChaCha8 rather than PCG as ChaCha8 | ||
| // implements io.Reader. | ||
| var seed [32]byte | ||
| for i := range 4 { | ||
| binary.LittleEndian.PutUint64(seed[i*8:], randv2.Uint64()) | ||
| } | ||
| return NewWithSource(randv2.NewChaCha8(seed)) | ||
| } | ||
|
|
||
| // RandSourceReader is a random source that also implements io.Reader. | ||
| type RandSourceReader interface { | ||
| randv2.Source | ||
| io.Reader | ||
| } | ||
|
|
||
| // NewWithSource creates a new Faker using the given random source. Useful to | ||
| // control the faker output, e.g. for testing. | ||
| func NewWithSource(src RandSourceReader) Faker { | ||
| return &faker{ | ||
| Rand: randv2.New(src), | ||
| Reader: src, | ||
| } | ||
| } | ||
|
|
||
| // faker is a private struct implementing Faker. | ||
| type faker struct { | ||
| *randv2.Rand | ||
| io.Reader | ||
| } | ||
|
|
||
| // join3 is a helper to build a string composed of three parts. | ||
| func join3(left, middle, right string) string { | ||
| var sb strings.Builder | ||
| sb.Grow(len(left) + len(middle) + len(right)) | ||
| sb.WriteString(left) | ||
| sb.WriteString(middle) | ||
| sb.WriteString(right) | ||
| return sb.String() | ||
|
kaworu marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // Copyright Authors of Cilium | ||
|
|
||
| package flow | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "io" | ||
| randv2 "math/rand/v2" | ||
|
|
||
| "github.com/cilium/fake" | ||
| "google.golang.org/protobuf/types/known/wrapperspb" | ||
|
|
||
| flowpb "github.com/cilium/cilium/api/v1/flow" | ||
| ) | ||
|
|
||
| // FlowFaker is the main interface exposed to generate fake flow data. | ||
| type FlowFaker interface { //nolint:interfacebloat | ||
| // AuthType generates a random AuthType. | ||
| AuthType() flowpb.AuthType | ||
| // DropReason generates a DropReason. | ||
| DropReason(options ...DropReasonOption) flowpb.DropReason | ||
| // Endpoint generates a random Endpoint. | ||
| Endpoint(options ...EndpointOption) *flowpb.Endpoint | ||
| // EventType generates a random EventType. | ||
| EventType() *flowpb.CiliumEventType | ||
| // New generates a random Hubble Flow. | ||
| NewFlow(options ...Option) *flowpb.Flow | ||
| // ICMPv4 generates a random flow ICMPv4 struct. | ||
| ICMPv4() *flowpb.ICMPv4 | ||
| // ICMPv6 generates a random flow ICMPv6 struct. | ||
| ICMPv6() *flowpb.ICMPv6 | ||
| // IP generates a random flow IP struct. | ||
| IP(options ...IPOption) *flowpb.IP | ||
| // IsReply returns either nil, or a wrapped boolean value reprenting true, | ||
| // or a wrapped boolean value reprenting false, with equal probability. | ||
| IsReply() *wrapperspb.BoolValue | ||
| // Policies generates a list of random policy references. | ||
| Policies() []*flowpb.Policy | ||
| // Layer4 generates a layer 4. If no option is provided, it will be TCP. | ||
| Layer4(options ...Layer4Option) *flowpb.Layer4 | ||
| // Service generates a random Service. | ||
| Service(options ...ServiceOption) *flowpb.Service | ||
| // TraceObservationPoint generates a random TraceObservationPoint. | ||
| TraceObservationPoint() flowpb.TraceObservationPoint | ||
| // TraceReason generates a random TraceReason. | ||
| TraceReason() flowpb.TraceReason | ||
| // TraceContext generates a TraceContext. | ||
| TraceContext(options ...TraceContextOption) *flowpb.TraceContext | ||
| // TrafficDirection generates a random TrafficDirection. | ||
| TrafficDirection() flowpb.TrafficDirection | ||
| // Verdict generates a FORWARDED or DROPPPED verdict randomly. | ||
| Verdict(options ...VerdictOption) flowpb.Verdict | ||
| } | ||
|
|
||
| // NewFaker creates a new Faker using a random seed. | ||
| func NewFaker() FlowFaker { | ||
| // NOTE: We seed from the global math/rand/v2 source rather than | ||
| // crypto/rand to avoid errors handling; the faker should not be used for | ||
| // security-sensitive purposes. We use ChaCha8 rather than PCG as ChaCha8 | ||
| // implements io.Reader. | ||
| var seed [32]byte | ||
| for i := range 4 { | ||
| binary.LittleEndian.PutUint64(seed[i*8:], randv2.Uint64()) | ||
| } | ||
| return NewWithSource(randv2.NewChaCha8(seed)) | ||
| } | ||
|
|
||
| // NewWithSource creates a new Faker using the given random source. Useful to | ||
| // control the faker output, e.g. for testing. | ||
| func NewWithSource(src fake.RandSourceReader) FlowFaker { | ||
| return &flowfaker{ | ||
| Faker: fake.NewWithSource(src), | ||
| Rand: randv2.New(src), | ||
| Reader: src, | ||
| } | ||
| } | ||
|
|
||
| // faker is a private struct implementing Faker. | ||
| type flowfaker struct { | ||
| fake.Faker | ||
| *randv2.Rand | ||
| io.Reader | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the rationale to introduce an interface rather than simply returning a struct? I don't really see the benefit of doing this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is to hide the embedded rand and make the contract explicit. I'm neutral toward this though (we don't have to embed the rand stuff).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some issues with this approach as a 13 method interface will never be implemented in practice and, even if it were implemented elsewhere, every time we add a new method, we would break the existing implementation(s). So I'd really prefer we return a concrete
*Faker. However, I think we should be able to address your concern regarding hiding the embedded rang as we can just not export it and keep it private.