Skip to content

Commit 854cf0c

Browse files
Merge pull request #6 from Vicente-Cheng/add-webhook
Add StorageClass Validator
2 parents f14e909 + 403b75b commit 854cf0c

File tree

2,027 files changed

+206631
-46078
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,027 files changed

+206631
-46078
lines changed

.github/workflows/factory.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ env:
1212
repo: "rancher"
1313
provisionerImageName: "harvester-lvm-provisioner"
1414
pluginImageName: "harvester-lvm-csi-plugin"
15+
webhookImageName: "harvester-lvm-csi-driver-webhook"
1516

1617
jobs:
1718
dapper-build:
@@ -65,3 +66,13 @@ jobs:
6566
file: package/Dockerfile.provisioner
6667
push: ${{ inputs.push }}
6768
tags: ${{ env.repo }}/${{ env.provisionerImageName }}:${{ inputs.tag }}
69+
70+
- name: Docker Build (LVM Webhook)
71+
uses: docker/build-push-action@v5
72+
with:
73+
provenance: false
74+
context: .
75+
platforms: linux/amd64,linux/arm64
76+
file: package/Dockerfile.webhook
77+
push: ${{ inputs.push }}
78+
tags: ${{ env.repo }}/${{ env.webhookImageName }}:${{ inputs.tag }}

cmd/webhook/main.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"os"
6+
7+
"github.com/harvester/webhook/pkg/config"
8+
"github.com/harvester/webhook/pkg/server"
9+
"github.com/harvester/webhook/pkg/server/admission"
10+
"github.com/rancher/wrangler/pkg/generated/controllers/core"
11+
ctlstorage "github.com/rancher/wrangler/v3/pkg/generated/controllers/storage"
12+
"github.com/rancher/wrangler/v3/pkg/kubeconfig"
13+
"github.com/rancher/wrangler/v3/pkg/signals"
14+
"github.com/sirupsen/logrus"
15+
"github.com/urfave/cli/v2"
16+
"k8s.io/client-go/rest"
17+
18+
"github.com/harvester/csi-driver-lvm/pkg/webhook/storageclass"
19+
)
20+
21+
const webhookName = "harvester-csi-driver-lvm-webhook"
22+
23+
func main() {
24+
var options config.Options
25+
var logLevel string
26+
27+
flags := []cli.Flag{
28+
&cli.StringFlag{
29+
Name: "loglevel",
30+
Usage: "Specify log level",
31+
EnvVars: []string{"LOGLEVEL"},
32+
Value: "info",
33+
Destination: &logLevel,
34+
},
35+
&cli.IntFlag{
36+
Name: "threadiness",
37+
EnvVars: []string{"THREADINESS"},
38+
Usage: "Specify controller threads",
39+
Value: 5,
40+
Destination: &options.Threadiness,
41+
},
42+
&cli.IntFlag{
43+
Name: "https-port",
44+
EnvVars: []string{"WEBHOOK_SERVER_HTTPS_PORT"},
45+
Usage: "HTTPS listen port",
46+
Value: 8443,
47+
Destination: &options.HTTPSListenPort,
48+
},
49+
&cli.StringFlag{
50+
Name: "namespace",
51+
EnvVars: []string{"NAMESPACE"},
52+
Destination: &options.Namespace,
53+
Usage: "The harvester namespace",
54+
Value: "harvester-system",
55+
Required: true,
56+
},
57+
&cli.StringFlag{
58+
Name: "controller-user",
59+
EnvVars: []string{"CONTROLLER_USER_NAME"},
60+
Destination: &options.ControllerUsername,
61+
Value: "harvester-csi-driver-lvm-webhook",
62+
Usage: "The harvester controller username",
63+
},
64+
&cli.StringFlag{
65+
Name: "gc-user",
66+
EnvVars: []string{"GARBAGE_COLLECTION_USER_NAME"},
67+
Destination: &options.GarbageCollectionUsername,
68+
Usage: "The system username that performs garbage collection",
69+
Value: "system:serviceaccount:kube-system:generic-garbage-collector",
70+
},
71+
}
72+
73+
cfg, err := kubeconfig.GetNonInteractiveClientConfig(os.Getenv("KUBECONFIG")).ClientConfig()
74+
if err != nil {
75+
logrus.Fatal(err)
76+
}
77+
78+
ctx := signals.SetupSignalContext()
79+
80+
app := cli.NewApp()
81+
app.Flags = flags
82+
app.Action = func(_ *cli.Context) error {
83+
setLogLevel(logLevel)
84+
err := runWebhookServer(ctx, cfg, &options)
85+
return err
86+
}
87+
88+
if err := app.Run(os.Args); err != nil {
89+
logrus.Fatalf("run webhook server failed: %v", err)
90+
}
91+
}
92+
93+
func runWebhookServer(ctx context.Context, cfg *rest.Config, options *config.Options) error {
94+
storageFactory, err := ctlstorage.NewFactoryFromConfig(cfg)
95+
if err != nil {
96+
return err
97+
}
98+
coreFactory, err := core.NewFactoryFromConfig(cfg)
99+
if err != nil {
100+
return err
101+
}
102+
nodeClient := coreFactory.Core().V1().Node()
103+
storageclassClient := storageFactory.Storage().V1().StorageClass()
104+
webhookServer := server.NewWebhookServer(ctx, cfg, webhookName, options)
105+
106+
storageclassValidator := storageclass.NewStorageClassValidator(storageclassClient, nodeClient)
107+
108+
var validators = []admission.Validator{
109+
storageclassValidator,
110+
}
111+
112+
if err := webhookServer.RegisterValidators(validators...); err != nil {
113+
return err
114+
}
115+
116+
if err := webhookServer.Start(); err != nil {
117+
return err
118+
}
119+
120+
<-ctx.Done()
121+
122+
return nil
123+
}
124+
125+
func setLogLevel(level string) {
126+
ll, err := logrus.ParseLevel(level)
127+
if err != nil {
128+
ll = logrus.DebugLevel
129+
}
130+
// set global log level
131+
logrus.SetLevel(ll)
132+
}

deploy/charts/templates/_helpers.tpl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,83 @@
1+
{{/*
2+
Expand the name of the chart.
3+
*/}}
4+
{{- define "harvester-csi-driver-lvm.name" -}}
5+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
6+
{{- end }}
7+
8+
{{/*
9+
Create chart name and version as used by the chart label.
10+
*/}}
11+
{{- define "harvester-csi-driver-lvm.chart" -}}
12+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
13+
{{- end }}
14+
15+
{{/*
16+
CSI-plugin labels
17+
*/}}
18+
{{- define "harvester-csi-driver-lvm.labels" -}}
19+
helm.sh/chart: {{ include "harvester-csi-driver-lvm.chart" . }}
20+
{{ include "harvester-csi-driver-lvm.selectorLabels" . }}
21+
{{- if .Chart.AppVersion }}
22+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
23+
{{- end }}
24+
app.kubernetes.io/managed-by: {{ .Release.Service }}
25+
app.kubernetes.io/component: storage
26+
{{- end }}
27+
28+
{{/*
29+
CSI-plugin Selector labels
30+
*/}}
31+
{{- define "harvester-csi-driver-lvm.selectorLabels" -}}
32+
app.kubernetes.io/name: {{ include "harvester-csi-driver-lvm.name" . }}
33+
app.kubernetes.io/instance: {{ .Release.Name }}
34+
{{- end }}
35+
36+
{{/*
37+
CSI-controller labels
38+
*/}}
39+
{{- define "harvester-csi-driver-lvm-controller.labels" -}}
40+
helm.sh/chart: {{ include "harvester-csi-driver-lvm.chart" . }}
41+
{{ include "harvester-csi-driver-lvm-controller.selectorLabels" . }}
42+
{{- if .Chart.AppVersion }}
43+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
44+
{{- end }}
45+
app.kubernetes.io/managed-by: {{ .Release.Service }}
46+
app.kubernetes.io/component: storage
47+
{{- end }}
48+
49+
{{/*
50+
CSI-controller Selector labels
51+
*/}}
52+
{{- define "harvester-csi-driver-lvm-controller.selectorLabels" -}}
53+
app.kubernetes.io/name: {{ include "harvester-csi-driver-lvm.name" . }}-controller
54+
app.kubernetes.io/instance: {{ .Release.Name }}
55+
{{- end }}
56+
57+
{{/*
58+
CSI-webhook labels
59+
*/}}
60+
{{- define "harvester-csi-driver-lvm-webhook.labels" -}}
61+
helm.sh/chart: {{ include "harvester-csi-driver-lvm.chart" . }}
62+
{{ include "harvester-csi-driver-lvm-webhook.selectorLabels" . }}
63+
{{- if .Chart.AppVersion }}
64+
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
65+
{{- end }}
66+
app.kubernetes.io/managed-by: {{ .Release.Service }}
67+
app.kubernetes.io/component: webhook
68+
{{- end }}
69+
70+
{{/*
71+
CSI-webhook Selector labels
72+
*/}}
73+
{{- define "harvester-csi-driver-lvm-webhook.selectorLabels" -}}
74+
app.kubernetes.io/name: {{ include "harvester-csi-driver-lvm.name" . }}-webhook
75+
app.kubernetes.io/instance: {{ .Release.Name }}
76+
{{- end }}
77+
78+
{{/*
79+
CSI components
80+
*/}}
181
{{- define "externalImages.csiAttacher" -}}
282
{{- if .Values.customCSISidecars.enabled -}}
383
{{- print .Values.customCSISidecars.attacher -}}

deploy/charts/templates/controller.yaml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@ apiVersion: apps/v1
44
metadata:
55
name: harvester-csi-driver-lvm-controller
66
labels:
7-
heritage: {{ .Release.Service }}
8-
release: {{ .Release.Name }}
7+
{{- include "harvester-csi-driver-lvm-controller.labels" . | nindent 4 }}
98
spec:
109
serviceName: harvester-csi-driver-lvm-controller
1110
replicas: 1
1211
selector:
1312
matchLabels:
14-
app: harvester-csi-driver-lvm-controller
13+
{{- include "harvester-csi-driver-lvm-controller.selectorLabels" . | nindent 6 }}
1514
template:
1615
metadata:
1716
labels:
18-
app: harvester-csi-driver-lvm-controller
17+
{{- include "harvester-csi-driver-lvm-controller.labels" . | nindent 8 }}
1918
spec:
2019
affinity:
2120
podAffinity:
2221
requiredDuringSchedulingIgnoredDuringExecution:
2322
- labelSelector:
2423
matchExpressions:
25-
- key: app
24+
- key: app.kubernetes.io/name
2625
operator: In
2726
values:
28-
- harvester-csi-driver-lvm-plugin
27+
- harvester-csi-driver-lvm
2928
topologyKey: kubernetes.io/hostname
3029
{{- if .Values.nodeSelector.provisioner }}
3130
nodeSelector:

deploy/charts/templates/csi.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ kind: DaemonSet
44
metadata:
55
name: harvester-csi-driver-lvm-plugin
66
labels:
7-
heritage: {{ .Release.Service }}
8-
release: {{ .Release.Name }}
7+
{{- include "harvester-csi-driver-lvm.labels" . | nindent 4 }}
98
spec:
109
revisionHistoryLimit: 10
1110
selector:
1211
matchLabels:
13-
app: harvester-csi-driver-lvm-plugin
12+
{{- include "harvester-csi-driver-lvm.selectorLabels" . | nindent 6 }}
1413
template:
1514
metadata:
1615
labels:
17-
app: harvester-csi-driver-lvm-plugin
16+
{{- include "harvester-csi-driver-lvm.labels" . | nindent 8 }}
1817
spec:
1918
serviceAccountName: harvester-csi-driver-lvm
2019
{{- if .Values.tolerations.plugin }}

deploy/charts/templates/rbac.yaml

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,46 @@ roleRef:
4949
kind: ClusterRole
5050
name: harvester-csi-driver-lvm
5151
apiGroup: rbac.authorization.k8s.io
52-
---
52+
---
53+
apiVersion: v1
54+
kind: ServiceAccount
55+
metadata:
56+
name: harvester-csi-driver-lvm-webhook
57+
namespace: {{ .Release.Namespace }}
58+
---
59+
apiVersion: rbac.authorization.k8s.io/v1
60+
kind: ClusterRole
61+
metadata:
62+
name: harvester-csi-driver-lvm-webhook
63+
rules:
64+
- apiGroups: [ "" ]
65+
resources: [ "secrets", "configmaps" ]
66+
verbs: [ "*" ]
67+
- apiGroups: [ "" ]
68+
resources: [ "nodes" ]
69+
verbs: [ "get", "list" ]
70+
- apiGroups: [ "storage.k8s.io" ]
71+
resources: [ "storageclasses" ]
72+
verbs: [ "*" ]
73+
- apiGroups: [ "apiregistration.k8s.io" ]
74+
resources: [ "apiservices" ]
75+
verbs: [ "get", "watch", "list" ]
76+
- apiGroups: [ "apiextensions.k8s.io" ]
77+
resources: [ "customresourcedefinitions" ]
78+
verbs: [ "get", "watch", "list" ]
79+
- apiGroups: [ "admissionregistration.k8s.io" ]
80+
resources: [ "validatingwebhookconfigurations", "mutatingwebhookconfigurations" ]
81+
verbs: [ "*" ]
82+
---
83+
apiVersion: rbac.authorization.k8s.io/v1
84+
kind: ClusterRoleBinding
85+
metadata:
86+
name: harvester-csi-driver-lvm-webhook
87+
roleRef:
88+
apiGroup: rbac.authorization.k8s.io
89+
kind: ClusterRole
90+
name: harvester-csi-driver-lvm-webhook
91+
subjects:
92+
- kind: ServiceAccount
93+
name: harvester-csi-driver-lvm-webhook
94+
namespace: {{ .Release.Namespace }}

0 commit comments

Comments
 (0)