Skip to content

Commit

Permalink
Added feature gate mechanism; added topology feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
verult committed Oct 16, 2018
1 parent 2715008 commit ea06ba5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
16 changes: 15 additions & 1 deletion cmd/csi-provisioner/csi-provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ limitations under the License.
package main

import (
"flag"
goflag "flag"
"fmt"
flag "github.com/spf13/pflag"
"math/rand"
"os"
"strconv"
Expand All @@ -37,6 +38,10 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"strings"

utilfeature "k8s.io/apiserver/pkg/util/feature"
utilflag "k8s.io/apiserver/pkg/util/flag"
)

var (
Expand All @@ -48,6 +53,7 @@ var (
volumeNamePrefix = flag.String("volume-name-prefix", "pvc", "Prefix to apply to the name of a created volume")
volumeNameUUIDLength = flag.Int("volume-name-uuid-length", -1, "Truncates generated UUID of a created volume to this length. Defaults behavior is to NOT truncate.")
showVersion = flag.Bool("version", false, "Show version.")
featureGates map[string]bool

provisionController *controller.ProvisionController
version = "unknown"
Expand All @@ -57,9 +63,17 @@ func init() {
var config *rest.Config
var err error

flag.Var(utilflag.NewMapStringBool(&featureGates), "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimental features. "+
"Options are:\n"+strings.Join(utilfeature.DefaultFeatureGate.KnownFeatures(), "\n"))

flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
flag.Parse()
flag.Set("logtostderr", "true")

if err := utilfeature.DefaultFeatureGate.SetFromMap(featureGates); err != nil {
glog.Fatal(err)
}

if *showVersion {
fmt.Println(os.Args[0], version)
os.Exit(0)
Expand Down
9 changes: 7 additions & 2 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ import (

"github.com/container-storage-interface/spec/lib/go/csi/v0"
csiclientset "k8s.io/csi-api/pkg/client/clientset/versioned"

"github.com/kubernetes-csi/external-provisioner/pkg/features"
utilfeature "k8s.io/apiserver/pkg/util/feature"
)

const (
Expand Down Expand Up @@ -411,7 +414,8 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
req.VolumeContentSource = volumeContentSource
}

if driverState.capabilities.Has(PluginCapability_ACCESSIBILITY_CONSTRAINTS) {
if driverState.capabilities.Has(PluginCapability_ACCESSIBILITY_CONSTRAINTS) &&
utilfeature.DefaultFeatureGate.Enabled(features.Topology) {
requirements, err := GenerateAccessibilityRequirements(
p.client,
p.csiAPIClient,
Expand Down Expand Up @@ -538,7 +542,8 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
},
}

if driverState.capabilities.Has(PluginCapability_ACCESSIBILITY_CONSTRAINTS) {
if driverState.capabilities.Has(PluginCapability_ACCESSIBILITY_CONSTRAINTS) &&
utilfeature.DefaultFeatureGate.Enabled(features.Topology) {
pv.Spec.NodeAffinity = GenerateVolumeNodeAffinity(rep.Volume.AccessibleTopology)
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/golang/mock/gomock"
"github.com/kubernetes-csi/csi-test/driver"
"github.com/kubernetes-csi/external-provisioner/pkg/features"
crdv1 "github.com/kubernetes-csi/external-snapshotter/pkg/apis/volumesnapshot/v1alpha1"
"github.com/kubernetes-csi/external-snapshotter/pkg/client/clientset/versioned/fake"
"github.com/kubernetes-incubator/external-storage/lib/controller"
Expand All @@ -38,6 +39,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilfeature "k8s.io/apiserver/pkg/util/feature"
utilfeaturetesting "k8s.io/apiserver/pkg/util/feature/testing"
"k8s.io/client-go/kubernetes"
fakeclientset "k8s.io/client-go/kubernetes/fake"
k8stesting "k8s.io/client-go/testing"
Expand Down Expand Up @@ -1502,6 +1505,8 @@ func TestProvisionFromSnapshot(t *testing.T) {

// TestProvisionWithTopology is a basic test of provisioner integration with topology functions.
func TestProvisionWithTopology(t *testing.T) {
defer utilfeaturetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.Topology, true)()

accessibleTopology := []*csi.Topology{
{
Segments: map[string]string{
Expand Down
35 changes: 35 additions & 0 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2018 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 features

import utilfeature "k8s.io/apiserver/pkg/util/feature"

const (
// owner: @verult
// alpha: v0.4
Topology utilfeature.Feature = "Topology"
)

func init() {
utilfeature.DefaultFeatureGate.Add(defaultKubernetesFeatureGates)
}

// defaultKubernetesFeatureGates consists of all known feature keys specific to external-provisioner.
// To add a new feature, define a key for it above and add it here.
var defaultKubernetesFeatureGates = map[utilfeature.Feature]utilfeature.FeatureSpec{
Topology: {Default: false, PreRelease: utilfeature.Alpha},
}

0 comments on commit ea06ba5

Please sign in to comment.