Skip to content

Commit

Permalink
moving code to cloudinstances
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislovecnm committed Sep 25, 2017
1 parent 2bc21c5 commit 47cc37d
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 68 deletions.
59 changes: 59 additions & 0 deletions pkg/cloudinstances/cloud_instance_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2016 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 cloudinstances

import (
"k8s.io/client-go/pkg/api/v1"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
)

// CloudInstanceGroup is the cloud backing of InstanceGroup.
type CloudInstanceGroup struct {
InstanceGroup *api.InstanceGroup
GroupName string
GroupTemplateName string
Status string
Ready []*CloudInstanceMember
NeedUpdate []*CloudInstanceMember
MinSize int
MaxSize int
}

// CloudInstanceGroupInstance describes an instance in an autoscaling group.
type CloudInstanceMember struct {
ID *string
Node *v1.Node
}

// FindCloudInstanceGroups joins data from the cloud and the instance groups into a map that can be used for updates.
func FindCloudInstanceGroups(cloud fi.Cloud, cluster *api.Cluster, instancegroups []*api.InstanceGroup, warnUnmatched bool, nodes []v1.Node) (map[string]CloudInstanceGroup, error) {

nodeMap := make(map[string]*v1.Node)
for i := range nodes {
node := &nodes[i]
nodeMap[node.Spec.ExternalID] = node
}

groups, err := cloud.GetCloudGroups(cluster, instancegroups, warnUnmatched, nodeMap)
if err != nil {
return nil, err
}

return groups, nil

}
33 changes: 7 additions & 26 deletions pkg/instancegroups/instancegroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,21 @@ import (

"github.com/golang/glog"
"github.com/spf13/cobra"
"k8s.io/client-go/pkg/api/v1"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/pkg/featureflag"
"k8s.io/kops/pkg/validation"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubernetes/pkg/kubectl/cmd"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
)

// FindCloudInstanceGroups joins data from the cloud and the instance groups into a map that can be used for updates.
func FindCloudInstanceGroups(cloud fi.Cloud, cluster *api.Cluster, instancegroups []*api.InstanceGroup, warnUnmatched bool, nodes []v1.Node) (map[string]*fi.CloudGroup, error) {

nodeMap := make(map[string]*v1.Node)
for i := range nodes {
node := &nodes[i]
nodeMap[node.Spec.ExternalID] = node
}

groups, err := cloud.GetCloudGroups(cluster, instancegroups, warnUnmatched, nodeMap)
if err != nil {
return nil, err
}

return groups, nil

}

// RollingUpdateInstanceGroup is the AWS ASG backing an InstanceGroup.
type RollingUpdateInstanceGroup struct {
// Cloud is the kops cloud provider
Cloud fi.Cloud
Cloud fi.Cloud
// CloudGroup is the kops cloud provider groups
CloudGroup *fi.CloudGroup
CloudGroup *cloudinstances.CloudInstanceGroup

// TODO should remove the need to have rollingupdate struct and add:
// TODO - the kubernetes client
Expand All @@ -67,7 +49,7 @@ type RollingUpdateInstanceGroup struct {
}

// NewRollingUpdateInstanceGroup create a new struct
func NewRollingUpdateInstanceGroup(cloud fi.Cloud, cloudGroup *fi.CloudGroup) (*RollingUpdateInstanceGroup, error) {
func NewRollingUpdateInstanceGroup(cloud fi.Cloud, cloudGroup *cloudinstances.CloudInstanceGroup) (*RollingUpdateInstanceGroup, error) {
if cloud == nil {
return nil, fmt.Errorf("cloud provider is required")
}
Expand All @@ -78,12 +60,11 @@ func NewRollingUpdateInstanceGroup(cloud fi.Cloud, cloudGroup *fi.CloudGroup) (*
// TODO check more values in cloudGroup that they are set properly

return &RollingUpdateInstanceGroup{
Cloud:cloud,
Cloud: cloud,
CloudGroup: cloudGroup,
}, nil
}


// TODO: Temporarily increase size of ASG?
// TODO: Remove from ASG first so status is immediately updated?
// TODO: Batch termination, like a rolling-update
Expand Down Expand Up @@ -252,7 +233,7 @@ func (r *RollingUpdateInstanceGroup) ValidateCluster(rollingUpdateData *RollingU
}

// DeleteInstance deletes an Cloud Instance.
func (r *RollingUpdateInstanceGroup) DeleteInstance(u *fi.CloudGroupInstance) error {
func (r *RollingUpdateInstanceGroup) DeleteInstance(u *cloudinstances.CloudInstanceMember) error {

id := fi.StringValue(u.ID)

Expand All @@ -274,7 +255,7 @@ func (r *RollingUpdateInstanceGroup) DeleteInstance(u *fi.CloudGroupInstance) er
}

// DrainNode drains a K8s node.
func (r *RollingUpdateInstanceGroup) DrainNode(u *fi.CloudGroupInstance, rollingUpdateData *RollingUpdateCluster) error {
func (r *RollingUpdateInstanceGroup) DrainNode(u *cloudinstances.CloudInstanceMember, rollingUpdateData *RollingUpdateCluster) error {
if rollingUpdateData.ClientConfig == nil {
return fmt.Errorf("clientConfig not set")
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/instancegroups/rollingupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
api "k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/upup/pkg/fi"
)

Expand All @@ -48,7 +49,7 @@ type RollingUpdateCluster struct {
}

// RollingUpdate performs a rolling update on a K8s Cluster.
func (c *RollingUpdateCluster) RollingUpdate(groups map[string]*fi.CloudGroup, instanceGroups *api.InstanceGroupList) error {
func (c *RollingUpdateCluster) RollingUpdate(groups map[string]*cloudinstances.CloudInstanceGroup, instanceGroups *api.InstanceGroupList) error {
if len(groups) == 0 {
glog.Infof("Cloud Instance Group length is zero. Not doing a rolling-update.")
return nil
Expand All @@ -57,9 +58,9 @@ func (c *RollingUpdateCluster) RollingUpdate(groups map[string]*fi.CloudGroup, i
var resultsMutex sync.Mutex
results := make(map[string]error)

masterGroups := make(map[string]*fi.CloudGroup)
nodeGroups := make(map[string]*fi.CloudGroup)
bastionGroups := make(map[string]*fi.CloudGroup)
masterGroups := make(map[string]*cloudinstances.CloudInstanceGroup)
nodeGroups := make(map[string]*cloudinstances.CloudInstanceGroup)
bastionGroups := make(map[string]*cloudinstances.CloudInstanceGroup)
for k, group := range groups {
switch group.InstanceGroup.Spec.Role {
case api.InstanceGroupRoleNode:
Expand All @@ -79,7 +80,7 @@ func (c *RollingUpdateCluster) RollingUpdate(groups map[string]*fi.CloudGroup, i

for k, bastionGroup := range bastionGroups {
wg.Add(1)
go func(k string, group *fi.CloudGroup) {
go func(k string, group *cloudinstances.CloudInstanceGroup) {
resultsMutex.Lock()
results[k] = fmt.Errorf("function panic bastions")
resultsMutex.Unlock()
Expand Down
9 changes: 5 additions & 4 deletions pkg/resources/digitalocean/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/kops/pkg/resources/digitalocean/dns"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
"k8s.io/kops/pkg/cloudinstances"
)

// TokenSource implements oauth2.TokenSource
Expand Down Expand Up @@ -81,21 +82,21 @@ func NewCloud(region string) (*Cloud, error) {
}

// GetCloudGroups is not implemented yet, that needs to return the instances and groups that back a kops cluster.
func (c *Cloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*fi.CloudGroup, error) {
func (c *Cloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
glog.V(8).Infof("digitalocean cloud provider GetCloudGroups not implemented yet")
return nil, fmt.Errorf("digital ocean cloud provider does not support getting cloud groups at this time.")
return nil, fmt.Errorf("digital ocean cloud provider does not support getting cloud groups at this time")
}

// DeleteGroup is not implemented yet, is a func that needs to delete a DO instance group.
func (c *Cloud) DeleteGroup(name string, template string) error {
glog.V(8).Infof("digitalocean cloud provider DeleteGroup not implemented yet")
return fmt.Errorf("digital ocean cloud provider does not support deleting cloud groups at this time.")
return fmt.Errorf("digital ocean cloud provider does not support deleting cloud groups at this time")
}

// DeleteInstance is not implemented yet, is func needs to delete a DO instance.
func (c *Cloud) DeleteInstance(id *string) error {
glog.V(8).Infof("digitalocean cloud provider DeleteInstance not implemented yet")
return fmt.Errorf("digital ocean cloud provider does not support deleting cloud instances at this time.")
return fmt.Errorf("digital ocean cloud provider does not support deleting cloud instances at this time")
}

// ProviderID returns the kops api identifier for DigitalOcean cloud provider
Expand Down
19 changes: 2 additions & 17 deletions upup/pkg/fi/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package fi
import (
"k8s.io/client-go/pkg/api/v1"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
)

Expand All @@ -37,7 +38,7 @@ type Cloud interface {
DeleteGroup(name string, template string) error

// GetCloudGroups returns a map of cloud instances that back a kops cluster
GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*CloudGroup, error)
GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]cloudinstances.CloudInstanceGroup, error)
}

type VPCInfo struct {
Expand All @@ -54,23 +55,7 @@ type SubnetInfo struct {
CIDR string
}

// CloudInstanceGroup is the cloud backing of InstanceGroup.
type CloudGroup struct {
InstanceGroup *kops.InstanceGroup
GroupName string
GroupTemplateName string
Status string
Ready []*CloudGroupInstance
NeedUpdate []*CloudGroupInstance
MinSize int
MaxSize int
}

// CloudInstanceGroupInstance describes an instance in an autoscaling group.
type CloudGroupInstance struct {
ID *string
Node *v1.Node
}

// zonesToCloud allows us to infer from certain well-known zones to a cloud
// Note it is safe to "overmap" zones that don't exist: we'll check later if the zones actually exist
Expand Down
11 changes: 6 additions & 5 deletions upup/pkg/fi/cloudup/awsup/aws_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
dnsproviderroute53 "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/aws/route53"
"k8s.io/kops/pkg/cloudinstances"
)

// By default, aws-sdk-go only retries 3 times, which doesn't give
Expand Down Expand Up @@ -287,8 +288,8 @@ func (c *awsCloudImplementation) DeleteInstance(id *string) error {
// TODO not used yet, as this requires a major refactor of rolling-update code, slowly but surely

// GetCloudGroups returns a groups of instanaces that back a kops instance groups
func (c *awsCloudImplementation) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*fi.CloudGroup, error) {
groups:= make(map[string]*fi.CloudGroup)
func (c *awsCloudImplementation) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
groups:= make(map[string]*cloudinstances.CloudInstanceGroup)
asgs, err := c.FindAutoscalingGroups()
if err != nil {
return nil, fmt.Errorf("unable to find autoscale groups: %v", err)
Expand Down Expand Up @@ -418,10 +419,10 @@ func MatchesAsgTags(tags map[string]string, actual []*autoscaling.TagDescription
return true
}

func (c *awsCloudImplementation) awsBuildCloudInstanceGroup(ig *kops.InstanceGroup, g *autoscaling.Group, nodeMap map[string]*v1.Node) *fi.CloudGroup {
func (c *awsCloudImplementation) awsBuildCloudInstanceGroup(ig *kops.InstanceGroup, g *autoscaling.Group, nodeMap map[string]*v1.Node) *cloudinstances.CloudInstanceGroup {

// TODO make this a method to create a new group, all of these are required
n := &fi.CloudGroup{
n := &cloudinstances.CloudInstanceGroup{
GroupName: aws.StringValue(g.AutoScalingGroupName),
InstanceGroup: ig,
GroupTemplateName: aws.StringValue(g.LaunchConfigurationName),
Expand All @@ -431,7 +432,7 @@ func (c *awsCloudImplementation) awsBuildCloudInstanceGroup(ig *kops.InstanceGro

readyLaunchConfigurationName := aws.StringValue(g.LaunchConfigurationName)
for _, i := range g.Instances {
c := &fi.CloudGroupInstance{
c := &cloudinstances.CloudInstanceMember{
ID: i.InstanceId,
}

Expand Down
3 changes: 2 additions & 1 deletion upup/pkg/fi/cloudup/awsup/mock_aws_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
dnsproviderroute53 "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/aws/route53"
"k8s.io/kops/pkg/cloudinstances"
)

type MockAWSCloud struct {
Expand Down Expand Up @@ -116,7 +117,7 @@ func (c *MockAWSCloud) DeleteInstance(id *string) error {
return nil
}

func (c *MockAWSCloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*fi.CloudGroup, error) {
func (c *MockAWSCloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
return nil, fmt.Errorf("not implemented yet")
}

Expand Down
3 changes: 2 additions & 1 deletion upup/pkg/fi/cloudup/baremetal/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"k8s.io/client-go/pkg/api/v1"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
)
Expand Down Expand Up @@ -51,7 +52,7 @@ func (c *Cloud) FindVPCInfo(id string) (*fi.VPCInfo, error) {

// GetCloudGroups is not implemented yet, that needs to return the instances and groups that back a kops cluster.
// Baremetal may not support this.
func (c *Cloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*fi.CloudGroup, error) {
func (c *Cloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
glog.V(8).Infof("baremetal cloud GetCloudGroups not implemented yet")
return nil, fmt.Errorf("baremetal provider does not support getting cloud groups at this time.")
}
Expand Down
10 changes: 6 additions & 4 deletions upup/pkg/fi/cloudup/gce/mock_gce_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package gce

import (
"fmt"

"github.com/golang/glog"
compute "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/storage/v1"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/cloudinstances"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
dnsproviderclouddns "k8s.io/kubernetes/federation/pkg/dnsprovider/providers/google/clouddns"
Expand Down Expand Up @@ -51,21 +53,21 @@ func buildMockGCECloud(region string, project string) *mockGCECloud {
}

// GetCloudGroups is not implemented yet
func (c *mockGCECloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*fi.CloudGroup, error) {
func (c *mockGCECloud) GetCloudGroups(cluster *kops.Cluster, instancegroups []*kops.InstanceGroup, warnUnmatched bool, nodeMap map[string]*v1.Node) (map[string]*cloudinstances.CloudInstanceGroup, error) {
glog.V(8).Infof("mockGCECloud cloud provider GetCloudGroups not implemented yet")
return nil, fmt.Errorf("mockGCECloud cloud provider does not support getting cloud groups at this time.")
return nil, fmt.Errorf("mockGCECloud cloud provider does not support getting cloud groups at this time")
}

// DeleteGroup is not implemented yet
func (c *mockGCECloud) DeleteGroup(name string, template string) error {
glog.V(8).Infof("mockGCECloud cloud provider DeleteGroup not implemented yet")
return fmt.Errorf("mockGCECloud cloud provider does not support deleting cloud groups at this time.")
return fmt.Errorf("mockGCECloud cloud provider does not support deleting cloud groups at this time")
}

// DeleteInstance is not implemented yet
func (c *mockGCECloud) DeleteInstance(id *string) error {
glog.V(8).Infof("mockGCECloud cloud provider DeleteInstance not implemented yet")
return fmt.Errorf("mockGCECloud cloud provider does not support deleting cloud instances at this time.")
return fmt.Errorf("mockGCECloud cloud provider does not support deleting cloud instances at this time")
}

// Zones is not implemented yet
Expand Down
Loading

0 comments on commit 47cc37d

Please sign in to comment.