Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 1564943

Browse files
kaufersDavid Chung
authored andcommitted
New ibmcloud type and volume auth instance plugin (#704)
Signed-off-by: Steven Kaufer <[email protected]>
1 parent 832f2f7 commit 1564943

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+85404
-14
lines changed

cmd/infrakit/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
_ "github.com/docker/infrakit/pkg/run/v0/file"
4747
_ "github.com/docker/infrakit/pkg/run/v0/group"
4848
_ "github.com/docker/infrakit/pkg/run/v0/hyperkit"
49+
_ "github.com/docker/infrakit/pkg/run/v0/ibmcloud"
4950
_ "github.com/docker/infrakit/pkg/run/v0/ingress"
5051
_ "github.com/docker/infrakit/pkg/run/v0/kubernetes"
5152
_ "github.com/docker/infrakit/pkg/run/v0/manager"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package client
2+
3+
import (
4+
"github.com/softlayer/softlayer-go/services"
5+
"github.com/softlayer/softlayer-go/session"
6+
)
7+
8+
// SoftlayerClient for all SL API calls
9+
type SoftlayerClient struct {
10+
sess *session.Session
11+
account services.Account
12+
}
13+
14+
// GetClient returns a SoftlayerClient instance
15+
func GetClient(user, apiKey string) *SoftlayerClient {
16+
client := &SoftlayerClient{
17+
sess: session.New(user, apiKey),
18+
}
19+
client.account = services.GetAccountService(client.sess)
20+
return client
21+
}
22+
23+
// AuthorizeToStorage authorizes a VM to a storage volume
24+
func (c *SoftlayerClient) AuthorizeToStorage(storageID, guestID int) error {
25+
resType := "SoftLayer_Virtual_Guest"
26+
_, err := services.GetNetworkStorageService(c.sess).Id(storageID).AllowAccessFromHost(&resType, &guestID)
27+
return err
28+
}
29+
30+
// DeauthorizeFromStorage removes the VM authorization for a storage volume
31+
func (c *SoftlayerClient) DeauthorizeFromStorage(storageID, guestID int) error {
32+
resType := "SoftLayer_Virtual_Guest"
33+
_, err := services.GetNetworkStorageService(c.sess).Id(storageID).RemoveAccessFromHost(&resType, &guestID)
34+
return err
35+
}
36+
37+
// GetAllowedStorageVirtualGuests gets all VM IDs that are authorized to the storage volume
38+
func (c *SoftlayerClient) GetAllowedStorageVirtualGuests(storageID int) ([]int, error) {
39+
resp, err := services.GetNetworkStorageService(c.sess).Id(storageID).GetAllowedVirtualGuests()
40+
if err != nil {
41+
return []int{}, err
42+
}
43+
result := []int{}
44+
for _, r := range resp {
45+
result = append(result, *r.Id)
46+
}
47+
return result, nil
48+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package instance
2+
3+
import (
4+
"strconv"
5+
6+
log "github.com/Sirupsen/logrus"
7+
"github.com/docker/infrakit/pkg/provider/ibmcloud/client"
8+
"github.com/docker/infrakit/pkg/spi/instance"
9+
"github.com/docker/infrakit/pkg/types"
10+
)
11+
12+
type plugin struct {
13+
SoftlayerClient *client.SoftlayerClient
14+
VolumeID int
15+
}
16+
17+
type propertyID struct {
18+
// Resource matches the resource structure of the tf.json resource section
19+
ID string `json:"id"`
20+
}
21+
22+
// NewVolumeAuthPlugin creates a new plugin that manages VM authorizations to the given volume
23+
func NewVolumeAuthPlugin(username, apiKey string, volumeID int) instance.Plugin {
24+
log.Infof("NewVolumeAuthPlugin, volumeID: %v", volumeID)
25+
return &plugin{
26+
SoftlayerClient: client.GetClient(username, apiKey),
27+
VolumeID: volumeID,
28+
}
29+
}
30+
31+
func (p *plugin) Validate(req *types.Any) error {
32+
return nil
33+
}
34+
35+
func (p *plugin) Label(instance instance.ID, labels map[string]string) error {
36+
return nil
37+
}
38+
39+
func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
40+
props := propertyID{}
41+
err := spec.Properties.Decode(&props)
42+
if err != nil {
43+
return nil, err
44+
}
45+
if err != nil {
46+
return nil, err
47+
}
48+
log.Infof("Authorizing volume %v to instance %v", p.VolumeID, props.ID)
49+
vmID, err := strconv.Atoi(props.ID)
50+
if err != nil {
51+
return nil, err
52+
}
53+
err = p.SoftlayerClient.AuthorizeToStorage(p.VolumeID, vmID)
54+
return nil, err
55+
}
56+
57+
func (p *plugin) Destroy(id instance.ID, ctx instance.Context) error {
58+
log.Infof("Deauthorizing volume %v from instance %v", p.VolumeID, string(id))
59+
vmID, err := strconv.Atoi(string(id))
60+
if err != nil {
61+
return nil
62+
}
63+
return p.SoftlayerClient.DeauthorizeFromStorage(p.VolumeID, vmID)
64+
}
65+
66+
func (p *plugin) DescribeInstances(tags map[string]string, properties bool) ([]instance.Description, error) {
67+
log.Infof("Describing authorized VMs for volume %v with tags %v", p.VolumeID, tags)
68+
vmIDs, err := p.SoftlayerClient.GetAllowedStorageVirtualGuests(p.VolumeID)
69+
if err != nil {
70+
return []instance.Description{}, nil
71+
}
72+
result := []instance.Description{}
73+
for _, vmID := range vmIDs {
74+
result = append(result,
75+
instance.Description{
76+
ID: instance.ID(strconv.Itoa(vmID)),
77+
},
78+
)
79+
}
80+
log.Infof("%v authorized VMs for volume %v: %v", len(result), p.VolumeID, result)
81+
return result, nil
82+
}

pkg/run/v0/ibmcloud/ibmcloud.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ibmcloud
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/docker/infrakit/pkg/discovery"
7+
"github.com/docker/infrakit/pkg/launch/inproc"
8+
logutil "github.com/docker/infrakit/pkg/log"
9+
"github.com/docker/infrakit/pkg/plugin"
10+
ibmcloud_auth_inst "github.com/docker/infrakit/pkg/provider/ibmcloud/plugin/instance"
11+
"github.com/docker/infrakit/pkg/run"
12+
"github.com/docker/infrakit/pkg/run/local"
13+
"github.com/docker/infrakit/pkg/spi/instance"
14+
"github.com/docker/infrakit/pkg/types"
15+
)
16+
17+
const (
18+
// Kind is the canonical name of the plugin for starting up, etc.
19+
Kind = "ibmcloud"
20+
21+
// EnvIBMCloudUsername is the name of the LB ENV variable name for the IBM Cloud Username.
22+
EnvIBMCloudUsername = "INFRAKIT_IBMCLOUD_USERNAME"
23+
24+
// EnvIBMCloudAPIKey is the name of the LB ENV variable name for the IBM Cloud API Key.
25+
EnvIBMCloudAPIKey = "INFRAKIT_IBMCLOUD_APIKEY"
26+
)
27+
28+
var (
29+
log = logutil.New("module", "run/v0/ibmcloud")
30+
)
31+
32+
func init() {
33+
inproc.Register(Kind, Run, DefaultOptions)
34+
}
35+
36+
// VolumeAuth is the type that contains the volume information to authorize
37+
type VolumeAuth struct {
38+
// VolumeID is the volume to authorize to the group members
39+
VolumeID int
40+
}
41+
42+
// Options capture the options for starting up the plugin.
43+
type Options struct {
44+
Username string
45+
APIKey string
46+
VolumeAuth VolumeAuth
47+
}
48+
49+
// DefaultOptions return an Options with default values filled in.
50+
var DefaultOptions = Options{
51+
Username: local.Getenv(EnvIBMCloudUsername, ""),
52+
APIKey: local.Getenv(EnvIBMCloudAPIKey, ""),
53+
VolumeAuth: VolumeAuth{
54+
VolumeID: 0,
55+
},
56+
}
57+
58+
// Run runs the plugin, blocking the current thread. Error is returned immediately
59+
// if the plugin cannot be started.
60+
func Run(plugins func() discovery.Plugins, name plugin.Name,
61+
config *types.Any) (transport plugin.Transport, impls map[run.PluginCode]interface{}, onStop func(), err error) {
62+
log.Debug("Run", "Name", name)
63+
64+
options := Options{}
65+
err = config.Decode(&options)
66+
if err != nil {
67+
return
68+
}
69+
if options.Username == "" || options.APIKey == "" {
70+
err = fmt.Errorf("IBM Cloud username and APIKey required")
71+
return
72+
}
73+
74+
var authInstPlugin instance.Plugin
75+
if options.VolumeAuth.VolumeID != 0 {
76+
authInstPlugin = ibmcloud_auth_inst.NewVolumeAuthPlugin(options.Username, options.APIKey, options.VolumeAuth.VolumeID)
77+
}
78+
79+
transport.Name = name
80+
impls = map[run.PluginCode]interface{}{}
81+
if authInstPlugin != nil {
82+
impls[run.Instance] = map[string]instance.Plugin{"instance-vol-auth": authInstPlugin}
83+
}
84+
85+
return
86+
}

vendor.conf

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ github.com/go-openapi/jsonreference 36d33bf
5252
github.com/go-openapi/loads ad6217c
5353
github.com/go-openapi/runtime bf2ff8f
5454
github.com/go-openapi/spec e51c28f
55-
github.com/go-openapi/validate dc8a684
55+
github.com/go-openapi/validate dc8a684
5656
github.com/go-openapi/strfmt 93a31ef
5757
github.com/go-openapi/swag 24ebf76
5858
github.com/gogo/protobuf v0.4
@@ -104,9 +104,11 @@ github.com/prometheus/client_golang/prometheus v0.8.0
104104
github.com/prometheus/client_model 6f38060
105105
github.com/prometheus/common 49fee29
106106
github.com/prometheus/procfs a1dba9c
107+
github.com/renier/xmlrpc ce4a1a4
107108
github.com/rneugeba/iso9660wrap ecec696
108109
github.com/satori/go.uuid v1.1.0
109110
github.com/sergi/go-diff/diffmatchpatch feef008
111+
github.com/softlayer/softlayer-go ba0eaed
110112
github.com/spiegela/gorackhd de12976d778d18841ab3b16899af215fb277e96b
111113
github.com/spf13/afero 06b7e5f
112114
github.com/spf13/cobra 6e91dde
@@ -152,16 +154,17 @@ k8s.io/apimachinery release-1.7
152154
k8s.io/client-go v4.0.0
153155
k8s.io/kubernetes/cmd/kubeadm v1.7.5
154156
k8s.io/kubernetes/pkg v1.7.5
155-
cloud.google.com/go v0.6.0-81-g5e7b3ea
156-
github.com/aokoli/goutils 1.0.0-1-g9c37978
157-
github.com/golang/protobuf 69b215d
158-
github.com/googleapis/gax-go da06d19
159-
github.com/gorilla/context v1.1-7-g08b5f42
160-
golang.org/x/crypto 453249f
161-
golang.org/x/oauth2 810daf0
162-
google.golang.org/api 48e49d16
163-
google.golang.org/appengine v1.0.0-29-g3a452f9
164-
github.com/krolaw/dhcp4 4de04cc
165-
github.com/krolaw/dhcp4/conn 4de04cc
166-
github.com/thebsdbox/go-tftp/server b3e1ec2
167-
github.com/whyrusleeping/go-tftp/packet 454ade9
157+
cloud.google.com/go v0.6.0-81-g5e7b3ea
158+
github.com/aokoli/goutils 1.0.0-1-g9c37978
159+
github.com/golang/protobuf 69b215d
160+
github.com/googleapis/gax-go da06d19
161+
github.com/gorilla/context v1.1-7-g08b5f42
162+
golang.org/x/crypto 453249f
163+
golang.org/x/oauth2 810daf0
164+
golang.org/x/text e113a52
165+
google.golang.org/api 48e49d16
166+
google.golang.org/appengine v1.0.0-29-g3a452f9
167+
github.com/krolaw/dhcp4 4de04cc
168+
github.com/krolaw/dhcp4/conn 4de04cc
169+
github.com/thebsdbox/go-tftp/server b3e1ec2
170+
github.com/whyrusleeping/go-tftp/packet 454ade9

vendor/github.com/renier/xmlrpc/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/renier/xmlrpc/LICENSE

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/renier/xmlrpc/README.md

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)