-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
spotinst.go
150 lines (126 loc) · 4.36 KB
/
spotinst.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Copyright 2019 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 spotinst
import (
"fmt"
"github.com/spotinst/spotinst-sdk-go/service/elastigroup"
awseg "github.com/spotinst/spotinst-sdk-go/service/elastigroup/providers/aws"
"github.com/spotinst/spotinst-sdk-go/service/ocean"
awsoc "github.com/spotinst/spotinst-sdk-go/service/ocean/providers/aws"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/credentials"
"github.com/spotinst/spotinst-sdk-go/spotinst/log"
"github.com/spotinst/spotinst-sdk-go/spotinst/session"
"k8s.io/klog"
kopsv "k8s.io/kops"
"k8s.io/kops/pkg/apis/kops"
)
// NewCloud returns a Cloud interface for the specified cloud provider.
func NewCloud(cloudProviderID kops.CloudProviderID) (Cloud, error) {
var (
cloud Cloud
sess = session.New(NewConfig())
eg = elastigroup.New(sess)
oc = ocean.New(sess)
)
switch cloudProviderID {
case kops.CloudProviderAWS:
cloud = &awsCloud{
eg: &awsElastigroupService{eg.CloudProviderAWS()},
oc: &awsOceanService{oc.CloudProviderAWS()},
ls: &awsOceanLaunchSpecService{oc.CloudProviderAWS()},
}
default:
return nil, fmt.Errorf("spotinst: unsupported cloud provider: %s", cloudProviderID)
}
return cloud, nil
}
// NewConfig returns a new configuration object.
func NewConfig() *spotinst.Config {
config := spotinst.DefaultConfig()
config.WithCredentials(NewCredentials())
config.WithLogger(NewStdLogger())
config.WithUserAgent("kubernetes-kops/" + kopsv.Version)
return config
}
// NewCredentials returns a new chain-credentials object.
func NewCredentials() *credentials.Credentials {
return credentials.NewChainCredentials(
new(credentials.EnvProvider),
new(credentials.FileProvider),
)
}
// NewStdLogger returns a new Logger.
func NewStdLogger() log.Logger {
return log.LoggerFunc(func(format string, args ...interface{}) {
klog.V(2).Infof(format, args...)
})
}
// NewInstanceGroups returns an InstanceGroup wrapper for the specified cloud provider.
func NewInstanceGroup(cloudProviderID kops.CloudProviderID,
instanceGroupType InstanceGroupType, obj interface{}) (InstanceGroup, error) {
switch cloudProviderID {
case kops.CloudProviderAWS:
{
switch instanceGroupType {
case InstanceGroupElastigroup:
return &awsElastigroupInstanceGroup{obj.(*awseg.Group)}, nil
case InstanceGroupOcean:
return &awsOceanInstanceGroup{obj.(*awsoc.Cluster)}, nil
default:
return nil, fmt.Errorf("spotinst: unsupported instance group type: %s", instanceGroupType)
}
}
default:
return nil, fmt.Errorf("spotinst: unsupported cloud provider: %s", cloudProviderID)
}
}
// NewElastigroup returns an Elastigroup wrapper for the specified cloud provider.
func NewElastigroup(cloudProviderID kops.CloudProviderID,
obj interface{}) (InstanceGroup, error) {
return NewInstanceGroup(
cloudProviderID,
InstanceGroupElastigroup,
obj)
}
// NewOcean returns an Ocean wrapper for the specified cloud provider.
func NewOcean(cloudProviderID kops.CloudProviderID,
obj interface{}) (InstanceGroup, error) {
return NewInstanceGroup(
cloudProviderID,
InstanceGroupOcean,
obj)
}
// NewLaunchSpec returns a LaunchSpec wrapper for the specified cloud provider.
func NewLaunchSpec(cloudProviderID kops.CloudProviderID, obj interface{}) (LaunchSpec, error) {
switch cloudProviderID {
case kops.CloudProviderAWS:
return &awsOceanLaunchSpec{obj.(*awsoc.LaunchSpec)}, nil
default:
return nil, fmt.Errorf("spotinst: unsupported cloud provider: %s", cloudProviderID)
}
}
// LoadCredentials attempts to load credentials using the default chain.
func LoadCredentials() (credentials.Value, error) {
var (
chain = NewCredentials()
creds credentials.Value
err error
)
// Attempt to load the credentials.
creds, err = chain.Get()
if err != nil {
return creds, fmt.Errorf("spotinst: unable to load credentials: %v", err)
}
return creds, nil
}