sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/services/agentpools/spec_test.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package agentpools
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	asocontainerservicev1 "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231001"
    24  	asocontainerservicev1preview "github.com/Azure/azure-service-operator/v2/api/containerservice/v1api20231102preview"
    25  	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
    26  	"github.com/google/go-cmp/cmp"
    27  	. "github.com/onsi/gomega"
    28  	"k8s.io/apimachinery/pkg/api/resource"
    29  	"k8s.io/utils/ptr"
    30  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    31  )
    32  
    33  func TestParameters(t *testing.T) {
    34  	t.Run("no existing agent pool", func(t *testing.T) {
    35  		g := NewGomegaWithT(t)
    36  
    37  		spec := &AgentPoolSpec{
    38  			Name:                 "name",
    39  			AzureName:            "azure name",
    40  			ResourceGroup:        "rg",
    41  			Cluster:              "cluster",
    42  			Version:              ptr.To("1.26.6"),
    43  			SKU:                  "sku",
    44  			Replicas:             1,
    45  			OSDiskSizeGB:         2,
    46  			VnetSubnetID:         "vnet subnet id",
    47  			Mode:                 "mode",
    48  			MaxCount:             ptr.To(3),
    49  			MinCount:             ptr.To(4),
    50  			NodeLabels:           map[string]string{"node": "labels"},
    51  			NodeTaints:           []string{"node taints"},
    52  			EnableAutoScaling:    true,
    53  			AvailabilityZones:    []string{"zones"},
    54  			MaxPods:              ptr.To(5),
    55  			OsDiskType:           ptr.To("disk type"),
    56  			EnableUltraSSD:       ptr.To(false),
    57  			OSType:               ptr.To("os type"),
    58  			EnableNodePublicIP:   ptr.To(true),
    59  			NodePublicIPPrefixID: "public IP prefix ID",
    60  			ScaleSetPriority:     ptr.To("scaleset priority"),
    61  			ScaleDownMode:        ptr.To("scale down mode"),
    62  			SpotMaxPrice:         ptr.To(resource.MustParse("123")),
    63  			KubeletConfig: &KubeletConfig{
    64  				CPUManagerPolicy: ptr.To("cpu manager policy"),
    65  			},
    66  			KubeletDiskType: ptr.To(infrav1.KubeletDiskType("kubelet disk type")),
    67  			AdditionalTags:  map[string]string{"additional": "tags"},
    68  			LinuxOSConfig: &infrav1.LinuxOSConfig{
    69  				Sysctls: &infrav1.SysctlConfig{
    70  					FsNrOpen: ptr.To(6),
    71  				},
    72  			},
    73  			EnableFIPS:             ptr.To(true),
    74  			EnableEncryptionAtHost: ptr.To(false),
    75  		}
    76  		expected := &asocontainerservicev1.ManagedClustersAgentPool{
    77  			Spec: asocontainerservicev1.ManagedClusters_AgentPool_Spec{
    78  				AzureName: "azure name",
    79  				Owner: &genruntime.KnownResourceReference{
    80  					Name: "cluster",
    81  				},
    82  				AvailabilityZones:      []string{"zones"},
    83  				Count:                  ptr.To(1),
    84  				EnableAutoScaling:      ptr.To(true),
    85  				EnableUltraSSD:         ptr.To(false),
    86  				EnableEncryptionAtHost: ptr.To(false),
    87  				KubeletDiskType:        ptr.To(asocontainerservicev1.KubeletDiskType("kubelet disk type")),
    88  				MaxCount:               ptr.To(3),
    89  				MaxPods:                ptr.To(5),
    90  				MinCount:               ptr.To(4),
    91  				Mode:                   ptr.To(asocontainerservicev1.AgentPoolMode("mode")),
    92  				NodeLabels:             map[string]string{"node": "labels"},
    93  				NodeTaints:             []string{"node taints"},
    94  				OrchestratorVersion:    ptr.To("1.26.6"),
    95  				OsDiskSizeGB:           ptr.To(asocontainerservicev1.ContainerServiceOSDisk(2)),
    96  				OsDiskType:             ptr.To(asocontainerservicev1.OSDiskType("disk type")),
    97  				OsType:                 ptr.To(asocontainerservicev1.OSType("os type")),
    98  				ScaleSetPriority:       ptr.To(asocontainerservicev1.ScaleSetPriority("scaleset priority")),
    99  				ScaleDownMode:          ptr.To(asocontainerservicev1.ScaleDownMode("scale down mode")),
   100  				Type:                   ptr.To(asocontainerservicev1.AgentPoolType_VirtualMachineScaleSets),
   101  				EnableNodePublicIP:     ptr.To(true),
   102  				Tags:                   map[string]string{"additional": "tags"},
   103  				EnableFIPS:             ptr.To(true),
   104  				KubeletConfig: &asocontainerservicev1.KubeletConfig{
   105  					CpuManagerPolicy: ptr.To("cpu manager policy"),
   106  				},
   107  				VmSize:       ptr.To("sku"),
   108  				SpotMaxPrice: ptr.To(ptr.To(resource.MustParse("123")).AsApproximateFloat64()),
   109  				VnetSubnetReference: &genruntime.ResourceReference{
   110  					ARMID: "vnet subnet id",
   111  				},
   112  				NodePublicIPPrefixReference: &genruntime.ResourceReference{
   113  					ARMID: "public IP prefix ID",
   114  				},
   115  				LinuxOSConfig: &asocontainerservicev1.LinuxOSConfig{
   116  					Sysctls: &asocontainerservicev1.SysctlConfig{
   117  						FsNrOpen: ptr.To(6),
   118  					},
   119  				},
   120  			},
   121  		}
   122  
   123  		actual, err := spec.Parameters(context.Background(), nil)
   124  
   125  		g.Expect(err).NotTo(HaveOccurred())
   126  		g.Expect(cmp.Diff(actual, expected)).To(BeEmpty())
   127  	})
   128  
   129  	t.Run("no existing preview agent pool", func(t *testing.T) {
   130  		g := NewGomegaWithT(t)
   131  
   132  		spec := &AgentPoolSpec{
   133  			Preview:              true,
   134  			Name:                 "name",
   135  			AzureName:            "azure name",
   136  			ResourceGroup:        "rg",
   137  			Cluster:              "cluster",
   138  			Version:              ptr.To("1.26.6"),
   139  			SKU:                  "sku",
   140  			Replicas:             1,
   141  			OSDiskSizeGB:         2,
   142  			VnetSubnetID:         "vnet subnet id",
   143  			Mode:                 "mode",
   144  			MaxCount:             ptr.To(3),
   145  			MinCount:             ptr.To(4),
   146  			NodeLabels:           map[string]string{"node": "labels"},
   147  			NodeTaints:           []string{"node taints"},
   148  			EnableAutoScaling:    true,
   149  			AvailabilityZones:    []string{"zones"},
   150  			MaxPods:              ptr.To(5),
   151  			OsDiskType:           ptr.To("disk type"),
   152  			EnableUltraSSD:       ptr.To(false),
   153  			OSType:               ptr.To("os type"),
   154  			EnableNodePublicIP:   ptr.To(true),
   155  			NodePublicIPPrefixID: "public IP prefix ID",
   156  			ScaleSetPriority:     ptr.To("scaleset priority"),
   157  			ScaleDownMode:        ptr.To("scale down mode"),
   158  			SpotMaxPrice:         ptr.To(resource.MustParse("123")),
   159  			KubeletConfig: &KubeletConfig{
   160  				CPUManagerPolicy: ptr.To("cpu manager policy"),
   161  			},
   162  			KubeletDiskType: ptr.To(infrav1.KubeletDiskType("kubelet disk type")),
   163  			AdditionalTags:  map[string]string{"additional": "tags"},
   164  			LinuxOSConfig: &infrav1.LinuxOSConfig{
   165  				Sysctls: &infrav1.SysctlConfig{
   166  					FsNrOpen: ptr.To(6),
   167  				},
   168  			},
   169  			EnableFIPS:             ptr.To(true),
   170  			EnableEncryptionAtHost: ptr.To(false),
   171  		}
   172  		expected := &asocontainerservicev1preview.ManagedClustersAgentPool{
   173  			Spec: asocontainerservicev1preview.ManagedClusters_AgentPool_Spec{
   174  				AzureName: "azure name",
   175  				Owner: &genruntime.KnownResourceReference{
   176  					Name: "cluster",
   177  				},
   178  				AvailabilityZones:      []string{"zones"},
   179  				Count:                  ptr.To(1),
   180  				EnableAutoScaling:      ptr.To(true),
   181  				EnableUltraSSD:         ptr.To(false),
   182  				EnableEncryptionAtHost: ptr.To(false),
   183  				KubeletDiskType:        ptr.To(asocontainerservicev1preview.KubeletDiskType("kubelet disk type")),
   184  				MaxCount:               ptr.To(3),
   185  				MaxPods:                ptr.To(5),
   186  				MinCount:               ptr.To(4),
   187  				Mode:                   ptr.To(asocontainerservicev1preview.AgentPoolMode("mode")),
   188  				NodeLabels:             map[string]string{"node": "labels"},
   189  				NodeTaints:             []string{"node taints"},
   190  				OrchestratorVersion:    ptr.To("1.26.6"),
   191  				OsDiskSizeGB:           ptr.To(asocontainerservicev1preview.ContainerServiceOSDisk(2)),
   192  				OsDiskType:             ptr.To(asocontainerservicev1preview.OSDiskType("disk type")),
   193  				OsType:                 ptr.To(asocontainerservicev1preview.OSType("os type")),
   194  				ScaleSetPriority:       ptr.To(asocontainerservicev1preview.ScaleSetPriority("scaleset priority")),
   195  				ScaleDownMode:          ptr.To(asocontainerservicev1preview.ScaleDownMode("scale down mode")),
   196  				Type:                   ptr.To(asocontainerservicev1preview.AgentPoolType_VirtualMachineScaleSets),
   197  				EnableNodePublicIP:     ptr.To(true),
   198  				Tags:                   map[string]string{"additional": "tags"},
   199  				EnableFIPS:             ptr.To(true),
   200  				KubeletConfig: &asocontainerservicev1preview.KubeletConfig{
   201  					CpuManagerPolicy: ptr.To("cpu manager policy"),
   202  				},
   203  				VmSize:       ptr.To("sku"),
   204  				SpotMaxPrice: ptr.To(ptr.To(resource.MustParse("123")).AsApproximateFloat64()),
   205  				VnetSubnetReference: &genruntime.ResourceReference{
   206  					ARMID: "vnet subnet id",
   207  				},
   208  				NodePublicIPPrefixReference: &genruntime.ResourceReference{
   209  					ARMID: "public IP prefix ID",
   210  				},
   211  				LinuxOSConfig: &asocontainerservicev1preview.LinuxOSConfig{
   212  					Sysctls: &asocontainerservicev1preview.SysctlConfig{
   213  						FsNrOpen: ptr.To(6),
   214  					},
   215  				},
   216  			},
   217  		}
   218  
   219  		actual, err := spec.Parameters(context.Background(), nil)
   220  
   221  		g.Expect(err).NotTo(HaveOccurred())
   222  		g.Expect(cmp.Diff(actual, expected)).To(BeEmpty())
   223  	})
   224  
   225  	t.Run("with existing agent pool", func(t *testing.T) {
   226  		g := NewGomegaWithT(t)
   227  
   228  		spec := &AgentPoolSpec{
   229  			AzureName:         "managed by CAPZ",
   230  			Replicas:          3,
   231  			EnableAutoScaling: true,
   232  			Version:           ptr.To("1.26.6"),
   233  		}
   234  		existing := &asocontainerservicev1.ManagedClustersAgentPool{
   235  			Spec: asocontainerservicev1.ManagedClusters_AgentPool_Spec{
   236  				AzureName: "set by the user",
   237  				PowerState: &asocontainerservicev1.PowerState{
   238  					Code: ptr.To(asocontainerservicev1.PowerState_Code("set by the user")),
   239  				},
   240  				OrchestratorVersion: ptr.To("1.27.2"),
   241  			},
   242  			Status: asocontainerservicev1.ManagedClusters_AgentPool_STATUS{
   243  				Count: ptr.To(1212),
   244  			},
   245  		}
   246  
   247  		actual, err := spec.Parameters(context.Background(), existing)
   248  		actualTyped, ok := actual.(*asocontainerservicev1.ManagedClustersAgentPool)
   249  		g.Expect(ok).To(BeTrue())
   250  
   251  		g.Expect(err).NotTo(HaveOccurred())
   252  		g.Expect(actualTyped.Spec.AzureName).To(Equal("managed by CAPZ"))
   253  		g.Expect(actualTyped.Spec.Count).To(Equal(ptr.To(1212)))
   254  		g.Expect(actualTyped.Spec.PowerState.Code).To(Equal(ptr.To(asocontainerservicev1.PowerState_Code("set by the user"))))
   255  		g.Expect(actualTyped.Spec.OrchestratorVersion).NotTo(BeNil())
   256  		g.Expect(*actualTyped.Spec.OrchestratorVersion).To(Equal("1.27.2"))
   257  	})
   258  
   259  	t.Run("with existing preview agent pool", func(t *testing.T) {
   260  		g := NewGomegaWithT(t)
   261  
   262  		spec := &AgentPoolSpec{
   263  			AzureName:         "managed by CAPZ",
   264  			Replicas:          3,
   265  			EnableAutoScaling: true,
   266  			Version:           ptr.To("1.26.6"),
   267  			Preview:           true,
   268  		}
   269  		existing := &asocontainerservicev1preview.ManagedClustersAgentPool{
   270  			Spec: asocontainerservicev1preview.ManagedClusters_AgentPool_Spec{
   271  				AzureName: "set by the user",
   272  				PowerState: &asocontainerservicev1preview.PowerState{
   273  					Code: ptr.To(asocontainerservicev1preview.PowerState_Code("set by the user")),
   274  				},
   275  				OrchestratorVersion: ptr.To("1.27.2"),
   276  			},
   277  			Status: asocontainerservicev1preview.ManagedClusters_AgentPool_STATUS{
   278  				Count: ptr.To(1212),
   279  			},
   280  		}
   281  
   282  		actual, err := spec.Parameters(context.Background(), existing)
   283  		actualTyped, ok := actual.(*asocontainerservicev1preview.ManagedClustersAgentPool)
   284  		g.Expect(ok).To(BeTrue())
   285  
   286  		g.Expect(err).NotTo(HaveOccurred())
   287  		g.Expect(actualTyped.Spec.AzureName).To(Equal("managed by CAPZ"))
   288  		g.Expect(actualTyped.Spec.Count).To(Equal(ptr.To(1212)))
   289  		g.Expect(actualTyped.Spec.PowerState.Code).To(Equal(ptr.To(asocontainerservicev1preview.PowerState_Code("set by the user"))))
   290  		g.Expect(actualTyped.Spec.OrchestratorVersion).NotTo(BeNil())
   291  		g.Expect(*actualTyped.Spec.OrchestratorVersion).To(Equal("1.27.2"))
   292  	})
   293  }