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

Allow changing AZ of masters #9017

Merged
merged 2 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion cmd/kops/create_ig.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func RunCreateInstanceGroup(ctx context.Context, f *util.Factory, cmd *cobra.Com
return fmt.Errorf("unexpected object type: %T", obj)
}

err = validation.ValidateInstanceGroup(group).ToAggregate()
err = validation.CrossValidateInstanceGroup(group, cluster).ToAggregate()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kops/edit_instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func RunEditInstanceGroup(ctx context.Context, f *util.Factory, cmd *cobra.Comma
return err
}

err = validation.CrossValidateInstanceGroup(fullGroup, fullCluster, true).ToAggregate()
err = validation.CrossValidateInstanceGroup(fullGroup, fullCluster).ToAggregate()
if err != nil {
return err
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/apis/kops/validation/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ func validateEtcdClusterUpdate(fp *field.Path, obj *kops.EtcdClusterSpec, status
allErrs = append(allErrs, validateEtcdMemberUpdate(fp, newMember, etcdClusterStatus, oldMember)...)
}
}
for k := range oldMembers {
newCluster := newMembers[k]
if newCluster == nil {
fp := fp.Child("etcdMembers").Key(k)
allErrs = append(allErrs, field.Forbidden(fp, "EtcdCluster members cannot be removed"))
}
}
}

return allErrs
Expand Down
50 changes: 49 additions & 1 deletion pkg/apis/kops/validation/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,54 @@ func TestValidEtcdChanges(t *testing.T) {
Status *kops.ClusterStatus
Details string
}{
{
OldSpec: &kops.EtcdClusterSpec{
Name: "main",
Members: []*kops.EtcdMemberSpec{
{
Name: "a",
InstanceGroup: fi.String("eu-central-1a"),
},
{
Name: "b",
InstanceGroup: fi.String("eu-central-1b"),
},
{
Name: "c",
InstanceGroup: fi.String("eu-central-1c"),
},
},
},

NewSpec: &kops.EtcdClusterSpec{
Name: "main",
Members: []*kops.EtcdMemberSpec{
{
Name: "a",
InstanceGroup: fi.String("eu-central-1a"),
},
{
Name: "b",
InstanceGroup: fi.String("eu-central-1b"),
},
{
Name: "d",
InstanceGroup: fi.String("eu-central-1d"),
},
},
},

Status: &kops.ClusterStatus{
EtcdClusters: []kops.EtcdClusterStatus{
{
Name: "main",
},
},
},

Details: "Could not move master from one zone to another",
},

{
OldSpec: &kops.EtcdClusterSpec{
Name: "main",
Expand Down Expand Up @@ -107,7 +155,7 @@ func TestValidEtcdChanges(t *testing.T) {
for _, g := range grid {
errorList := validateEtcdClusterUpdate(nil, g.NewSpec, g.Status, g.OldSpec)
if len(errorList) != 0 {
t.Error(g.Details)
t.Errorf("%v: %v", g.Details, errorList)
}
}
}
24 changes: 23 additions & 1 deletion pkg/apis/kops/validation/instancegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package validation

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws/arn"
Expand Down Expand Up @@ -189,9 +190,13 @@ func validateVolumeMountSpec(path *field.Path, spec *kops.VolumeMountSpec) field

// CrossValidateInstanceGroup performs validation of the instance group, including that it is consistent with the Cluster
// It calls ValidateInstanceGroup, so all that validation is included.
func CrossValidateInstanceGroup(g *kops.InstanceGroup, cluster *kops.Cluster, strict bool) field.ErrorList {
func CrossValidateInstanceGroup(g *kops.InstanceGroup, cluster *kops.Cluster) field.ErrorList {
allErrs := ValidateInstanceGroup(g)

if g.Spec.Role == kops.InstanceGroupRoleMaster {
allErrs = append(allErrs, ValidateMasterInstanceGroup(g, cluster)...)
}

// Check that instance groups are defined in subnets that are defined in the cluster
{
clusterSubnets := make(map[string]*kops.ClusterSubnetSpec)
Expand All @@ -210,6 +215,23 @@ func CrossValidateInstanceGroup(g *kops.InstanceGroup, cluster *kops.Cluster, st
return allErrs
}

func ValidateMasterInstanceGroup(g *kops.InstanceGroup, cluster *kops.Cluster) field.ErrorList {
allErrs := field.ErrorList{}
for _, etcd := range cluster.Spec.EtcdClusters {
hasEtcd := false
for _, m := range etcd.Members {
if fi.StringValue(m.InstanceGroup) == g.ObjectMeta.Name {
hasEtcd = true
break
}
}
if !hasEtcd {
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "metadata", "name"), fmt.Sprintf("InstanceGroup \"%s\" with role Master must have a member in etcd cluster \"%s\"", g.ObjectMeta.Name, etcd.Name)))
}
}
return allErrs
}

var validUserDataTypes = []string{
"text/x-include-once-url",
"text/x-include-url",
Expand Down
89 changes: 89 additions & 0 deletions pkg/apis/kops/validation/instancegroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package validation
import (
"testing"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
Expand Down Expand Up @@ -90,3 +91,91 @@ func TestValidateInstanceProfile(t *testing.T) {
}
}
}

func TestValidMasterInstanceGroup(t *testing.T) {
grid := []struct {
Cluster *kops.Cluster
IG *kops.InstanceGroup
ExpectedErrors int
Description string
}{
{
Cluster: &kops.Cluster{
Spec: kops.ClusterSpec{
EtcdClusters: []*kops.EtcdClusterSpec{
{
Name: "main",
Members: []*kops.EtcdMemberSpec{
{
Name: "a",
InstanceGroup: fi.String("eu-central-1a"),
},
{
Name: "b",
InstanceGroup: fi.String("eu-central-1b"),
},
{
Name: "c",
InstanceGroup: fi.String("eu-central-1c"),
},
},
},
},
},
},
IG: &kops.InstanceGroup{
ObjectMeta: v1.ObjectMeta{
Name: "eu-central-1a",
},
Spec: kops.InstanceGroupSpec{
Role: kops.InstanceGroupRoleMaster,
},
},
ExpectedErrors: 0,
Description: "Valid instance group failed to validate",
},
{
Cluster: &kops.Cluster{
Spec: kops.ClusterSpec{
EtcdClusters: []*kops.EtcdClusterSpec{
{
Name: "main",
Members: []*kops.EtcdMemberSpec{
{
Name: "a",
InstanceGroup: fi.String("eu-central-1a"),
},
{
Name: "b",
InstanceGroup: fi.String("eu-central-1b"),
},
{
Name: "c",
InstanceGroup: fi.String("eu-central-1c"),
},
},
},
},
},
},
IG: &kops.InstanceGroup{
ObjectMeta: v1.ObjectMeta{
Name: "eu-central-1d",
},
Spec: kops.InstanceGroupSpec{
Role: kops.InstanceGroupRoleMaster,
},
},
ExpectedErrors: 1,
Description: "Master IG without etcd member validated",
},
}

for _, g := range grid {
errList := ValidateMasterInstanceGroup(g.IG, g.Cluster)
if len(errList) != g.ExpectedErrors {
t.Error(g.Description)
}
}

}
2 changes: 1 addition & 1 deletion pkg/apis/kops/validation/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ func DeepValidate(c *kops.Cluster, groups []*kops.InstanceGroup, strict bool) er
}

for _, g := range groups {
errs := CrossValidateInstanceGroup(g, c, strict)
errs := CrossValidateInstanceGroup(g, c)

// Additional cloud-specific validation rules,
// such as making sure that identifiers match the expected formats for the given cloud
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ etcdClusters:
kubeAPIServer:
allowPrivileged: true
anonymousAuth: false
apiServerCount: 1
apiServerCount: 3
authorizationMode: AlwaysAllow
bindAddress: 0.0.0.0
cloudProvider: aws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ etcdClusters:
kubeAPIServer:
allowPrivileged: true
anonymousAuth: false
apiServerCount: 1
apiServerCount: 3
authorizationMode: AlwaysAllow
bindAddress: 0.0.0.0
cloudProvider: aws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ etcdClusters:
kubeAPIServer:
allowPrivileged: true
anonymousAuth: false
apiServerCount: 1
apiServerCount: 3
authorizationMode: AlwaysAllow
bindAddress: 0.0.0.0
cloudProvider: aws
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ spec:
- etcdMembers:
- instanceGroup: master-us-test-1a
name: a
- instanceGroup: master-us-test-1b
name: b
- instanceGroup: master-us-test-1c
name: c
name: main
- etcdMembers:
- instanceGroup: master-us-test-1a
name: a
- instanceGroup: master-us-test-1b
name: b
- instanceGroup: master-us-test-1c
name: c
name: events
kubelet:
anonymousAuth: false
Expand Down
60 changes: 58 additions & 2 deletions tests/integration/update_cluster/existing_iam/kubernetes.tf
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ resource "aws_ebs_volume" "a-etcd-events-existing-iam-example-com" {
tags = {
"KubernetesCluster" = "existing-iam.example.com"
"Name" = "a.etcd-events.existing-iam.example.com"
"k8s.io/etcd/events" = "a/a"
"k8s.io/etcd/events" = "a/a,b,c"
"k8s.io/role/master" = "1"
"kubernetes.io/cluster/existing-iam.example.com" = "owned"
}
Expand All @@ -231,7 +231,63 @@ resource "aws_ebs_volume" "a-etcd-main-existing-iam-example-com" {
tags = {
"KubernetesCluster" = "existing-iam.example.com"
"Name" = "a.etcd-main.existing-iam.example.com"
"k8s.io/etcd/main" = "a/a"
"k8s.io/etcd/main" = "a/a,b,c"
"k8s.io/role/master" = "1"
"kubernetes.io/cluster/existing-iam.example.com" = "owned"
}
type = "gp2"
}

resource "aws_ebs_volume" "b-etcd-events-existing-iam-example-com" {
availability_zone = "us-test-1b"
encrypted = false
size = 20
tags = {
"KubernetesCluster" = "existing-iam.example.com"
"Name" = "b.etcd-events.existing-iam.example.com"
"k8s.io/etcd/events" = "b/a,b,c"
"k8s.io/role/master" = "1"
"kubernetes.io/cluster/existing-iam.example.com" = "owned"
}
type = "gp2"
}

resource "aws_ebs_volume" "b-etcd-main-existing-iam-example-com" {
availability_zone = "us-test-1b"
encrypted = false
size = 20
tags = {
"KubernetesCluster" = "existing-iam.example.com"
"Name" = "b.etcd-main.existing-iam.example.com"
"k8s.io/etcd/main" = "b/a,b,c"
"k8s.io/role/master" = "1"
"kubernetes.io/cluster/existing-iam.example.com" = "owned"
}
type = "gp2"
}

resource "aws_ebs_volume" "c-etcd-events-existing-iam-example-com" {
availability_zone = "us-test-1c"
encrypted = false
size = 20
tags = {
"KubernetesCluster" = "existing-iam.example.com"
"Name" = "c.etcd-events.existing-iam.example.com"
"k8s.io/etcd/events" = "c/a,b,c"
"k8s.io/role/master" = "1"
"kubernetes.io/cluster/existing-iam.example.com" = "owned"
}
type = "gp2"
}

resource "aws_ebs_volume" "c-etcd-main-existing-iam-example-com" {
availability_zone = "us-test-1c"
encrypted = false
size = 20
tags = {
"KubernetesCluster" = "existing-iam.example.com"
"Name" = "c.etcd-main.existing-iam.example.com"
"k8s.io/etcd/main" = "c/a,b,c"
"k8s.io/role/master" = "1"
"kubernetes.io/cluster/existing-iam.example.com" = "owned"
}
Expand Down
Loading