Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
7644c6af
Commit
7644c6af
authored
Jun 28, 2017
by
Brendan Burns
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add initial support for the Azure instance metadata service.
parent
8bece8ec
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
178 additions
and
0 deletions
+178
-0
BUILD
pkg/cloudprovider/providers/azure/BUILD
+1
-0
azure.go
pkg/cloudprovider/providers/azure/azure.go
+3
-0
azure_instance_metadata.go
pkg/cloudprovider/providers/azure/azure_instance_metadata.go
+103
-0
azure_instances.go
pkg/cloudprovider/providers/azure/azure_instances.go
+10
-0
azure_test.go
pkg/cloudprovider/providers/azure/azure_test.go
+61
-0
No files found.
pkg/cloudprovider/providers/azure/BUILD
View file @
7644c6af
...
...
@@ -15,6 +15,7 @@ go_library(
"azure_backoff.go",
"azure_blob.go",
"azure_file.go",
"azure_instance_metadata.go",
"azure_instances.go",
"azure_loadbalancer.go",
"azure_routes.go",
...
...
pkg/cloudprovider/providers/azure/azure.go
View file @
7644c6af
...
...
@@ -96,6 +96,9 @@ type Config struct {
CloudProviderRateLimitQPS
float32
`json:"cloudProviderRateLimitQPS" yaml:"cloudProviderRateLimitQPS"`
// Rate limit Bucket Size
CloudProviderRateLimitBucket
int
`json:"cloudProviderRateLimitBucket" yaml:"cloudProviderRateLimitBucket"`
// Use instance metadata service where possible
UseInstanceMetadata
bool
`json:"useInstanceMetadata" yaml:"useInstanceMetadata"`
}
// Cloud holds the config and clients
...
...
pkg/cloudprovider/providers/azure/azure_instance_metadata.go
0 → 100644
View file @
7644c6af
/*
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
azure
import
(
"encoding/json"
"io/ioutil"
"net/http"
)
// This is just for tests injection
var
metadataURL
=
"http://169.254.169.254/metadata"
// SetMetadataURLForTesting is used to modify the URL used for
// accessing the metadata server. Should only be used for testing!
func
SetMetadataURLForTesting
(
url
string
)
{
metadataURL
=
url
}
// NetworkMetadata contains metadata about an instance's network
type
NetworkMetadata
struct
{
Interface
[]
NetworkInterface
`json:"interface"`
}
// NetworkInterface represents an instances network interface.
type
NetworkInterface
struct
{
IPV4
NetworkData
`json:"ipv4"`
IPV6
NetworkData
`json:"ipv6"`
MAC
string
`json:"macAddress"`
}
// NetworkData contains IP information for a network.
type
NetworkData
struct
{
IPAddress
[]
IPAddress
`json:"ipAddress"`
Subnet
[]
Subnet
`json:"subnet"`
}
// IPAddress represents IP address information.
type
IPAddress
struct
{
PrivateIP
string
`json:"privateIPAddress"`
PublicIP
string
`json:"publicIPAddress"`
}
// Subnet represents subnet information.
type
Subnet
struct
{
Address
string
`json:"address"`
Prefix
string
`json:"prefix"`
}
// QueryMetadataJSON queries the metadata server and populates the passed in object
func
QueryMetadataJSON
(
path
string
,
obj
interface
{})
error
{
data
,
err
:=
queryMetadataBytes
(
path
,
"json"
)
if
err
!=
nil
{
return
err
}
return
json
.
Unmarshal
(
data
,
obj
)
}
// QueryMetadataText queries the metadata server and returns the corresponding text
func
QueryMetadataText
(
path
string
)
(
string
,
error
)
{
data
,
err
:=
queryMetadataBytes
(
path
,
"text"
)
if
err
!=
nil
{
return
""
,
err
}
return
string
(
data
),
err
}
func
queryMetadataBytes
(
path
,
format
string
)
([]
byte
,
error
)
{
client
:=
&
http
.
Client
{}
req
,
err
:=
http
.
NewRequest
(
"GET"
,
metadataURL
+
path
,
nil
)
if
err
!=
nil
{
return
nil
,
err
}
req
.
Header
.
Add
(
"Metadata"
,
"True"
)
q
:=
req
.
URL
.
Query
()
q
.
Add
(
"format"
,
format
)
q
.
Add
(
"api-version"
,
"2017-04-02"
)
req
.
URL
.
RawQuery
=
q
.
Encode
()
resp
,
err
:=
client
.
Do
(
req
)
if
err
!=
nil
{
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
return
ioutil
.
ReadAll
(
resp
.
Body
)
}
pkg/cloudprovider/providers/azure/azure_instances.go
View file @
7644c6af
...
...
@@ -29,6 +29,16 @@ import (
// NodeAddresses returns the addresses of the specified instance.
func
(
az
*
Cloud
)
NodeAddresses
(
name
types
.
NodeName
)
([]
v1
.
NodeAddress
,
error
)
{
if
az
.
UseInstanceMetadata
{
text
,
err
:=
QueryMetadataText
(
"instance/network/interface/0/ipv4/ipAddress/0/privateIpAddress"
)
if
err
!=
nil
{
return
nil
,
err
}
return
[]
v1
.
NodeAddress
{
{
Type
:
v1
.
NodeInternalIP
,
Address
:
text
},
{
Type
:
v1
.
NodeHostName
,
Address
:
string
(
name
)},
},
nil
}
ip
,
err
:=
az
.
getIPForMachine
(
name
)
if
err
!=
nil
{
glog
.
Errorf
(
"error: az.NodeAddresses, az.getIPForMachine(%s), err=%v"
,
name
,
err
)
...
...
pkg/cloudprovider/providers/azure/azure_test.go
View file @
7644c6af
...
...
@@ -17,7 +17,11 @@ limitations under the License.
package
azure
import
(
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
...
...
@@ -801,3 +805,60 @@ func TestSplitProviderID(t *testing.T) {
}
}
func
TestMetadataParsing
(
t
*
testing
.
T
)
{
data
:=
`
{
"interface": [
{
"ipv4": {
"ipAddress": [
{
"privateIpAddress": "10.0.1.4",
"publicIpAddress": "X.X.X.X"
}
],
"subnet": [
{
"address": "10.0.1.0",
"prefix": "24"
}
]
},
"ipv6": {
"ipAddress": [
]
},
"macAddress": "002248020E1E"
}
]
}
`
network
:=
NetworkMetadata
{}
if
err
:=
json
.
Unmarshal
([]
byte
(
data
),
&
network
);
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
ip
:=
network
.
Interface
[
0
]
.
IPV4
.
IPAddress
[
0
]
.
PrivateIP
if
ip
!=
"10.0.1.4"
{
t
.
Errorf
(
"Unexpected value: %s, expected 10.0.1.4"
,
ip
)
}
server
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprintln
(
w
,
data
)
}))
defer
server
.
Close
()
SetMetadataURLForTesting
(
server
.
URL
)
networkJSON
:=
NetworkMetadata
{}
if
err
:=
QueryMetadataJSON
(
"/some/path"
,
&
networkJSON
);
err
!=
nil
{
t
.
Errorf
(
"Unexpected error: %v"
,
err
)
}
if
!
reflect
.
DeepEqual
(
network
,
networkJSON
)
{
t
.
Errorf
(
"Unexpected inequality:
\n
%#v
\n
vs
\n
%#v"
,
network
,
networkJSON
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment