-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): automate policy generation (#4197)
Signed-off-by: Ilya Lobkov <ilya.lobkov@konghq.com> Signed-off-by: Paul Parkanzky <paul.parkanzky@konghq.com>
- Loading branch information
1 parent
b53348f
commit 7ac2bd9
Showing
21 changed files
with
1,263 additions
and
130 deletions.
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" | ||
``` | ||
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 | ||
``` | ||
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.