Giter VIP home page Giter VIP logo

kube-trigger's Introduction

kube-trigger

Go Report Card LICENSE Releases codecov Docker Pulls TODOs Go Checks Unit Tests

kube-trigger is a workflow based trigger that combines listeners, filter events and action triggers in a programmable way with CUElang.

kube-trigger overview

Overview

Although there is kube in the name, it is actually not limited to Kubernetes and can do much more than that. It has an extensible architecture that can extend its capabilities fairly easily. We have docs (not yet) on how to extend Source, Filter, and Action. All users are welcomed to contribute their own extensions.

Source

A Source is what listens to events (event source). For example, a resource-watcher source can watch Kubernetes resources from multi-clusters. Once a Kubernetes resource (e.g. Deployment) is changed, it will raise an event that will be passed to Filter for further processing.

source:
  type: resource-watcher
  properties:
    apiVersion: apps/v1
    kind: Deployment
    events:
      - update

Filters

A Filter will filter the events that are raised by Source, i.e, drop events that do not satisfy a certain criteria. For example, users can check the status of the Deployment to decide whether to filter out events. All the events that passed the Filter will then trigger an Action.

filter: context.data.status.readyReplicas == context.data.status.replicas

Actions

An Action is a job that does what the user specified when an event happens. For example, the user can send notifications, log events, execute a command, or patch some Kubernetes objects when an event happens.

For example, you can use the built-in action patch-resource to patch resources in Kubernetes like:

action:
  type: patch-resource
  properties:
    resource:
      apiVersion: v1
      kind: ConfigMap
      name: my-cm
      namespace: default
    patch:
      type: merge
      data:
        data: 
          foo: bar

The underlying mechanism of action is using CUE to render the template and execute it with your parameters. For example, the above action can be written in CUE as:

import (
	"vela/kube"
)

patchObject: kube.#Patch & {
	$params: {
		resource: {
			apiVersion: parameter.resource.apiVersion
			kind:       parameter.resource.kind
			metadata: {
				name:      parameter.resource.name
				namespace: parameter.resource.namespace
			}
		}
		patch: parameter.patch
	}
}

// users' parameters to be passed to the action
parameter: {
	// +usage=The resource to patch
	resource: {
		// +usage=The api version of the resource
		apiVersion: string
		// +usage=The kind of the resource
		kind: string
		// +usage=The metadata of the resource
		metadata: {
			// +usage=The name of the resource
			name: string
			// +usage=The namespace of the resource
			namespace: *"default" | string
		}
	}
	// +usage=The patch to be applied to the resource with kubernetes patch
	patch: *{
		// +usage=The type of patch being provided
		type: "merge"
		data: {...}
	} | {
		// +usage=The type of patch being provided
		type: "json"
		data: [{...}]
	} | {
		// +usage=The type of patch being provided
		type: "strategic"
		data: {...}
	}
}

Quick Start

To quickly know the concepts of kube-trigger, let's use a real use-case as an exmaple ( see #4418). TL;DR, the user want the Application to be automatically updated whenever the ConfigMaps that are referenced by ref-objects are updated.

To accomplish this, we will:

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

And the trigger config file will look like:

triggers:
  - source:
      type: resource-watcher
      properties:
        # We are interested in ConfigMap events.
        apiVersion: "v1"
        kind: ConfigMap
        namespace: default
        # Only watch update event.
        events:
          - update
    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.
        nameSelector:
          fromLabel: "watch-this"

See examples directory for more instructions.

Usage

You can run kube-trigger in two modes: standalone and in-cluster.

Configuration File

A config file instructs kube-trigger to use what Source, Filter, and Action, and how they are configured.

No matter you are running kube-trigger as standalone or in-cluster, the config format is similar, so it is beneficial to know the format first. We will use yaml format as an example (json and cue are also supported).

# A trigger is a group of Source, Filter, and Action.
# You can add multiple triggers.
triggers:
  - source:
      type: <your-source-type>
      properties: ...
      # ... properties
    filter: <your-filter>
    action:
      type: <your-action-type>
      properties: ...

Standalone

When running in standalone mode, you will need to provide a config file to kube-trigger binary.

kube-trigger can accept cue, yaml, and json config files. You can also specify a directory to load all the supported files inside that directory. -c/--config cli flag and CONFIG environment variable can be used to specify config file.

An example config file looks like this:

# A trigger is a group of Source, Filters, and Actions.
# You can add multiple triggers.
triggers:
  - source:
      type: resource-watcher
      properties:
        # We are interested in ConfigMap events.
        apiVersion: "v1"
        kind: ConfigMap
        namespace: default
        # Only watch update event.
        events:
          - update
    # 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.
        nameSelector:
          fromLabel: "watch-this"

Let's assume your config file is config.yaml, to run kube-trigger:

  • ./kube-trigger -c=config.yaml
  • CONFIG=config.yaml ./kube-trigger

In-Cluster

We have one CRD called TriggerService. TriggerInstance is what creates a kube-trigger instance (similar to running ./kube-trigger in-cluster but no config with config specified in its spec.

# You can find this file in config/samples/standard_v1alpha1_triggerservice.yaml
apiVersion: standard.oam.dev/v1alpha1
kind: TriggerService
metadata:
  name: kubetrigger-sample-config
  namespace: default
spec:
  selector:
    instance: kubetrigger-sample
  triggers:
    - source:
        type: resource-watcher
        properties:
          apiVersion: "v1"
          kind: ConfigMap
          namespace: default
          events:
            - update
      filter: |
        context: data: metadata: name: =~"this-will-trigger-update-.*"
      action:
        type: bump-application-revision
        properties:
          namespace: default
          # Select Applications to bump using labels.
          nameSelector:
            fromLabel: "watch-this"

Advanced kube-trigger Configuration

In addition to config files, you can also do advanced configurations. Advanced kube-trigger Configurations are internal configurations to fine-tune your kube-trigger instance. In most cases, you probably don't need to fiddle with these settings.

Log Level

Frequently-used values: debug, info, error

Default: info

CLI ENV KubeTrigger CRD
--log-level LOG_LEVEL TODO

Action Retry

Re-run Action if it fails.

Default: false

CLI ENV KubeTrigger CRD
--action-retry ACTION_RETRY TODO

Max Retry

Max retry count if an Action fails, valid only when action retrying is enabled.

Default: 5

CLI ENV KubeTrigger CRD
--max-retry MAX_RETRY .spec.workerConfig.maxRetry

Retry Delay

First delay to retry actions in seconds, subsequent delay will grow exponentially, valid only when action retrying is enabled.

Default: 2

CLI ENV KubeTrigger CRD
--retry-delay RETRY_DELAY .spec.workerConfig.retryDelay

Per-Worker QPS

Long-term QPS limiting per Action worker, this is shared between all watchers.

Default: 2

CLI ENV KubeTrigger CRD
--per-worker-qps PER_WORKER_QPS .spec.workerConfig.perWorkerQPS

Queue Size

Queue size for running actions, this is shared between all watchers

Default: 50

CLI ENV KubeTrigger CRD
--queue-size QUEUE_SIZE .spec.workerConfig.queueSize

Job Timeout

Timeout for running each action in seconds.

Default: 10

CLI ENV KubeTrigger CRD
--timeout TIMEOUT .spec.workerConfig.timeout

Worker Count

Number of workers for running actions, this is shared between all watchers.

Default: 4

CLI ENV KubeTrigger CRD
--workers WORKERS .spec.workerConfig.workerCount

Registry Size

Cache size for filters and actions.

Default: 100

CLI ENV KubeTrigger CRD
--registry-size REGISTRY_SIZE .spec.registrySize

Roadmap

v0.0.1-alpha.x

  • Basic build infrastructure
  • Complete a basic proof-of-concept sample
  • linters, license checker
  • GitHub Actions
  • Rate-limited worker
  • Make the configuration as CRD, launch new process/pod for new watcher
  • Notification for more than one app: selector from compose of Namespace; Labels; Name
  • Refine README, quick starts
  • Refactor CRD according to #2

v0.0.1-beta.x

Code enhancements

  • Add missing unit tests
  • Add missing integration tests

v0.0.x

User experience

  • Refine health status of CRs
  • Make it run as Addon, build component definition, and examples
  • Kubernetes dynamic admission control with validation webhook
  • Auto-generate usage docs of Sources, Filters, and Actions from CUE markers
  • Show available Sources, Filters, and Actions in cli

v0.1.x

Webhook support

  • Contribution Guide
  • New Action: webhook
  • New Source: webhook

v0.2.x

Observability

  • New Action: execute VelaQL(CUE and K8s operations)
  • New Source: cron
  • New Action: notifications(email, dingtalk, slack, telegram)
  • New Action: log (loki, clickhouse)

Planned for later releases

  • Allow user set custom RBAC for each TriggerInstance
  • New Action: workflow-run
  • New Action: execute-command
  • New Action: metric (prometheus)
  • Refine controller logic
  • Remove cache informer, make it with no catch but list watch events with unique queue.

kube-trigger's People

Contributors

charlie0129 avatar fogdong avatar leejanee avatar leil24 avatar mcduller avatar semmet95 avatar somefive avatar wonderflow avatar yangsoon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kube-trigger's Issues

User experience enhancements

  • Start a default TriggerInstance once controller is deployed, named default (optionally this feature can be disabled)
  • TriggerService without selectors automatically uses the default instance
  • change label app: xxx to instance: xxx

[Question] Execute kubectl command / create k8s resource

There is the following task - need to create a kubernetes job (or triger it from a cronjob) or execute a kubectl command at a certain Kubernetes event.

I couldn't find such functionality (v0.1.1).
Please tell me if such functionality is available now or these are plans for the future.
Where can I view all possible "actions"?

Thank you!

support collecting event of pods in vela application

  1. vela ql can get the pods from application, with resource topology featrues.
  2. kube-trigger could watch and filter events from pods.
  3. kube-trigger could send the event to loki/clickhouse or other storage for log.
  4. o11y addon can read that events.
  5. we can also add vector addon for transform logs.

Real scenario: kubevela/kubevela#1365

make kind name "KubeTriggerConfig" to be "TriggerService"

Let's refactor the schema to be:

triggers:
  -   k8s-resource-watcher:
       apiVersion: v1
       kind: ConfigMap
       namespace: default
       # Only watch update event.
       events:
         - update
    filters:
      # Filter the events above.
      - cue-validator:
          template: |
            metadata: name: =~"this-will-trigger-update-.*"
    actions:
      - bump-application-revision:
          namespace: default
          labelSelectors:
            my-label: my-value

It can be more convenient as trigger config.

Add api group to clusterrole's first rule in config/manager

Describe the bug
Getting the error

The ClusterRole "kube-trigger-manager-role" is invalid: rules[0].apiGroups: Required value: resource rules must supply at least one api group

when applying config/manager/role.yaml file.

To Reproduce
Steps to reproduce the behavior:

  1. Follow the Quick Start guide.
  2. Apply all the files in config/crd, config/definition, and config/manager respectively.
  3. kubectl apply -f will throw the error when trying to apply role.yaml.

Expected behavior
Successful creation of ClusterRole kube-trigger-manager-role

Kube Trigger Version

  • kube-trigger version: 1.0.0 (kube-trigger addon)

Cluster information

  • Minikube version: v1.30.1
  • Kuberentes version: v1.24.13
  • Kubevela version: v1.8.1

Additional context
I enabled the kube-trigger addon after installing kubevela .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.