forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotinst.go
98 lines (80 loc) · 2.82 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
/*
Copyright 2017 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/golang/glog"
"github.com/spotinst/spotinst-sdk-go/service/elastigroup"
"github.com/spotinst/spotinst-sdk-go/service/elastigroup/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"
kopsv "k8s.io/kops"
"k8s.io/kops/pkg/apis/kops"
)
// NewService returns a Service interface for the specified cloud provider.
func NewService(cloudProviderID kops.CloudProviderID) (Service, error) {
svc := elastigroup.New(session.New(NewConfig()))
switch cloudProviderID {
case kops.CloudProviderAWS:
return &awsService{svc.CloudProviderAWS()}, nil
default:
return nil, fmt.Errorf("spotinst: unsupported cloud provider: %s", cloudProviderID)
}
}
// 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{}) {
glog.V(2).Infof(format, args...)
})
}
// NewElastigroup returns an Elastigroup wrapper for the specified cloud provider.
func NewElastigroup(cloudProviderID kops.CloudProviderID,
obj interface{}) (Elastigroup, error) {
switch cloudProviderID {
case kops.CloudProviderAWS:
return &awsElastigroup{obj.(*aws.Group)}, 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
}