Skip to content

Commit fa239cb

Browse files
authored
Merge pull request #182 from xing-yang/new_beta_split_controller
Split snapshot controller using beta APIs
2 parents 89889f0 + 9000e9c commit fa239cb

31 files changed

+2857
-1866
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
.PHONY: all csi-snapshotter clean test
15+
.PHONY: all snapshot-controller csi-snapshotter clean test
1616

17-
CMDS=csi-snapshotter
17+
CMDS=snapshot-controller csi-snapshotter
1818
all: build
1919
include release-tools/build.make
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM gcr.io/distroless/static:latest
22
LABEL maintainers="Kubernetes Authors"
3-
LABEL description="CSI External Snapshotter"
3+
LABEL description="CSI External Snapshotter Sidecar"
44

55
COPY ./bin/csi-snapshotter csi-snapshotter
66
ENTRYPOINT ["/csi-snapshotter"]

cmd/csi-snapshotter/main.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import (
3737
"github.com/kubernetes-csi/csi-lib-utils/connection"
3838
"github.com/kubernetes-csi/csi-lib-utils/leaderelection"
3939
csirpc "github.com/kubernetes-csi/csi-lib-utils/rpc"
40-
"github.com/kubernetes-csi/external-snapshotter/pkg/controller"
40+
controller "github.com/kubernetes-csi/external-snapshotter/pkg/sidecar-controller"
4141
"github.com/kubernetes-csi/external-snapshotter/pkg/snapshotter"
4242

4343
clientset "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned"
@@ -56,17 +56,13 @@ const (
5656

5757
// Command line flags
5858
var (
59-
snapshotterName = flag.String("snapshotter", "", "This option is deprecated.")
60-
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
61-
connectionTimeout = flag.Duration("connection-timeout", 0, "The --connection-timeout flag is deprecated")
62-
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
63-
createSnapshotContentRetryCount = flag.Int("create-snapshotcontent-retrycount", 5, "Number of retries when we create a snapshot content object for a snapshot.")
64-
createSnapshotContentInterval = flag.Duration("create-snapshotcontent-interval", 10*time.Second, "Interval between retries when we create a snapshot content object for a snapshot.")
65-
resyncPeriod = flag.Duration("resync-period", 60*time.Second, "Resync interval of the controller.")
66-
snapshotNamePrefix = flag.String("snapshot-name-prefix", "snapshot", "Prefix to apply to the name of a created snapshot")
67-
snapshotNameUUIDLength = flag.Int("snapshot-name-uuid-length", -1, "Length in characters for the generated uuid of a created snapshot. Defaults behavior is to NOT truncate.")
68-
showVersion = flag.Bool("version", false, "Show version.")
69-
csiTimeout = flag.Duration("timeout", defaultCSITimeout, "The timeout for any RPCs to the CSI driver. Default is 1 minute.")
59+
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
60+
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
61+
resyncPeriod = flag.Duration("resync-period", 60*time.Second, "Resync interval of the controller.")
62+
snapshotNamePrefix = flag.String("snapshot-name-prefix", "snapshot", "Prefix to apply to the name of a created snapshot")
63+
snapshotNameUUIDLength = flag.Int("snapshot-name-uuid-length", -1, "Length in characters for the generated uuid of a created snapshot. Defaults behavior is to NOT truncate.")
64+
showVersion = flag.Bool("version", false, "Show version.")
65+
csiTimeout = flag.Duration("timeout", defaultCSITimeout, "The timeout for any RPCs to the CSI driver. Default is 1 minute.")
7066

7167
leaderElection = flag.Bool("leader-election", false, "Enables leader election.")
7268
leaderElectionNamespace = flag.String("leader-election-namespace", "", "The namespace where the leader election resource exists. Defaults to the pod namespace if not set.")
@@ -88,14 +84,6 @@ func main() {
8884
}
8985
klog.Infof("Version: %s", version)
9086

91-
if *connectionTimeout != 0 {
92-
klog.Warning("--connection-timeout is deprecated and will have no effect")
93-
}
94-
95-
if *snapshotterName != "" {
96-
klog.Warning("--snapshotter is deprecated and will have no effect")
97-
}
98-
9987
// Create the client config. Use kubeconfig if given, otherwise assume in-cluster.
10088
config, err := buildConfig(*kubeconfig)
10189
if err != nil {
@@ -133,13 +121,13 @@ func main() {
133121
defer cancel()
134122

135123
// Find driver name
136-
*snapshotterName, err = csirpc.GetDriverName(ctx, csiConn)
124+
driverName, err := csirpc.GetDriverName(ctx, csiConn)
137125
if err != nil {
138126
klog.Errorf("error getting CSI driver name: %v", err)
139127
os.Exit(1)
140128
}
141129

142-
klog.V(2).Infof("CSI driver name: %q", *snapshotterName)
130+
klog.V(2).Infof("CSI driver name: %q", driverName)
143131

144132
// Check it's ready
145133
if err = csirpc.ProbeForever(csiConn, *csiTimeout); err != nil {
@@ -154,7 +142,7 @@ func main() {
154142
os.Exit(1)
155143
}
156144
if !supportsCreateSnapshot {
157-
klog.Errorf("CSI driver %s does not support ControllerCreateSnapshot", *snapshotterName)
145+
klog.Errorf("CSI driver %s does not support ControllerCreateSnapshot", driverName)
158146
os.Exit(1)
159147
}
160148

@@ -163,19 +151,15 @@ func main() {
163151
os.Exit(1)
164152
}
165153

166-
klog.V(2).Infof("Start NewCSISnapshotController with snapshotter [%s] kubeconfig [%s] csiTimeout [%+v] csiAddress [%s] createSnapshotContentRetryCount [%d] createSnapshotContentInterval [%+v] resyncPeriod [%+v] snapshotNamePrefix [%s] snapshotNameUUIDLength [%d]", *snapshotterName, *kubeconfig, *csiTimeout, *csiAddress, createSnapshotContentRetryCount, *createSnapshotContentInterval, *resyncPeriod, *snapshotNamePrefix, snapshotNameUUIDLength)
154+
klog.V(2).Infof("Start NewCSISnapshotSideCarController with snapshotter [%s] kubeconfig [%s] csiTimeout [%+v] csiAddress [%s] resyncPeriod [%+v] snapshotNamePrefix [%s] snapshotNameUUIDLength [%d]", driverName, *kubeconfig, *csiTimeout, *csiAddress, *resyncPeriod, *snapshotNamePrefix, snapshotNameUUIDLength)
167155

168156
snapShotter := snapshotter.NewSnapshotter(csiConn)
169-
ctrl := controller.NewCSISnapshotController(
157+
ctrl := controller.NewCSISnapshotSideCarController(
170158
snapClient,
171159
kubeClient,
172-
*snapshotterName,
173-
factory.Snapshot().V1beta1().VolumeSnapshots(),
160+
driverName,
174161
factory.Snapshot().V1beta1().VolumeSnapshotContents(),
175162
factory.Snapshot().V1beta1().VolumeSnapshotClasses(),
176-
coreFactory.Core().V1().PersistentVolumeClaims(),
177-
*createSnapshotContentRetryCount,
178-
*createSnapshotContentInterval,
179163
snapShotter,
180164
*csiTimeout,
181165
*resyncPeriod,
@@ -200,7 +184,7 @@ func main() {
200184
if !*leaderElection {
201185
run(context.TODO())
202186
} else {
203-
lockName := fmt.Sprintf("%s-%s", prefix, strings.Replace(*snapshotterName, "/", "-", -1))
187+
lockName := fmt.Sprintf("%s-%s", prefix, strings.Replace(driverName, "/", "-", -1))
204188
le := leaderelection.NewLeaderElection(kubeClient, lockName, run)
205189
if *leaderElectionNamespace != "" {
206190
le.WithNamespace(*leaderElectionNamespace)

cmd/snapshot-controller/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM gcr.io/distroless/static:latest
2+
LABEL maintainers="Kubernetes Authors"
3+
LABEL description="Snapshot Controller"
4+
5+
COPY ./bin/snapshot-controller snapshot-controller
6+
ENTRYPOINT ["/snapshot-controller"]

cmd/snapshot-controller/main.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"flag"
22+
"fmt"
23+
"os"
24+
"os/signal"
25+
"time"
26+
27+
"k8s.io/client-go/kubernetes"
28+
"k8s.io/client-go/kubernetes/scheme"
29+
"k8s.io/client-go/rest"
30+
"k8s.io/client-go/tools/clientcmd"
31+
"k8s.io/klog"
32+
33+
"github.com/kubernetes-csi/csi-lib-utils/leaderelection"
34+
controller "github.com/kubernetes-csi/external-snapshotter/pkg/common-controller"
35+
36+
clientset "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned"
37+
snapshotscheme "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned/scheme"
38+
informers "github.com/kubernetes-csi/external-snapshotter/pkg/client/informers/externalversions"
39+
coreinformers "k8s.io/client-go/informers"
40+
)
41+
42+
const (
43+
// Number of worker threads
44+
threads = 10
45+
)
46+
47+
// Command line flags
48+
var (
49+
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.")
50+
createSnapshotContentRetryCount = flag.Int("create-snapshotcontent-retrycount", 5, "Number of retries when we create a snapshot content object for a snapshot.")
51+
createSnapshotContentInterval = flag.Duration("create-snapshotcontent-interval", 10*time.Second, "Interval between retries when we create a snapshot content object for a snapshot.")
52+
resyncPeriod = flag.Duration("resync-period", 60*time.Second, "Resync interval of the controller.")
53+
showVersion = flag.Bool("version", false, "Show version.")
54+
55+
leaderElection = flag.Bool("leader-election", false, "Enables leader election.")
56+
leaderElectionNamespace = flag.String("leader-election-namespace", "", "The namespace where the leader election resource exists. Defaults to the pod namespace if not set.")
57+
)
58+
59+
var (
60+
version = "unknown"
61+
)
62+
63+
func main() {
64+
klog.InitFlags(nil)
65+
flag.Set("logtostderr", "true")
66+
flag.Parse()
67+
68+
if *showVersion {
69+
fmt.Println(os.Args[0], version)
70+
os.Exit(0)
71+
}
72+
klog.Infof("Version: %s", version)
73+
74+
// Create the client config. Use kubeconfig if given, otherwise assume in-cluster.
75+
config, err := buildConfig(*kubeconfig)
76+
if err != nil {
77+
klog.Error(err.Error())
78+
os.Exit(1)
79+
}
80+
81+
kubeClient, err := kubernetes.NewForConfig(config)
82+
if err != nil {
83+
klog.Error(err.Error())
84+
os.Exit(1)
85+
}
86+
87+
snapClient, err := clientset.NewForConfig(config)
88+
if err != nil {
89+
klog.Errorf("Error building snapshot clientset: %s", err.Error())
90+
os.Exit(1)
91+
}
92+
93+
factory := informers.NewSharedInformerFactory(snapClient, *resyncPeriod)
94+
coreFactory := coreinformers.NewSharedInformerFactory(kubeClient, *resyncPeriod)
95+
96+
// Add Snapshot types to the defualt Kubernetes so events can be logged for them
97+
snapshotscheme.AddToScheme(scheme.Scheme)
98+
99+
klog.V(2).Infof("Start NewCSISnapshotController with kubeconfig [%s] createSnapshotContentRetryCount [%d] createSnapshotContentInterval [%d] resyncPeriod [%+v]", *kubeconfig, *createSnapshotContentRetryCount, *createSnapshotContentInterval, *resyncPeriod)
100+
101+
ctrl := controller.NewCSISnapshotCommonController(
102+
snapClient,
103+
kubeClient,
104+
factory.Snapshot().V1beta1().VolumeSnapshots(),
105+
factory.Snapshot().V1beta1().VolumeSnapshotContents(),
106+
factory.Snapshot().V1beta1().VolumeSnapshotClasses(),
107+
coreFactory.Core().V1().PersistentVolumeClaims(),
108+
*createSnapshotContentRetryCount,
109+
*createSnapshotContentInterval,
110+
*resyncPeriod,
111+
)
112+
113+
run := func(context.Context) {
114+
// run...
115+
stopCh := make(chan struct{})
116+
factory.Start(stopCh)
117+
coreFactory.Start(stopCh)
118+
go ctrl.Run(threads, stopCh)
119+
120+
// ...until SIGINT
121+
c := make(chan os.Signal, 1)
122+
signal.Notify(c, os.Interrupt)
123+
<-c
124+
close(stopCh)
125+
}
126+
127+
if !*leaderElection {
128+
run(context.TODO())
129+
} else {
130+
lockName := "snapshot-controller-leader"
131+
le := leaderelection.NewLeaderElection(kubeClient, lockName, run)
132+
if *leaderElectionNamespace != "" {
133+
le.WithNamespace(*leaderElectionNamespace)
134+
}
135+
if err := le.Run(); err != nil {
136+
klog.Fatalf("failed to initialize leader election: %v", err)
137+
}
138+
}
139+
}
140+
141+
func buildConfig(kubeconfig string) (*rest.Config, error) {
142+
if kubeconfig != "" {
143+
return clientcmd.BuildConfigFromFlags("", kubeconfig)
144+
}
145+
return rest.InClusterConfig()
146+
}
File renamed without changes.

deploy/kubernetes/rbac.yaml renamed to deploy/kubernetes/csi-snapshotter/rbac-csi-snapshotter.yaml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,6 @@ metadata:
2020
# rename if there are conflicts
2121
name: external-snapshotter-runner
2222
rules:
23-
- apiGroups: [""]
24-
resources: ["persistentvolumes"]
25-
verbs: ["get", "list", "watch"]
26-
- apiGroups: [""]
27-
resources: ["persistentvolumeclaims"]
28-
verbs: ["get", "list", "watch", "update"]
29-
- apiGroups: ["storage.k8s.io"]
30-
resources: ["storageclasses"]
31-
verbs: ["get", "list", "watch"]
3223
- apiGroups: [""]
3324
resources: ["events"]
3425
verbs: ["list", "watch", "create", "update", "patch"]
@@ -48,15 +39,6 @@ rules:
4839
- apiGroups: ["snapshot.storage.k8s.io"]
4940
resources: ["volumesnapshotcontents/status"]
5041
verbs: ["update"]
51-
- apiGroups: ["snapshot.storage.k8s.io"]
52-
resources: ["volumesnapshots"]
53-
verbs: ["get", "list", "watch", "update"]
54-
- apiGroups: ["snapshot.storage.k8s.io"]
55-
resources: ["volumesnapshots/status"]
56-
verbs: ["update"]
57-
- apiGroups: ["apiextensions.k8s.io"]
58-
resources: ["customresourcedefinitions"]
59-
verbs: ["create", "list", "watch", "delete", "get", "update"]
6042

6143
---
6244
kind: ClusterRoleBinding

deploy/kubernetes/setup-csi-snapshotter.yaml renamed to deploy/kubernetes/csi-snapshotter/setup-csi-snapshotter.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This YAML file shows how to deploy the CSI snapshotter together
22
# with the hostpath CSI driver. It depends on the RBAC rules
3-
# from rbac.yaml and rbac-external-provisioner.yaml.
3+
# from rbac-csi-snapshotter.yaml and rbac-external-provisioner.yaml.
44
#
55
# Because external-snapshotter and external-provisioner get
66
# deployed in the same pod, we have to merge the permissions
@@ -72,11 +72,10 @@ spec:
7272
serviceAccount: csi-snapshotter
7373
containers:
7474
- name: csi-provisioner
75-
image: quay.io/k8scsi/csi-provisioner:v1.3.0
75+
image: quay.io/k8scsi/csi-provisioner:v1.5.0-rc1
7676
args:
77-
- "--provisioner=csi-hostpath"
77+
- "--v=5"
7878
- "--csi-address=$(ADDRESS)"
79-
- "--connection-timeout=15s"
8079
env:
8180
- name: ADDRESS
8281
value: /csi/csi.sock
@@ -85,20 +84,21 @@ spec:
8584
- name: socket-dir
8685
mountPath: /csi
8786
- name: csi-snapshotter
88-
image: quay.io/k8scsi/csi-snapshotter:v1.2.0
87+
# NOTE: replace with official image when released: quay.io/k8scsi/csi-snapshotter:v2.0.0
88+
image: csi-snapshotter:testbeta #quay.io/k8scsi/csi-snapshotter:testbeta
8989
args:
90+
- "--v=5"
9091
- "--csi-address=$(ADDRESS)"
91-
- "--connection-timeout=15s"
9292
- "--leader-election=false"
9393
env:
9494
- name: ADDRESS
9595
value: /csi/csi.sock
96-
imagePullPolicy: Always
96+
imagePullPolicy: IfNotPresent #Always
9797
volumeMounts:
9898
- name: socket-dir
9999
mountPath: /csi
100100
- name: hostpath
101-
image: quay.io/k8scsi/hostpathplugin:v1.1.0
101+
image: quay.io/k8scsi/hostpathplugin:v1.2.0
102102
args:
103103
- "--v=5"
104104
- "--endpoint=$(CSI_ENDPOINT)"

0 commit comments

Comments
 (0)