Exemplo n.º 1
0
func DeployResize(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, imageInformation string, size int) error {
	if lock.AcquireLock(LockKind, getLockName(namespace, imageInformation), 0) == false {
		return errors.New("Deployment is controlled by the other command")
	}

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

	deployInformation, err := GetStorage().LoadDeployInformation(namespace, imageInformation)
	if err != nil {
		log.Error(err)
		return err
	}

	replicationControllerName := deployInformation.ImageInformationName + deployInformation.CurrentVersion

	delta := size - deployInformation.ReplicaAmount

	_, _, err = control.ResizeReplicationController(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName, delta, deployInformation.ReplicaAmount+delta, 1)
	if err != nil {
		log.Error(err)
		return err
	}

	deployInformation.ReplicaAmount = size
	err = GetStorage().saveDeployInformation(deployInformation)
	if err != nil {
		log.Error(err)
		return err
	}

	return nil
}
Exemplo n.º 2
0
func DeployDelete(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, imageInformation string) error {
	if lock.AcquireLock(LockKind, getLockName(namespace, imageInformation), 0) == false {
		return errors.New("Deployment is controlled by the other command")
	}

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

	deployInformation, err := GetStorage().LoadDeployInformation(namespace, imageInformation)
	if err != nil {
		log.Error(err)
		return err
	}

	err = GetStorage().DeleteDeployInformation(namespace, imageInformation)
	if err != nil {
		log.Error(err)
		return err
	}

	replicationControllerName := deployInformation.ImageInformationName + deployInformation.CurrentVersion

	err = control.DeleteReplicationControllerAndRelatedPod(kubeApiServerEndPoint, kubeApiServerToken, namespace, replicationControllerName)
	if err != nil {
		log.Error(err)
		return err
	}

	err = control.DeleteService(kubeApiServerEndPoint, kubeApiServerToken, namespace, deployInformation.ImageInformationName)
	if err != nil {
		log.Error(err)
		return err
	}

	return nil
}
Exemplo n.º 3
0
func DeployUpdate(
	kubeApiServerEndPoint string, kubeApiServerToken string, namespace string,
	imageInformationName string, version string, description string,
	environmentSlice []control.ReplicationControllerContainerEnvironment) 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")
	}

	deployInformation, err := GetStorage().LoadDeployInformation(namespace, imageInformationName)
	if err != nil {
		log.Error("Load deploy information error: %s imageInformationName %s version %s", err, imageInformationName, version)
		return err
	}

	oldVersion := deployInformation.CurrentVersion
	deployInformation.CurrentVersion = version
	deployInformation.Description = description

	deployInformation.CurrentVersionDescription = imageRecord.Description

	oldReplicationControllerName := deployInformation.ImageInformationName + oldVersion
	newReplicationControllerName := deployInformation.ImageInformationName + version

	err = control.RollingUpdateReplicationControllerWithSingleContainer(
		kubeApiServerEndPoint, kubeApiServerToken, namespace,
		oldReplicationControllerName, newReplicationControllerName,
		imageRecord.Path, imageRecord.Version, waitingDuration, environmentSlice)
	if err != nil {
		log.Error("Rollingupdate replication controller error: %s", err)
		return err
	}

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

	return nil
}
Exemplo n.º 4
0
func Build(imageInformation *ImageInformation, description string) (*ImageRecord, string, error) {
	if lock.AcquireLock(LockKind, imageInformation.Name, 0) == false {
		log.Error("Image %s is under construction", imageInformation.Name)
		return nil, "", errors.New("Image is under construction")
	}
	defer lock.ReleaseLock(LockKind, imageInformation.Name)

	switch imageInformation.Kind {
	case "git":
		return BuildFromGit(imageInformation, description)
	case "scp":
		return BuildFromSCP(imageInformation, description)
	case "sftp":
		return BuildFromSFTP(imageInformation, description)
	default:
		return nil, "", errors.New("No such kind: " + imageInformation.Kind)
	}
}
Exemplo n.º 5
0
func LaunchClusterApplication(kubeApiServerEndPoint string, kubeApiServerToken string, namespace string, name string, environmentSlice []interface{}, size int, replicationControllerExtraJsonMap map[string]interface{}) error {
	if lock.AcquireLock(LockKind, getLockName(namespace, name), 0) == false {
		return errors.New("Template is under deployment")
	}

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

	cluster, err := GetStorage().LoadClusterApplication(name)
	if err != nil {
		log.Error("Load cluster application error %s", err)
		return err
	}

	switch cluster.ScriptType {
	case "none":
		return LaunchClusterApplicationNoScript(kubeApiServerEndPoint, kubeApiServerToken, namespace, cluster, environmentSlice, size, replicationControllerExtraJsonMap)
	case "python":
		return LaunchClusterApplicationPython(kubeApiServerEndPoint, kubeApiServerToken, namespace, cluster, environmentSlice, size, replicationControllerExtraJsonMap)
	default:
		log.Error("No such script type: %s", cluster.ScriptType)
		return errors.New("No such script type: " + cluster.ScriptType)
	}
}
Exemplo n.º 6
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
}