Extracting property values to make ARM output variables accessible (#3877)

This commit is contained in:
Tariq Ibrahim 2018-09-24 17:14:43 -07:00 коммит произвёл Jack Francis
Родитель c9de40e9a5
Коммит 98b037780c
11 изменённых файлов: 660 добавлений и 107 удалений

Просмотреть файл

@ -138,10 +138,6 @@ const (
DefaultKubernetesGCLowThreshold = 80
// DefaultGeneratorCode specifies the source generator of the cluster template.
DefaultGeneratorCode = "acsengine"
// DefaultOrchestratorName specifies the 3 character orchestrator code of the cluster template and affects resource naming.
DefaultOrchestratorName = "k8s"
// DefaultOpenshiftOrchestratorName specifies the 3 character orchestrator code of the cluster template and affects resource naming.
DefaultOpenshiftOrchestratorName = "ocp"
// DefaultEtcdVersion specifies the default etcd version to install
DefaultEtcdVersion = "3.2.23"
// DefaultEtcdDiskSize specifies the default size for Kubernetes master etcd disk volumes in GB

Просмотреть файл

@ -707,7 +707,7 @@ func setHostedMasterProfileDefaults(a *api.Properties) {
func setDefaultCerts(a *api.Properties) (bool, error) {
if a.MasterProfile != nil && a.OrchestratorProfile.OrchestratorType == api.OpenShift {
return certgen.OpenShiftSetDefaultCerts(a, DefaultOpenshiftOrchestratorName, GenerateClusterID(a))
return certgen.OpenShiftSetDefaultCerts(a, api.DefaultOpenshiftOrchestratorName, a.GetClusterID())
}
if a.MasterProfile == nil || a.OrchestratorProfile.OrchestratorType != api.Kubernetes {

Просмотреть файл

@ -6,10 +6,8 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"hash/fnv"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"regexp"
@ -46,23 +44,6 @@ func init() {
keyvaultSecretPathRe = regexp.MustCompile(`^(/subscriptions/\S+/resourceGroups/\S+/providers/Microsoft.KeyVault/vaults/\S+)/secrets/([^/\s]+)(/(\S+))?$`)
}
// GenerateClusterID creates a unique 8 string cluster ID
func GenerateClusterID(properties *api.Properties) string {
uniqueNameSuffixSize := 8
// the name suffix uniquely identifies the cluster and is generated off a hash
// from the master dns name
h := fnv.New64a()
if properties.MasterProfile != nil {
h.Write([]byte(properties.MasterProfile.DNSPrefix))
} else if properties.HostedMasterProfile != nil {
h.Write([]byte(properties.HostedMasterProfile.DNSPrefix))
} else {
h.Write([]byte(properties.AgentPoolProfiles[0].Name))
}
rand.Seed(int64(h.Sum64()))
return fmt.Sprintf("%08d", rand.Uint32())[:uniqueNameSuffixSize]
}
// GenerateKubeConfig returns a JSON string representing the KubeConfig
func GenerateKubeConfig(properties *api.Properties, location string) (string, error) {
if properties == nil {
@ -478,18 +459,6 @@ func isNSeriesSKU(profile *api.AgentPoolProfile) bool {
return false
}
func isCustomVNET(a []*api.AgentPoolProfile) bool {
if a != nil {
for _, agentPoolProfile := range a {
if !agentPoolProfile.IsCustomVNET() {
return false
}
}
return true
}
return false
}
func getDCOSCustomDataPublicIPStr(orchestratorType string, masterCount int) string {
if orchestratorType == api.DCOS {
var buf bytes.Buffer

Просмотреть файл

@ -7,7 +7,6 @@ import (
"io/ioutil"
"path"
"path/filepath"
"regexp"
"strings"
"testing"
@ -506,41 +505,6 @@ func TestIsNSeriesSKU(t *testing.T) {
}
}
func TestIsCustomVNET(t *testing.T) {
a := []*api.AgentPoolProfile{
{
VnetSubnetID: "subnetlink1",
},
{
VnetSubnetID: "subnetlink2",
},
}
if !isCustomVNET(a) {
t.Fatalf("Expected isCustomVNET to be true when subnet exists for all agent pool profile")
}
a = []*api.AgentPoolProfile{
{
VnetSubnetID: "subnetlink1",
},
{
VnetSubnetID: "",
},
}
if isCustomVNET(a) {
t.Fatalf("Expected isCustomVNET to be false when subnet exists for some agent pool profile")
}
a = nil
if isCustomVNET(a) {
t.Fatalf("Expected isCustomVNET to be false when agent pool profiles is nil")
}
}
func TestGenerateIpList(t *testing.T) {
count := 3
forth := 240
@ -592,25 +556,3 @@ func TestGenerateKubeConfig(t *testing.T) {
t.Fatalf("Expected an error result from nil Properties child properties")
}
}
func TestGenerateClusterID(t *testing.T) {
p := &api.Properties{
AgentPoolProfiles: []*api.AgentPoolProfile{
{
Name: "foo_agent",
},
},
}
clusterID := GenerateClusterID(p)
r, err := regexp.Compile("[0-9]{8}")
if err != nil {
t.Errorf("unexpected error while parsing regex : %s", err.Error())
}
if !r.MatchString(clusterID) {
t.Fatal("ClusterID should be an 8 digit integer string")
}
}

Просмотреть файл

@ -20,7 +20,7 @@ type ArtifactWriter struct {
// WriteTLSArtifacts saves TLS certificates and keys to the server filesystem
func (w *ArtifactWriter) WriteTLSArtifacts(containerService *api.ContainerService, apiVersion, template, parameters, artifactsDir string, certsGenerated bool, parametersOnly bool) error {
if len(artifactsDir) == 0 {
artifactsDir = fmt.Sprintf("%s-%s", containerService.Properties.OrchestratorProfile.OrchestratorType, GenerateClusterID(containerService.Properties))
artifactsDir = fmt.Sprintf("%s-%s", containerService.Properties.OrchestratorProfile.OrchestratorType, containerService.Properties.GetClusterID())
artifactsDir = path.Join("_output", artifactsDir)
}

Просмотреть файл

@ -18,7 +18,7 @@ func TestWriteTLSArtifacts(t *testing.T) {
},
}
dir := "_testoutputdir"
defaultDir := fmt.Sprintf("%s-%s", cs.Properties.OrchestratorProfile.OrchestratorType, GenerateClusterID(cs.Properties))
defaultDir := fmt.Sprintf("%s-%s", cs.Properties.OrchestratorProfile.OrchestratorType, cs.Properties.GetClusterID())
defaultDir = path.Join("_output", defaultDir)
defer os.RemoveAll(dir)
defer os.RemoveAll(defaultDir)

Просмотреть файл

@ -294,13 +294,7 @@ func assignKubernetesParameters(properties *api.Properties, parametersMap params
}
}
if properties.HostedMasterProfile != nil {
addValue(parametersMap, "orchestratorName", "aks")
} else if properties.OrchestratorProfile.IsOpenShift() {
addValue(parametersMap, "orchestratorName", DefaultOpenshiftOrchestratorName)
} else {
addValue(parametersMap, "orchestratorName", DefaultOrchestratorName)
}
addValue(parametersMap, "orchestratorName", properties.K8sOrchestratorName())
/**
The following parameters could be either a plain text, or referenced to a secret in a keyvault:

Просмотреть файл

@ -206,7 +206,7 @@ func (t *TemplateGenerator) getTemplateFuncMap(cs *api.ContainerService) templat
return cs.Properties.MasterProfile != nil && cs.Properties.MasterProfile.IsVirtualMachineScaleSets()
},
"IsHostedMaster": func() bool {
return cs.Properties.HostedMasterProfile != nil
return cs.Properties.IsHostedMasterProfile()
},
"IsDCOS19": func() bool {
return cs.Properties.OrchestratorProfile.OrchestratorType == api.DCOS &&
@ -395,7 +395,7 @@ func (t *TemplateGenerator) getTemplateFuncMap(cs *api.ContainerService) templat
return getSecurityRules(ports)
},
"GetUniqueNameSuffix": func() string {
return GenerateClusterID(cs.Properties)
return cs.Properties.GetClusterID()
},
"GetVNETAddressPrefixes": func() string {
return getVNETAddressPrefixes(cs.Properties)
@ -802,12 +802,24 @@ func (t *TemplateGenerator) getTemplateFuncMap(cs *api.ContainerService) templat
imageRef := cs.Properties.MasterProfile.ImageRef
return imageRef != nil && len(imageRef.Name) > 0 && len(imageRef.ResourceGroup) > 0
},
"GetRouteTableName": func() string {
return cs.Properties.GetRouteTableName()
},
"GetNSGName": func() string {
return cs.Properties.GetNSGName()
},
"GetMasterEtcdServerPort": func() int {
return DefaultMasterEtcdServerPort
},
"GetMasterEtcdClientPort": func() int {
return DefaultMasterEtcdClientPort
},
"GetPrimaryAvailabilitySetName": func() string {
return cs.Properties.GetPrimaryAvailabilitySetName()
},
"GetPrimaryScaleSetName": func() string {
return cs.Properties.GetPrimaryScaleSetName()
},
"UseCloudControllerManager": func() bool {
return cs.Properties.OrchestratorProfile.KubernetesConfig.UseCloudControllerManager != nil && *cs.Properties.OrchestratorProfile.KubernetesConfig.UseCloudControllerManager
},
@ -907,7 +919,7 @@ func (t *TemplateGenerator) getTemplateFuncMap(cs *api.ContainerService) templat
return a - b
},
"IsCustomVNET": func() bool {
return isCustomVNET(cs.Properties.AgentPoolProfiles)
return cs.Properties.AreAgentProfilesCustomVNET()
},
"quote": strconv.Quote,
"shellQuote": func(s string) string {

Просмотреть файл

@ -62,6 +62,12 @@ const (
const (
// AvailabilitySet means that the vms are in an availability set
AvailabilitySet = "AvailabilitySet"
// DefaultOrchestratorName specifies the 3 character orchestrator code of the cluster template and affects resource naming.
DefaultOrchestratorName = "k8s"
// DefaultOpenshiftOrchestratorName specifies the 3 character orchestrator code of the cluster template and affects resource naming.
DefaultOpenshiftOrchestratorName = "ocp"
// DefaultHostedProfileMasterName specifies the 3 character orchestrator code of the clusters with hosted master profiles.
DefaultHostedProfileMasterName = "aks"
// DefaultFirstConsecutiveKubernetesStaticIP specifies the static IP address on Kubernetes master 0
DefaultFirstConsecutiveKubernetesStaticIP = "10.240.255.5"
// DefaultFirstConsecutiveKubernetesStaticIPVMSS specifies the static IP address on Kubernetes master 0 of VMSS
@ -72,6 +78,12 @@ const (
// DefaultKubernetesFirstConsecutiveStaticIPOffsetVMSS specifies the IP address offset of master 0 in VMSS
// when VNET integration is enabled.
DefaultKubernetesFirstConsecutiveStaticIPOffsetVMSS = 4
// DefaultSubnetNameResourceSegmentIndex specifies the default subnet name resource segment index.
DefaultSubnetNameResourceSegmentIndex = 10
// DefaultVnetResourceGroupSegmentIndex specifies the default virtual network resource segment index.
DefaultVnetResourceGroupSegmentIndex = 4
// DefaultVnetNameResourceSegmentIndex specifies the default virtual network name segment index.
DefaultVnetNameResourceSegmentIndex = 8
// VirtualMachineScaleSets means that the vms are in a virtual machine scaleset
VirtualMachineScaleSets = "VirtualMachineScaleSets"
// ScaleSetPriorityRegular is the default ScaleSet Priority
@ -164,11 +176,15 @@ const (
NetworkPolicyNone = "none"
// NetworkPluginKubenet is the string expression for the kubenet NetworkPlugin config
NetworkPluginKubenet = "kubenet"
// NetworkPluginAzure is thee string expression for Azure CNI plugin.
// NetworkPluginAzure is the string expression for Azure CNI plugin.
NetworkPluginAzure = "azure"
// DefaultSinglePlacementGroup determines the acs-engine provided default for supporting large VMSS
// (true = single placement group 0-100 VMs, false = multiple placement group 0-1000 VMs)
DefaultSinglePlacementGroup = true
// ARMNetworkNamespace is the ARM-specific namespace for ARM's network providers.
ARMNetworkNamespace = "Microsoft.Networks"
// ARMVirtualNetworksResourceType is the ARM resource type for virtual network resources of ARM.
ARMVirtualNetworksResourceType = "virtualNetworks"
)
const (

Просмотреть файл

@ -1,6 +1,9 @@
package api
import (
"fmt"
"hash/fnv"
"math/rand"
"net"
neturl "net/url"
"strconv"
@ -47,6 +50,7 @@ type ContainerService struct {
// Properties represents the ACS cluster definition
type Properties struct {
ClusterID string
ProvisioningState ProvisioningState `json:"provisioningState,omitempty"`
OrchestratorProfile *OrchestratorProfile `json:"orchestratorProfile,omitempty"`
MasterProfile *MasterProfile `json:"masterProfile,omitempty"`
@ -65,6 +69,18 @@ type Properties struct {
AzProfile *AzProfile `json:"azProfile,omitempty"`
}
// ClusterMetadata represents the metadata of the ACS cluster.
type ClusterMetadata struct {
SubnetName string `json:"subnetName,omitempty"`
VNetResourceGroupName string `json:"vnetResourceGroupName,omitempty"`
VirtualNetworkName string `json:"virtualNetworkName,omitempty"`
SecurityGroupName string `json:"securityGroupName,omitempty"`
RouteTableName string `json:"routeTableName,omitempty"`
PrimaryAvailabilitySetName string `json:"primaryAvailabilitySetName,omitempty"`
PrimaryScaleSetName string `json:"primaryScaleSetName,omitempty"`
VMPrefix string `json:"vmPrefix,omitempty"`
}
// AddonProfile represents an addon for managed cluster
type AddonProfile struct {
Enabled bool `json:"enabled"`
@ -717,6 +733,152 @@ func (p *Properties) HasVMSSAgentPool() bool {
return false
}
// K8sOrchestratorName returns the 3 character orchestrator code for kubernetes-based clusters.
func (p *Properties) K8sOrchestratorName() string {
if p.OrchestratorProfile.IsKubernetes() ||
p.OrchestratorProfile.IsOpenShift() {
if p.HostedMasterProfile != nil {
return DefaultHostedProfileMasterName
} else if p.OrchestratorProfile.IsOpenShift() {
return DefaultOpenshiftOrchestratorName
} else {
return DefaultOrchestratorName
}
}
return ""
}
func (p *Properties) getAgentVMPrefix() string {
return p.K8sOrchestratorName() + "-agentpool-" + p.GetClusterID() + "-"
}
func (p *Properties) getMasterVMPrefix() string {
return p.K8sOrchestratorName() + "-master-" + p.GetClusterID() + "-"
}
// GetVMPrefix returns the agent VM prefix or master VM prefix based on the cluster configuration.
func (p *Properties) GetVMPrefix() string {
if p.IsHostedMasterProfile() {
return p.getAgentVMPrefix()
}
return p.getMasterVMPrefix()
}
// GetRouteTableName returns the route table name of the cluster.
func (p *Properties) GetRouteTableName() string {
return p.GetVMPrefix() + "routetable"
}
// GetNSGName returns the name of the network security group of the cluster.
func (p *Properties) GetNSGName() string {
return p.GetVMPrefix() + "nsg"
}
// GetPrimaryAvailabilitySetName returns the name of the primary availability set of the cluster
func (p *Properties) GetPrimaryAvailabilitySetName() string {
return p.AgentPoolProfiles[0].Name + "-availabilitySet-" + p.GetClusterID()
}
// GetPrimaryScaleSetName returns the name of the primary scale set node of the cluster
func (p *Properties) GetPrimaryScaleSetName() string {
return p.K8sOrchestratorName() + "-" + p.AgentPoolProfiles[0].Name + "-" + p.GetClusterID() + "-vmss"
}
// IsHostedMasterProfile returns true if the cluster has a hosted master
func (p *Properties) IsHostedMasterProfile() bool {
return p.HostedMasterProfile != nil
}
// GetVNetResourceGroupName returns the virtual network resource group name of the cluster
func (p *Properties) GetVNetResourceGroupName() string {
var vnetResourceGroupName string
if p.IsHostedMasterProfile() && p.AreAgentProfilesCustomVNET() {
vnetResourceGroupName = strings.Split(p.AgentPoolProfiles[0].VnetSubnetID, "/")[DefaultVnetResourceGroupSegmentIndex]
} else if !p.IsHostedMasterProfile() && p.MasterProfile.IsCustomVNET() {
vnetResourceGroupName = strings.Split(p.MasterProfile.VnetSubnetID, "/")[DefaultVnetResourceGroupSegmentIndex]
}
return vnetResourceGroupName
}
// GetVirtualNetworkName returns the virtual network name of the cluster
func (p *Properties) GetVirtualNetworkName() string {
var vnetName string
if p.IsHostedMasterProfile() && p.AreAgentProfilesCustomVNET() {
vnetName = strings.Split(p.AgentPoolProfiles[0].VnetSubnetID, "/")[DefaultVnetNameResourceSegmentIndex]
} else if !p.IsHostedMasterProfile() && p.MasterProfile.IsCustomVNET() {
vnetName = strings.Split(p.MasterProfile.VnetSubnetID, "/")[DefaultVnetNameResourceSegmentIndex]
} else {
vnetName = p.K8sOrchestratorName() + "-vnet-" + p.GetClusterID()
}
return vnetName
}
// GetSubnetName returns the subnet name of the cluster based on its current configuration.
func (p *Properties) GetSubnetName() string {
var subnetName string
if p.IsHostedMasterProfile() {
if p.AreAgentProfilesCustomVNET() {
subnetName = strings.Split(p.AgentPoolProfiles[0].VnetSubnetID, "/")[DefaultSubnetNameResourceSegmentIndex]
} else {
subnetName = p.K8sOrchestratorName() + "-subnet"
}
} else {
if p.MasterProfile.IsCustomVNET() {
subnetName = strings.Split(p.MasterProfile.VnetSubnetID, "/")[DefaultSubnetNameResourceSegmentIndex]
} else {
subnetName = p.K8sOrchestratorName() + "-subnet"
}
}
return subnetName
}
// AreAgentProfilesCustomVNET returns true if all of the agent profiles in the clusters are configured with VNET.
func (p *Properties) AreAgentProfilesCustomVNET() bool {
if p.AgentPoolProfiles != nil {
for _, agentPoolProfile := range p.AgentPoolProfiles {
if !agentPoolProfile.IsCustomVNET() {
return false
}
}
return true
}
return false
}
// GetClusterID creates a unique 8 string cluster ID.
func (p *Properties) GetClusterID() string {
if p.ClusterID == "" {
uniqueNameSuffixSize := 8
// the name suffix uniquely identifies the cluster and is generated off a hash
// from the master dns name
h := fnv.New64a()
if p.MasterProfile != nil {
h.Write([]byte(p.MasterProfile.DNSPrefix))
} else if p.HostedMasterProfile != nil {
h.Write([]byte(p.HostedMasterProfile.DNSPrefix))
} else {
h.Write([]byte(p.AgentPoolProfiles[0].Name))
}
rand.Seed(int64(h.Sum64()))
p.ClusterID = fmt.Sprintf("%08d", rand.Uint32())[:uniqueNameSuffixSize]
}
return p.ClusterID
}
// GetClusterMetadata returns a instance of the struct type api.ClusterMetadata.
func (p *Properties) GetClusterMetadata() *ClusterMetadata {
return &ClusterMetadata{
SubnetName: p.GetSubnetName(),
VNetResourceGroupName: p.GetVNetResourceGroupName(),
VirtualNetworkName: p.GetVirtualNetworkName(),
SecurityGroupName: p.GetNSGName(),
RouteTableName: p.GetRouteTableName(),
PrimaryAvailabilitySetName: p.GetPrimaryAvailabilitySetName(),
PrimaryScaleSetName: p.GetPrimaryScaleSetName(),
VMPrefix: p.GetVMPrefix(),
}
}
// HasZonesForAllAgentPools returns true if all of the agent pools have zones
func (p *Properties) HasZonesForAllAgentPools() bool {
for _, ap := range p.AgentPoolProfiles {

Просмотреть файл

@ -2,6 +2,7 @@ package api
import (
"log"
"regexp"
"testing"
"github.com/Azure/acs-engine/pkg/helpers"
@ -1581,3 +1582,464 @@ func getMockAddon(name string) KubernetesAddon {
},
}
}
func TestAreAgentProfilesCustomVNET(t *testing.T) {
p := Properties{}
p.AgentPoolProfiles = []*AgentPoolProfile{
{
VnetSubnetID: "subnetlink1",
},
{
VnetSubnetID: "subnetlink2",
},
}
if !p.AreAgentProfilesCustomVNET() {
t.Fatalf("Expected isCustomVNET to be true when subnet exists for all agent pool profile")
}
p.AgentPoolProfiles = []*AgentPoolProfile{
{
VnetSubnetID: "subnetlink1",
},
{
VnetSubnetID: "",
},
}
if p.AreAgentProfilesCustomVNET() {
t.Fatalf("Expected isCustomVNET to be false when subnet exists for some agent pool profile")
}
p.AgentPoolProfiles = nil
if p.AreAgentProfilesCustomVNET() {
t.Fatalf("Expected isCustomVNET to be false when agent pool profiles is nil")
}
}
func TestGenerateClusterID(t *testing.T) {
p := &Properties{
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "foo_agent",
},
},
}
clusterID := p.GetClusterID()
r, err := regexp.Compile("[0-9]{8}")
if err != nil {
t.Errorf("unexpected error while parsing regex : %s", err.Error())
}
if !r.MatchString(clusterID) {
t.Fatal("ClusterID should be an 8 digit integer string")
}
p = &Properties{
HostedMasterProfile: &HostedMasterProfile{
DNSPrefix: "foodnsprefx",
},
}
clusterID = p.GetClusterID()
r, err = regexp.Compile("[0-9]{8}")
if err != nil {
t.Errorf("unexpected error while parsing regex : %s", err.Error())
}
if !r.MatchString(clusterID) {
t.Fatal("ClusterID should be an 8 digit integer string")
}
}
func TestGetPrimaryAvailabilitySetName(t *testing.T) {
p := &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
MasterProfile: &MasterProfile{
Count: 1,
DNSPrefix: "foo",
VMSize: "Standard_DS2_v2",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: AvailabilitySet,
},
},
}
expected := "agentpool-availabilitySet-28513887"
got := p.GetPrimaryAvailabilitySetName()
if got != expected {
t.Errorf("expected primary availability set name %s, but got %s", expected, got)
}
}
func TestGetPrimaryScaleSetName(t *testing.T) {
p := &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
MasterProfile: &MasterProfile{
Count: 1,
DNSPrefix: "foo",
VMSize: "Standard_DS2_v2",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
},
},
}
expected := "k8s-agentpool-28513887-vmss"
got := p.GetPrimaryScaleSetName()
if got != expected {
t.Errorf("expected primary availability set name %s, but got %s", expected, got)
}
}
func TestGetRouteTableName(t *testing.T) {
p := &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
HostedMasterProfile: &HostedMasterProfile{
FQDN: "fqdn",
DNSPrefix: "foo",
Subnet: "mastersubnet",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
},
},
}
actualRTName := p.GetRouteTableName()
expectedRTName := "aks-agentpool-28513887-routetable"
actualNSGName := p.GetNSGName()
expectedNSGName := "aks-agentpool-28513887-nsg"
if actualRTName != expectedRTName {
t.Errorf("expected route table name %s, but got %s", expectedRTName, actualRTName)
}
if actualNSGName != expectedNSGName {
t.Errorf("expected route table name %s, but got %s", expectedNSGName, actualNSGName)
}
p = &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
MasterProfile: &MasterProfile{
Count: 1,
DNSPrefix: "foo",
VMSize: "Standard_DS2_v2",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
},
},
}
actualRTName = p.GetRouteTableName()
expectedRTName = "k8s-master-28513887-routetable"
actualNSGName = p.GetNSGName()
expectedNSGName = "k8s-master-28513887-nsg"
if actualRTName != expectedRTName {
t.Errorf("expected route table name %s, but got %s", actualRTName, expectedRTName)
}
if actualNSGName != expectedNSGName {
t.Errorf("expected route table name %s, but got %s", actualNSGName, expectedNSGName)
}
}
func TestGetSubnetName(t *testing.T) {
tests := []struct {
name string
properties *Properties
expectedSubnetName string
}{
{
name: "Cluster with HosterMasterProfile",
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
HostedMasterProfile: &HostedMasterProfile{
FQDN: "fqdn",
DNSPrefix: "foo",
Subnet: "mastersubnet",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
},
},
},
expectedSubnetName: "aks-subnet",
},
{
name: "Cluster with HosterMasterProfile and custom VNET",
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
HostedMasterProfile: &HostedMasterProfile{
FQDN: "fqdn",
DNSPrefix: "foo",
Subnet: "mastersubnet",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
VnetSubnetID: "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Network/virtualNetworks/ExampleCustomVNET/subnets/BazAgentSubnet",
},
},
},
expectedSubnetName: "BazAgentSubnet",
},
{
name: "Cluster with MasterProfile",
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
MasterProfile: &MasterProfile{
Count: 1,
DNSPrefix: "foo",
VMSize: "Standard_DS2_v2",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
},
},
},
expectedSubnetName: "k8s-subnet",
},
{
name: "Cluster with MasterProfile and custom VNET",
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
MasterProfile: &MasterProfile{
Count: 1,
DNSPrefix: "foo",
VMSize: "Standard_DS2_v2",
VnetSubnetID: "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Network/virtualNetworks/ExampleCustomVNET/subnets/BazAgentSubnet",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
},
},
},
expectedSubnetName: "BazAgentSubnet",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
actual := test.properties.GetSubnetName()
if actual != test.expectedSubnetName {
t.Errorf("expected subnet name %s, but got %s", test.expectedSubnetName, actual)
}
})
}
}
func TestProperties_GetVirtualNetworkName(t *testing.T) {
tests := []struct {
name string
properties *Properties
expectedVirtualNetworkName string
}{
{
name: "Cluster with HostedMasterProfile and Custom VNET AgentProfiles",
properties: &Properties{
HostedMasterProfile: &HostedMasterProfile{
FQDN: "fqdn",
DNSPrefix: "foo",
Subnet: "mastersubnet",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
VnetSubnetID: "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Network/virtualNetworks/ExampleCustomVNET/subnets/BazAgentSubnet",
},
},
},
expectedVirtualNetworkName: "ExampleCustomVNET",
},
{
name: "Cluster with HostedMasterProfile and AgentProfiles",
properties: &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
HostedMasterProfile: &HostedMasterProfile{
FQDN: "fqdn",
DNSPrefix: "foo",
Subnet: "mastersubnet",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
},
},
},
expectedVirtualNetworkName: "aks-vnet-28513887",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
actual := test.properties.GetVirtualNetworkName()
if actual != test.expectedVirtualNetworkName {
t.Errorf("expected virtual network name %s, but got %s", test.expectedVirtualNetworkName, actual)
}
})
}
}
func TestProperties_GetVNetResourceGroupName(t *testing.T) {
p := &Properties{
HostedMasterProfile: &HostedMasterProfile{
FQDN: "fqdn",
DNSPrefix: "foo",
Subnet: "mastersubnet",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: VirtualMachineScaleSets,
VnetSubnetID: "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Network/virtualNetworks/ExampleCustomVNET/subnets/BazAgentSubnet",
},
},
}
expectedVNETResourceGroupName := "RESOURCE_GROUP_NAME"
actual := p.GetVNetResourceGroupName()
if expectedVNETResourceGroupName != actual {
t.Errorf("expected vnet resource group name name %s, but got %s", expectedVNETResourceGroupName, actual)
}
}
func TestProperties_GetClusterMetadata(t *testing.T) {
p := &Properties{
OrchestratorProfile: &OrchestratorProfile{
OrchestratorType: Kubernetes,
},
MasterProfile: &MasterProfile{
Count: 1,
DNSPrefix: "foo",
VMSize: "Standard_DS2_v2",
VnetSubnetID: "/subscriptions/SUBSCRIPTION_ID/resourceGroups/SAMPLE_RESOURCE_GROUP_NAME/providers/Microsoft.Network/virtualNetworks/ExampleCustomVNET/subnets/BazAgentSubnet",
},
AgentPoolProfiles: []*AgentPoolProfile{
{
Name: "agentpool",
VMSize: "Standard_D2_v2",
Count: 1,
AvailabilityProfile: AvailabilitySet,
},
},
}
metadata := p.GetClusterMetadata()
if metadata == nil {
t.Error("did not expect cluster metadata to be nil")
}
expectedSubnetName := "BazAgentSubnet"
if metadata.SubnetName != expectedSubnetName {
t.Errorf("expected subnet name %s, but got %s", expectedSubnetName, metadata.SubnetName)
}
expectedVNetResourceGroupName := "SAMPLE_RESOURCE_GROUP_NAME"
if metadata.VNetResourceGroupName != expectedVNetResourceGroupName {
t.Errorf("expected vNetResourceGroupName name %s, but got %s", expectedVNetResourceGroupName, metadata.VNetResourceGroupName)
}
expectedVirtualNetworkName := "ExampleCustomVNET"
if metadata.VirtualNetworkName != expectedVirtualNetworkName {
t.Errorf("expected VirtualNetworkName name %s, but got %s", expectedVirtualNetworkName, metadata.VirtualNetworkName)
}
expectedRouteTableName := "k8s-master-28513887-routetable"
if metadata.RouteTableName != expectedRouteTableName {
t.Errorf("expected RouteTableName name %s, but got %s", expectedVirtualNetworkName, metadata.RouteTableName)
}
expectedSecurityGroupName := "k8s-master-28513887-nsg"
if metadata.SecurityGroupName != expectedSecurityGroupName {
t.Errorf("expected SecurityGroupName name %s, but got %s", expectedSecurityGroupName, metadata.SecurityGroupName)
}
expectedPrimaryAvailabilitySetName := "agentpool-availabilitySet-28513887"
if metadata.PrimaryAvailabilitySetName != expectedPrimaryAvailabilitySetName {
t.Errorf("expected PrimaryAvailabilitySetName name %s, but got %s", expectedPrimaryAvailabilitySetName, metadata.PrimaryAvailabilitySetName)
}
expectedPrimaryScaleSetName := "k8s-agentpool-28513887-vmss"
if metadata.PrimaryScaleSetName != expectedPrimaryScaleSetName {
t.Errorf("expected PrimaryScaleSetName name %s, but got %s", expectedPrimaryScaleSetName, metadata.PrimaryScaleSetName)
}
}