protected override ManagedCluster BuildNewCluster() { BeforeBuildNewCluster(); var pubKey = new List <ContainerServiceSshPublicKey> { new ContainerServiceSshPublicKey(SshKeyValue) }; var linuxProfile = new ContainerServiceLinuxProfile(LinuxProfileAdminUserName, new ContainerServiceSshConfiguration(pubKey)); var acsServicePrincipal = EnsureServicePrincipal(ClientIdAndSecret?.UserName, ClientIdAndSecret?.Password?.ToString()); var spProfile = new ManagedClusterServicePrincipalProfile( acsServicePrincipal.SpId, acsServicePrincipal.ClientSecret); var aadProfile = GetAadProfile(); var defaultAgentPoolProfile = GetAgentPoolProfile(); var windowsProfile = GetWindowsProfile(); var networkProfile = GetNetworkProfile(); 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, windowsProfile: windowsProfile, servicePrincipalProfile: spProfile, aadProfile: aadProfile, networkProfile: networkProfile); if (EnableRbac.IsPresent) { managedCluster.EnableRBAC = EnableRbac; } //if(EnablePodSecurityPolicy.IsPresent) //{ // managedCluster.EnablePodSecurityPolicy = EnablePodSecurityPolicy; //} if (this.IsParameterBound(c => c.AcrNameToAttach)) { AddAcrRoleAssignment(AcrNameToAttach, acsServicePrincipal); } return(managedCluster); }
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); }
public override void ExecuteCmdlet() { base.ExecuteCmdlet(); switch (ParameterSetName) { case IdParameterSet: { var resource = new ResourceIdentifier(Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } case InputObjectParameterSet: { var resource = new ResourceIdentifier(InputObject.Id); ResourceGroupName = resource.ResourceGroupName; Name = resource.ResourceName; break; } } var msg = $"{Name} in {ResourceGroupName}"; ConfirmAction(Force.IsPresent, Resources.ResetTheCredentialOfAksCluster, Resources.ResetingTheCredentialOfAksCluster, msg, () => { RunCmdLet(() => { ManagedClusterServicePrincipalProfile servicePrincipalProfile = new ManagedClusterServicePrincipalProfile() { ClientId = ServicePrincipalIdAndSecret.UserName, Secret = ServicePrincipalIdAndSecret.Password.ConvertToString() }; Client.ManagedClusters.ResetServicePrincipalProfile(ResourceGroupName, Name, servicePrincipalProfile); if (PassThru) { WriteObject(true); } }); }); }
/// <summary> /// Reset Service Principal Profile of a managed cluster. /// </summary> /// <remarks> /// Update the service principal Profile for a managed cluster. /// </remarks> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='resourceGroupName'> /// The name of the resource group. /// </param> /// <param name='resourceName'> /// The name of the managed cluster resource. /// </param> /// <param name='parameters'> /// Parameters supplied to the Reset Service Principal Profile operation for a /// Managed Cluster. /// </param> /// <param name='cancellationToken'> /// The cancellation token. /// </param> public static async Task BeginResetServicePrincipalProfileAsync(this IManagedClustersOperations operations, string resourceGroupName, string resourceName, ManagedClusterServicePrincipalProfile parameters, CancellationToken cancellationToken = default(CancellationToken)) { (await operations.BeginResetServicePrincipalProfileWithHttpMessagesAsync(resourceGroupName, resourceName, parameters, null, cancellationToken).ConfigureAwait(false)).Dispose(); }
/// <summary> /// Reset Service Principal Profile of a managed cluster. /// </summary> /// <remarks> /// Update the service principal Profile for a managed cluster. /// </remarks> /// <param name='operations'> /// The operations group for this extension method. /// </param> /// <param name='resourceGroupName'> /// The name of the resource group. /// </param> /// <param name='resourceName'> /// The name of the managed cluster resource. /// </param> /// <param name='parameters'> /// Parameters supplied to the Reset Service Principal Profile operation for a /// Managed Cluster. /// </param> public static void BeginResetServicePrincipalProfile(this IManagedClustersOperations operations, string resourceGroupName, string resourceName, ManagedClusterServicePrincipalProfile parameters) { operations.BeginResetServicePrincipalProfileAsync(resourceGroupName, resourceName, parameters).GetAwaiter().GetResult(); }
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)); }); } }
private ManagedCluster BuildNewCluster() { BeforeBuildNewCluster(); var pubKey = new List <ContainerServiceSshPublicKey> { new ContainerServiceSshPublicKey(SshKeyValue) }; var linuxProfile = new ContainerServiceLinuxProfile(LinuxProfileAdminUserName, new ContainerServiceSshConfiguration(pubKey)); acsServicePrincipal = EnsureServicePrincipal(ServicePrincipalIdAndSecret?.UserName, ServicePrincipalIdAndSecret?.Password?.ConvertToString()); var spProfile = new ManagedClusterServicePrincipalProfile( acsServicePrincipal.SpId, acsServicePrincipal.ClientSecret); var aadProfile = GetAadProfile(); var defaultAgentPoolProfile = GetAgentPoolProfile(); var windowsProfile = GetWindowsProfile(); var networkProfile = GetNetworkProfile(); var apiServerAccessProfile = CreateOrUpdateApiServerAccessProfile(null); var addonProfiles = CreateAddonsProfiles(); 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, windowsProfile: windowsProfile, servicePrincipalProfile: spProfile, aadProfile: aadProfile, addonProfiles: addonProfiles, networkProfile: networkProfile, apiServerAccessProfile: apiServerAccessProfile); if (EnableRbac.IsPresent) { managedCluster.EnableRBAC = EnableRbac; } if (this.IsParameterBound(c => c.FqdnSubdomain)) { managedCluster.FqdnSubdomain = FqdnSubdomain; } //if(EnablePodSecurityPolicy.IsPresent) //{ // managedCluster.EnablePodSecurityPolicy = EnablePodSecurityPolicy; //} return(managedCluster); }
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)); }); } }