Skip to content

Commit

Permalink
Add kwok operator
Browse files Browse the repository at this point in the history
  • Loading branch information
wzshiming committed Jul 31, 2024
1 parent 6ec1124 commit 5a138ac
Show file tree
Hide file tree
Showing 60 changed files with 2,475 additions and 3 deletions.
31 changes: 29 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ KUBE_RELEASES ?= $(shell echo $(SUPPORTED_KUBE_RELEASES) | cut -d ' ' -f 1-$(NUM
# The latest kube release
LATEST_KUBE_RELEASE ?= $(shell echo $(SUPPORTED_KUBE_RELEASES) | cut -d ' ' -f 1)

BINARY ?= kwok kwokctl
BINARY ?= kwok kwokctl kwok-operator

IMAGE_PREFIX ?=

Expand All @@ -64,9 +64,11 @@ PRE_RELEASE ?=

ifeq ($(STAGING_IMAGE_PREFIX),)
KWOK_IMAGE ?= kwok
KWOK_OPERATOR_IMAGE ?= operator
CLUSTER_IMAGE ?= cluster
else
KWOK_IMAGE ?= $(STAGING_IMAGE_PREFIX)/kwok
KWOK_OPERATOR_IMAGE ?= $(STAGING_IMAGE_PREFIX)/kwok-operator
CLUSTER_IMAGE ?= $(STAGING_IMAGE_PREFIX)/cluster
endif

Expand All @@ -76,7 +78,7 @@ IMAGE_PLATFORMS ?= linux/amd64 linux/arm64

BINARY_PLATFORMS ?= linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64

MANIFESTS ?= kwok kwokctl stage/fast metrics/usage
MANIFESTS ?= kwok kwokctl operator stage/fast metrics/usage

BUILDER ?= docker
DOCKER_CLI_EXPERIMENTAL ?= enabled
Expand Down Expand Up @@ -187,6 +189,31 @@ cross-image:
--builder=${BUILDER} \
--push=${PUSH}

## operator-image: Build kwok operator image
.PHONY: operator-image
operator-image:
@./images/kwok-operator/build.sh \
$(addprefix --extra-tag=, $(EXTRA_TAGS)) \
--image=${KWOK_OPERATOR_IMAGE} \
--version=${VERSION} \
--staging-prefix=${STAGING_PREFIX} \
--dry-run=${DRY_RUN} \
--builder=${BUILDER} \
--push=${PUSH}

## cross-operator-image: Build kwok operator images for all supported platforms
.PHONY: cross-operator-image
cross-operator-image:
@./images/kwok-operator/build.sh \
$(addprefix --platform=, $(IMAGE_PLATFORMS)) \
$(addprefix --extra-tag=, $(EXTRA_TAGS)) \
--image=${KWOK_OPERATOR_IMAGE} \
--version=${VERSION} \
--staging-prefix=${STAGING_PREFIX} \
--dry-run=${DRY_RUN} \
--builder=${BUILDER} \
--push=${PUSH}

## cluster-image: Build cluster image
.PHONY: cluster-image
cluster-image:
Expand Down
1 change: 1 addition & 0 deletions charts/operator/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.helmignore
17 changes: 17 additions & 0 deletions charts/operator/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: v2
description: KWOK Operator
type: application
home: https://kwok.sigs.k8s.io
icon: https://github.com/kubernetes-sigs/kwok/raw/main/logo/kwok.png
keywords:
- kubernetes
- kwok
- operator
sources:
- https://github.com/kubernetes-sigs/kwok
name: operator
maintainers:
- name: wzshiming
email: [email protected]
appVersion: v0.6.0
version: 0.0.1
49 changes: 49 additions & 0 deletions charts/operator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# KWOK (Kubernetes WithOut Kubelet)

[KWOK](https://kwok.sigs.k8s.io/) - Simulates thousands of Nodes and Clusters.

## Installing the Chart

Before you can install the chart you will need to add the `kwok` repo to [Helm](https://helm.sh/).

```shell
helm repo add kwok https://kwok.sigs.k8s.io/charts/
```

After you've installed the repo you can install the chart.

```shell
helm upgrade --namespace kube-system --install kwok kwok/kwok
```

Set up default stage policy (required)
> NOTE: This configures the pod/node emulation behavior, if not it will do nothing.
```shell
helm upgrade --install kwok kwok/stage-fast
```

Set up default metrics usage policy (optional)

```shell
helm upgrade --install kwok kwok/metrics-usage
```

## Configuration

The following table lists the configurable parameters of the kwok chart and their default values.

| Key | Type | Default | Description |
|-----|------|---------|-------------|
| affinity | object | `{}` | |
| fullnameOverride | string | `"kwok-controller"` | Override the `fullname` of the chart. |
| image.pullPolicy | string | `"IfNotPresent"` | Image pull policy. |
| image.repository | string | `"registry.k8s.io/kwok/kwok"` | Image repository. |
| image.tag | string | `""` | Overrides the image tag whose default is {{ .Chart.AppVersion }}. |
| imagePullSecrets | list | `[]` | Image pull secrets. |
| nameOverride | string | `""` | Override the `name` of the chart. |
| nodeSelector | object | `{}` | |
| podSecurityContext | object | `{}` | |
| replicas | int | `1` | The replica count for Deployment. |
| resources | object | `{}` | |
| securityContext | object | `{}` | |
9 changes: 9 additions & 0 deletions charts/operator/README.md.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# {{ template "chart.description" . }}

{{ template "extra.usage" . }}

## Configuration

The following table lists the configurable parameters of the kwok chart and their default values.

{{ template "chart.valuesTable" . }}
1 change: 1 addition & 0 deletions charts/operator/crds
62 changes: 62 additions & 0 deletions charts/operator/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "kwok.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "kwok.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "kwok.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "kwok.labels" -}}
helm.sh/chart: {{ include "kwok.chart" . }}
{{ include "kwok.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "kwok.selectorLabels" -}}
app.kubernetes.io/name: {{ include "kwok.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "kwok.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "kwok.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
46 changes: 46 additions & 0 deletions charts/operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "kwok.fullname" . }}
labels:
{{- include "kwok.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicas }}
selector:
matchLabels:
{{- include "kwok.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "kwok.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "kwok.fullname" . }}
{{- with .Values.podSecurityContext }}
securityContext:
{{- toYaml . | nindent 8 }}
{{- end }}
restartPolicy: Always
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
securityContext:
{{- toYaml .Values.securityContext | nindent 10 }}
resources:
{{- toYaml .Values.resources | nindent 10 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{- toYaml . | nindent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{- toYaml . | nindent 8 }}
{{- end }}
33 changes: 33 additions & 0 deletions charts/operator/templates/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: kwok-operator
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- create
- delete
- get
- apiGroups:
- kwok.x-k8s.io
resources:
- controllers
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- kwok.x-k8s.io
resources:
- controllers/status
verbs:
- patch
- update
12 changes: 12 additions & 0 deletions charts/operator/templates/role_binding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kwok-operator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kwok-operator
subjects:
- kind: ServiceAccount
name: kwok-operator
namespace: {{ .Release.Namespace }}
5 changes: 5 additions & 0 deletions charts/operator/templates/service_account.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: kwok-operator
namespace: {{ .Release.Namespace }}
26 changes: 26 additions & 0 deletions charts/operator/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
image:
# -- Image pull policy.
pullPolicy: IfNotPresent
# -- Image repository.
repository: registry.k8s.io/kwok/operator
# -- Overrides the image tag whose default is {{ .Chart.AppVersion }}.
tag: ""

# -- Image pull secrets.
imagePullSecrets: []

# -- Override the `name` of the chart.
nameOverride: ""

# -- Override the `fullname` of the chart.
fullnameOverride: "kwok-controller"

podSecurityContext: {}
securityContext: {}

nodeSelector: {}
resources: {}
affinity: {}

# -- The replica count for Deployment.
replicas: 1
53 changes: 53 additions & 0 deletions cmd/kwok-operator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package main is the entry point for the kwok binary.
package main

import (
"os"

"github.com/spf13/pflag"

"sigs.k8s.io/kwok/pkg/config"
"sigs.k8s.io/kwok/pkg/log"
"sigs.k8s.io/kwok/pkg/operator/cmd"
"sigs.k8s.io/kwok/pkg/utils/signals"
)

func main() {
flagset := pflag.NewFlagSet("global", pflag.ContinueOnError)
flagset.ParseErrorsWhitelist.UnknownFlags = true
flagset.Usage = func() {}

ctx := signals.SetupSignalContext()
ctx, logger := log.InitFlags(ctx, flagset)

ctx, err := config.InitFlags(ctx, flagset)
if err != nil {
_, _ = os.Stderr.Write([]byte(flagset.FlagUsages()))
logger.Error("Init config flags", err)
os.Exit(1)
}

command := cmd.NewCommand(ctx)
command.PersistentFlags().AddFlagSet(flagset)
err = command.ExecuteContext(ctx)
if err != nil {
logger.Error("Execute exit", err)
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions hack/releases-helm-charts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ chart_dir="./charts"
index_dir="${ROOT_DIR}/site/static/charts"

package_and_index "${index_dir}" "${chart_dir}/kwok" "kwok-chart" || :
package_and_index "${index_dir}" "${chart_dir}/kwok-operator" "kwok-operator-chart" || :
package_and_index "${index_dir}" "${chart_dir}/stage-fast" "kwok-stage-fast-chart" || :
package_and_index "${index_dir}" "${chart_dir}/metrics-usage" "kwok-metrics-usage-chart" || :
Loading

0 comments on commit 5a138ac

Please sign in to comment.