/**
         * Update a cluster and waits for it to become active
         *
         * @param containerEngineClient the service client to use to delete the cluster
         * @param clusterId the cluster ID
         * @param newClusterName The new cluster name
         *
         */
        private static async Task UpdateCluster(
            ContainerEngineClient containerEngineClient, string clusterId, string newClusterName)
        {
            UpdateClusterDetails updateClusterDetails = new UpdateClusterDetails
            {
                Name = newClusterName
            };
            UpdateClusterRequest updateClusterRequest = new UpdateClusterRequest
            {
                ClusterId            = clusterId,
                UpdateClusterDetails = updateClusterDetails
            };
            UpdateClusterResponse updateClusterResponse = await containerEngineClient.UpdateCluster(updateClusterRequest);

            string workRequestId = updateClusterResponse.OpcWorkRequestId;
            GetWorkRequestResponse workRequestResponse = WaitForWorkRequestFinished(containerEngineClient, workRequestId);

            GetClusterRequest getClusterRequest = new GetClusterRequest
            {
                ClusterId = clusterId
            };
            GetClusterResponse getClusterResponse = await containerEngineClient.GetCluster(getClusterRequest);

            Cluster cluster = getClusterResponse.Cluster;

            logger.Info($"Cluster name is changed to {cluster.Name}");
        }
示例#2
0
 protected override void ProcessRecord()
 {
     base.ProcessRecord();
     client?.Dispose();
     client = new ContainerEngineClient(AuthProvider, new Oci.Common.ClientConfiguration
     {
         RetryConfiguration = retryConfig,
         TimeoutMillis      = TimeOutInMillis,
         ClientUserAgent    = PSUserAgent
     });
     try
     {
         string region = GetPreferredRegion();
         if (region != null)
         {
             WriteDebug("Choosing Region:" + region);
             client.SetRegion(region);
         }
         if (Endpoint != null)
         {
             WriteDebug("Choosing Endpoint:" + Endpoint);
             client.SetEndpoint(Endpoint);
         }
     }
     catch (Exception ex)
     {
         TerminatingErrorDuringExecution(ex);
     }
 }
        /**
         * Update a node pool and waits until the work request finished
         *
         * @param containerEngineClient the service client to use to update the node pool
         * @param nodePoolId the node pool ID
         * @param newNodePoolName The new node pool name
         *
         */
        private static async Task UpdateNodePool(ContainerEngineClient containerEngineClient, string nodePoolId,
                                                 string newDisplayName)
        {
            var updateNodePoolDetails = new UpdateNodePoolDetails
            {
                Name = newDisplayName
            };
            var updateNodePoolRequest = new UpdateNodePoolRequest
            {
                NodePoolId            = nodePoolId,
                UpdateNodePoolDetails = updateNodePoolDetails
            };
            var updateNodePoolResponse = await containerEngineClient.UpdateNodePool(updateNodePoolRequest);

            string workRequestId       = updateNodePoolResponse.OpcWorkRequestId;
            var    workRequestResponse = WaitForWorkRequestFinished(containerEngineClient, workRequestId);

            GetNodePoolRequest getNodePoolRequest = new GetNodePoolRequest
            {
                NodePoolId = nodePoolId
            };
            GetNodePoolResponse getNodePoolResponse = await containerEngineClient.GetNodePool(getNodePoolRequest);

            NodePool nodePool = getNodePoolResponse.NodePool;

            logger.Info($"Node pool name changed to {nodePool.Name}");
        }
        /**
         * Deletes a node pool and waits for it to be deleted.
         *
         * @param containerEngineClient the service client to use to delete the node pool
         * @param nodePoolId the ID of the node pool to be deleted
         *
         */
        private static async Task DeleteNodePool(ContainerEngineClient containerEngineClient, string nodePoolId)
        {
            DeleteNodePoolRequest deleteNodePoolRequest = new DeleteNodePoolRequest
            {
                NodePoolId = nodePoolId
            };
            DeleteNodePoolResponse deleteNodePoolResponse = await containerEngineClient.DeleteNodePool(deleteNodePoolRequest);

            string workRequestId = deleteNodePoolResponse.OpcWorkRequestId;

            WaitForWorkRequestFinished(containerEngineClient, workRequestId);
        }
        /**
         * Deletes a cluster and waits for it to be deleted.
         *
         * @param containerEngineClient the service client to use to delete the cluster
         * @param clusterId the ID of the cluster to be deleted
         *
         */
        private static async Task DeleteCluster(ContainerEngineClient containerEngineClient, string clusterId)
        {
            DeleteClusterRequest deleteClusterRequest = new DeleteClusterRequest
            {
                ClusterId = clusterId
            };
            DeleteClusterResponse deleteClusterResponse = await containerEngineClient.DeleteCluster(deleteClusterRequest);

            string workRequestId = deleteClusterResponse.OpcWorkRequestId;

            WaitForWorkRequestFinished(containerEngineClient, workRequestId);
        }
        /**
         * Retrieve a list of Kubernetes versions
         *
         * @param containerEngineClient the service client to use to retrieve Kubernetes versions
         *
         * @return the first one in the list of Kubernetes versions
         *
         */
        private static async Task <string> GetKubernetesVersion(ContainerEngineClient containerEngineClient)
        {
            GetClusterOptionsRequest getRequest = new GetClusterOptionsRequest
            {
                ClusterOptionId = "all"
            };
            GetClusterOptionsResponse getResponse = await containerEngineClient.GetClusterOptions(getRequest);

            string kubernetesVersion = getResponse.ClusterOptions.KubernetesVersions[0];

            logger.Info($"Kubernetes version: {kubernetesVersion}");
            return(kubernetesVersion);
        }
        /**
         * Wait for a work request finished
         * @param containerEngineClient the service client to use to get work request
         * @param workRequestId the id of work request
         *
         * @return a work request response object
         */
        private static GetWorkRequestResponse WaitForWorkRequestFinished(ContainerEngineClient containerEngineClient, string workRequestId)
        {
            var waiterConfiguration = new WaiterConfiguration
            {
                MaxAttempts           = 20,
                GetNextDelayInSeconds = DelayStrategy.GetExponentialDelayInSeconds
            };
            GetWorkRequestRequest getWorkRequestRequest = new GetWorkRequestRequest
            {
                WorkRequestId = workRequestId
            };

            return(containerEngineClient.Waiters.ForWorkRequest(getWorkRequestRequest, waiterConfiguration, WorkRequestStatus.Succeeded).Execute());
        }
        /**
         * Creates a Cluster and waits for it to become active
         *
         * @param containerEngineClient the containerEngineclient used to create the cluster
         * @param vcnId the ID of the VCN which will own the subnets
         * @param subnetIds list of subnet ids
         * @param kubernetesVersion kubernetesVersion
         * @param compartmentId
         *
         * @return the created cluster
         */
        private static async Task <Cluster> CreateCluster(
            ContainerEngineClient containerEngineClient, string vcnId, List <string> subnetIds,
            string kubernetesVersion, string compartmentId)
        {
            logger.Info("Creating Cluster.......");

            CreateClusterDetails createClusterDetails = new CreateClusterDetails
            {
                Name              = ClusterDisplayName,
                CompartmentId     = compartmentId,
                VcnId             = vcnId,
                KubernetesVersion = kubernetesVersion,
                Options           = new ClusterCreateOptions
                {
                    ServiceLbSubnetIds = subnetIds
                }
            };
            CreateClusterRequest createClusterRequest = new CreateClusterRequest
            {
                CreateClusterDetails = createClusterDetails
            };
            CreateClusterResponse clusterResponse = await containerEngineClient.CreateCluster(createClusterRequest);

            string workRequestId = clusterResponse.OpcWorkRequestId;

            logger.Info($"cluster work request ID is {workRequestId}");

            GetWorkRequestResponse workRequestResponse = WaitForWorkRequestFinished(containerEngineClient, workRequestId);

            var clusterId = GetClusterId(workRequestResponse);

            logger.Info($"cluster ID is {clusterId}");

            GetClusterRequest getClusterRequest = new GetClusterRequest
            {
                ClusterId = clusterId
            };
            GetClusterResponse getClusterResponse = await containerEngineClient.GetCluster(getClusterRequest);

            return(getClusterResponse.Cluster);
        }
        /**
         * Creates a node pool in a cluster and waits until the work request finished
         *
         * @param containerEngineClient the containerEngineclient used to create the node pool
         * @param compartmentId The compartment ID
         * @param clusterId The ID of the cluster that the node pool is added in
         * @param displayName The display name of the node pool
         * @param kubernetesVersion kubernetesVersion
         * @param nodeImageName The image to use on each node in the node pool
         * @param nodeShape The number of CPUs and the amount of memory allocated to each node in the node pool
         * @param initialNodeLabels The initial node label
         * @param nodePoolNodeConfigDetails The node pool size and the placementConfig of nodes.
         * @return the created node pool
         */
        private static async Task <NodePool> CreateNodePool(ContainerEngineClient containerEngineClient, string compartmentId,
                                                            string clusterId, string displayName, string kubernetesVersion, string nodeImageName,
                                                            string nodeShape, List <KeyValue> initialNodeLabels, CreateNodePoolNodeConfigDetails nodePoolNodeConfigDetails)
        {
            var createNodePoolDetails = new CreateNodePoolDetails
            {
                CompartmentId     = compartmentId,
                ClusterId         = clusterId,
                Name              = displayName,
                KubernetesVersion = kubernetesVersion,
                NodeImageName     = nodeImageName,
                InitialNodeLabels = initialNodeLabels,
                NodeConfigDetails = nodePoolNodeConfigDetails,
                NodeShape         = nodeShape
            };
            var createNodePoolRequest = new CreateNodePoolRequest
            {
                CreateNodePoolDetails = createNodePoolDetails
            };
            var createNodePoolResponse = await containerEngineClient.CreateNodePool(createNodePoolRequest);

            string workRequestId = createNodePoolResponse.OpcWorkRequestId;

            logger.Info($"Create node pool work request ID: {workRequestId}");

            GetWorkRequestResponse workRequestResponse = WaitForWorkRequestFinished(containerEngineClient, workRequestId);
            var nodePoolId = GetWorkRequestResourceId(workRequestResponse, "nodepool");

            logger.Info($"Node pool ID is {nodePoolId}");

            GetNodePoolRequest getNodePoolRequest = new GetNodePoolRequest
            {
                NodePoolId = nodePoolId
            };
            GetNodePoolResponse getNodePoolResponse = await containerEngineClient.GetNodePool(getNodePoolRequest);

            return(getNodePoolResponse.NodePool);
        }
        public static async Task MainContainerEngine(string[] args)
        {
            logger.Info("Starting example");

            string compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID");
            var    provider      = new ConfigFileAuthenticationDetailsProvider(OciConfigProfileName);

            var containerEngineClient = new ContainerEngineClient(provider);
            var vcnClient             = new VirtualNetworkClient(provider);
            var identityClient        = new IdentityClient(provider);

            Vcn           vcn     = null;
            List <Subnet> subnets = null;

            Cluster cluster = null;

            try
            {
                IdentityModels.AvailabilityDomain availablityDomain = await GetAvailabilityDomain(identityClient, compartmentId);

                logger.Info($"availability domain is {availablityDomain.Name}");

                vcn = await CreateVcn(vcnClient, compartmentId);

                subnets = await CreateSubnet(vcnClient, compartmentId, availablityDomain, vcn.Id);

                //Create a Container Engine Cluster
                List <string> subnetIds = new List <string>();
                foreach (Subnet subnet in subnets)
                {
                    subnetIds.Add(subnet.Id);
                }

                var kubernetesVersion = await GetKubernetesVersion(containerEngineClient);

                cluster = await CreateCluster(containerEngineClient, vcn.Id, subnetIds, kubernetesVersion, compartmentId);

                // Update the container engine cluster
                await UpdateCluster(containerEngineClient, cluster.Id, ClusterNewDisplayName);
            }
            catch (Exception e)
            {
                logger.Error($"Failed to create container engine cluster: {e}");
            }
            finally
            {
                logger.Info("Cleaning up...");

                if (cluster != null)
                {
                    await DeleteCluster(containerEngineClient, cluster.Id);
                }

                for (int i = 0; i < 2; i++)
                {
                    if (subnets[i] != null)
                    {
                        await DeleteSubnet(vcnClient, subnets[i]);
                    }
                }

                if (vcn != null)
                {
                    await DeleteVcn(vcnClient, vcn);
                }

                containerEngineClient.Dispose();
                vcnClient.Dispose();
                identityClient.Dispose();
            }
            logger.Info("End example");
        }
        public static async Task MainContainerEngineNodePool()
        {
            logger.Info("Starting example");

            string compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID");
            var    provider      = new ConfigFileAuthenticationDetailsProvider(OciConfigProfileName);

            var containerEngineClient = new ContainerEngineClient(provider);
            var vcnClient             = new VirtualNetworkClient(provider);
            var identityClient        = new IdentityClient(provider);

            Vcn           vcn           = null;
            List <Subnet> subnets       = null;
            List <string> lbSubnetIds   = new List <string>();
            List <string> poolSubnetIds = new List <string>();
            Cluster       cluster       = null;
            NodePool      nodePool      = null;

            try
            {
                List <IdentityModels.AvailabilityDomain> availablityDomains = await GetAvailabilityDomains(identityClient, compartmentId);

                vcn = await CreateVcn(vcnClient, compartmentId);

                subnets = await CreateSubnets(vcnClient, compartmentId, vcn);

                lbSubnetIds.Add(subnets[0].Id);
                poolSubnetIds.Add(subnets[1].Id);

                var kubernetesVersion = await GetKubernetesVersion(containerEngineClient);

                cluster = await CreateCluster(containerEngineClient, vcn.Id, lbSubnetIds, kubernetesVersion, compartmentId);

                // Add node pool in the cluster
                KeyValue keyValue = new KeyValue
                {
                    Key   = "key1",
                    Value = "value1"
                };
                List <KeyValue> initialNodeLabels = new List <KeyValue> {
                    keyValue
                };

                List <NodePoolPlacementConfigDetails> nodePoolPlacementConfigDetails = new List <NodePoolPlacementConfigDetails>();
                foreach (var availabilityDomain in availablityDomains)
                {
                    nodePoolPlacementConfigDetails.Add(new NodePoolPlacementConfigDetails
                    {
                        AvailabilityDomain = availabilityDomain.Name,
                        SubnetId           = poolSubnetIds[0]
                    });
                }
                ;

                var createNodePoolNodeConfigDetails = new CreateNodePoolNodeConfigDetails
                {
                    Size             = availablityDomains.Count,
                    PlacementConfigs = nodePoolPlacementConfigDetails
                };
                nodePool = await CreateNodePool(containerEngineClient, compartmentId, cluster.Id, NodePoolDisplayName,
                                                kubernetesVersion, NodeImageName, NodeShape, initialNodeLabels, createNodePoolNodeConfigDetails);

                logger.Info("Created node pool");

                // Update the node pool
                await UpdateNodePool(containerEngineClient, nodePool.Id, NewNodePoolDisplayName);
            }
            catch (Exception e)
            {
                logger.Error($"Failed to create container engine cluster: {e}");
            }
            finally
            {
                logger.Info("Cleaning up...");

                if (nodePool != null)
                {
                    await DeleteNodePool(containerEngineClient, nodePool.Id);
                }
                if (cluster != null)
                {
                    await DeleteCluster(containerEngineClient, cluster.Id);
                }
                for (int i = 0; i < 2; i++)
                {
                    if (subnets[i] != null)
                    {
                        await DeleteSubnet(vcnClient, subnets[i]);
                    }
                }
                if (vcn != null)
                {
                    await DeleteVcn(vcnClient, vcn);
                }

                containerEngineClient.Dispose();
                vcnClient.Dispose();
                identityClient.Dispose();
            }
            logger.Info("End example");
        }