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"); }
/** * Creates subnets in a VCN and waits for the subnets to become available to use. * * @param vcnClient the service client to use to create the subnet * @param compartmentId the OCID of the compartment which owns the VCN * @param availabilityDomain the availability domain where the subnet will be created * @param cidrBlock the cidr block used to create subnet * @param vcnId the ID of the VCN which will own the subnet * @param subnetName the subnet that will be created * * @return the created subnets * */ private static async Task <List <Subnet> > CreateSubnet(VirtualNetworkClient virtualNetworkClient, string compartmentId, IdentityModels.AvailabilityDomain availabilityDomain, string vcnId) { List <Subnet> subnets = new List <Subnet>(); for (int i = 0; i < 2; i++) { CreateSubnetDetails createSubnetDetails = new CreateSubnetDetails { AvailabilityDomain = availabilityDomain.Name, CompartmentId = compartmentId, DisplayName = SubnetDisplayNames[i], CidrBlock = SubnetCidrBlocks[i], VcnId = vcnId }; CreateSubnetRequest createSubnetRequest = new CreateSubnetRequest { CreateSubnetDetails = createSubnetDetails }; CreateSubnetResponse createSubnetResponse = await virtualNetworkClient.CreateSubnet(createSubnetRequest); GetSubnetRequest getSubnetRequest = new GetSubnetRequest { SubnetId = createSubnetResponse.Subnet.Id }; GetSubnetResponse getSubnetResponse = virtualNetworkClient.Waiters.ForSubnet(getSubnetRequest, Subnet.LifecycleStateEnum.Available).Execute(); Subnet subnet = getSubnetResponse.Subnet; logger.Info($"Created Subnet: {subnet.Id}"); subnets.Add(getSubnetResponse.Subnet); } return(subnets); }