implement digitalocean volumes task

This commit is contained in:
andrewsykim 2017-08-20 00:28:41 -04:00
parent 9c86800207
commit 525fde3609
9 changed files with 424 additions and 4 deletions

View File

@ -137,6 +137,7 @@ codegen: kops-gobindata
PATH=${GOPATH_1ST}/bin:${PATH} go generate k8s.io/kops/upup/pkg/fi/cloudup/awstasks
PATH=${GOPATH_1ST}/bin:${PATH} go generate k8s.io/kops/upup/pkg/fi/cloudup/gcetasks
PATH=${GOPATH_1ST}/bin:${PATH} go generate k8s.io/kops/upup/pkg/fi/assettasks
PATH=${GOPATH_1ST}/bin:${PATH} go generate k8s.io/kops/upup/pkg/fi/cloudup/dotasks
PATH=${GOPATH_1ST}/bin:${PATH} go generate k8s.io/kops/upup/pkg/fi/fitasks
.PHONY: protobuf

View File

@ -102,6 +102,7 @@ k8s.io/kops/upup/pkg/fi/cloudup/awsup
k8s.io/kops/upup/pkg/fi/cloudup/cloudformation
k8s.io/kops/upup/pkg/fi/cloudup/dnstasks
k8s.io/kops/upup/pkg/fi/cloudup/do
k8s.io/kops/upup/pkg/fi/cloudup/dotasks
k8s.io/kops/upup/pkg/fi/cloudup/gce
k8s.io/kops/upup/pkg/fi/cloudup/gcetasks
k8s.io/kops/upup/pkg/fi/cloudup/terraform

View File

@ -25,6 +25,7 @@ import (
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awstasks"
"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/gcetasks"
)
@ -88,6 +89,8 @@ func (b *MasterVolumeBuilder) Build(c *fi.ModelBuilderContext) error {
switch kops.CloudProviderID(b.Cluster.Spec.CloudProvider) {
case kops.CloudProviderAWS:
b.addAWSVolume(c, name, volumeSize, subnet, etcd, m, allMembers)
case kops.CloudProviderDO:
b.addDOVolume(c, name, volumeSize, subnet, etcd, m, allMembers)
case kops.CloudProviderGCE:
b.addGCEVolume(c, name, volumeSize, subnet, etcd, m, allMembers)
case kops.CloudProviderVSphere:
@ -137,6 +140,17 @@ func (b *MasterVolumeBuilder) addAWSVolume(c *fi.ModelBuilderContext, name strin
c.AddTask(t)
}
func (b *MasterVolumeBuilder) addDOVolume(c *fi.ModelBuilderContext, name string, volumeSize int32, subnet *kops.ClusterSubnetSpec, etcd *kops.EtcdClusterSpec, m *kops.EtcdMemberSpec, allMembers []string) {
t := &dotasks.Volume{
Name: s(name),
Lifecycle: b.Lifecycle,
SizeGB: fi.Int64(int64(volumeSize)),
Region: s(subnet.Zone),
}
c.AddTask(t)
}
func (b *MasterVolumeBuilder) addGCEVolume(c *fi.ModelBuilderContext, name string, volumeSize int32, subnet *kops.ClusterSubnetSpec, etcd *kops.EtcdClusterSpec, m *kops.EtcdMemberSpec, allMembers []string) {
volumeType := fi.StringValue(m.VolumeType)
if volumeType == "" {

View File

@ -44,7 +44,7 @@ func (t *TokenSource) Token() (*oauth2.Token, error) {
// Cloud exposes all the interfaces required to operate on DigitalOcean resources
type Cloud struct {
client *godo.Client
Client *godo.Client
dns dnsprovider.Interface
@ -70,7 +70,7 @@ func NewCloud(region string) (*Cloud, error) {
client := godo.NewClient(oauthClient)
return &Cloud{
client: client,
Client: client,
dns: dns.NewProvider(client),
Region: region,
}, nil
@ -86,6 +86,11 @@ func (c *Cloud) DNS() (dnsprovider.Interface, error) {
return c.dns, nil
}
// Volume returns an implementation of godo.StorageService
func (c *Cloud) Volumes() godo.StorageService {
return c.Client.Storage
}
// FindVPCInfo is not implemented, it's only here to satisfy the fi.Cloud interface
func (c *Cloud) FindVPCInfo(id string) (*fi.VPCInfo, error) {
return nil, errors.New("not implemented")

View File

@ -46,6 +46,7 @@ import (
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/do"
"k8s.io/kops/upup/pkg/fi/cloudup/dotasks"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/gcetasks"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
@ -335,7 +336,9 @@ func (c *ApplyClusterCmd) Run() error {
}
// this is a no-op for now, add tasks to this list as more DO support is added
l.AddTypes(map[string]interface{}{})
l.AddTypes(map[string]interface{}{
"volume": &dotasks.Volume{},
})
}
case kops.CloudProviderAWS:
{
@ -514,6 +517,10 @@ func (c *ApplyClusterCmd) Run() error {
l.Builders = append(l.Builders,
&model.IAMModelBuilder{KopsModelContext: modelContext, Lifecycle: iamLifecycle},
)
case kops.CloudProviderDO:
l.Builders = append(l.Builders,
&model.MasterVolumeBuilder{KopsModelContext: modelContext, Lifecycle: clusterLifecycle},
)
case kops.CloudProviderGCE:
gceModelContext := &gcemodel.GCEModelContext{

View File

@ -0,0 +1,135 @@
/*
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 dotasks
import (
"context"
"github.com/digitalocean/godo"
"k8s.io/kops/pkg/resources/digitalocean"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/do"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
//go:generate fitask -type=Volume
type Volume struct {
Name *string
ID *string
Lifecycle *fi.Lifecycle
SizeGB *int64
Region *string
}
var _ fi.CompareWithID = &Volume{}
func (v *Volume) CompareWithID() *string {
return v.ID
}
func (v *Volume) Find(c *fi.Context) (*Volume, error) {
cloud := c.Cloud.(*digitalocean.Cloud)
volService := cloud.Volumes()
volumes, _, err := volService.ListVolumes(context.TODO(), &godo.ListVolumeParams{
Region: cloud.Region,
Name: fi.StringValue(v.Name),
})
if err != nil {
return nil, err
}
for _, volume := range volumes {
if volume.Name == fi.StringValue(v.Name) {
return &Volume{
Name: fi.String(volume.Name),
ID: fi.String(volume.ID),
Lifecycle: v.Lifecycle,
SizeGB: fi.Int64(volume.SizeGigaBytes),
Region: fi.String(volume.Region.Slug),
}, nil
}
}
// Volume = nil if not found
return nil, nil
}
func (v *Volume) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(v, c)
}
func (_ *Volume) CheckChanges(a, e, changes *Volume) error {
if a != nil {
if changes.Name != nil {
return fi.CannotChangeField("Name")
}
if changes.ID != nil {
return fi.CannotChangeField("ID")
}
if changes.Region != nil {
return fi.CannotChangeField("Region")
}
} else {
if e.Name == nil {
return fi.RequiredField("Name")
}
if e.SizeGB == nil {
return fi.RequiredField("SizeGB")
}
if e.Region == nil {
return fi.RequiredField("Region")
}
}
return nil
}
func (_ *Volume) RenderDO(t *do.DOAPITarget, a, e, changes *Volume) error {
if a != nil {
// in general, we shouldn't need to render changes to a volume
// however there can be cases where we may want to resize or rename.
// consider this in later stages of DO support on kops
return nil
}
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),
})
return err
}
// terraformVolume represents the digitalocean_volume resource in terraform
// https://www.terraform.io/docs/providers/do/r/volume.html
type terraformVolume struct {
Name *string `json:"name"`
SizeGB *int64 `json:"size"`
Region *string `json:"region"`
}
func (_ *Volume) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *Volume) error {
tf := &terraformVolume{
Name: e.Name,
SizeGB: e.SizeGB,
Region: e.Region,
}
return t.RenderResource("digitalocean_volume", *e.Name, tf)
}

View File

@ -0,0 +1,70 @@
/*
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.
*/
// Code generated by ""fitask" -type=Volume"; DO NOT EDIT
package dotasks
import (
"encoding/json"
"k8s.io/kops/upup/pkg/fi"
)
// Volume
// JSON marshalling boilerplate
type realVolume Volume
// UnmarshalJSON implements conversion to JSON, supporitng an alternate specification of the object as a string
func (o *Volume) UnmarshalJSON(data []byte) error {
var jsonName string
if err := json.Unmarshal(data, &jsonName); err == nil {
o.Name = &jsonName
return nil
}
var r realVolume
if err := json.Unmarshal(data, &r); err != nil {
return err
}
*o = Volume(r)
return nil
}
var _ fi.HasLifecycle = &Volume{}
// GetLifecycle returns the Lifecycle of the object, implementing fi.HasLifecycle
func (o *Volume) GetLifecycle() *fi.Lifecycle {
return o.Lifecycle
}
var _ fi.HasName = &Volume{}
// GetName returns the Name of the object, implementing fi.HasName
func (o *Volume) GetName() *string {
return o.Name
}
// SetName sets the Name of the object, implementing fi.SetName
func (o *Volume) SetName(name string) {
o.Name = &name
}
// String is the stringer function for the task, producing readable output using fi.TaskAsString
func (o *Volume) String() string {
return fi.TaskAsString(o)
}

View File

@ -0,0 +1,177 @@
/*
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 dotasks
import (
"errors"
"reflect"
"testing"
"github.com/digitalocean/godo"
"github.com/digitalocean/godo/context"
"k8s.io/kops/pkg/resources/digitalocean"
"k8s.io/kops/upup/pkg/fi"
)
type fakeStorageClient struct {
listFn func(context.Context, *godo.ListVolumeParams) ([]godo.Volume, *godo.Response, error)
getFn func(context.Context, string) (*godo.Volume, *godo.Response, error)
createFn func(context.Context, *godo.VolumeCreateRequest) (*godo.Volume, *godo.Response, error)
deleteFn func(context.Context, string) (*godo.Response, error)
listSnapshotFn func(ctx context.Context, volumeID string, opts *godo.ListOptions) ([]godo.Snapshot, *godo.Response, error)
getSnapshotFn func(context.Context, string) (*godo.Snapshot, *godo.Response, error)
createSnapshotFn func(context.Context, *godo.SnapshotCreateRequest) (*godo.Snapshot, *godo.Response, error)
deleteSnapshotFn func(context.Context, string) (*godo.Response, error)
}
func (f fakeStorageClient) ListVolumes(ctx context.Context, listOpts *godo.ListVolumeParams) ([]godo.Volume, *godo.Response, error) {
return f.listFn(ctx, listOpts)
}
func (f fakeStorageClient) GetVolume(ctx context.Context, id string) (*godo.Volume, *godo.Response, error) {
return f.getFn(ctx, id)
}
func (f fakeStorageClient) CreateVolume(ctx context.Context, req *godo.VolumeCreateRequest) (*godo.Volume, *godo.Response, error) {
return f.createFn(ctx, req)
}
func (f fakeStorageClient) DeleteVolume(ctx context.Context, id string) (*godo.Response, error) {
return f.deleteFn(ctx, id)
}
func (f fakeStorageClient) ListSnapshots(ctx context.Context, volumeID string, opts *godo.ListOptions) ([]godo.Snapshot, *godo.Response, error) {
return f.listSnapshotFn(ctx, volumeID, opts)
}
func (f fakeStorageClient) GetSnapshot(ctx context.Context, id string) (*godo.Snapshot, *godo.Response, error) {
return f.getSnapshotFn(ctx, id)
}
func (f fakeStorageClient) CreateSnapshot(ctx context.Context, req *godo.SnapshotCreateRequest) (*godo.Snapshot, *godo.Response, error) {
return f.createSnapshotFn(ctx, req)
}
func (f fakeStorageClient) DeleteSnapshot(ctx context.Context, id string) (*godo.Response, error) {
return f.deleteSnapshotFn(ctx, id)
}
func newCloud(client *godo.Client) *digitalocean.Cloud {
return &digitalocean.Cloud{
Client: client,
Region: "nyc1",
}
}
func newContext(cloud fi.Cloud) *fi.Context {
return &fi.Context{
Cloud: cloud,
}
}
func Test_Find(t *testing.T) {
testcases := []struct {
name string
storage fakeStorageClient
inVolume *Volume
outVolume *Volume
err error
}{
{
"successfully found volume",
fakeStorageClient{
listFn: func(context.Context, *godo.ListVolumeParams) (
[]godo.Volume, *godo.Response, error) {
return []godo.Volume{
{
Name: "test0",
ID: "100",
SizeGigaBytes: int64(100),
Region: &godo.Region{Slug: "nyc1"},
},
}, nil, nil
},
},
&Volume{
Name: fi.String("test0"),
SizeGB: fi.Int64(int64(100)),
Region: fi.String("nyc1"),
},
&Volume{
Name: fi.String("test0"),
ID: fi.String("100"),
SizeGB: fi.Int64(int64(100)),
Region: fi.String("nyc1"),
},
nil,
},
{
"no volume found",
fakeStorageClient{
listFn: func(context.Context, *godo.ListVolumeParams) (
[]godo.Volume, *godo.Response, error) {
return []godo.Volume{}, nil, nil
},
},
&Volume{
Name: fi.String("test1"),
SizeGB: fi.Int64(int64(100)),
Region: fi.String("nyc1"),
},
nil,
nil,
},
{
"error from server",
fakeStorageClient{
listFn: func(context.Context, *godo.ListVolumeParams) (
[]godo.Volume, *godo.Response, error) {
return []godo.Volume{}, nil, errors.New("error!")
},
},
&Volume{
Name: fi.String("test1"),
SizeGB: fi.Int64(int64(100)),
Region: fi.String("nyc1"),
},
nil,
errors.New("error!"),
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
cloud := newCloud(godo.NewClient(nil))
cloud.Client.Storage = tc.storage
ctx := newContext(cloud)
actualVolume, err := tc.inVolume.Find(ctx)
if !reflect.DeepEqual(actualVolume, tc.outVolume) {
t.Error("unexpected volume")
t.Logf("actual volume: %v", actualVolume)
t.Logf("expected volume: %v", tc.outVolume)
}
if !reflect.DeepEqual(err, tc.err) {
t.Error("unexpected error")
t.Logf("actual err: %v", err)
t.Logf("expected err: %v", tc.err)
}
})
}
}

View File

@ -22,6 +22,7 @@ import (
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/do"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/vsphere"
"k8s.io/kubernetes/federation/pkg/dnsprovider"
@ -108,7 +109,16 @@ func BuildCloud(cluster *kops.Cluster) (fi.Cloud, error) {
}
case "digitalocean":
{
return nil, fmt.Errorf("digitalocean not supported yet!")
// for development purposes we're going to assume
// single region setups for DO. Reconsider this logic
// when setting up multi-region kubernetes clusters on DO
region := cluster.Spec.Subnets[0].Zone
doCloud, err := do.NewDOCloud(region)
if err != nil {
return nil, fmt.Errorf("error initializin digitalocean cloud!")
}
cloud = doCloud
}
default: