Skip to content

Commit

Permalink
Chore: refactor filter and action with cuex
Browse files Browse the repository at this point in the history
Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
  • Loading branch information
FogDong committed Jan 30, 2023
1 parent 2a0175f commit dc20549
Show file tree
Hide file tree
Showing 54 changed files with 799 additions and 4,108 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-binary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ permissions:
contents: write

env:
GO_VERSION: '1.17'
GO_VERSION: '1.19'

jobs:
kube-trigger:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
workflow_dispatch: { }

env:
GO_VERSION: '1.17'
GO_VERSION: '1.19'
GOLANGCI_VERSION: 'v1.47.2'

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/upload-test-binary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:

env:
GOLANGCI_VERSION: 'v1.47.2'
GO_VERSION: '1.17'
GO_VERSION: '1.19'

jobs:
detect-noop:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ default.etcd
# Generated Dockerfile
# This is automatically generated by Make depending on which image you want to build.
Dockerfile

vendor/
2 changes: 1 addition & 1 deletion Dockerfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# You need to enable Docker BuildKit for this to work.
# Also refer to Makefile for how it can be done.

ARG BUILD_IMAGE=golang:1.17
ARG BUILD_IMAGE=golang:1.19
ARG BASE_IMAGE=gcr.io/distroless/static:nonroot

# Force native build platform, and cross-build to target platform later.
Expand Down
55 changes: 22 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extensions.

### Sources

A Source is what listens to events (event source). For example, a `k8s-resource-watcher` source can watch Kubernetes
A Source is what listens to events (event source). For example, a `resource-watcher` source can watch Kubernetes
resources. Once a Kubernetes resource (e.g. ConfigMap) is changed, it will raise an event that will be passed
to [Filters](#Filters) for further processing.

Expand All @@ -40,7 +40,7 @@ updated whenever the ConfigMaps that are referenced by `ref-objects` are updated

To accomplish this, we will:

- use a `k8s-resource-watcher` Source to listen to update events of ConfigMaps
- use a `resource-watcher` Source to listen to update events of ConfigMaps
- use a `cue-validator` Filter to only keep the ConfigMaps that we are interested in
- trigger an `bump-application-revision` Action to update Application.

Expand Down Expand Up @@ -85,7 +85,7 @@ An example config file looks like this:
# You can add multiple triggers.
triggers:
- source:
type: k8s-resource-watcher
type: resource-watcher
properties:
# We are interested in ConfigMap events.
apiVersion: "v1"
Expand All @@ -94,23 +94,17 @@ triggers:
# Only watch update event.
events:
- update
filters:
- type: cue-validator
# Filter the events above.
properties:
# Filter by validating the object data using CUE.
# For example, we are filtering by ConfigMap names (metadata.name) from above.
# Only ConfigMaps with names that satisfy this regexp "this-will-trigger-update-.*" will be kept.
template: |
metadata: name: =~"this-will-trigger-update-.*"
actions:
# Filter the events above.
filter: |
context: data: metadata: name: =~"this-will-trigger-update-.*"
action:
# Bump Application Revision to update Application.
- type: bump-application-revision
properties:
namespace: default
# Select Applications to bump using labels.
labelSelectors:
my-label: my-value
type: bump-application-revision
properties:
namespace: default
# Select Applications to bump using labels.
matchingLabels:
my-label: my-value
```

Let's assume your config file is `config.yaml`, to run kube-trigger:
Expand Down Expand Up @@ -141,26 +135,21 @@ spec:
instance: kubetrigger-sample
triggers:
- source:
type: k8s-resource-watcher
type: resource-watcher
properties:
apiVersion: "v1"
kind: ConfigMap
namespace: default
events:
- update
filters:
- type: cue-validator
properties:
template: |
// Filter by object name.
// I used regular expressions here.
metadata: name: =~"this-will-trigger-update-.*"
actions:
- type: bump-application-revision
properties:
namespace: default
labelSelectors:
my-label: my-value
filter: |
context: data: metadata: name: =~"this-will-trigger-update-.*"
action:
type: bump-application-revision
properties:
namespace: default
matchingLabels:
my-label: my-value
```

## Advanced kube-trigger Instance Configuration
Expand Down
30 changes: 4 additions & 26 deletions api/v1alpha1/triggerservice_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ type TriggerServiceList struct {

// TriggerMeta is the meta data of a trigger.
type TriggerMeta struct {
Source Source `json:"source"`
Filters []FilterMeta `json:"filters"`
Actions []ActionMeta `json:"actions"`
Source Source `json:"source"`
Filter string `json:"filter"`
Action ActionMeta `json:"action"`
}

// ActionMeta is what users type in their configurations, specifying what action
Expand All @@ -66,21 +66,6 @@ type ActionMeta struct {
// Properties are user-provided parameters. You should parse it yourself.
// +kubebuilder:pruning:PreserveUnknownFields
Properties *runtime.RawExtension `json:"properties,omitempty"`

// Raw is the raw string representation of this action. Typically, you will
// not use it. This is for identifying action instances.
Raw string `json:"raw,omitempty"`
}

// FilterMeta is what users type in their configurations, specifying what filter
// they want to use and what properties they provided.
type FilterMeta struct {
// Type is the type (identifier) of this filter.
Type string `json:"type"`

// Properties are user-provided parameters. You should parse it yourself.
// +kubebuilder:pruning:PreserveUnknownFields
Properties *runtime.RawExtension `json:"properties,omitempty"`
}

// Source defines the Source of trigger.
Expand All @@ -92,18 +77,11 @@ type Source struct {

const (
// SourceTypeResourceWatcher is the source type for K8sResourceWatcher.
SourceTypeResourceWatcher string = "k8s-resource-watcher"
SourceTypeResourceWatcher string = "resource-watcher"
// SourceTypeWebhookTrigger is the source type for WebhookTrigger.
SourceTypeWebhookTrigger string = "webhook-trigger"
)

const (
// ActionTypeBumpApplicationRevision is the source type for BumpApplicationRevision.
ActionTypeBumpApplicationRevision string = "bump-application-revision"
// ActionTypePatchResource is the source type for WebhookTrigger.
ActionTypePatchResource string = "patch-resource"
)

func init() {
SchemeBuilder.Register(&TriggerService{}, &TriggerServiceList{})
}
35 changes: 1 addition & 34 deletions api/v1alpha1/zz_generated.deepcopy.go

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

61 changes: 19 additions & 42 deletions config/crd/standard.oam.dev_triggerservices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,47 +42,24 @@ spec:
items:
description: TriggerMeta is the meta data of a trigger.
properties:
actions:
items:
description: ActionMeta is what users type in their configurations,
specifying what action they want to use and what properties
they provided.
properties:
properties:
description: Properties are user-provided parameters.
You should parse it yourself.
type: object
x-kubernetes-preserve-unknown-fields: true
raw:
description: Raw is the raw string representation of this
action. Typically, you will not use it. This is for
identifying action instances.
type: string
type:
description: Type is the type (identifier) of this action.
type: string
required:
- type
type: object
type: array
filters:
items:
description: FilterMeta is what users type in their configurations,
specifying what filter they want to use and what properties
they provided.
action:
description: ActionMeta is what users type in their configurations,
specifying what action they want to use and what properties
they provided.
properties:
properties:
properties:
description: Properties are user-provided parameters.
You should parse it yourself.
type: object
x-kubernetes-preserve-unknown-fields: true
type:
description: Type is the type (identifier) of this filter.
type: string
required:
- type
type: object
type: array
description: Properties are user-provided parameters. You
should parse it yourself.
type: object
x-kubernetes-preserve-unknown-fields: true
type:
description: Type is the type (identifier) of this action.
type: string
required:
- type
type: object
filter:
type: string
source:
description: Source defines the Source of trigger.
properties:
Expand All @@ -96,8 +73,8 @@ spec:
- type
type: object
required:
- actions
- filters
- action
- filter
- source
type: object
type: array
Expand Down
27 changes: 10 additions & 17 deletions config/samples/standard_v1alpha1_triggerservice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ spec:
# You can add multiple triggers.
triggers:
- source:
type: k8s-resource-watcher
type: resource-watcher
properties:
# We are interested in ConfigMap events.
apiVersion: "v1"
Expand All @@ -21,20 +21,13 @@ spec:
# Only watch update event.
events:
- update
filters:
- type: cue-validator
# Filter the events above.
properties:
# Filter by validating the object data using CUE.
# For example, we are filtering by ConfigMap names (metadata.name) from above.
# Only ConfigMaps with names that satisfy this regexp "this-will-trigger-update-.*" will be kept.
template: |
metadata: name: =~"this-will-trigger-update-.*"
actions:
filter: |
context: data: metadata: name: =~"this-will-trigger-update-.*"
action:
# Bump Application Revision to update Application.
- type: bump-application-revision
properties:
namespace: default
# Select Applications to bump using labels.
labelSelectors:
my-label: my-value
type: bump-application-revision
properties:
namespace: default
# Select Applications to bump using labels.
matchingLabels:
my-label: my-value
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ updated whenever the ConfigMaps that are referenced by `ref-objects` are updated

## What we want to achieve?

- use a `k8s-resource-watcher` Source to listen to update events of ConfigMaps
- use a `resource-watcher` Source to listen to update events of ConfigMaps
- use a `cue-validator` Filter to only keep the ConfigMaps that we are interested in
- trigger an `bump-application-revision` Action to update Application.

Expand Down
Loading

0 comments on commit dc20549

Please sign in to comment.