Skip to content

Commit ccc5ee3

Browse files
committed
instances: initial implementation of Instances interface
1 parent 35aeabd commit ccc5ee3

File tree

6 files changed

+869
-7
lines changed

6 files changed

+869
-7
lines changed

Diff for: go.mod

+6
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ replace (
2828

2929
require (
3030
github.com/aws/aws-sdk-go v1.28.2
31+
github.com/golang/mock v1.3.1
32+
github.com/google/go-cmp v0.4.0
33+
github.com/google/uuid v1.1.1
3134
github.com/spf13/cobra v1.0.0
3235
github.com/spf13/pflag v1.0.5
3336
github.com/stretchr/testify v1.4.0
37+
gopkg.in/gcfg.v1 v1.2.0
38+
k8s.io/api v0.0.0
3439
k8s.io/apimachinery v0.0.0
3540
k8s.io/apiserver v0.0.0
3641
k8s.io/cloud-provider v0.0.0
3742
k8s.io/component-base v0.0.0
3843
k8s.io/klog v1.0.0
44+
k8s.io/klog/v2 v2.2.0
3945
k8s.io/kubernetes v1.19.1
4046
k8s.io/legacy-cloud-providers v0.0.0
4147
k8s.io/utils v0.0.0-20200729134348-d5654de09c73

Diff for: go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 h1:5ZkaAPbicIKTF
262262
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
263263
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
264264
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
265+
github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s=
265266
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
267+
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
266268
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
267269
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
268270
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
@@ -887,6 +889,7 @@ k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6 h1:+WnxoVtG8TMiudHBSEtrVL
887889
k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6/go.mod h1:UuqjUnNftUyPE5H64/qeyjQoUZhGpeFDVdxjTeEVN2o=
888890
k8s.io/kubernetes v1.19.1 h1:3Gdl9EtBiV3SYuzml1915nFLVxlx08L6bRz1b07C60k=
889891
k8s.io/kubernetes v1.19.1/go.mod h1:yhT1/ltQajQsha3tnYc9QPFYSumGM45nlZdjf7WqE1A=
892+
k8s.io/kubernetes v1.19.2 h1:sEvBYVM1/H5hqejFR10u8ndreYARV3DiTrqi2AY31ok=
890893
k8s.io/kubernetes/staging/src/k8s.io/api v0.0.0-20200909111720-206bcadf021e h1:mdkfNp9Iu1erEZy/9kefsctTTyey3aAMcCQ5VTzScIE=
891894
k8s.io/kubernetes/staging/src/k8s.io/api v0.0.0-20200909111720-206bcadf021e/go.mod h1:Y4VjjNur38HL6/QxaTVK2yno1zjEQlvcvwbbRQs2DtQ=
892895
k8s.io/kubernetes/staging/src/k8s.io/apiextensions-apiserver v0.0.0-20200909111720-206bcadf021e/go.mod h1:BvtZU215FgO19Oy19K6h8qwajFfjxYqGewgjuYHWGRw=

Diff for: pkg/providers/v2/cloud.go

+35-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
2929
"github.com/aws/aws-sdk-go/aws/ec2metadata"
3030
"github.com/aws/aws-sdk-go/aws/session"
31+
"github.com/aws/aws-sdk-go/service/ec2"
32+
3133
cloudprovider "k8s.io/cloud-provider"
3234
)
3335

@@ -46,8 +48,11 @@ var _ cloudprovider.Interface = (*cloud)(nil)
4648

4749
// cloud is the AWS v2 implementation of the cloud provider interface
4850
type cloud struct {
49-
creds *credentials.Credentials
50-
region string
51+
creds *credentials.Credentials
52+
instances cloudprovider.InstancesV2
53+
region string
54+
ec2 EC2
55+
metadata EC2Metadata
5156
}
5257

5358
// EC2Metadata is an abstraction over the AWS metadata service.
@@ -107,10 +112,33 @@ func newCloud() (cloudprovider.Interface, error) {
107112
return nil, err
108113
}
109114

110-
return &cloud{
111-
creds: creds,
112-
region: region,
113-
}, nil
115+
instances, err := newInstances(az, creds)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
ec2Sess, err := session.NewSession(&aws.Config{
121+
Region: aws.String(region),
122+
Credentials: creds,
123+
})
124+
if err != nil {
125+
return nil, fmt.Errorf("unable to initialize AWS session: %v", err)
126+
}
127+
128+
ec2Service := ec2.New(ec2Sess)
129+
if err != nil {
130+
return nil, fmt.Errorf("error creating AWS ec2 client: %q", err)
131+
}
132+
133+
awsCloud := &cloud{
134+
creds: creds,
135+
instances: instances,
136+
region: region,
137+
metadata: metadataClient,
138+
ec2: ec2Service,
139+
}
140+
141+
return awsCloud, nil
114142
}
115143

116144
// Initialize passes a Kubernetes clientBuilder interface to the cloud provider
@@ -158,5 +186,5 @@ func (c *cloud) HasClusterID() bool {
158186
// Also returns true if the interface is supported, false otherwise.
159187
// WARNING: InstancesV2 is an experimental interface and is subject to change in v1.20.
160188
func (c *cloud) InstancesV2() (cloudprovider.InstancesV2, bool) {
161-
return nil, false
189+
return c.instances, true
162190
}

0 commit comments

Comments
 (0)