|
| 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 | + "strings" |
| 26 | + "time" |
| 27 | + |
| 28 | + "google.golang.org/grpc" |
| 29 | + |
| 30 | + "k8s.io/client-go/kubernetes" |
| 31 | + "k8s.io/client-go/kubernetes/scheme" |
| 32 | + "k8s.io/client-go/rest" |
| 33 | + "k8s.io/client-go/tools/clientcmd" |
| 34 | + "k8s.io/klog" |
| 35 | + |
| 36 | + "github.com/container-storage-interface/spec/lib/go/csi" |
| 37 | + "github.com/kubernetes-csi/csi-lib-utils/leaderelection" |
| 38 | + csirpc "github.com/kubernetes-csi/csi-lib-utils/rpc" |
| 39 | + controller "github.com/kubernetes-csi/external-snapshotter/pkg/common_controller" |
| 40 | + |
| 41 | + clientset "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned" |
| 42 | + snapshotscheme "github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned/scheme" |
| 43 | + informers "github.com/kubernetes-csi/external-snapshotter/pkg/client/informers/externalversions" |
| 44 | + coreinformers "k8s.io/client-go/informers" |
| 45 | +) |
| 46 | + |
| 47 | +const ( |
| 48 | + // Number of worker threads |
| 49 | + threads = 10 |
| 50 | +) |
| 51 | + |
| 52 | +// Command line flags |
| 53 | +var ( |
| 54 | + kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Required only when running out of cluster.") |
| 55 | + resyncPeriod = flag.Duration("resync-period", 60*time.Second, "Resync interval of the controller.") |
| 56 | + showVersion = flag.Bool("version", false, "Show version.") |
| 57 | + |
| 58 | + leaderElection = flag.Bool("leader-election", false, "Enables leader election.") |
| 59 | + leaderElectionNamespace = flag.String("leader-election-namespace", "", "The namespace where the leader election resource exists. Defaults to the pod namespace if not set.") |
| 60 | +) |
| 61 | + |
| 62 | +var ( |
| 63 | + version = "unknown" |
| 64 | + prefix = "external-snapshotter-leader" |
| 65 | +) |
| 66 | + |
| 67 | +func main() { |
| 68 | + klog.InitFlags(nil) |
| 69 | + flag.Set("logtostderr", "true") |
| 70 | + flag.Parse() |
| 71 | + |
| 72 | + if *showVersion { |
| 73 | + fmt.Println(os.Args[0], version) |
| 74 | + os.Exit(0) |
| 75 | + } |
| 76 | + klog.Infof("Version: %s", version) |
| 77 | + |
| 78 | + // Create the client config. Use kubeconfig if given, otherwise assume in-cluster. |
| 79 | + config, err := buildConfig(*kubeconfig) |
| 80 | + if err != nil { |
| 81 | + klog.Error(err.Error()) |
| 82 | + os.Exit(1) |
| 83 | + } |
| 84 | + |
| 85 | + kubeClient, err := kubernetes.NewForConfig(config) |
| 86 | + if err != nil { |
| 87 | + klog.Error(err.Error()) |
| 88 | + os.Exit(1) |
| 89 | + } |
| 90 | + |
| 91 | + snapClient, err := clientset.NewForConfig(config) |
| 92 | + if err != nil { |
| 93 | + klog.Errorf("Error building snapshot clientset: %s", err.Error()) |
| 94 | + os.Exit(1) |
| 95 | + } |
| 96 | + |
| 97 | + factory := informers.NewSharedInformerFactory(snapClient, *resyncPeriod) |
| 98 | + coreFactory := coreinformers.NewSharedInformerFactory(kubeClient, *resyncPeriod) |
| 99 | + |
| 100 | + // Add Snapshot types to the defualt Kubernetes so events can be logged for them |
| 101 | + snapshotscheme.AddToScheme(scheme.Scheme) |
| 102 | + |
| 103 | + klog.V(2).Infof("Start NewCSISnapshotController with kubeconfig [%s] resyncPeriod [%+v]", *kubeconfig, *resyncPeriod) |
| 104 | + |
| 105 | + ctrl := controller.NewCSISnapshotCommonController( |
| 106 | + snapClient, |
| 107 | + kubeClient, |
| 108 | + factory.Snapshot().V1beta1().VolumeSnapshots(), |
| 109 | + factory.Snapshot().V1beta1().VolumeSnapshotContents(), |
| 110 | + factory.Snapshot().V1beta1().VolumeSnapshotClasses(), |
| 111 | + coreFactory.Core().V1().PersistentVolumeClaims(), |
| 112 | + *resyncPeriod, |
| 113 | + ) |
| 114 | + |
| 115 | + run := func(context.Context) { |
| 116 | + // run... |
| 117 | + stopCh := make(chan struct{}) |
| 118 | + factory.Start(stopCh) |
| 119 | + coreFactory.Start(stopCh) |
| 120 | + go ctrl.Run(threads, stopCh) |
| 121 | + |
| 122 | + // ...until SIGINT |
| 123 | + c := make(chan os.Signal, 1) |
| 124 | + signal.Notify(c, os.Interrupt) |
| 125 | + <-c |
| 126 | + close(stopCh) |
| 127 | + } |
| 128 | + |
| 129 | + if !*leaderElection { |
| 130 | + run(context.TODO()) |
| 131 | + } else { |
| 132 | + lockName := fmt.Sprintf("%s-%s", prefix, strings.Replace("common-snapshotter", "/", "-", -1)) |
| 133 | + le := leaderelection.NewLeaderElection(kubeClient, lockName, run) |
| 134 | + if *leaderElectionNamespace != "" { |
| 135 | + le.WithNamespace(*leaderElectionNamespace) |
| 136 | + } |
| 137 | + if err := le.Run(); err != nil { |
| 138 | + klog.Fatalf("failed to initialize leader election: %v", err) |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func buildConfig(kubeconfig string) (*rest.Config, error) { |
| 144 | + if kubeconfig != "" { |
| 145 | + return clientcmd.BuildConfigFromFlags("", kubeconfig) |
| 146 | + } |
| 147 | + return rest.InClusterConfig() |
| 148 | +} |
| 149 | + |
| 150 | +func supportsControllerCreateSnapshot(ctx context.Context, conn *grpc.ClientConn) (bool, error) { |
| 151 | + capabilities, err := csirpc.GetControllerCapabilities(ctx, conn) |
| 152 | + if err != nil { |
| 153 | + return false, err |
| 154 | + } |
| 155 | + |
| 156 | + return capabilities[csi.ControllerServiceCapability_RPC_CREATE_DELETE_SNAPSHOT], nil |
| 157 | +} |
0 commit comments