Add VMSS client
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||||
|
|
||||||
|
go_library(
|
||||||
|
name = "go_default_library",
|
||||||
|
srcs = [
|
||||||
|
"azure_vmssclient.go",
|
||||||
|
"doc.go",
|
||||||
|
"interface.go",
|
||||||
|
],
|
||||||
|
importmap = "k8s.io/kubernetes/vendor/k8s.io/legacy-cloud-providers/azure/clients/vmssclient",
|
||||||
|
importpath = "k8s.io/legacy-cloud-providers/azure/clients/vmssclient",
|
||||||
|
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/retry:go_default_library",
|
||||||
|
"//vendor/github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute: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/github.com/Azure/go-autorest/autorest/to:go_default_library",
|
||||||
|
"//vendor/k8s.io/klog:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
go_test(
|
||||||
|
name = "go_default_test",
|
||||||
|
srcs = ["azure_vmssclient_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/compute/mgmt/2019-07-01/compute: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/vmssclient/mockvmssclient:all-srcs",
|
||||||
|
],
|
||||||
|
tags = ["automanaged"],
|
||||||
|
visibility = ["//visibility:public"],
|
||||||
|
)
|
@@ -0,0 +1,346 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
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 vmssclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
|
||||||
|
"github.com/Azure/go-autorest/autorest"
|
||||||
|
"github.com/Azure/go-autorest/autorest/azure"
|
||||||
|
"github.com/Azure/go-autorest/autorest/to"
|
||||||
|
|
||||||
|
"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/retry"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _ Interface = &Client{}
|
||||||
|
|
||||||
|
// Client implements VMSS client Interface.
|
||||||
|
// TODO(feiskyer): export prometheus metrics for each API call.
|
||||||
|
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 VMSS 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 VirtualMachineScaleSetClient (read ops) using rate limit config: QPS=%g, bucket=%d",
|
||||||
|
config.RateLimitConfig.CloudProviderRateLimitQPS,
|
||||||
|
config.RateLimitConfig.CloudProviderRateLimitBucket)
|
||||||
|
klog.V(2).Infof("Azure VirtualMachineScaleSetClient (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 VirtualMachineScaleSet.
|
||||||
|
func (c *Client) Get(ctx context.Context, resourceGroupName string, VMScaleSetName string) (compute.VirtualMachineScaleSet, *retry.Error) {
|
||||||
|
// Report errors if the client is rate limited.
|
||||||
|
if !c.rateLimiterReader.TryAccept() {
|
||||||
|
return compute.VirtualMachineScaleSet{}, retry.GetRateLimitError(false, "VMSSGet")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report errors if the client is throttled.
|
||||||
|
if c.RetryAfterReader.After(time.Now()) {
|
||||||
|
rerr := retry.GetThrottlingError("VMSSGet", "client throttled", c.RetryAfterReader)
|
||||||
|
return compute.VirtualMachineScaleSet{}, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
result, rerr := c.getVMSS(ctx, resourceGroupName, VMScaleSetName)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// getVMSS gets a VirtualMachineScaleSet.
|
||||||
|
func (c *Client) getVMSS(ctx context.Context, resourceGroupName string, VMScaleSetName string) (compute.VirtualMachineScaleSet, *retry.Error) {
|
||||||
|
resourceID := armclient.GetResourceID(
|
||||||
|
c.subscriptionID,
|
||||||
|
resourceGroupName,
|
||||||
|
"Microsoft.Compute/virtualMachineScaleSets",
|
||||||
|
VMScaleSetName,
|
||||||
|
)
|
||||||
|
result := compute.VirtualMachineScaleSet{}
|
||||||
|
|
||||||
|
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", "vmss.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", "vmss.get.respond", resourceID, err)
|
||||||
|
return result, retry.GetError(response, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
result.Response = autorest.Response{Response: response}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// List gets a list of VirtualMachineScaleSets in the resource group.
|
||||||
|
func (c *Client) List(ctx context.Context, resourceGroupName string) ([]compute.VirtualMachineScaleSet, *retry.Error) {
|
||||||
|
// Report errors if the client is rate limited.
|
||||||
|
if !c.rateLimiterReader.TryAccept() {
|
||||||
|
return nil, retry.GetRateLimitError(false, "VMSSList")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report errors if the client is throttled.
|
||||||
|
if c.RetryAfterReader.After(time.Now()) {
|
||||||
|
rerr := retry.GetThrottlingError("VMSSList", "client throttled", c.RetryAfterReader)
|
||||||
|
return nil, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
result, rerr := c.listVMSS(ctx, resourceGroupName)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// listVMSS gets a list of VirtualMachineScaleSets in the resource group.
|
||||||
|
func (c *Client) listVMSS(ctx context.Context, resourceGroupName string) ([]compute.VirtualMachineScaleSet, *retry.Error) {
|
||||||
|
resourceID := fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets",
|
||||||
|
autorest.Encode("path", c.subscriptionID),
|
||||||
|
autorest.Encode("path", resourceGroupName))
|
||||||
|
result := make([]compute.VirtualMachineScaleSet, 0)
|
||||||
|
page := &VirtualMachineScaleSetListResultPage{}
|
||||||
|
page.fn = c.listNextResults
|
||||||
|
|
||||||
|
resp, rerr := c.armClient.GetResource(ctx, resourceID, "")
|
||||||
|
defer c.armClient.CloseResponse(ctx, resp)
|
||||||
|
if rerr != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "vmss.list.request", resourceID, rerr.Error())
|
||||||
|
return result, rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
page.vmsslr, err = c.listResponder(resp)
|
||||||
|
if err != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "vmss.list.respond", resourceID, err)
|
||||||
|
return result, retry.GetError(resp, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for page.NotDone() {
|
||||||
|
result = append(result, *page.Response().Value...)
|
||||||
|
if err = page.NextWithContext(ctx); err != nil {
|
||||||
|
klog.V(5).Infof("Received error in %s: resourceID: %s, error: %s", "vmss.list.next", resourceID, err)
|
||||||
|
return result, retry.GetError(page.Response().Response.Response, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOrUpdate creates or updates a VirtualMachineScaleSet.
|
||||||
|
func (c *Client) CreateOrUpdate(ctx context.Context, resourceGroupName string, VMScaleSetName string, parameters compute.VirtualMachineScaleSet) *retry.Error {
|
||||||
|
// Report errors if the client is rate limited.
|
||||||
|
if !c.rateLimiterWriter.TryAccept() {
|
||||||
|
return retry.GetRateLimitError(true, "VMSSCreateOrUpdate")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report errors if the client is throttled.
|
||||||
|
if c.RetryAfterWriter.After(time.Now()) {
|
||||||
|
rerr := retry.GetThrottlingError("VMSSCreateOrUpdate", "client throttled", c.RetryAfterWriter)
|
||||||
|
return rerr
|
||||||
|
}
|
||||||
|
|
||||||
|
rerr := c.createOrUpdateVMSS(ctx, resourceGroupName, VMScaleSetName, parameters)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// createOrUpdateVMSS creates or updates a VirtualMachineScaleSet.
|
||||||
|
func (c *Client) createOrUpdateVMSS(ctx context.Context, resourceGroupName string, VMScaleSetName string, parameters compute.VirtualMachineScaleSet) *retry.Error {
|
||||||
|
resourceID := armclient.GetResourceID(
|
||||||
|
c.subscriptionID,
|
||||||
|
resourceGroupName,
|
||||||
|
"Microsoft.Compute/virtualMachineScaleSets",
|
||||||
|
VMScaleSetName,
|
||||||
|
)
|
||||||
|
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", "vmss.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", "vmss.put.respond", resourceID, rerr.Error())
|
||||||
|
return rerr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) createOrUpdateResponder(resp *http.Response) (*compute.VirtualMachineScaleSet, *retry.Error) {
|
||||||
|
result := &compute.VirtualMachineScaleSet{}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) listResponder(resp *http.Response) (result compute.VirtualMachineScaleSetListResult, err error) {
|
||||||
|
err = autorest.Respond(
|
||||||
|
resp,
|
||||||
|
autorest.ByIgnoring(),
|
||||||
|
azure.WithErrorUnlessStatusCode(http.StatusOK),
|
||||||
|
autorest.ByUnmarshallingJSON(&result))
|
||||||
|
result.Response = autorest.Response{Response: resp}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// virtualMachineScaleSetListResultPreparer prepares a request to retrieve the next set of results.
|
||||||
|
// It returns nil if no more results exist.
|
||||||
|
func (c *Client) virtualMachineScaleSetListResultPreparer(ctx context.Context, vmsslr compute.VirtualMachineScaleSetListResult) (*http.Request, error) {
|
||||||
|
if vmsslr.NextLink == nil || len(to.String(vmsslr.NextLink)) < 1 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
decorators := []autorest.PrepareDecorator{
|
||||||
|
autorest.WithBaseURL(to.String(vmsslr.NextLink)),
|
||||||
|
}
|
||||||
|
return c.armClient.PrepareGetRequest(ctx, decorators...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// listNextResults retrieves the next set of results, if any.
|
||||||
|
func (c *Client) listNextResults(ctx context.Context, lastResults compute.VirtualMachineScaleSetListResult) (result compute.VirtualMachineScaleSetListResult, err error) {
|
||||||
|
req, err := c.virtualMachineScaleSetListResultPreparer(ctx, lastResults)
|
||||||
|
if err != nil {
|
||||||
|
return result, autorest.NewErrorWithError(err, "vmssclient", "listNextResults", nil, "Failure preparing next results request")
|
||||||
|
}
|
||||||
|
if req == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, rerr := c.armClient.Send(ctx, req)
|
||||||
|
defer c.armClient.CloseResponse(ctx, resp)
|
||||||
|
if rerr != nil {
|
||||||
|
result.Response = autorest.Response{Response: resp}
|
||||||
|
return result, autorest.NewErrorWithError(rerr.Error(), "vmssclient", "listNextResults", resp, "Failure sending next results request")
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err = c.listResponder(resp)
|
||||||
|
if err != nil {
|
||||||
|
err = autorest.NewErrorWithError(err, "vmssclient", "listNextResults", resp, "Failure responding to next results request")
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// VirtualMachineScaleSetListResultPage contains a page of VirtualMachineScaleSet values.
|
||||||
|
type VirtualMachineScaleSetListResultPage struct {
|
||||||
|
fn func(context.Context, compute.VirtualMachineScaleSetListResult) (compute.VirtualMachineScaleSetListResult, error)
|
||||||
|
vmsslr compute.VirtualMachineScaleSetListResult
|
||||||
|
}
|
||||||
|
|
||||||
|
// NextWithContext advances to the next page of values. If there was an error making
|
||||||
|
// the request the page does not advance and the error is returned.
|
||||||
|
func (page *VirtualMachineScaleSetListResultPage) NextWithContext(ctx context.Context) (err error) {
|
||||||
|
next, err := page.fn(ctx, page.vmsslr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
page.vmsslr = next
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next advances to the next page of values. If there was an error making
|
||||||
|
// the request the page does not advance and the error is returned.
|
||||||
|
// Deprecated: Use NextWithContext() instead.
|
||||||
|
func (page *VirtualMachineScaleSetListResultPage) Next() error {
|
||||||
|
return page.NextWithContext(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotDone returns true if the page enumeration should be started or is not yet complete.
|
||||||
|
func (page VirtualMachineScaleSetListResultPage) NotDone() bool {
|
||||||
|
return !page.vmsslr.IsEmpty()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Response returns the raw server response from the last page request.
|
||||||
|
func (page VirtualMachineScaleSetListResultPage) Response() compute.VirtualMachineScaleSetListResult {
|
||||||
|
return page.vmsslr
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values returns the slice of values for the current page or nil if there are no values.
|
||||||
|
func (page VirtualMachineScaleSetListResultPage) Values() []compute.VirtualMachineScaleSet {
|
||||||
|
if page.vmsslr.IsEmpty() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return *page.vmsslr.Value
|
||||||
|
}
|
@@ -0,0 +1,142 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
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 vmssclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
|
||||||
|
"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.Compute/virtualMachineScaleSets/vmss1"
|
||||||
|
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)
|
||||||
|
|
||||||
|
vmssClient := getTestVMSSClient(armClient)
|
||||||
|
expectedVMSS := compute.VirtualMachineScaleSet{Response: autorest.Response{}}
|
||||||
|
result, rerr := vmssClient.Get(context.TODO(), "rg", "vmss1")
|
||||||
|
assert.Equal(t, expectedVMSS, 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.Compute/virtualMachineScaleSets/vmss1"
|
||||||
|
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)
|
||||||
|
|
||||||
|
vmssClient := getTestVMSSClient(armClient)
|
||||||
|
expectedVMSS := compute.VirtualMachineScaleSet{Response: autorest.Response{}}
|
||||||
|
result, rerr := vmssClient.Get(context.TODO(), "rg", "vmss1")
|
||||||
|
assert.Equal(t, expectedVMSS, result)
|
||||||
|
assert.NotNil(t, rerr)
|
||||||
|
assert.Equal(t, http.StatusInternalServerError, rerr.HTTPStatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestList(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
resourceID := "/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets"
|
||||||
|
armClient := mockarmclient.NewMockInterface(ctrl)
|
||||||
|
vmssList := []compute.VirtualMachineScaleSet{getTestVMSS("vmss1"), getTestVMSS("vmss2"), getTestVMSS("vmss3")}
|
||||||
|
responseBody, err := json.Marshal(compute.VirtualMachineScaleSetListResult{Value: &vmssList})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
armClient.EXPECT().GetResource(gomock.Any(), resourceID, "").Return(
|
||||||
|
&http.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewReader(responseBody)),
|
||||||
|
}, nil).Times(1)
|
||||||
|
armClient.EXPECT().CloseResponse(gomock.Any(), gomock.Any()).Times(1)
|
||||||
|
|
||||||
|
vmssClient := getTestVMSSClient(armClient)
|
||||||
|
result, rerr := vmssClient.List(context.TODO(), "rg")
|
||||||
|
assert.Nil(t, rerr)
|
||||||
|
assert.Equal(t, 3, len(result))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateOrUpdate(t *testing.T) {
|
||||||
|
ctrl := gomock.NewController(t)
|
||||||
|
defer ctrl.Finish()
|
||||||
|
|
||||||
|
vmss := getTestVMSS("vmss1")
|
||||||
|
armClient := mockarmclient.NewMockInterface(ctrl)
|
||||||
|
response := &http.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewReader([]byte(""))),
|
||||||
|
}
|
||||||
|
armClient.EXPECT().PutResource(gomock.Any(), to.String(vmss.ID), vmss).Return(response, nil).Times(1)
|
||||||
|
armClient.EXPECT().CloseResponse(gomock.Any(), gomock.Any()).Times(1)
|
||||||
|
|
||||||
|
vmssClient := getTestVMSSClient(armClient)
|
||||||
|
rerr := vmssClient.CreateOrUpdate(context.TODO(), "rg", "vmss1", vmss)
|
||||||
|
assert.Nil(t, rerr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTestVMSS(name string) compute.VirtualMachineScaleSet {
|
||||||
|
return compute.VirtualMachineScaleSet{
|
||||||
|
ID: to.StringPtr("/subscriptions/subscriptionID/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/vmss1"),
|
||||||
|
Name: to.StringPtr(name),
|
||||||
|
Location: to.StringPtr("eastus"),
|
||||||
|
Sku: &compute.Sku{
|
||||||
|
Name: to.StringPtr("Standard"),
|
||||||
|
Capacity: to.Int64Ptr(3),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTestVMSSClient(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 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 vmssclient implements the client for VMSS.
|
||||||
|
package vmssclient // import "k8s.io/legacy-cloud-providers/azure/clients/vmssclient"
|
@@ -0,0 +1,45 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
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 vmssclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
|
||||||
|
"k8s.io/legacy-cloud-providers/azure/retry"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// APIVersion is the API version for VMSS.
|
||||||
|
APIVersion = "2019-07-01"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Interface is the client interface for VirtualMachineScaleSet.
|
||||||
|
// 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/vmssclient/interface.go -package=mockvmssclient Interface > $GOPATH/src/k8s.io/kubernetes/staging/src/k8s.io/legacy-cloud-providers/azure/clients/vmssclient/mockvmssclient/interface.go
|
||||||
|
type Interface interface {
|
||||||
|
// Get gets a VirtualMachineScaleSet.
|
||||||
|
Get(ctx context.Context, resourceGroupName string, VMScaleSetName string) (result compute.VirtualMachineScaleSet, rerr *retry.Error)
|
||||||
|
|
||||||
|
// List gets a list of VirtualMachineScaleSets in the resource group.
|
||||||
|
List(ctx context.Context, resourceGroupName string) (result []compute.VirtualMachineScaleSet, rerr *retry.Error)
|
||||||
|
|
||||||
|
// CreateOrUpdate creates or updates a VirtualMachineScaleSet.
|
||||||
|
CreateOrUpdate(ctx context.Context, resourceGroupName string, VMScaleSetName string, parameters compute.VirtualMachineScaleSet) *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/vmssclient/mockvmssclient",
|
||||||
|
importpath = "k8s.io/legacy-cloud-providers/azure/clients/vmssclient/mockvmssclient",
|
||||||
|
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/compute/mgmt/2019-07-01/compute: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 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 mockvmssclient implements the mock client for VMSS.
|
||||||
|
package mockvmssclient // import "k8s.io/legacy-cloud-providers/azure/clients/vmssclient/mockvmssclient"
|
@@ -0,0 +1,95 @@
|
|||||||
|
// +build !providerless
|
||||||
|
|
||||||
|
/*
|
||||||
|
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 mockvmssclient
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "context"
|
||||||
|
reflect "reflect"
|
||||||
|
|
||||||
|
compute "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
|
||||||
|
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, VMScaleSetName string) (compute.VirtualMachineScaleSet, *retry.Error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "Get", ctx, resourceGroupName, VMScaleSetName)
|
||||||
|
ret0, _ := ret[0].(compute.VirtualMachineScaleSet)
|
||||||
|
ret1, _ := ret[1].(*retry.Error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get indicates an expected call of Get
|
||||||
|
func (mr *MockInterfaceMockRecorder) Get(ctx, resourceGroupName, VMScaleSetName interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockInterface)(nil).Get), ctx, resourceGroupName, VMScaleSetName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// List mocks base method
|
||||||
|
func (m *MockInterface) List(ctx context.Context, resourceGroupName string) ([]compute.VirtualMachineScaleSet, *retry.Error) {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "List", ctx, resourceGroupName)
|
||||||
|
ret0, _ := ret[0].([]compute.VirtualMachineScaleSet)
|
||||||
|
ret1, _ := ret[1].(*retry.Error)
|
||||||
|
return ret0, ret1
|
||||||
|
}
|
||||||
|
|
||||||
|
// List indicates an expected call of List
|
||||||
|
func (mr *MockInterfaceMockRecorder) List(ctx, resourceGroupName interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "List", reflect.TypeOf((*MockInterface)(nil).List), ctx, resourceGroupName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOrUpdate mocks base method
|
||||||
|
func (m *MockInterface) CreateOrUpdate(ctx context.Context, resourceGroupName, VMScaleSetName string, parameters compute.VirtualMachineScaleSet) *retry.Error {
|
||||||
|
m.ctrl.T.Helper()
|
||||||
|
ret := m.ctrl.Call(m, "CreateOrUpdate", ctx, resourceGroupName, VMScaleSetName, parameters)
|
||||||
|
ret0, _ := ret[0].(*retry.Error)
|
||||||
|
return ret0
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateOrUpdate indicates an expected call of CreateOrUpdate
|
||||||
|
func (mr *MockInterfaceMockRecorder) CreateOrUpdate(ctx, resourceGroupName, VMScaleSetName, parameters interface{}) *gomock.Call {
|
||||||
|
mr.mock.ctrl.T.Helper()
|
||||||
|
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateOrUpdate", reflect.TypeOf((*MockInterface)(nil).CreateOrUpdate), ctx, resourceGroupName, VMScaleSetName, parameters)
|
||||||
|
}
|
Reference in New Issue
Block a user