private ManagedClusterAgentPoolProfile GetAgentPoolProfile() { var defaultAgentPoolProfile = new ManagedClusterAgentPoolProfile( name: NodeName ?? "default", count: NodeCount, vmSize: NodeVmSize, osDiskSizeGB: NodeOsDiskSize, type: NodeVmSetType ?? "VirtualMachineScaleSets", vnetSubnetID: NodeVnetSubnetID); defaultAgentPoolProfile.OsType = "Linux"; if (this.IsParameterBound(c => c.NodeMaxPodCount)) { defaultAgentPoolProfile.MaxPods = NodeMaxPodCount; } if (this.IsParameterBound(c => c.NodeMinCount)) { defaultAgentPoolProfile.MinCount = NodeMinCount; } if (this.IsParameterBound(c => c.NodeMaxCount)) { defaultAgentPoolProfile.MaxCount = NodeMaxCount; } if (EnableNodeAutoScaling.IsPresent) { defaultAgentPoolProfile.EnableAutoScaling = EnableNodeAutoScaling.ToBool(); } if (EnableNodePublicIp.IsPresent) { defaultAgentPoolProfile.EnableNodePublicIP = EnableNodePublicIp.ToBool(); } if (this.IsParameterBound(c => c.NodePublicIPPrefixID)) { defaultAgentPoolProfile.NodePublicIPPrefixID = NodePublicIPPrefixID; } if (this.IsParameterBound(c => c.NodeScaleSetEvictionPolicy)) { defaultAgentPoolProfile.ScaleSetEvictionPolicy = NodeScaleSetEvictionPolicy; } if (this.IsParameterBound(c => c.NodeSetPriority)) { defaultAgentPoolProfile.ScaleSetPriority = NodeSetPriority; } if (this.IsParameterBound(c => c.NodePoolLabel)) { defaultAgentPoolProfile.NodeLabels = new Dictionary <string, string>(); foreach (var key in NodePoolLabel.Keys) { defaultAgentPoolProfile.NodeLabels.Add(key.ToString(), NodePoolLabel[key].ToString()); } } defaultAgentPoolProfile.Mode = NodePoolMode; return(defaultAgentPoolProfile); }
protected virtual ManagedCluster BuildNewCluster() { BeforeBuildNewCluster(); var defaultAgentPoolProfile = new ManagedClusterAgentPoolProfile( name: NodeName ?? "default", count: NodeCount, vmSize: NodeVmSize, osDiskSizeGB: NodeOsDiskSize); if (this.IsParameterBound(c => c.NodeMinCount)) { defaultAgentPoolProfile.MinCount = NodeMinCount; } if (this.IsParameterBound(c => c.NodeMaxCount)) { defaultAgentPoolProfile.MaxCount = NodeMaxCount; } if (EnableNodeAutoScaling.IsPresent) { defaultAgentPoolProfile.EnableAutoScaling = EnableNodeAutoScaling.ToBool(); } var pubKey = new List <ContainerServiceSshPublicKey> { new ContainerServiceSshPublicKey(SshKeyValue) }; var linuxProfile = new ContainerServiceLinuxProfile(LinuxProfileAdminUserName, new ContainerServiceSshConfiguration(pubKey)); var acsServicePrincipal = EnsureServicePrincipal(ServicePrincipalIdAndSecret?.UserName, ServicePrincipalIdAndSecret?.Password?.ConvertToString()); var spProfile = new ManagedClusterServicePrincipalProfile( acsServicePrincipal.SpId, acsServicePrincipal.ClientSecret); WriteVerbose(string.Format(Resources.DeployingYourManagedKubeCluster, AcsSpFilePath)); var managedCluster = new ManagedCluster( Location, name: Name, tags: TagsConversionHelper.CreateTagDictionary(Tag, true), dnsPrefix: DnsNamePrefix, kubernetesVersion: KubernetesVersion, agentPoolProfiles: new List <ManagedClusterAgentPoolProfile> { defaultAgentPoolProfile }, linuxProfile: linuxProfile, servicePrincipalProfile: spProfile); return(managedCluster); }
private ManagedClusterAgentPoolProfile GetAgentPoolProfile() { var defaultAgentPoolProfile = new ManagedClusterAgentPoolProfile( name: NodeName ?? "default", count: NodeCount, vmSize: NodeVmSize, osDiskSizeGB: NodeOsDiskSize, type: NodeVmSetType ?? "AvailabilitySet", vnetSubnetID: NodeVnetSubnetID); if (this.IsParameterBound(c => c.NodeOsType)) { defaultAgentPoolProfile.OsType = NodeOsType; } if (this.IsParameterBound(c => c.NodeMaxPodCount)) { defaultAgentPoolProfile.MaxPods = NodeMaxPodCount; } if (this.IsParameterBound(c => c.NodeMinCount)) { defaultAgentPoolProfile.MinCount = NodeMinCount; } if (this.IsParameterBound(c => c.NodeMaxCount)) { defaultAgentPoolProfile.MaxCount = NodeMaxCount; } if (EnableNodeAutoScaling.IsPresent) { defaultAgentPoolProfile.EnableAutoScaling = EnableNodeAutoScaling.ToBool(); } //if (EnableNodePublicIp.IsPresent) //{ // defaultAgentPoolProfile.EnableNodePublicIP = EnableNodePublicIp.ToBool(); //} if (this.IsParameterBound(c => c.NodeScaleSetEvictionPolicy)) { defaultAgentPoolProfile.ScaleSetEvictionPolicy = NodeScaleSetEvictionPolicy; } if (this.IsParameterBound(c => c.NodeSetPriority)) { defaultAgentPoolProfile.ScaleSetPriority = NodeSetPriority; } defaultAgentPoolProfile.Mode = NodePoolMode; return(defaultAgentPoolProfile); }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ManagedCluster cluster = null; switch (ParameterSetName) { case IdParameterSet: { var resource = new ResourceIdentifier(Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } case InputObjectParameterSet: { WriteVerbose(Resources.UsingClusterFromPipeline); cluster = PSMapper.Instance.Map <ManagedCluster>(InputObject); var resource = new ResourceIdentifier(cluster.Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } } var msg = $"{Name} in {ResourceGroupName}"; if (ShouldProcess(msg, Resources.UpdateOrCreateAManagedKubernetesCluster)) { RunCmdLet(() => { AcsServicePrincipal acsServicePrincipal; if (Exists()) { if (cluster == null) { cluster = Client.ManagedClusters.Get(ResourceGroupName, Name); } if (this.IsParameterBound(c => c.Location)) { throw new AzPSArgumentException( Resources.LocationCannotBeUpdateForExistingCluster, nameof(Location), desensitizedMessage: Resources.LocationCannotBeUpdateForExistingCluster); } if (this.IsParameterBound(c => c.DnsNamePrefix)) { WriteVerbose(Resources.UpdatingDnsNamePrefix); cluster.DnsPrefix = DnsNamePrefix; } if (this.IsParameterBound(c => c.SshKeyValue)) { WriteVerbose(Resources.UpdatingSshKeyValue); cluster.LinuxProfile.Ssh.PublicKeys = new List <ContainerServiceSshPublicKey> { new ContainerServiceSshPublicKey(GetSshKey(SshKeyValue)) }; } if (this.IsParameterBound(c => c.ServicePrincipalIdAndSecret)) { WriteVerbose(Resources.UpdatingServicePrincipal); acsServicePrincipal = EnsureServicePrincipal(ServicePrincipalIdAndSecret.UserName, ServicePrincipalIdAndSecret.Password?.ConvertToString()); var spProfile = new ManagedClusterServicePrincipalProfile( acsServicePrincipal.SpId, acsServicePrincipal.ClientSecret); cluster.ServicePrincipalProfile = spProfile; } if (this.IsParameterBound(c => c.LinuxProfileAdminUserName)) { WriteVerbose(Resources.UpdatingAdminUsername); cluster.LinuxProfile.AdminUsername = LinuxProfileAdminUserName; } if (NeedUpdateNodeAgentPool()) { ManagedClusterAgentPoolProfile defaultAgentPoolProfile; string nodePoolName = "default"; if (this.IsParameterBound(c => c.NodeName)) { nodePoolName = NodeName; } if (cluster.AgentPoolProfiles.Any(x => x.Name == nodePoolName)) { defaultAgentPoolProfile = cluster.AgentPoolProfiles.First(x => x.Name == nodePoolName); } else { throw new AzPSArgumentException( Resources.SpecifiedAgentPoolDoesNotExist, nameof(Name), desensitizedMessage: Resources.SpecifiedAgentPoolDoesNotExist); } if (this.IsParameterBound(c => c.NodeMinCount)) { defaultAgentPoolProfile.MinCount = NodeMinCount; } if (this.IsParameterBound(c => c.NodeMaxCount)) { defaultAgentPoolProfile.MaxCount = NodeMaxCount; } if (this.IsParameterBound(c => c.EnableNodeAutoScaling)) { defaultAgentPoolProfile.EnableAutoScaling = EnableNodeAutoScaling.ToBool(); } if (this.IsParameterBound(c => c.NodeVmSize)) { WriteVerbose(Resources.UpdatingNodeVmSize); defaultAgentPoolProfile.VmSize = NodeVmSize; } if (this.IsParameterBound(c => c.NodeCount)) { WriteVerbose(Resources.UpdatingNodeCount); defaultAgentPoolProfile.Count = NodeCount; } if (this.IsParameterBound(c => c.NodeOsDiskSize)) { WriteVerbose(Resources.UpdatingNodeOsDiskSize); defaultAgentPoolProfile.OsDiskSizeGB = NodeOsDiskSize; } if (this.IsParameterBound(c => c.NodePoolMode)) { WriteVerbose(Resources.UpdatingNodePoolMode); defaultAgentPoolProfile.Mode = NodePoolMode; } } if (this.IsParameterBound(c => c.KubernetesVersion) && this.IsParameterBound(c => c.NodeImageOnly)) { throw new AzPSArgumentException(Resources.UpdateKubernetesVersionAndNodeImageOnlyConflict, "KubernetesVersion"); } bool allAgentPoolVirtualMachineScaleSets = cluster.AgentPoolProfiles.All(c => c.Type.ToLower().Equals("virtualmachinescalesets")); if (this.IsParameterBound(c => c.NodeImageOnly)) { if (!ShouldProcess(Resources.ConfirmOnlyUpgradeNodeVersion, "")) { return; } foreach (var agentPoolProfile in cluster.AgentPoolProfiles) { if (!allAgentPoolVirtualMachineScaleSets) { throw new AzPSApplicationException(Resources.NotUsingVirtualMachineScaleSets); } var agentPoolClient = Client.AgentPools.Get(ResourceGroupName, Name, agentPoolProfile.Name); AgentPool parameter = new AgentPool { Count = agentPoolClient.Count, VmSize = agentPoolClient.VmSize, OsDiskSizeGB = agentPoolClient.OsDiskSizeGB, MaxPods = agentPoolClient.MaxPods, Mode = agentPoolClient.Mode, OsType = agentPoolClient.OsType, OrchestratorVersion = cluster.KubernetesVersion, }; Client.AgentPools.CreateOrUpdate(ResourceGroupName, Name, agentPoolProfile.Name, parameter); } cluster = Client.ManagedClusters.Get(ResourceGroupName, Name); WriteObject(PSMapper.Instance.Map <PSKubernetesCluster>(cluster)); return; } if (this.IsParameterBound(c => c.KubernetesVersion)) { WriteVerbose(Resources.UpdatingKubernetesVersion); cluster.KubernetesVersion = KubernetesVersion; } bool upgradeAllNode = false; if (cluster.MaxAgentPools < 8 || !allAgentPoolVirtualMachineScaleSets) { if (this.IsParameterBound(c => c.ControlPlaneOnly)) { if (!ShouldProcess(string.Format(Resources.ConfirmControlPlaneOnlyInVMASCluster, KubernetesVersion), "")) { return; } } upgradeAllNode = true; } else { if (!this.IsParameterBound(c => c.ControlPlaneOnly)) { if (!ShouldProcess(string.Format(Resources.ConfirmNotControlPlaneOnly, KubernetesVersion), "")) { return; } upgradeAllNode = true; } else { if (!ShouldProcess(string.Format(Resources.ConfirmControlPlaneOnly, KubernetesVersion), "")) { return; } } } if (upgradeAllNode) { cluster.AgentPoolProfiles.ForEach(c => c.OrchestratorVersion = KubernetesVersion); } if (this.IsParameterBound(c => c.Tag)) { WriteVerbose(Resources.UpdatingTags); cluster.Tags = TagsConversionHelper.CreateTagDictionary(Tag, true); } //To avoid server error: for agentPoolProfiles.availabilityZones, server will expect //$null instead of empty collection, otherwise it will throw error. foreach (var profile in cluster.AgentPoolProfiles) { if (profile.AvailabilityZones?.Count == 0) { profile.AvailabilityZones = null; } } WriteVerbose(Resources.UpdatingYourManagedKubernetesCluster); } else { WriteVerbose(Resources.PreparingForDeploymentOfYourNewManagedKubernetesCluster); cluster = BuildNewCluster(); } if (this.IsParameterBound(c => c.AcrNameToAttach) || this.IsParameterBound(c => c.AcrNameToDetach)) { acsServicePrincipal = EnsureServicePrincipal(ServicePrincipalIdAndSecret?.UserName, ServicePrincipalIdAndSecret?.Password?.ConvertToString()); if (this.IsParameterBound(c => c.AcrNameToAttach)) { AddAcrRoleAssignment(AcrNameToAttach, nameof(AcrNameToAttach), acsServicePrincipal); } if (this.IsParameterBound(c => c.AcrNameToDetach)) { RemoveAcrRoleAssignment(AcrNameToDetach, nameof(AcrNameToDetach), acsServicePrincipal); } } cluster.NetworkProfile = SetNetworkProfile(cluster.NetworkProfile); cluster.ApiServerAccessProfile = CreateOrUpdateApiServerAccessProfile(cluster.ApiServerAccessProfile); if (this.IsParameterBound(c => c.FqdnSubdomain)) { cluster.FqdnSubdomain = FqdnSubdomain; } var kubeCluster = Client.ManagedClusters.CreateOrUpdate(ResourceGroupName, Name, cluster); WriteObject(PSMapper.Instance.Map <PSKubernetesCluster>(kubeCluster)); }); } }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ManagedCluster cluster = null; switch (ParameterSetName) { case IdParameterSet: { var resource = new ResourceIdentifier(Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } case InputObjectParameterSet: { WriteVerbose(Resources.UsingClusterFromPipeline); cluster = PSMapper.Instance.Map <ManagedCluster>(InputObject); var resource = new ResourceIdentifier(cluster.Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } } var msg = $"{Name} in {ResourceGroupName}"; if (ShouldProcess(msg, Resources.UpdateOrCreateAManagedKubernetesCluster)) { RunCmdLet(() => { if (Exists()) { if (cluster == null) { cluster = Client.ManagedClusters.Get(ResourceGroupName, Name); } if (this.IsParameterBound(c => c.Location)) { throw new CmdletInvocationException(Resources.LocationCannotBeUpdateForExistingCluster); } if (this.IsParameterBound(c => c.DnsNamePrefix)) { WriteVerbose(Resources.UpdatingDnsNamePrefix); cluster.DnsPrefix = DnsNamePrefix; } if (this.IsParameterBound(c => c.SshKeyValue)) { WriteVerbose(Resources.UpdatingSshKeyValue); cluster.LinuxProfile.Ssh.PublicKeys = new List <ContainerServiceSshPublicKey> { new ContainerServiceSshPublicKey(GetSshKey(SshKeyValue)) }; } if (this.IsParameterBound(c => c.ClientIdAndSecret)) { WriteVerbose(Resources.UpdatingServicePrincipal); var acsServicePrincipal = EnsureServicePrincipal(ClientIdAndSecret.UserName, ClientIdAndSecret.Password.ToString()); var spProfile = new ManagedClusterServicePrincipalProfile( acsServicePrincipal.SpId, acsServicePrincipal.ClientSecret); cluster.ServicePrincipalProfile = spProfile; } if (this.IsParameterBound(c => c.LinuxProfileAdminUserName)) { WriteVerbose(Resources.UpdatingAdminUsername); cluster.LinuxProfile.AdminUsername = LinuxProfileAdminUserName; } if (NeedUpdateNodeAgentPool()) { ManagedClusterAgentPoolProfile defaultAgentPoolProfile; string nodePoolName = "default"; if (this.IsParameterBound(c => c.NodeName)) { nodePoolName = NodeName; } if (cluster.AgentPoolProfiles.Any(x => x.Name == nodePoolName)) { defaultAgentPoolProfile = cluster.AgentPoolProfiles.First(x => x.Name == nodePoolName); } else { throw new PSArgumentException(Resources.SpecifiedAgentPoolDoesNotExist); } if (this.IsParameterBound(c => c.NodeMinCount)) { defaultAgentPoolProfile.MinCount = NodeMinCount; } if (this.IsParameterBound(c => c.NodeMaxCount)) { defaultAgentPoolProfile.MaxCount = NodeMaxCount; } if (this.IsParameterBound(c => c.EnableNodeAutoScaling)) { defaultAgentPoolProfile.EnableAutoScaling = EnableNodeAutoScaling.ToBool(); } if (this.IsParameterBound(c => c.NodeVmSize)) { WriteVerbose(Resources.UpdatingNodeVmSize); defaultAgentPoolProfile.VmSize = NodeVmSize; } if (this.IsParameterBound(c => c.NodeCount)) { WriteVerbose(Resources.UpdatingNodeCount); defaultAgentPoolProfile.Count = NodeCount; } if (this.IsParameterBound(c => c.NodeOsDiskSize)) { WriteVerbose(Resources.UpdatingNodeOsDiskSize); defaultAgentPoolProfile.OsDiskSizeGB = NodeOsDiskSize; } } if (this.IsParameterBound(c => c.KubernetesVersion)) { WriteVerbose(Resources.UpdatingKubernetesVersion); cluster.KubernetesVersion = KubernetesVersion; } if (this.IsParameterBound(c => c.Tag)) { WriteVerbose(Resources.UpdatingTags); cluster.Tags = TagsConversionHelper.CreateTagDictionary(Tag, true); } //To avoid server error: for agentPoolProfiles.availabilityZones, server will expect //$null instead of empty collection, otherwise it will throw error. foreach (var profile in cluster.AgentPoolProfiles) { if (profile.AvailabilityZones?.Count == 0) { profile.AvailabilityZones = null; } } WriteVerbose(Resources.UpdatingYourManagedKubernetesCluster); } else { WriteVerbose(Resources.PreparingForDeploymentOfYourNewManagedKubernetesCluster); cluster = BuildNewCluster(); } var kubeCluster = Client.ManagedClusters.CreateOrUpdate(ResourceGroupName, Name, cluster); WriteObject(PSMapper.Instance.Map <PSKubernetesCluster>(kubeCluster)); }); } }