-
Notifications
You must be signed in to change notification settings - Fork 335
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(*): automate policy generation #4197
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cb64fdd
feat(*): policy generation
lobkovilya d33bab5
feat(*): make check
lobkovilya 807bc87
feat(*): make check
lobkovilya 49e2e5e
feat(*): make check
lobkovilya 2988671
feat(*): make check
lobkovilya 767c919
feat(*): update README.md
lobkovilya 725e349
feat(*): make check
lobkovilya b0cab1b
feat(*): delete generated policy
lobkovilya 74e1e0c
feat(*): make check
lobkovilya 2fe4ad8
feat(*): make check
lobkovilya 2520bba
feat(*): validation
lobkovilya 4a3fe6e
Merge branch 'master' into feat/policy-gen
lobkovilya ecd5747
feat(*): update README.md
lobkovilya d571414
feat(*): support for multiple versions
lobkovilya 96bbcad
feat(*): get rid of mustache
lobkovilya 8c43d35
feat(*): make check
lobkovilya f3638cb
feat(*): add tags for OpenAPI schema
lobkovilya 9e530b4
feat(*): update README
lobkovilya 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 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
40 changes: 40 additions & 0 deletions
40
pkg/plugins/policies/donothingpolicy/api/v1alpha1/donothingpolicy.proto
This file contains 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,40 @@ | ||
syntax = "proto3"; | ||
|
||
package kuma.plugins.policies.donothingpolicy.v1alpha1; | ||
|
||
import "mesh/options.proto"; | ||
option go_package = "github.com/kumahq/kuma/pkg/plugins/policies/donothingpolicy/api/v1alpha1"; | ||
|
||
import "mesh/v1alpha1/selector.proto"; | ||
import "config.proto"; | ||
|
||
option (doc.config) = { | ||
type : Policy, | ||
name : "DoNothingPolicy", | ||
file_name : "donothingpolicy" | ||
}; | ||
|
||
// DoNothingPolicy defines permission for traffic between dataplanes. | ||
message DoNothingPolicy { | ||
|
||
option (kuma.mesh.resource).name = "DoNothingPolicyResource"; | ||
option (kuma.mesh.resource).type = "DoNothingPolicy"; | ||
option (kuma.mesh.resource).package = "mesh"; | ||
option (kuma.mesh.resource).kds.send_to_zone = true; | ||
option (kuma.mesh.resource).ws.name = "donothingpolicy"; | ||
option (kuma.mesh.resource).ws.plural = "donothingpolicies"; | ||
option (kuma.mesh.resource).allow_to_inspect = true; | ||
|
||
// List of selectors to match dataplanes that are sources of traffic. | ||
repeated kuma.mesh.v1alpha1.Selector sources = 1 [ (doc.required) = true ]; | ||
// List of selectors to match services that are destinations of traffic. | ||
repeated kuma.mesh.v1alpha1.Selector destinations = 2 | ||
[ (doc.required) = true ]; | ||
|
||
message Conf { | ||
// Set true in case of doing nothing | ||
bool enableDoNothing = 1; | ||
} | ||
|
||
Conf conf = 3; | ||
} |
This file contains 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 @@ | ||
package policies |
This file contains 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,13 @@ | ||
package policies | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
func AddToScheme(s *runtime.Scheme) error { | ||
// Example: | ||
// if err := my_new_policy.AddToScheme(s); err != nil { | ||
// return err | ||
//} | ||
return nil | ||
} |
This file contains 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,108 @@ | ||
## How to generate a new Kuma policy | ||
|
||
1. Create a new directory for the policy in `pkg/plugins/policies`. Example: | ||
```shell | ||
mkdir -p pkg/plugins/policies/donothingpolicy | ||
``` | ||
|
||
2. Create a proto file for new policy in `pkg/plugins/policies/donothingpolicy/api/v1alpha1`. For example | ||
donothingpolicy.proto: | ||
```protobuf | ||
syntax = "proto3"; | ||
|
||
package kuma.plugins.policies.donothingpolicy.v1alpha1; | ||
|
||
import "mesh/options.proto"; | ||
option go_package = "github.com/kumahq/kuma/pkg/plugins/policies/donothingpolicy/api/v1alpha1"; | ||
|
||
import "mesh/v1alpha1/selector.proto"; | ||
import "config.proto"; | ||
|
||
option (doc.config) = { | ||
type : Policy, | ||
name : "DoNothingPolicy", | ||
file_name : "donothingpolicy" | ||
}; | ||
|
||
// DoNothingPolicy defines permission for traffic between dataplanes. | ||
message DoNothingPolicy { | ||
|
||
option (kuma.mesh.resource).name = "DoNothingPolicyResource"; | ||
option (kuma.mesh.resource).type = "DoNothingPolicy"; | ||
option (kuma.mesh.resource).package = "mesh"; | ||
option (kuma.mesh.resource).kds.send_to_zone = true; | ||
option (kuma.mesh.resource).ws.name = "donothingpolicy"; | ||
option (kuma.mesh.resource).ws.plural = "donothingpolicies"; | ||
option (kuma.mesh.resource).allow_to_inspect = true; | ||
|
||
// List of selectors to match dataplanes that are sources of traffic. | ||
repeated kuma.mesh.v1alpha1.Selector sources = 1 [ (doc.required) = true ]; | ||
// List of selectors to match services that are destinations of traffic. | ||
repeated kuma.mesh.v1alpha1.Selector destinations = 2 [ (doc.required) = true ]; | ||
|
||
message Conf { | ||
bool enableDoNothing = 1; | ||
} | ||
|
||
Conf conf = 3; | ||
|
||
} | ||
``` | ||
|
||
3. Call `make generate/policy/<POLICY_NAME>`. Example: | ||
```shell | ||
make generate/policy/donothingpolicy | ||
``` | ||
|
||
4. **Optional.** Add validation. Create file `validator.go`, file with such name won't be cleaned up | ||
by `make cleanup/policy/donothingpolicy`. Implement method `validate() error`: | ||
```go | ||
package v1alpha1 | ||
|
||
func (t *DoNothingPolicyResource) validate() error { | ||
// validate resource here | ||
return nil | ||
} | ||
``` | ||
|
||
5. Add import to `pkg/plugins/policies/imports.go`: | ||
```go | ||
_ "github.com/kumahq/kuma/pkg/plugins/policies/donothingpolicy" | ||
lobkovilya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
6. Add `AddToScheme` call to the `pkg/plugins/policies/scheme.go`: | ||
```go | ||
if err := donothingpolicy.AddToScheme(s); err != nil { | ||
return err | ||
} | ||
``` | ||
|
||
8. Update `cp-rbac.yaml` manually, automation is yet to come. | ||
|
||
## How to use | ||
|
||
Now you can check swagger-ui for this policy: | ||
|
||
```shell | ||
docker run -p 80:8080 -e SWAGGER_JSON=/policy/rest.yaml -v $PWD/pkg/plugins/policies/donothingpolicy/api/v1alpha1:/policy swaggerapi/swagger-ui | ||
``` | ||
lobkovilya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
To actually do something with created policy a ResourceSetHook should be registered: | ||
|
||
```go | ||
// plugin.go | ||
|
||
type myHook struct {} | ||
|
||
func (m *myHook) Modify(resourceSet *core_xds.ResourceSet, ctx xds_context.Context, proxy *core_xds.Proxy) error { | ||
// modify resourceSet here | ||
return nil | ||
} | ||
|
||
func (p *myPlugin) AfterBootstrap(mctx *core_plugins.MutablePluginContext, _ core_plugins.PluginConfig) error { | ||
mctx.XDSHooks().AddResourceSetHook(&myHook{}) | ||
return nil | ||
} | ||
``` | ||
|
||
where `myPlugin` is a `BootstrapPlugin`. |
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.
maybe best just to link to the file