Skip to content

Commit

Permalink
Merge pull request #1465 from DualSpark/k-c-m-attachedetachflag
Browse files Browse the repository at this point in the history
Updates for new k-c-m flag
  • Loading branch information
justinsb authored Jan 19, 2017
2 parents d569b04 + 2f86c3a commit a77c1ed
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/apis/kops/componentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package kops

import metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"

type KubeletConfigSpec struct {
APIServers string `json:"apiServers,omitempty" flag:"api-servers"`

Expand Down Expand Up @@ -533,6 +535,10 @@ type KubeControllerManagerConfig struct {
//// corresponding flag of the kube-apiserver. WARNING: the generic garbage
//// collector is an alpha feature.
//EnableGarbageCollector bool `json:"enableGarbageCollector"`

// ReconcilerSyncLoopPeriod is the amount of time the reconciler sync states loop
// wait between successive executions. Is set to 1 min by kops by default
AttachDetachReconcileSyncPeriod *metav1.Duration `json:"attachDetachReconcileSyncPeriod,omitempty" flag:"attach-detach-reconcile-sync-period"`
}

type KubeSchedulerConfig struct {
Expand Down
6 changes: 6 additions & 0 deletions pkg/apis/kops/v1alpha1/componentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package v1alpha1

import metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"

type KubeletConfigSpec struct {
APIServers string `json:"apiServers,omitempty" flag:"api-servers"`

Expand Down Expand Up @@ -530,6 +532,10 @@ type KubeControllerManagerConfig struct {
//// corresponding flag of the kube-apiserver. WARNING: the generic garbage
//// collector is an alpha feature.
//EnableGarbageCollector bool `json:"enableGarbageCollector"`

// ReconcilerSyncLoopPeriod is the amount of time the reconciler sync states loop
// wait between successive executions. Is set to 1 min by kops by default
AttachDetachReconcileSyncPeriod *metav1.Duration `json:"attachDetachReconcileSyncPeriod,omitempty" flag:"attach-detach-reconcile-sync-period"`
}

type KubeSchedulerConfig struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/kops/v1alpha1/zz_generated.conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ func autoConvert_v1alpha1_KubeControllerManagerConfig_To_kops_KubeControllerMana
} else {
out.LeaderElection = nil
}
out.AttachDetachReconcileSyncPeriod = in.AttachDetachReconcileSyncPeriod
return nil
}

Expand Down Expand Up @@ -1041,6 +1042,7 @@ func autoConvert_kops_KubeControllerManagerConfig_To_v1alpha1_KubeControllerMana
} else {
out.LeaderElection = nil
}
out.AttachDetachReconcileSyncPeriod = in.AttachDetachReconcileSyncPeriod
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/kops/v1alpha2/componentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.

package v1alpha2

import metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"

type KubeletConfigSpec struct {
APIServers string `json:"apiServers,omitempty" flag:"api-servers"`

Expand Down Expand Up @@ -170,6 +172,9 @@ type KubeControllerManagerConfig struct {
RootCAFile string `json:"rootCAFile,omitempty" flag:"root-ca-file"`
// leaderElection defines the configuration of leader election client.
LeaderElection *LeaderElectionConfiguration `json:"leaderElection,omitempty"`
// ReconcilerSyncLoopPeriod is the amount of time the reconciler sync states loop
// wait between successive executions. Is set to 1 min by kops by default
AttachDetachReconcileSyncPeriod *metav1.Duration `json:"attachDetachReconcileSyncPeriod,omitempty" flag:"attach-detach-reconcile-sync-period"`
}

type KubeSchedulerConfig struct {
Expand Down
9 changes: 7 additions & 2 deletions pkg/flagbuilder/build_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package flagbuilder

import (
"fmt"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi/utils"
"reflect"
"sort"
"strings"

"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi/utils"
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
)

// BuildFlags builds flag arguments based on "flag" tags on the structure
Expand Down Expand Up @@ -104,6 +106,9 @@ func BuildFlags(options interface{}) (string, error) {
case bool, int, int32, int64, float32, float64:
vString := fmt.Sprintf("%v", v)
flag = fmt.Sprintf("--%s=%s", flagName, vString)
case metav1.Duration:
vString := v.Duration.String()
flag = fmt.Sprintf("--%s=%s", flagName, vString)

default:
return fmt.Errorf("BuildFlags of value type not handled: %T %s=%v", v, path, v)
Expand Down
104 changes: 104 additions & 0 deletions pkg/model/components/kubecontrollermanager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright 2017 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 components

import (
"fmt"
"time"

"github.com/golang/glog"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi/loader"
metav1 "k8s.io/kubernetes/pkg/apis/meta/v1"
)

const (
defaultAttachDetachReconcileSyncPeriod = time.Minute
)

// KubeControllerManagerOptionsBuilder adds options for the kubernetes controller manager to the model.
type KubeControllerManagerOptionsBuilder struct {
Context *OptionsContext
}

var _ loader.OptionsBuilder = &KubeControllerManagerOptionsBuilder{}

// BuildOptions generates the configurations used to create kubernetes controller manager manifest
func (b *KubeControllerManagerOptionsBuilder) BuildOptions(o interface{}) error {

options := o.(*kops.ClusterSpec)

if options.KubeControllerManager == nil {
options.KubeControllerManager = &kops.KubeControllerManagerConfig{}
}

k8sv148, err := kops.ParseKubernetesVersion("v1.4.8")

if err != nil {
return fmt.Errorf("Unable to parse kubernetesVersion %s", err)
}

k8sv152, err := kops.ParseKubernetesVersion("v1.5.2")

if err != nil {
return fmt.Errorf("Unable to parse kubernetesVersion %s", err)
}

if err != nil {
return fmt.Errorf("Unable to parse kubernetesVersion %s", err)
}

kubernetesVersion, err := b.Context.KubernetesVersion()
if err != nil {
return fmt.Errorf("Unable to parse kubernetesVersion %s", err)
}

// In 1.4.8+ and 1.5.2+ k8s added the capability to tune the duration upon which the volume attach detach
// component is called.
// See https://github.com/kubernetes/kubernetes/pull/39551
// TLDR; set this too low, and have a few EBS Volumes, and you will spam AWS api

// if 1.4.8+ and 1.5.2+
if kubernetesVersion.GTE(*k8sv148) || kubernetesVersion.GTE(*k8sv152) {

glog.V(4).Infof("Kubernetes version %q supports AttachDetachReconcileSyncPeriod; will configure", kubernetesVersion)
// If not set ... or set to 0s ... which is stupid
if options.KubeControllerManager.AttachDetachReconcileSyncPeriod == nil ||
options.KubeControllerManager.AttachDetachReconcileSyncPeriod.Duration.String() == "0s" {

glog.V(8).Infof("AttachDetachReconcileSyncPeriod is not set; will set to default %v", defaultAttachDetachReconcileSyncPeriod)
options.KubeControllerManager.AttachDetachReconcileSyncPeriod = &metav1.Duration{Duration: defaultAttachDetachReconcileSyncPeriod}

// If less than 1 min and greater than 1 sec ... you get a warning
} else if options.KubeControllerManager.AttachDetachReconcileSyncPeriod.Duration < defaultAttachDetachReconcileSyncPeriod &&
options.KubeControllerManager.AttachDetachReconcileSyncPeriod.Duration > time.Second {

glog.Infof("KubeControllerManager AttachDetachReconcileSyncPeriod is set lower than recommended: %s", defaultAttachDetachReconcileSyncPeriod)

// If less than 1sec you get an error. Controller is coded to not allow configuration
// less than one second.
} else if options.KubeControllerManager.AttachDetachReconcileSyncPeriod.Duration < time.Second {
return fmt.Errorf("AttachDetachReconcileSyncPeriod cannot be set to less than 1 second")
}
} else {

glog.V(4).Infof("not setting AttachDetachReconcileSyncPeriod, k8s version is too low")
options.KubeControllerManager.AttachDetachReconcileSyncPeriod = nil
}

return nil
}
85 changes: 85 additions & 0 deletions pkg/model/components/kubecontrollermanager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2017 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 components

import (
"testing"
"time"

api "k8s.io/kops/pkg/apis/kops"
)

type ClusterParams struct {
CloudProvider string
KubernetesVersion string
UpdatePolicy string
}

func buildCluster() *api.Cluster {

return &api.Cluster{
Spec: api.ClusterSpec{
CloudProvider: "aws",
KubernetesVersion: "v1.4.0",
},
}
}

func Test_Build_KCM_Builder_Lower_Version(t *testing.T) {
c := buildCluster()

kcm := &KubeControllerManagerOptionsBuilder{
Context: &OptionsContext{
Cluster: c,
},
}

spec := c.Spec
err := kcm.BuildOptions(&spec)

if err != nil {
t.Fatalf("unexpected error from BuildOptions: %v", err)
}

if spec.KubeControllerManager.AttachDetachReconcileSyncPeriod != nil {
t.Fatalf("AttachDetachReconcileSyncPeriod should not be set for old kubernetes version %s", spec.KubernetesVersion)
}

}

func Test_Build_KCM_Builder_High_Enough_Version(t *testing.T) {
c := buildCluster()
c.Spec.KubernetesVersion = "1.4.8"

kcm := &KubeControllerManagerOptionsBuilder{
Context: &OptionsContext{
Cluster: c,
},
}

spec := c.Spec
err := kcm.BuildOptions(&spec)

if err != nil {
t.Fatalf("unexpected error from BuildOptions %s", err)
}

if spec.KubeControllerManager.AttachDetachReconcileSyncPeriod.Duration != time.Minute {
t.Fatalf("AttachDetachReconcileSyncPeriod should be set to 1m - %s", spec.KubeControllerManager.AttachDetachReconcileSyncPeriod.Duration.String())
}

}
1 change: 1 addition & 0 deletions upup/pkg/fi/cloudup/populate_cluster_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ func (c *populateClusterSpec) run() error {
codeModels = append(codeModels, &components.DockerOptionsBuilder{Context: optionsContext})
codeModels = append(codeModels, &components.NetworkingOptionsBuilder{Context: optionsContext})
codeModels = append(codeModels, &components.KubeletOptionsBuilder{Context: optionsContext})
codeModels = append(codeModels, &components.KubeControllerManagerOptionsBuilder{Context: optionsContext})
fileModels = append(fileModels, m)

default:
Expand Down
43 changes: 43 additions & 0 deletions upup/pkg/fi/cloudup/populatecluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,46 @@ func TestPopulateCluster_DockerVersion(t *testing.T) {
}
}
}

func TestPopulateCluster_KubeController_High_Enough_Version(t *testing.T) {
c := buildMinimalCluster()
c.Spec.KubernetesVersion = "v1.5.2"

err := PerformAssignments(c)
if err != nil {
t.Fatalf("error from PerformAssignments: %v", err)
}

addEtcdClusters(c)

full, err := PopulateClusterSpec(c)
if err != nil {
t.Fatalf("Unexpected error from PopulateCluster: %v", err)
}

if full.Spec.KubeControllerManager.AttachDetachReconcileSyncPeriod == nil {
t.Fatalf("AttachDetachReconcileSyncPeriod not set correctly")
}

}

func TestPopulateCluster_KubeController_Fail(t *testing.T) {
c := buildMinimalCluster()
c.Spec.KubernetesVersion = "1.4.7"

err := PerformAssignments(c)
if err != nil {
t.Fatalf("error from PerformAssignments: %v", err)
}

addEtcdClusters(c)

full, err := PopulateClusterSpec(c)
if err != nil {
t.Fatalf("Unexpected error from PopulateCluster: %v", err)
}

if full.Spec.KubeControllerManager.AttachDetachReconcileSyncPeriod != nil {
t.Fatalf("AttachDetachReconcileSyncPeriodh is not supported in 1.4.7")
}
}

0 comments on commit a77c1ed

Please sign in to comment.