Update DO objects to use tags

This commit is contained in:
Srikanth 2019-09-06 00:01:45 +05:30
parent 8843a55073
commit 35ca8b01c1
5 changed files with 62 additions and 18 deletions

View File

@ -18,7 +18,7 @@ package domodel
import (
"strings"
"strconv"
"k8s.io/kops/pkg/model"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/dotasks"
@ -45,7 +45,8 @@ func (d *DropletBuilder) Build(c *fi.ModelBuilderContext) error {
// replace "." with "-" since DO API does not accept "."
clusterTag := "KubernetesCluster:" + strings.Replace(d.ClusterName(), ".", "-", -1)
indexCount := 0
// In the future, DigitalOcean will use Machine API to manage groups,
// for now create d.InstanceGroups.Spec.MinSize amount of droplets
for _, ig := range d.InstanceGroups {
@ -61,8 +62,15 @@ func (d *DropletBuilder) Build(c *fi.ModelBuilderContext) error {
droplet.Size = fi.String(ig.Spec.MachineType)
droplet.Image = fi.String(ig.Spec.Image)
droplet.SSHKey = fi.String(sshKeyFingerPrint)
droplet.Tags = []string{clusterTag}
if ig.IsMaster() {
indexCount++
clusterTagIndex := "K8S-INDEX:" + strconv.Itoa(indexCount)
droplet.Tags = []string{clusterTag, clusterTagIndex}
} else {
droplet.Tags = []string{clusterTag}
}
userData, err := d.BootstrapScript.ResourceNodeUp(ig, d.Cluster)
if err != nil {
return err

View File

@ -31,6 +31,7 @@ import (
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/dotasks"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/do"
"k8s.io/kops/upup/pkg/fi/cloudup/gcetasks"
"k8s.io/kops/upup/pkg/fi/cloudup/openstack"
"k8s.io/kops/upup/pkg/fi/cloudup/openstacktasks"
@ -184,11 +185,19 @@ func (b *MasterVolumeBuilder) addDOVolume(c *fi.ModelBuilderContext, name string
name = name[:64]
}
tags := make(map[string]string)
tags[do.TagNameEtcdClusterPrefix+etcd.Name] = gce.SafeClusterName(m.Name)
// We always add an owned tags (these can't be shared)
tags[do.TagKubernetesClusterNamePrefix] = gce.SafeClusterName(b.Cluster.ObjectMeta.Name)
t := &dotasks.Volume{
Name: s(name),
Lifecycle: b.Lifecycle,
SizeGB: fi.Int64(int64(volumeSize)),
Region: s(zone),
Tags: tags,
}
c.AddTask(t)

View File

@ -21,8 +21,9 @@ import (
"k8s.io/kops/upup/pkg/fi"
)
const TagNameEtcdClusterPrefix = "k8s.io/etcd/"
const TagNameEtcdClusterPrefix = "etcdCluster-"
const TagNameRolePrefix = "k8s.io/role/"
const TagKubernetesClusterNamePrefix = "KubernetesCluster"
func NewDOCloud(region string) (fi.Cloud, error) {
return digitalocean.NewCloud(region)

View File

@ -19,9 +19,10 @@ package dotasks
import (
"context"
"errors"
//"strconv"
"github.com/digitalocean/godo"
"k8s.io/klog"
"k8s.io/kops/pkg/resources/digitalocean"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/do"
@ -142,21 +143,30 @@ func (_ *Droplet) RenderDO(t *do.DOAPITarget, a, e, changes *Droplet) error {
newDropletCount = expectedCount - actualCount
}
var dropletNames []string
// var dropletTags []string
// indexCount := 0
for i := 0; i < newDropletCount; i++ {
dropletNames = append(dropletNames, fi.StringValue(e.Name))
// indexCount++
// clusterTagIndex := "k8s-index:" + strconv.Itoa(indexCount)
// dropletTags = append(e.Tags, clusterTagIndex)
_, _, err = t.Cloud.Droplets().Create(context.TODO(), &godo.DropletCreateRequest{
Name: fi.StringValue(e.Name),
Region: fi.StringValue(e.Region),
Size: fi.StringValue(e.Size),
Image: godo.DropletCreateImage{Slug: fi.StringValue(e.Image)},
PrivateNetworking: true,
Tags: e.Tags,
UserData: userData,
SSHKeys: []godo.DropletCreateSSHKey{{Fingerprint: fi.StringValue(e.SSHKey)}},
})
if err != nil {
klog.Errorf("Error creating droplet with Name=%s", fi.StringValue(e.Name))
return err
}
}
_, _, err = t.Cloud.Droplets().CreateMultiple(context.TODO(), &godo.DropletMultiCreateRequest{
Names: dropletNames,
Region: fi.StringValue(e.Region),
Size: fi.StringValue(e.Size),
Image: godo.DropletCreateImage{Slug: fi.StringValue(e.Image)},
PrivateNetworking: true,
Tags: e.Tags,
UserData: userData,
SSHKeys: []godo.DropletCreateSSHKey{{Fingerprint: fi.StringValue(e.SSHKey)}},
})
return err
}

View File

@ -18,9 +18,10 @@ package dotasks
import (
"context"
"strings"
"github.com/digitalocean/godo"
"k8s.io/klog"
"k8s.io/kops/pkg/resources/digitalocean"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/do"
@ -35,6 +36,7 @@ type Volume struct {
SizeGB *int64
Region *string
Tags map[string]string
}
var _ fi.CompareWithID = &Volume{}
@ -108,12 +110,26 @@ func (_ *Volume) RenderDO(t *do.DOAPITarget, a, e, changes *Volume) error {
return nil
}
tagArray := []string{}
klog.V(2).Info("Looping DO tag arrays")
for k, v := range e.Tags {
s := []string{k, v};
// DO tags don't accept =. Separate the key and value with an ":"
strJoin := strings.Join(s, ":")
klog.V(2).Infof("DO - Join the volume tag - %s", strJoin)
tagArray = append(tagArray, strJoin)
}
volService := t.Cloud.Volumes()
_, _, err := volService.CreateVolume(context.TODO(), &godo.VolumeCreateRequest{
Name: fi.StringValue(e.Name),
Region: fi.StringValue(e.Region),
SizeGigaBytes: fi.Int64Value(e.SizeGB),
Tags: tagArray,
})
return err
}