Beispiel #1
0
func DeleteImageInformationAndRelatedRecord(imageInformationName string) error {
	imageRecordSlice, err := GetStorage().LoadImageRecordWithImageInformationName(imageInformationName)
	if err != nil {
		log.Error(err)
		return err
	}

	filteredImageRecordSlice := make([]ImageRecord, 0)
	for _, imageRecord := range imageRecordSlice {
		// Image is successfully built before
		if imageRecord.Failure == false {
			filteredImageRecordSlice = append(filteredImageRecordSlice, imageRecord)
		}
	}

	hasError := false
	buffer := bytes.Buffer{}

	if len(filteredImageRecordSlice) > 0 {
		imageRecord := filteredImageRecordSlice[0]
		privateRegistry, err := registry.GetPrivateRegistryFromPathAndTestAvailable(imageRecord.Path)
		if err != nil {
			hasError = true
			buffer.WriteString(err.Error())
		} else {
			err := privateRegistry.DeleteAllImageInRepository(imageRecord.ImageInformation)
			if err != nil {
				hasError = true
				buffer.WriteString(err.Error())
			}
		}

		err = RemoveImageFromAllHost(filteredImageRecordSlice)
		if err != nil {
			hasError = true
			buffer.WriteString(err.Error())
		}
	}

	err = GetStorage().DeleteImageInformationAndRelatedRecord(imageInformationName)
	if err != nil {
		hasError = true
		buffer.WriteString(err.Error())
	}

	err = RequestDeleteBuildLogBelongingToImageInformation(imageInformationName)
	if err != nil {
		hasError = true
		buffer.WriteString(err.Error())
	}

	if hasError {
		log.Error(buffer.String())
		return errors.New(buffer.String())
	} else {
		return nil
	}
}
Beispiel #2
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
}
Beispiel #3
0
func DeleteImageRecord(imageInformationName string, imageRecordVersion string) error {
	imageRecord, err := GetStorage().LoadImageRecord(imageInformationName, imageRecordVersion)
	if err != nil {
		log.Error(err)
		return err
	}

	hasError := false
	buffer := bytes.Buffer{}

	deletedTagSlice := make([]string, 0)
	// Image is successfully built before
	if imageRecord.Failure == false {
		privateRegistry, err := registry.GetPrivateRegistryFromPathAndTestAvailable(imageRecord.Path)
		if err != nil {
			hasError = true
			buffer.WriteString(err.Error())
		} else {
			beforeDeleteTagSlice, err := privateRegistry.GetAllImageTag(imageRecord.ImageInformation)
			if err != nil {
				hasError = true
				buffer.WriteString(err.Error())
			} else {
				err := privateRegistry.DeleteImageInRepository(imageRecord.ImageInformation, imageRecord.Version)
				if err != nil {
					hasError = true
					buffer.WriteString(err.Error())
				} else {
					afterDeleteTagSlice, err := privateRegistry.GetAllImageTag(imageRecord.ImageInformation)
					if err != nil {
						hasError = true
						buffer.WriteString(err.Error())
					} else {
						for _, beforeDeleteTag := range beforeDeleteTagSlice {
							if isTagInSlice(beforeDeleteTag, afterDeleteTagSlice) == false {
								deletedTagSlice = append(deletedTagSlice, beforeDeleteTag)
							}
						}
					}
				}
			}
		}

		imageRecordSlice := make([]ImageRecord, 0)
		imageRecordSlice = append(imageRecordSlice, *imageRecord)
		err = RemoveImageFromAllHost(imageRecordSlice)
		if err != nil {
			hasError = true
			buffer.WriteString(err.Error())
		}
	} else {
		// Failure means no image is created and pushed to private-registry and saved in any host
		deletedTagSlice = append(deletedTagSlice, imageRecordVersion)
	}

	for _, deletedTag := range deletedTagSlice {
		err = GetStorage().DeleteImageRecord(imageInformationName, deletedTag)
		if err != nil {
			hasError = true
			buffer.WriteString(err.Error())
		}
	}

	err = RequestDeleteBuildLog(imageInformationName, imageRecordVersion)
	if err != nil {
		hasError = true
		buffer.WriteString(err.Error())
	}

	if hasError {
		log.Error(buffer.String())
		return errors.New(buffer.String())
	} else {
		return nil
	}
}
Beispiel #4
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
}