private static Iaas loadIaas(IaasProvider iaasProvider) throws InvalidIaasProviderException { try { if (iaasProvider.getClassName() == null) { String msg = "You have not specified a class which represents the iaas of type: [" + iaasProvider.getType() + "]."; log.error(msg); throw new InvalidIaasProviderException(msg); } Constructor<?> c = Class.forName(iaasProvider.getClassName()).getConstructor(IaasProvider.class); Iaas iaas = (Iaas) c.newInstance(iaasProvider); return iaas; } catch (Exception e) { String msg = "Class [" + iaasProvider.getClassName() + "] which represents the iaas of type: [" + iaasProvider.getType() + "] has failed to instantiate."; log.error(msg, e); throw new InvalidIaasProviderException(msg, e); } }
public static Iaas setDefaultIaas(IaasProvider iaasProvider) throws InvalidIaasProviderException { Iaas iaas = loadIaas(iaasProvider); try { ComputeServiceBuilderUtil.buildDefaultComputeService(iaasProvider); iaasProvider.setIaas(iaas); return iaas; } catch (Exception e) { String msg = "Unable to build the jclouds object for iaas " + "of type: " + iaasProvider.getType(); log.error(msg, e); throw new InvalidIaasProviderException(msg, e); } }
public static Iaas getIaas(IaasProvider iaasProvider) throws InvalidIaasProviderException { if (iaasProvider.getImage() != null) { return setIaas(iaasProvider); } else { return setDefaultIaas(iaasProvider); } }
/** * Create new pod and pass environment variables. * * @param memberContext * @param kubernetesApi * @param kubernetesClusterContext * @throws KubernetesClientException */ private void createPod( ClusterContext clusterContext, MemberContext memberContext, KubernetesApiClient kubernetesApi, KubernetesClusterContext kubernetesClusterContext) throws KubernetesClientException { String applicationId = memberContext.getApplicationId(); String cartridgeType = memberContext.getCartridgeType(); String clusterId = memberContext.getClusterId(); String memberId = memberContext.getMemberId(); if (log.isInfoEnabled()) { log.info( String.format( "Creating kubernetes pod: [application] %s [cartridge] %s [member] %s", applicationId, cartridgeType, memberId)); } Partition partition = memberContext.getPartition(); if (partition == null) { String message = String.format( "Partition not found in member context: [application] %s [cartridge] %s " + "[member] %s ", applicationId, cartridgeType, memberId); log.error(message); throw new RuntimeException(message); } Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType); if (cartridge == null) { String message = "Could not find cartridge: [cartridge] " + cartridgeType; log.error(message); throw new RuntimeException(message); } // Set default values to zero to avoid cpu and memory restrictions int cpu = Integer.getInteger(KUBERNETES_CONTAINER_CPU_DEFAULT, 0); int memory = Integer.getInteger(KUBERNETES_CONTAINER_MEMORY_DEFAULT, 0); Property cpuProperty = cartridge.getProperties().getProperty(KUBERNETES_CONTAINER_CPU); if (cpuProperty != null) { cpu = Integer.parseInt(cpuProperty.getValue()); } Property memoryProperty = cartridge.getProperties().getProperty(KUBERNETES_CONTAINER_MEMORY); if (memoryProperty != null) { memory = Integer.parseInt(memoryProperty.getValue()); } IaasProvider iaasProvider = CloudControllerContext.getInstance() .getIaasProviderOfPartition(cartridge.getUuid(), partition.getUuid()); if (iaasProvider == null) { String message = "Could not find iaas provider: [partition] " + partition.getUuid(); log.error(message); throw new RuntimeException(message); } // Add dynamic payload to the member context memberContext.setDynamicPayload(payload.toArray(new NameValuePair[payload.size()])); // Create pod long podSeqNo = kubernetesClusterContext.getPodSeqNo().incrementAndGet(); String podId = "pod" + "-" + podSeqNo; String podLabel = DigestUtils.md5Hex(clusterId); String dockerImage = iaasProvider.getImage(); List<EnvVar> environmentVariables = KubernetesIaasUtil.prepareEnvironmentVariables(clusterContext, memberContext); List<ContainerPort> ports = KubernetesIaasUtil.convertPortMappings(Arrays.asList(cartridge.getPortMappings())); log.info( String.format( "Starting pod: [application] %s [cartridge] %s [member] %s " + "[cpu] %d [memory] %d MB", memberContext.getApplicationId(), memberContext.getCartridgeType(), memberContext.getMemberId(), cpu, memory)); kubernetesApi.createPod(podId, podLabel, dockerImage, cpu, memory, ports, environmentVariables); log.info( String.format( "Pod started successfully: [application] %s [cartridge] %s [member] %s " + "[pod] %s [cpu] %d [memory] %d MB", memberContext.getApplicationId(), memberContext.getCartridgeType(), memberContext.getMemberId(), podId, cpu, memory)); // Add pod id to member context memberContext.setKubernetesPodId(podId); memberContext.setKubernetesPodLabel(podLabel); // Create instance metadata InstanceMetadata instanceMetadata = new InstanceMetadata(); instanceMetadata.setImageId(dockerImage); instanceMetadata.setCpu(cpu); instanceMetadata.setRam(memory); memberContext.setInstanceMetadata(instanceMetadata); // Persist cloud controller context CloudControllerContext.getInstance().persist(); }
@SuppressWarnings("unchecked") public static Cartridge toCartridge(CartridgeConfig config) { if (config == null) { return null; } Cartridge cartridge = new Cartridge(); // populate cartridge cartridge.setType(config.getType()); cartridge.setDisplayName(config.getDisplayName()); cartridge.setDescription(config.getDescription()); cartridge.setHostName(config.getHostName()); String[] deploymentDirs = config.getDeploymentDirs(); if ((deploymentDirs != null) && (deploymentDirs.length > 0)) { cartridge.setDeploymentDirs(Arrays.asList(deploymentDirs)); } cartridge.setProvider(config.getProvider()); cartridge.setVersion(config.getVersion()); cartridge.setBaseDir(config.getBaseDir()); if (config.getPortMappings() != null) { cartridge.setPortMappings(Arrays.asList(config.getPortMappings())); } if (config.getPersistence() != null) { cartridge.setPersistence(config.getPersistence()); } cartridge.setMultiTenant(config.isMultiTenant()); cartridge.setDefaultAutoscalingPolicy(config.getDefaultAutoscalingPolicy()); cartridge.setDefaultDeploymentPolicy(config.getDefaultDeploymentPolicy()); cartridge.setServiceGroup(config.getServiceGroup()); org.apache.stratos.cloud.controller.pojo.Properties props = config.getProperties(); if (props != null) { for (Property prop : props.getProperties()) { cartridge.addProperty(prop.getName(), prop.getValue()); } } // populate LB config cartridge.setLbConfig(config.getLbConfig()); List<IaasProvider> iaases = FasterLookUpDataHolder.getInstance().getIaasProviders(); // populate IaaSes IaasConfig[] iaasConfigs = config.getIaasConfigs(); if (iaasConfigs != null) { for (IaasConfig iaasConfig : iaasConfigs) { if (iaasConfig != null) { IaasProvider iaasProvider = null; if (iaases != null) { // check whether this is a reference to a predefined IaaS. for (IaasProvider iaas : iaases) { if (iaas.getType().equals(iaasConfig.getType())) { iaasProvider = new IaasProvider(iaas); break; } } } if (iaasProvider == null) { iaasProvider = new IaasProvider(); iaasProvider.setType(iaasConfig.getType()); } String className = iaasConfig.getClassName(); if (className != null) { iaasProvider.setClassName(className); } String name = iaasConfig.getName(); if (name != null) { iaasProvider.setName(name); } String identity = iaasConfig.getIdentity(); if (identity != null) { iaasProvider.setIdentity(identity); } String credential = iaasConfig.getCredential(); if (credential != null) { iaasProvider.setCredential(credential); } String provider = iaasConfig.getProvider(); if (provider != null) { iaasProvider.setProvider(provider); } String imageId = iaasConfig.getImageId(); if (imageId != null) { iaasProvider.setImage(imageId); } byte[] payload = iaasConfig.getPayload(); if (payload != null) { iaasProvider.setPayload(payload); } org.apache.stratos.cloud.controller.pojo.Properties props1 = iaasConfig.getProperties(); if (props1 != null) { for (Property prop : props1.getProperties()) { iaasProvider.addProperty(prop.getName(), prop.getValue()); } } NetworkInterfaces networkInterfaces = iaasConfig.getNetworkInterfaces(); if (networkInterfaces != null && networkInterfaces.getNetworkInterfaces() != null) { iaasProvider.setNetworkInterfaces(networkInterfaces.getNetworkInterfaces()); } cartridge.addIaasProvider(iaasProvider); } } } return cartridge; }