mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Add interface clients based on armclient
This commit is contained in:
parent
f88d3524d5
commit
dba3331ce4
@ -0,0 +1,57 @@
|
|||||||
|
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||||
|
|
||||||
|
go_library(
|
||||||
|
name = "go_default_library",
|
||||||
|
srcs = [
|
||||||
|
"azure_interfaceclient.go",
|
||||||
|
"doc.go",
|
||||||
|
"interface.go",
|
||||||
|
],
|
||||||
|
importmap = "k8s.io/kubernetes/vendor/k8s.io/legacy-cloud-providers/azure/clients/interfaceclient",
|
||||||
|
importpath = "k8s.io/legacy-cloud-providers/azure/clients/interfaceclient",
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
deps = [
|
||||||
|
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/clients:go_default_library",
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/clients/armclient:go_default_library",
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/metrics:go_default_library",
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/retry:go_default_library",
|
||||||
|
"//vendor/github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network:go_default_library",
|
||||||
|
"//vendor/github.com/Azure/go-autorest/autorest:go_default_library",
|
||||||
|
"//vendor/github.com/Azure/go-autorest/autorest/azure:go_default_library",
|
||||||
|
"//vendor/k8s.io/klog:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
go_test(
|
||||||
|
name = "go_default_test",
|
||||||
|
srcs = ["azure_interfaceclient_test.go"],
|
||||||
|
embed = [":go_default_library"],
|
||||||
|
deps = [
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/clients:go_default_library",
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/clients/armclient:go_default_library",
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/clients/armclient/mockarmclient:go_default_library",
|
||||||
|
"//vendor/github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network:go_default_library",
|
||||||
|
"//vendor/github.com/Azure/go-autorest/autorest:go_default_library",
|
||||||
|
"//vendor/github.com/Azure/go-autorest/autorest/to:go_default_library",
|
||||||
|
"//vendor/github.com/golang/mock/gomock:go_default_library",
|
||||||
|
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "package-srcs",
|
||||||
|
srcs = glob(["**"]),
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:private"],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "all-srcs",
|
||||||
|
srcs = [
|
||||||
|
":package-srcs",
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/clients/interfaceclient/mockinterfaceclient:all-srcs",
|
||||||
|
],
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
)
|
@ -0,0 +1,276 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2020 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 interfaceclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
|
||||||
|
"github.com/Azure/go-autorest/autorest"
|
||||||
|
"github.com/Azure/go-autorest/autorest/azure"
|
||||||
|
|
||||||
|
"k8s.io/client-go/util/flowcontrol"
|
||||||
|
"k8s.io/klog"
|
||||||
|
azclients "k8s.io/legacy-cloud-providers/azure/clients"
|
||||||
|
"k8s.io/legacy-cloud-providers/azure/clients/armclient"
|
||||||
|
"k8s.io/legacy-cloud-providers/azure/metrics"
|
||||||
|
"k8s.io/legacy-cloud-providers/azure/retry"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ Interface = &Client{}
|
||||||
|
|
||||||
|
// Client implements network interface client.
|
||||||
|
type Client struct {
|
||||||
|
armClient armclient.Interface
|
||||||
|
subscriptionID string
|
||||||
|
|
||||||
|
// Rate limiting configures.
|
||||||
|
rateLimiterReader flowcontrol.RateLimiter
|
||||||
|
rateLimiterWriter flowcontrol.RateLimiter
|
||||||
|
|
||||||
|
// ARM throttling configures.
|
||||||
|
RetryAfterReader time.Time
|
||||||
|
RetryAfterWriter time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// New creates a new network interface client with ratelimiting.
|
||||||
|
func New(config *azclients.ClientConfig) *Client {
|
||||||
|
baseURI := config.ResourceManagerEndpoint
|
||||||
|
authorizer := autorest.NewBearerAuthorizer(config.ServicePrincipalToken)
|
||||||
|
armClient := armclient.New(authorizer, baseURI, "", APIVersion, config.Location, config.Backoff)
|
||||||
|
rateLimiterReader, rateLimiterWriter := azclients.NewRateLimiter(config.RateLimitConfig)
|
||||||
|
|
||||||
|
klog.V(2).Infof("Azure InterfacesClient (read ops) using rate limit config: QPS=%g, bucket=%d",
|
||||||
|
config.RateLimitConfig.CloudProviderRateLimitQPS,
|
||||||
|
config.RateLimitConfig.CloudProviderRateLimitBucket)
|
||||||
|
klog.V(2).Infof("Azure InterfacesClient (write ops) using rate limit config: QPS=%g, bucket=%d",
|
||||||
|
config.RateLimitConfig.CloudProviderRateLimitQPSWrite,
|
||||||
|
config.RateLimitConfig.CloudProviderRateLimitBucketWrite)
|
||||||
|
|
||||||
|
client := &Client{
|
||||||
|
armClient: armClient,
|
||||||
|
rateLimiterReader: rateLimiterReader,
|
||||||
|
rateLimiterWriter: rateLimiterWriter,
|
||||||
|
subscriptionID: config.SubscriptionID,
|
||||||
|
}
|
||||||
|
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get gets a network.Interface.
|
||||||
|
func (c *Client) Get(ctx context.Context, resourceGroupName string, networkInterfaceName string, expand string) (network.Interface, *retry.Error) {
|
||||||
|
mc := metrics.NewMetricContext("interfaces", "get", resourceGroupName, c.subscriptionID, "")
|
||||||
|
|
||||||
|
// Report errors if the client is rate limited.
|
||||||
|
if !c.rateLimiterReader.TryAccept() {
|
||||||
|
mc.RateLimitedCount()
|
||||||
|
return network.Interface{}, retry.GetRateLimitError(false, "NicGet")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report errors if the client is throttled.
|
||||||
|
if c.RetryAfterReader.After(time.Now()) {
|
||||||
|
mc.ThrottledCount()
|
||||||
|
rerr := retry.GetThrottlingError("NicGet", "client throttled", c.RetryAfterReader)
|
||||||
|
return network.Interface{}, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
result, rerr := c.getNetworkInterface(ctx, resourceGroupName, networkInterfaceName, expand)
|
||||||
|
mc.Observe(rerr.Error())
|
||||||
|
if rerr != nil {
|
||||||
|
if rerr.IsThrottled() {
|
||||||
|
// Update RetryAfterReader so that no more requests would be sent until RetryAfter expires.
|
||||||
|
c.RetryAfterReader = rerr.RetryAfter
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getNetworkInterface gets a network.Interface.
|
||||||
|
func (c *Client) getNetworkInterface(ctx context.Context, resourceGroupName string, networkInterfaceName string, expand string) (network.Interface, *retry.Error) {
|
||||||
|
resourceID := armclient.GetResourceID(
|
||||||
|
c.subscriptionID,
|
||||||
|
resourceGroupName,
|
||||||
|
"Microsoft.Network/networkInterfaces",
|
||||||
|
networkInterfaceName,
|
||||||
|
)
|
||||||
|
result := network.Interface{}
|
||||||
|
|
||||||
|
response, rerr := c.armClient.GetResource(ctx, resourceID, "")
|
||||||
|
defer c.armClient.CloseResponse(ctx, response)
|
||||||
|
if rerr != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "nic.get.request", resourceID, rerr.Error())
|
||||||
|
return result, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
err := autorest.Respond(
|
||||||
|
response,
|
||||||
|
azure.WithErrorUnlessStatusCode(http.StatusOK),
|
||||||
|
autorest.ByUnmarshallingJSON(&result))
|
||||||
|
if err != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "nic.get.respond", resourceID, err)
|
||||||
|
return result, retry.GetError(response, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Response = autorest.Response{Response: response}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVirtualMachineScaleSetNetworkInterface gets a network.Interface of VMSS VM.
|
||||||
|
func (c *Client) GetVirtualMachineScaleSetNetworkInterface(ctx context.Context, resourceGroupName string, virtualMachineScaleSetName string, virtualmachineIndex string, networkInterfaceName string, expand string) (network.Interface, *retry.Error) {
|
||||||
|
mc := metrics.NewMetricContext("interfaces", "get_vmss_nic", resourceGroupName, c.subscriptionID, "")
|
||||||
|
|
||||||
|
// Report errors if the client is rate limited.
|
||||||
|
if !c.rateLimiterReader.TryAccept() {
|
||||||
|
mc.RateLimitedCount()
|
||||||
|
return network.Interface{}, retry.GetRateLimitError(false, "NicGetVirtualMachineScaleSetNetworkInterface")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report errors if the client is throttled.
|
||||||
|
if c.RetryAfterReader.After(time.Now()) {
|
||||||
|
mc.ThrottledCount()
|
||||||
|
rerr := retry.GetThrottlingError("NicGetVirtualMachineScaleSetNetworkInterface", "client throttled", c.RetryAfterReader)
|
||||||
|
return network.Interface{}, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
result, rerr := c.getVMSSNetworkInterface(ctx, resourceGroupName, virtualMachineScaleSetName, virtualmachineIndex, networkInterfaceName, expand)
|
||||||
|
mc.Observe(rerr.Error())
|
||||||
|
if rerr != nil {
|
||||||
|
if rerr.IsThrottled() {
|
||||||
|
// Update RetryAfterReader so that no more requests would be sent until RetryAfter expires.
|
||||||
|
c.RetryAfterReader = rerr.RetryAfter
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getVMSSNetworkInterface gets a network.Interface of VMSS VM.
|
||||||
|
func (c *Client) getVMSSNetworkInterface(ctx context.Context, resourceGroupName string, virtualMachineScaleSetName string, virtualmachineIndex string, networkInterfaceName string, expand string) (network.Interface, *retry.Error) {
|
||||||
|
resourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s/virtualMachines/%s/networkInterfaces/%s",
|
||||||
|
autorest.Encode("path", c.subscriptionID),
|
||||||
|
autorest.Encode("path", resourceGroupName),
|
||||||
|
autorest.Encode("path", virtualMachineScaleSetName),
|
||||||
|
autorest.Encode("path", virtualmachineIndex),
|
||||||
|
autorest.Encode("path", networkInterfaceName),
|
||||||
|
)
|
||||||
|
|
||||||
|
result := network.Interface{}
|
||||||
|
queryParameters := map[string]interface{}{
|
||||||
|
"api-version": ComputeAPIVersion,
|
||||||
|
}
|
||||||
|
if len(expand) > 0 {
|
||||||
|
queryParameters["$expand"] = autorest.Encode("query", expand)
|
||||||
|
}
|
||||||
|
decorators := []autorest.PrepareDecorator{
|
||||||
|
autorest.WithQueryParameters(queryParameters),
|
||||||
|
}
|
||||||
|
response, rerr := c.armClient.GetResourceWithDecorators(ctx, resourceID, decorators)
|
||||||
|
defer c.armClient.CloseResponse(ctx, response)
|
||||||
|
if rerr != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "vmssnic.get.request", resourceID, rerr.Error())
|
||||||
|
return result, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
err := autorest.Respond(
|
||||||
|
response,
|
||||||
|
azure.WithErrorUnlessStatusCode(http.StatusOK),
|
||||||
|
autorest.ByUnmarshallingJSON(&result))
|
||||||
|
if err != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "vmssnic.get.respond", resourceID, err)
|
||||||
|
return result, retry.GetError(response, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Response = autorest.Response{Response: response}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOrUpdate creates or updates a network.Interface.
|
||||||
|
func (c *Client) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkInterfaceName string, parameters network.Interface) *retry.Error {
|
||||||
|
mc := metrics.NewMetricContext("interfaces", "create_or_update", resourceGroupName, c.subscriptionID, "")
|
||||||
|
|
||||||
|
// Report errors if the client is rate limited.
|
||||||
|
if !c.rateLimiterWriter.TryAccept() {
|
||||||
|
mc.RateLimitedCount()
|
||||||
|
return retry.GetRateLimitError(true, "NicCreateOrUpdate")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report errors if the client is throttled.
|
||||||
|
if c.RetryAfterWriter.After(time.Now()) {
|
||||||
|
mc.ThrottledCount()
|
||||||
|
rerr := retry.GetThrottlingError("NicCreateOrUpdate", "client throttled", c.RetryAfterWriter)
|
||||||
|
return rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
rerr := c.createOrUpdateInterface(ctx, resourceGroupName, networkInterfaceName, parameters)
|
||||||
|
mc.Observe(rerr.Error())
|
||||||
|
if rerr != nil {
|
||||||
|
if rerr.IsThrottled() {
|
||||||
|
// Update RetryAfterReader so that no more requests would be sent until RetryAfter expires.
|
||||||
|
c.RetryAfterWriter = rerr.RetryAfter
|
||||||
|
}
|
||||||
|
|
||||||
|
return rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// createOrUpdateInterface creates or updates a network.Interface.
|
||||||
|
func (c *Client) createOrUpdateInterface(ctx context.Context, resourceGroupName string, networkInterfaceName string, parameters network.Interface) *retry.Error {
|
||||||
|
resourceID := armclient.GetResourceID(
|
||||||
|
c.subscriptionID,
|
||||||
|
resourceGroupName,
|
||||||
|
"Microsoft.Network/networkInterfaces",
|
||||||
|
networkInterfaceName,
|
||||||
|
)
|
||||||
|
response, rerr := c.armClient.PutResource(ctx, resourceID, parameters)
|
||||||
|
defer c.armClient.CloseResponse(ctx, response)
|
||||||
|
if rerr != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "nic.put.request", resourceID, rerr.Error())
|
||||||
|
return rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
if response != nil && response.StatusCode != http.StatusNoContent {
|
||||||
|
_, rerr = c.createOrUpdateResponder(response)
|
||||||
|
if rerr != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "nic.put.respond", resourceID, rerr.Error())
|
||||||
|
return rerr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) createOrUpdateResponder(resp *http.Response) (*network.Interface, *retry.Error) {
|
||||||
|
result := &network.Interface{}
|
||||||
|
err := autorest.Respond(
|
||||||
|
resp,
|
||||||
|
azure.WithErrorUnlessStatusCode(http.StatusOK, http.StatusCreated),
|
||||||
|
autorest.ByUnmarshallingJSON(&result))
|
||||||
|
result.Response = autorest.Response{Response: resp}
|
||||||
|
return result, retry.GetError(resp, err)
|
||||||
|
}
|
@ -0,0 +1,152 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2020 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 interfaceclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
|
||||||
|
"github.com/Azure/go-autorest/autorest"
|
||||||
|
"github.com/Azure/go-autorest/autorest/to"
|
||||||
|
"github.com/golang/mock/gomock"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
|
azclients "k8s.io/legacy-cloud-providers/azure/clients"
|
||||||
|
"k8s.io/legacy-cloud-providers/azure/clients/armclient"
|
||||||
|
"k8s.io/legacy-cloud-providers/azure/clients/armclient/mockarmclient"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetNotFound(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
resourceID := "/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Network/networkInterfaces/nic1"
|
||||||
|
response := &http.Response{
|
||||||
|
StatusCode: http.StatusNotFound,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewReader([]byte("{}"))),
|
||||||
|
}
|
||||||
|
armClient := mockarmclient.NewMockInterface(ctrl)
|
||||||
|
armClient.EXPECT().GetResource(gomock.Any(), resourceID, "").Return(response, nil).Times(1)
|
||||||
|
armClient.EXPECT().CloseResponse(gomock.Any(), gomock.Any()).Times(1)
|
||||||
|
|
||||||
|
nicClient := getTestInterfaceClient(armClient)
|
||||||
|
expected := network.Interface{Response: autorest.Response{}}
|
||||||
|
result, rerr := nicClient.Get(context.TODO(), "rg", "nic1", "")
|
||||||
|
assert.Equal(t, expected, result)
|
||||||
|
assert.NotNil(t, rerr)
|
||||||
|
assert.Equal(t, http.StatusNotFound, rerr.HTTPStatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetInternalError(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
resourceID := "/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Network/networkInterfaces/nic1"
|
||||||
|
response := &http.Response{
|
||||||
|
StatusCode: http.StatusInternalServerError,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewReader([]byte("{}"))),
|
||||||
|
}
|
||||||
|
armClient := mockarmclient.NewMockInterface(ctrl)
|
||||||
|
armClient.EXPECT().GetResource(gomock.Any(), resourceID, "").Return(response, nil).Times(1)
|
||||||
|
armClient.EXPECT().CloseResponse(gomock.Any(), gomock.Any()).Times(1)
|
||||||
|
|
||||||
|
nicClient := getTestInterfaceClient(armClient)
|
||||||
|
expected := network.Interface{Response: autorest.Response{}}
|
||||||
|
result, rerr := nicClient.Get(context.TODO(), "rg", "nic1", "")
|
||||||
|
assert.Equal(t, expected, result)
|
||||||
|
assert.NotNil(t, rerr)
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, rerr.HTTPStatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetVirtualMachineScaleSetNetworkInterface(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
resourceID := "/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/vmss/virtualMachines/0/networkInterfaces/nic1"
|
||||||
|
testInterface := getTestVMSSInterface("nic1")
|
||||||
|
networkInterface, err := testInterface.MarshalJSON()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
response := &http.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewReader(networkInterface)),
|
||||||
|
}
|
||||||
|
armClient := mockarmclient.NewMockInterface(ctrl)
|
||||||
|
armClient.EXPECT().GetResourceWithDecorators(gomock.Any(), resourceID, gomock.Any()).Return(response, nil).Times(1)
|
||||||
|
armClient.EXPECT().CloseResponse(gomock.Any(), gomock.Any()).Times(1)
|
||||||
|
|
||||||
|
nicClient := getTestInterfaceClient(armClient)
|
||||||
|
expected := getTestVMSSInterface("nic1")
|
||||||
|
expected.Response = autorest.Response{Response: response}
|
||||||
|
result, rerr := nicClient.GetVirtualMachineScaleSetNetworkInterface(context.TODO(), "rg", "vmss", "0", "nic1", "")
|
||||||
|
assert.Equal(t, expected, result)
|
||||||
|
assert.Nil(t, rerr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateOrUpdate(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
testInterface := getTestInterface("nic1")
|
||||||
|
armClient := mockarmclient.NewMockInterface(ctrl)
|
||||||
|
response := &http.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||||
|
}
|
||||||
|
armClient.EXPECT().PutResource(gomock.Any(), to.String(testInterface.ID), testInterface).Return(response, nil).Times(1)
|
||||||
|
armClient.EXPECT().CloseResponse(gomock.Any(), gomock.Any()).Times(1)
|
||||||
|
|
||||||
|
nicClient := getTestInterfaceClient(armClient)
|
||||||
|
rerr := nicClient.CreateOrUpdate(context.TODO(), "rg", "nic1", testInterface)
|
||||||
|
assert.Nil(t, rerr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTestInterface(name string) network.Interface {
|
||||||
|
resourceID := fmt.Sprintf("/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Network/networkInterfaces/%s", name)
|
||||||
|
return network.Interface{
|
||||||
|
ID: to.StringPtr(resourceID),
|
||||||
|
Name: to.StringPtr(name),
|
||||||
|
Location: to.StringPtr("eastus"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTestVMSSInterface(name string) network.Interface {
|
||||||
|
resourceID := fmt.Sprintf("/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/vmss/virtualMachines/0/networkInterfaces/%s", name)
|
||||||
|
return network.Interface{
|
||||||
|
ID: to.StringPtr(resourceID),
|
||||||
|
Location: to.StringPtr("eastus"),
|
||||||
|
InterfacePropertiesFormat: &network.InterfacePropertiesFormat{
|
||||||
|
Primary: to.BoolPtr(true),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTestInterfaceClient(armClient armclient.Interface) *Client {
|
||||||
|
rateLimiterReader, rateLimiterWriter := azclients.NewRateLimiter(&azclients.RateLimitConfig{})
|
||||||
|
return &Client{
|
||||||
|
armClient: armClient,
|
||||||
|
subscriptionID: "subscriptionID",
|
||||||
|
rateLimiterReader: rateLimiterReader,
|
||||||
|
rateLimiterWriter: rateLimiterWriter,
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2020 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 interfaceclient implements the client for network interfaces.
|
||||||
|
package interfaceclient // import "k8s.io/legacy-cloud-providers/azure/clients/interfaceclient"
|
@ -0,0 +1,48 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2020 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 interfaceclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
|
||||||
|
"k8s.io/legacy-cloud-providers/azure/retry"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// APIVersion is the API version for network.
|
||||||
|
APIVersion = "2019-06-01"
|
||||||
|
|
||||||
|
// ComputeAPIVersion is the API version for compute. It is required to get VMSS network interface.
|
||||||
|
ComputeAPIVersion = "2017-03-30"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Interface is the client interface for NetworkInterface.
|
||||||
|
// Don't forget to run the following command to generate the mock client:
|
||||||
|
// mockgen -source=$GOPATH/src/k8s.io/kubernetes/staging/src/k8s.io/legacy-cloud-providers/azure/clients/interfaceclient/interface.go -package=mockinterfaceclient Interface > $GOPATH/src/k8s.io/kubernetes/staging/src/k8s.io/legacy-cloud-providers/azure/clients/interfaceclient/mockinterfaceclient/interface.go
|
||||||
|
type Interface interface {
|
||||||
|
// Get gets a network.Interface.
|
||||||
|
Get(ctx context.Context, resourceGroupName string, networkInterfaceName string, expand string) (result network.Interface, rerr *retry.Error)
|
||||||
|
|
||||||
|
// GetVirtualMachineScaleSetNetworkInterface gets a network.Interface of VMSS VM.
|
||||||
|
GetVirtualMachineScaleSetNetworkInterface(ctx context.Context, resourceGroupName string, virtualMachineScaleSetName string, virtualmachineIndex string, networkInterfaceName string, expand string) (result network.Interface, rerr *retry.Error)
|
||||||
|
|
||||||
|
// CreateOrUpdate creates or updates a network.Interface.
|
||||||
|
CreateOrUpdate(ctx context.Context, resourceGroupName string, networkInterfaceName string, parameters network.Interface) *retry.Error
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
||||||
|
|
||||||
|
go_library(
|
||||||
|
name = "go_default_library",
|
||||||
|
srcs = [
|
||||||
|
"doc.go",
|
||||||
|
"interface.go",
|
||||||
|
],
|
||||||
|
importmap = "k8s.io/kubernetes/vendor/k8s.io/legacy-cloud-providers/azure/clients/interfaceclient/mockinterfaceclient",
|
||||||
|
importpath = "k8s.io/legacy-cloud-providers/azure/clients/interfaceclient/mockinterfaceclient",
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
deps = [
|
||||||
|
"//staging/src/k8s.io/legacy-cloud-providers/azure/retry:go_default_library",
|
||||||
|
"//vendor/github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network:go_default_library",
|
||||||
|
"//vendor/github.com/golang/mock/gomock:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "package-srcs",
|
||||||
|
srcs = glob(["**"]),
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:private"],
|
||||||
|
)
|
||||||
|
|
||||||
|
filegroup(
|
||||||
|
name = "all-srcs",
|
||||||
|
srcs = [":package-srcs"],
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
)
|
@ -0,0 +1,20 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2020 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 mockinterfaceclient implements the mock client for network interfaces.
|
||||||
|
package mockinterfaceclient // import "k8s.io/legacy-cloud-providers/azure/clients/interfaceclient/mockinterfaceclient"
|
@ -0,0 +1,95 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2020 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 mockinterfaceclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
network "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-06-01/network"
|
||||||
|
gomock "github.com/golang/mock/gomock"
|
||||||
|
retry "k8s.io/legacy-cloud-providers/azure/retry"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MockInterface is a mock of Interface interface
|
||||||
|
type MockInterface struct {
|
||||||
|
ctrl *gomock.Controller
|
||||||
|
recorder *MockInterfaceMockRecorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockInterfaceMockRecorder is the mock recorder for MockInterface
|
||||||
|
type MockInterfaceMockRecorder struct {
|
||||||
|
mock *MockInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMockInterface creates a new mock instance
|
||||||
|
func NewMockInterface(ctrl *gomock.Controller) *MockInterface {
|
||||||
|
mock := &MockInterface{ctrl: ctrl}
|
||||||
|
mock.recorder = &MockInterfaceMockRecorder{mock}
|
||||||
|
return mock
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXPECT returns an object that allows the caller to indicate expected use
|
||||||
|
func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder {
|
||||||
|
return m.recorder
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get mocks base method
|
||||||
|
func (m *MockInterface) Get(ctx context.Context, resourceGroupName, networkInterfaceName, expand string) (network.Interface, *retry.Error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Get", ctx, resourceGroupName, networkInterfaceName, expand)
|
||||||
|
ret0, _ := ret[0].(network.Interface)
|
||||||
|
ret1, _ := ret[1].(*retry.Error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get indicates an expected call of Get
|
||||||
|
func (mr *MockInterfaceMockRecorder) Get(ctx, resourceGroupName, networkInterfaceName, expand interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockInterface)(nil).Get), ctx, resourceGroupName, networkInterfaceName, expand)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVirtualMachineScaleSetNetworkInterface mocks base method
|
||||||
|
func (m *MockInterface) GetVirtualMachineScaleSetNetworkInterface(ctx context.Context, resourceGroupName, virtualMachineScaleSetName, virtualmachineIndex, networkInterfaceName, expand string) (network.Interface, *retry.Error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "GetVirtualMachineScaleSetNetworkInterface", ctx, resourceGroupName, virtualMachineScaleSetName, virtualmachineIndex, networkInterfaceName, expand)
|
||||||
|
ret0, _ := ret[0].(network.Interface)
|
||||||
|
ret1, _ := ret[1].(*retry.Error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVirtualMachineScaleSetNetworkInterface indicates an expected call of GetVirtualMachineScaleSetNetworkInterface
|
||||||
|
func (mr *MockInterfaceMockRecorder) GetVirtualMachineScaleSetNetworkInterface(ctx, resourceGroupName, virtualMachineScaleSetName, virtualmachineIndex, networkInterfaceName, expand interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetVirtualMachineScaleSetNetworkInterface", reflect.TypeOf((*MockInterface)(nil).GetVirtualMachineScaleSetNetworkInterface), ctx, resourceGroupName, virtualMachineScaleSetName, virtualmachineIndex, networkInterfaceName, expand)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOrUpdate mocks base method
|
||||||
|
func (m *MockInterface) CreateOrUpdate(ctx context.Context, resourceGroupName, networkInterfaceName string, parameters network.Interface) *retry.Error {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "CreateOrUpdate", ctx, resourceGroupName, networkInterfaceName, parameters)
|
||||||
|
ret0, _ := ret[0].(*retry.Error)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOrUpdate indicates an expected call of CreateOrUpdate
|
||||||
|
func (mr *MockInterfaceMockRecorder) CreateOrUpdate(ctx, resourceGroupName, networkInterfaceName, parameters interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateOrUpdate", reflect.TypeOf((*MockInterface)(nil).CreateOrUpdate), ctx, resourceGroupName, networkInterfaceName, parameters)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user