Move node setup to pkg and refactor

This commit is contained in:
John Gardiner Myers 2020-06-30 22:24:08 -07:00
parent a5b60ccac3
commit f1a9297cb5
2 changed files with 76 additions and 58 deletions

View File

@ -30,7 +30,6 @@ import (
"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/klog"
"k8s.io/kops/cmd/kops/util"
@ -59,7 +58,6 @@ type CreateClusterOptions struct {
Target string
NodeSize string
MasterSize string
NodeCount int32
MasterVolumeSize int32
NodeVolumeSize int32
Project string
@ -390,17 +388,19 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
return fmt.Errorf("--name is required")
}
cluster, err := clientset.GetCluster(ctx, c.ClusterName)
if err != nil {
if apierrors.IsNotFound(err) {
cluster = nil
} else {
return err
{
cluster, err := clientset.GetCluster(ctx, c.ClusterName)
if err != nil {
if apierrors.IsNotFound(err) {
cluster = nil
} else {
return err
}
}
}
if cluster != nil {
return fmt.Errorf("cluster %q already exists; use 'kops update cluster' to apply changes", c.ClusterName)
if cluster != nil {
return fmt.Errorf("cluster %q already exists; use 'kops update cluster' to apply changes", c.ClusterName)
}
}
if c.OpenstackNetworkID != "" {
@ -412,13 +412,19 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
return err
}
// TODO: push more of the following logic into cloudup.NewCluster()
cluster = clusterResult.Cluster
cluster := clusterResult.Cluster
instanceGroups := clusterResult.InstanceGroups
channel := clusterResult.Channel
allZones := clusterResult.AllZones
zoneToSubnetMap := clusterResult.ZoneToSubnetMap
masters := clusterResult.Masters
var masters []*api.InstanceGroup
var nodes []*api.InstanceGroup
for _, ig := range instanceGroups {
switch ig.Spec.Role {
case api.InstanceGroupRoleMaster:
masters = append(masters, ig)
case api.InstanceGroupRoleNode:
nodes = append(nodes, ig)
}
}
cloudLabels, err := parseCloudLabels(c.CloudLabels)
if err != nil {
@ -428,30 +434,6 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
cluster.Spec.CloudLabels = cloudLabels
}
var nodes []*api.InstanceGroup
if len(nodes) == 0 {
g := &api.InstanceGroup{}
g.Spec.Role = api.InstanceGroupRoleNode
g.ObjectMeta.Name = "nodes"
subnetNames := sets.NewString()
for _, zone := range c.Zones {
subnet := zoneToSubnetMap[zone]
if subnet == nil {
klog.Fatalf("subnet not found in zoneToSubnetMap")
}
subnetNames.Insert(subnet.Name)
}
g.Spec.Subnets = subnetNames.List()
if api.CloudProviderID(cluster.Spec.CloudProvider) == api.CloudProviderGCE {
g.Spec.Zones = c.Zones
}
instanceGroups = append(instanceGroups, g)
nodes = append(nodes, g)
}
if c.NodeSize != "" {
for _, group := range nodes {
group.Spec.MachineType = c.NodeSize
@ -480,13 +462,6 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
}
}
if c.NodeCount != 0 {
for _, group := range nodes {
group.Spec.MinSize = fi.Int32(c.NodeCount)
group.Spec.MaxSize = fi.Int32(c.NodeCount)
}
}
if c.MasterTenancy != "" {
for _, group := range masters {
group.Spec.Tenancy = c.MasterTenancy
@ -533,6 +508,10 @@ func RunCreateCluster(ctx context.Context, f *util.Factory, out io.Writer, c *Cr
cluster.Spec.DNSZone = c.DNSZone
}
// TODO: push more of the following logic into cloudup.NewCluster()
channel := clusterResult.Channel
allZones := clusterResult.AllZones
if c.CloudProvider != "" {
if featureflag.Spotinst.Enabled() {
if cluster.Spec.CloudConfig == nil {

View File

@ -86,6 +86,10 @@ type NewClusterOptions struct {
EncryptEtcdStorage bool
// EtcdStorageType is the underlying cloud storage class of the etcd volumes.
EtcdStorageType string
// NodeCount is the number of nodes to create. Defaults to leaving the count unspecified
// on the InstanceGroup, which results in a count of 2.
NodeCount int32
}
func (o *NewClusterOptions) InitDefaults() {
@ -100,10 +104,8 @@ type NewClusterResult struct {
InstanceGroups []*api.InstanceGroup
// TODO remove after more create_cluster logic refactored in
Channel *api.Channel
AllZones sets.String
ZoneToSubnetMap map[string]*api.ClusterSubnetSpec
Masters []*api.InstanceGroup
Channel *api.Channel
AllZones sets.String
}
// NewCluster initializes cluster and instance groups specifications as
@ -190,13 +192,19 @@ func NewCluster(opt *NewClusterOptions, clientset simple.Clientset) (*NewCluster
return nil, err
}
nodes, err := setupNodes(opt, &cluster, zoneToSubnetMap)
if err != nil {
return nil, err
}
instanceGroups := append([]*api.InstanceGroup(nil), masters...)
instanceGroups = append(instanceGroups, nodes...)
result := NewClusterResult{
Cluster: &cluster,
InstanceGroups: masters,
Channel: channel,
AllZones: allZones,
ZoneToSubnetMap: zoneToSubnetMap,
Masters: masters,
Cluster: &cluster,
InstanceGroups: instanceGroups,
Channel: channel,
AllZones: allZones,
}
return &result, nil
}
@ -605,3 +613,34 @@ func trimCommonPrefix(names []string) []string {
return names
}
func setupNodes(opt *NewClusterOptions, cluster *api.Cluster, zoneToSubnetMap map[string]*api.ClusterSubnetSpec) ([]*api.InstanceGroup, error) {
var nodes []*api.InstanceGroup
g := &api.InstanceGroup{}
g.Spec.Role = api.InstanceGroupRoleNode
g.ObjectMeta.Name = "nodes"
subnetNames := sets.NewString()
for _, zone := range opt.Zones {
subnet := zoneToSubnetMap[zone]
if subnet == nil {
klog.Fatalf("subnet not found in zoneToSubnetMap")
}
subnetNames.Insert(subnet.Name)
}
g.Spec.Subnets = subnetNames.List()
if api.CloudProviderID(cluster.Spec.CloudProvider) == api.CloudProviderGCE {
g.Spec.Zones = opt.Zones
}
if opt.NodeCount != 0 {
g.Spec.MinSize = fi.Int32(opt.NodeCount)
g.Spec.MaxSize = fi.Int32(opt.NodeCount)
}
nodes = append(nodes, g)
return nodes, nil
}