Beispiel #1
0
 public Monitor_AdapterAnonymousInnerClass(TxPushStrategyConfigIT outerInstance, ManagedCluster cluster)
 {
     this.outerInstance = outerInstance;
     this._cluster      = cluster;
 }
 /// <summary>
 /// Creates or updates a Service Fabric managed cluster resource.
 /// </summary>
 /// <remarks>
 /// Create or update a Service Fabric managed cluster resource with the
 /// specified name.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='clusterName'>
 /// The name of the cluster resource.
 /// </param>
 /// <param name='parameters'>
 /// The cluster resource.
 /// </param>
 public static ManagedCluster CreateOrUpdate(this IManagedClustersOperations operations, string resourceGroupName, string clusterName, ManagedCluster parameters)
 {
     return(operations.CreateOrUpdateAsync(resourceGroupName, clusterName, parameters).GetAwaiter().GetResult());
 }
Beispiel #3
0
 private int CreateTransactionOnMaster(ManagedCluster cluster)
 {
     return(CreateTransaction(cluster, cluster.Master));
 }
Beispiel #4
0
 private int CreateTransactionOn(ManagedCluster cluster, InstanceId serverId)
 {
     return(CreateTransaction(cluster, cluster.GetMemberByServerId(serverId)));
 }
Beispiel #5
0
 /// <summary>
 /// Creates or updates a Service Fabric managed cluster resource.
 /// </summary>
 /// <remarks>
 /// Create or update a Service Fabric managed cluster resource with the
 /// specified name.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='clusterName'>
 /// The name of the cluster resource.
 /// </param>
 /// <param name='parameters'>
 /// The cluster resource.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <ManagedCluster> BeginCreateOrUpdateAsync(this IManagedClustersOperations operations, string resourceGroupName, string clusterName, ManagedCluster parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.BeginCreateOrUpdateWithHttpMessagesAsync(resourceGroupName, clusterName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Beispiel #6
0
    public MyCluster(MyConfig cfg)
    {
        var resourceGroup = new ResourceGroup("rg");

        var adApp = new Application("app");

        var adSp = new ServicePrincipal("service-principal", new ServicePrincipalArgs
        {
            ApplicationId = adApp.ApplicationId
        });

        var adSpPassword = new ServicePrincipalPassword("sp-password", new ServicePrincipalPasswordArgs
        {
            ServicePrincipalId = adSp.Id,
            Value   = cfg.Password,
            EndDate = "2099-01-01T00:00:00Z"
        });

        var k8sCluster = new ManagedCluster("cluster", new ManagedClusterArgs
        {
            ResourceGroupName = resourceGroup.Name,
            AddonProfiles     =
            {
                ["KubeDashboard"] = new ManagedClusterAddonProfileArgs {
                    Enabled = true
                }
            },
            AgentPoolProfiles =
            {
                new ManagedClusterAgentPoolProfileArgs
                {
                    Count        = cfg.NodeCount,
                    VmSize       = cfg.NodeSize,
                    MaxPods      = 110,
                    Mode         = "System",
                    Name         = "agentpool",
                    OsDiskSizeGB = 30,
                    OsType       = "Linux",
                    Type         = "VirtualMachineScaleSets"
                }
            },
            DnsPrefix         = resourceGroup.Name,
            EnableRBAC        = true,
            KubernetesVersion = cfg.K8SVersion,
            LinuxProfile      = new ContainerServiceLinuxProfileArgs
            {
                AdminUsername = cfg.AdminUserName,
                Ssh           = new ContainerServiceSshConfigurationArgs
                {
                    PublicKeys = new ContainerServiceSshPublicKeyArgs
                    {
                        KeyData = cfg.SshPublicKey
                    }
                }
            },
            NodeResourceGroup       = "node-resource-group",
            ServicePrincipalProfile = new ManagedClusterServicePrincipalProfileArgs
            {
                ClientId = adApp.ApplicationId,
                Secret   = adSpPassword.Value
            }
        });

        this.ClusterName = k8sCluster.Name;

        this.Kubeconfig = Output.Tuple(k8sCluster.Name, resourceGroup.Name)
                          .Apply(pair =>
        {
            var k8sClusterName    = pair.Item1;
            var resourceGroupName = pair.Item2;

            return(ListManagedClusterUserCredentials.InvokeAsync(new ListManagedClusterUserCredentialsArgs
            {
                ResourceGroupName = resourceGroupName,
                ResourceName = k8sClusterName
            }));
        })
                          .Apply(x => x.Kubeconfigs[0].Value)
                          .Apply(Convert.FromBase64String)
                          .Apply(Encoding.UTF8.GetString);

        this.Provider = new K8s.Provider("k8s-provider", new K8s.ProviderArgs
        {
            KubeConfig = Kubeconfig
        });
    }
Beispiel #7
0
    public MyStack()
    {
        // Create an Azure Resource Group
        var resourceGroup = new ResourceGroup("azure-cs-aks");

        // Create an AD service principal
        var adApp = new Application("aks", new ApplicationArgs
        {
            DisplayName = "aks"
        });
        var adSp = new ServicePrincipal("aksSp", new ServicePrincipalArgs
        {
            ApplicationId = adApp.ApplicationId
        });

        // Generate random password
        var password = new RandomPassword("password", new RandomPasswordArgs
        {
            Length  = 20,
            Special = true
        });

        // Create the Service Principal Password
        var adSpPassword = new ServicePrincipalPassword("aksSpPassword", new ServicePrincipalPasswordArgs
        {
            ServicePrincipalId = adSp.Id,
            Value   = password.Result,
            EndDate = "2099-01-01T00:00:00Z"
        });

        // Generate an SSH key
        var sshKey = new PrivateKey("ssh-key", new PrivateKeyArgs
        {
            Algorithm = "RSA",
            RsaBits   = 4096
        });

        var cluster = new ManagedCluster("my-aks", new ManagedClusterArgs
        {
            ResourceGroupName = resourceGroup.Name,
            AgentPoolProfiles =
            {
                new ManagedClusterAgentPoolProfileArgs
                {
                    Count        = 3,
                    MaxPods      = 110,
                    Mode         = "System",
                    Name         = "agentpool",
                    OsDiskSizeGB = 30,
                    OsType       = "Linux",
                    Type         = "VirtualMachineScaleSets",
                    VmSize       = "Standard_DS2_v2",
                }
            },
            DnsPrefix         = "AzureNativeprovider",
            EnableRBAC        = true,
            KubernetesVersion = "1.18.14",
            LinuxProfile      = new ContainerServiceLinuxProfileArgs
            {
                AdminUsername = "******",
                Ssh           = new ContainerServiceSshConfigurationArgs
                {
                    PublicKeys =
                    {
                        new ContainerServiceSshPublicKeyArgs
                        {
                            KeyData = sshKey.PublicKeyOpenssh,
                        }
                    }
                }
            },
            NodeResourceGroup       = $"MC_azure-cs_my_aks",
            ServicePrincipalProfile = new ManagedClusterServicePrincipalProfileArgs
            {
                ClientId = adApp.ApplicationId,
                Secret   = adSpPassword.Value
            }
        });

        // Export the KubeConfig
        this.KubeConfig = GetKubeConfig(resourceGroup.Name, cluster.Name);
    }
        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 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);
                            var 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))
                        {
                            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));
                });
            }
        }
Beispiel #9
0
        protected override ManagedCluster BuildNewCluster()
        {
            BeforeBuildNewCluster();

            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);

            var aadProfile = GetAadProfile();

            var defaultAgentPoolProfile = GetAgentPoolProfile();

            var windowsProfile = GetWindowsProfile();

            var networkProfile = GetNetworkProfile();

            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);

            if (EnableRbac.IsPresent)
            {
                managedCluster.EnableRBAC = EnableRbac;
            }
            //if(EnablePodSecurityPolicy.IsPresent)
            //{
            //    managedCluster.EnablePodSecurityPolicy = EnablePodSecurityPolicy;
            //}

            if (this.IsParameterBound(c => c.AcrNameToAttach))
            {
                AddAcrRoleAssignment(AcrNameToAttach, acsServicePrincipal);
            }

            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(() =>
                {
                    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));
                });
            }
        }
Beispiel #11
0
        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 (MyInvocation.BoundParameters.ContainsKey("Location"))
                        {
                            throw new CmdletInvocationException(Resources.LocationCannotBeUpdateForExistingCluster);
                        }

                        if (MyInvocation.BoundParameters.ContainsKey("DnsNamePrefix"))
                        {
                            WriteVerbose(Resources.UpdatingDnsNamePrefix);
                            cluster.DnsPrefix = DnsNamePrefix;
                        }

                        if (MyInvocation.BoundParameters.ContainsKey("SshKeyValue"))
                        {
                            WriteVerbose(Resources.UpdatingSshKeyValue);
                            cluster.LinuxProfile.Ssh.PublicKeys = new List <ContainerServiceSshPublicKey>
                            {
                                new ContainerServiceSshPublicKey(GetSshKey(SshKeyValue))
                            };
                        }

                        if (ParameterSetName == SpParamSet)
                        {
                            WriteVerbose(Resources.UpdatingServicePrincipal);
                            var acsServicePrincipal = EnsureServicePrincipal(ClientIdAndSecret.UserName, ClientIdAndSecret.Password.ToString());

                            var spProfile = new ContainerServiceServicePrincipalProfile(
                                acsServicePrincipal.SpId,
                                acsServicePrincipal.ClientSecret);
                            cluster.ServicePrincipalProfile = spProfile;
                        }

                        if (MyInvocation.BoundParameters.ContainsKey("AdminUserName"))
                        {
                            WriteVerbose(Resources.UpdatingAdminUsername);
                            cluster.LinuxProfile.AdminUsername = AdminUserName;
                        }

                        ContainerServiceAgentPoolProfile defaultAgentPoolProfile;
                        if (cluster.AgentPoolProfiles.Any(x => x.Name == "default"))
                        {
                            defaultAgentPoolProfile = cluster.AgentPoolProfiles.First(x => x.Name == "default");
                        }
                        else
                        {
                            defaultAgentPoolProfile = new ContainerServiceAgentPoolProfile(
                                "default",
                                NodeVmSize,
                                NodeCount,
                                NodeOsDiskSize,
                                DnsNamePrefix ?? DefaultDnsPrefix());
                            cluster.AgentPoolProfiles.Add(defaultAgentPoolProfile);
                        }

                        if (MyInvocation.BoundParameters.ContainsKey("NodeVmSize"))
                        {
                            WriteVerbose(Resources.UpdatingNodeVmSize);
                            defaultAgentPoolProfile.VmSize = NodeVmSize;
                        }

                        if (MyInvocation.BoundParameters.ContainsKey("NodeCount"))
                        {
                            WriteVerbose(Resources.UpdatingNodeCount);
                            defaultAgentPoolProfile.Count = NodeCount;
                        }

                        if (MyInvocation.BoundParameters.ContainsKey("NodeOsDiskSize"))
                        {
                            WriteVerbose(Resources.UpdatingNodeOsDiskSize);
                            defaultAgentPoolProfile.OsDiskSizeGB = NodeOsDiskSize;
                        }

                        if (MyInvocation.BoundParameters.ContainsKey("KubernetesVersion"))
                        {
                            WriteVerbose(Resources.UpdatingKubernetesVersion);
                            cluster.KubernetesVersion = KubernetesVersion;
                        }

                        if (MyInvocation.BoundParameters.ContainsKey("Tag"))
                        {
                            WriteVerbose(Resources.UpdatingTags);
                            cluster.Tags = TagsConversionHelper.CreateTagDictionary(Tag, true);
                        }

                        WriteVerbose(Resources.UpdatingYourManagedKubernetesCluster);
                    }
                    else
                    {
                        WriteVerbose(Resources.PreparingForDeploymentOfYourNewManagedKubernetesCluster);
                        cluster = BuildNewCluster();
                    }

                    var kubeCluster = Client.ManagedClusters.CreateOrUpdate(ResourceGroupName, Name, cluster);
                    WriteObject(PSMapper.Instance.Map <PSKubernetesCluster>(kubeCluster));
                });
            }
        }