Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for new k-c-m flag #1465

Merged
merged 3 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 k-c-m to the model
type KubeControllerManagerOptionsBuilder struct {
Context *OptionsContext
}

var _ loader.OptionsBuilder = &KubeControllerManagerOptionsBuilder{}

// BuildOptions tests for options to be added to the model
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")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this blank line for clarity?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it might be easier to call semver.Parse or semver.ParseTolerant directly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@justinsb / @kris-nova Should we look at singleton patter for this data? http://blog.ralch.com/tutorial/design-patterns/golang-singleton/ - More code, but I think a lot of our code kinda follows this, and is not implemented.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simpler is better I think.

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" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can do == 0 rather than calling .String() == "0s"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That did not work when I tested it :(


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")
}
}