func postReplicationController(request *restful.Request, response *restful.Response) {
	namespace := request.PathParameter("namespace")

	kubeApiServerEndPoint, kubeApiServerToken, err := configuration.GetAvailablekubeApiServerEndPoint()
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get kube apiserver endpoint and token failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["namespace"] = namespace
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	replicationController := new(control.ReplicationController)
	err = request.ReadEntity(&replicationController)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	err = control.CreateReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, *replicationController)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Create replication controller failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["kubeApiServerEndPoint"] = kubeApiServerEndPoint
		jsonMap["namespace"] = namespace
		jsonMap["replicationController"] = replicationController
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Esempio n. 2
0
func DeployCreate(
	kubeApiServerEndPoint string, kubeApiServerToken string,
	namespace string, imageInformationName string,
	version string, description string, replicaAmount int,
	deployContainerPortSlice []DeployContainerPort,
	replicationControllerContainerEnvironmentSlice []control.ReplicationControllerContainerEnvironment,
	resourceMap map[string]interface{},
	extraJsonMap map[string]interface{},
	autoUpdateForNewBuild bool) error {
	if lock.AcquireLock(LockKind, getLockName(namespace, imageInformationName), 0) == false {
		return errors.New("Deployment is controlled by the other command")
	}

	defer lock.ReleaseLock(LockKind, getLockName(namespace, imageInformationName))

	imageRecord, err := image.GetStorage().LoadImageRecord(imageInformationName, version)
	if err != nil {
		log.Error("Load image record error: %s imageInformationName %s version %s", err, imageInformationName, version)
		return err
	}

	// Check whether the image in the private-registry
	privateRegistry, err := registry.GetPrivateRegistryFromPathAndTestAvailable(imageRecord.Path)
	if err != nil {
		log.Error("Get private registry access error: " + err.Error())
		return err
	}
	if privateRegistry.IsImageTagAvailable(imageRecord.ImageInformation, imageRecord.Version) == false {
		return errors.New("The image is not in the private-registry")
	}

	selectorName := imageInformationName
	replicationControllerName := selectorName + version
	image := imageRecord.Path

	// Automatically generate the basic default service. For advanced configuration, it should be modified in the service
	servicePortSlice := make([]control.ServicePort, 0)
	for _, deployContainerPort := range deployContainerPortSlice {
		containerPort := strconv.Itoa(deployContainerPort.ContainerPort)
		servicePort := control.ServicePort{
			deployContainerPort.Name,
			"TCP",
			deployContainerPort.ContainerPort,
			containerPort,
			deployContainerPort.NodePort, // -1 means not to use. 0 means auto-generated. > 0 means the port number to use
		}
		servicePortSlice = append(servicePortSlice, servicePort)
	}
	selectorLabelMap := make(map[string]interface{})
	selectorLabelMap["name"] = selectorName
	serviceLabelMap := make(map[string]interface{})
	serviceLabelMap["name"] = imageInformationName
	service := control.Service{
		imageInformationName,
		namespace,
		servicePortSlice,
		selectorLabelMap,
		"",
		serviceLabelMap,
		"",
	}
	err = control.CreateService(kubeApiServerEndPoint, kubeApiServerToken, namespace, service)
	if err != nil {
		log.Error("Create service error: %s", err)
		return err
	}

	// Replication controller
	replicationControllerContainerPortSlice := make([]control.ReplicationControllerContainerPort, 0)
	for _, deployContainerPort := range deployContainerPortSlice {
		replicationControllerContainerPortSlice = append(replicationControllerContainerPortSlice,
			control.ReplicationControllerContainerPort{deployContainerPort.Name, deployContainerPort.ContainerPort})
	}

	replicationControllerContainerSlice := make([]control.ReplicationControllerContainer, 0)
	replicationControllerContainerSlice = append(
		replicationControllerContainerSlice,
		control.ReplicationControllerContainer{
			replicationControllerName,
			image,
			replicationControllerContainerPortSlice,
			replicationControllerContainerEnvironmentSlice,
			resourceMap,
		})

	replicationController := control.ReplicationController{
		replicationControllerName,
		replicaAmount,
		control.ReplicationControllerSelector{
			selectorName,
			version,
		},
		control.ReplicationControllerLabel{
			replicationControllerName,
		},
		replicationControllerContainerSlice,
		extraJsonMap,
	}

	err = control.CreateReplicationController(kubeApiServerEndPoint, kubeApiServerToken,
		namespace, replicationController)
	if err != nil {
		log.Error("Create replication controller error: %s", err)
		return err
	}

	deployInformation := &DeployInformation{
		namespace,
		imageInformationName,
		version,
		imageRecord.Description,
		description,
		replicaAmount,
		deployContainerPortSlice,
		replicationControllerContainerEnvironmentSlice,
		resourceMap,
		extraJsonMap,
		time.Now(),
		autoUpdateForNewBuild,
	}

	err = GetStorage().saveDeployInformation(deployInformation)
	if err != nil {
		log.Error("Save deploy information error: %s", err)
		return err
	}

	return nil
}