Example #1
0
func deployCreate(ginCtx *gin.Context) {
	givenDeploy, err := commonDeploy(ginCtx)
	validator := validators.New()
	valid, err := validator.Validate(givenDeploy)
	if !valid {
		glog.Errorf("Invalid request, validation not passed.")
		ginCtx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request."})
		ginCtx.Error(errors.New("Invalid request"))
		return
	}
	team, uid := buildTeamLabel(ginCtx)
	if givenDeploy.Labels == nil {
		givenDeploy.Labels = make(map[string]string, 2)
	}
	givenDeploy.Labels["team"] = team
	givenDeploy.Labels["user"] = uid

	ginCtx.Set("data", givenDeploy)
	if err != nil {
		glog.Errorf("Could not update deploy, caused by: %s", err.Error())
		ginCtx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		ginCtx.Error(err)
		return
	}

	memoryLimit, e := mapMemory(givenDeploy.MemoryLimit)
	if e != nil {
		glog.Errorf("Could not create a deploy, caused by: %s", e.Error())
		ginCtx.JSON(http.StatusBadRequest, gin.H{"error": e.Error()})
		ginCtx.Error(err)
		return
	}

	volumes := make([]*Volume, len(givenDeploy.Volumes))
	for i, vol := range givenDeploy.Volumes {
		volumes[i] = &Volume{HostPath: vol.HostPath, ContainerPath: vol.ContainerPath, Mode: vol.Mode}
	}

	var beReq = &CreateRequest{BaseRequest: BaseRequest{
		Name: givenDeploy.Name, Ports: givenDeploy.Ports, Labels: givenDeploy.Labels, ImageURL: givenDeploy.ImageURL, Env: givenDeploy.Env, Replicas: givenDeploy.Replicas,
		CPULimit: givenDeploy.CPULimit, MemoryLimit: memoryLimit, Force: givenDeploy.Force, Volumes: volumes}}
	beRes, err := se.Backend.Deploy(beReq)
	if err != nil {
		glog.Errorf("Could not create a deploy, caused by: %s", err.Error())
		ginCtx.JSON(http.StatusNotAcceptable, gin.H{"error": err.Error()})
		ginCtx.Error(err)
		return
	}
	glog.Infof("Deployed: %+v\n", beRes)
	ginCtx.JSON(http.StatusOK, gin.H{"name": beRes})
}
Example #2
0
func deployUpsert(ginCtx *gin.Context) {
	deploy, err := commonDeploy(ginCtx)
	validator := validators.New()
	valid, err := validator.Validate(deploy)
	if !valid {
		glog.Errorf("Invalid request, validation not passed.")
		ginCtx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request."})
		ginCtx.Error(errors.New("Invalid request"))
		return
	}
	ginCtx.Set("data", deploy)
	if err != nil {
		glog.Errorf("Could not update deploy, caused by: %s", err.Error())
		ginCtx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		ginCtx.Error(err)
		return
	}

	memoryLimit, err := mapMemory(deploy.MemoryLimit)
	if err != nil {
		glog.Errorf("Could not create a deploy, caused by: %s", err.Error())
		ginCtx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		ginCtx.Error(err)
		return
	}

	var beReq = UpdateRequest{BaseRequest: BaseRequest{Name: deploy.Name, Ports: deploy.Ports, Labels: deploy.Labels, ImageURL: deploy.ImageURL, Env: deploy.Env, Replicas: deploy.Replicas,
		CPULimit: deploy.CPULimit, MemoryLimit: memoryLimit, Force: deploy.Force}}

	_, err = se.Backend.UpdateDeployment(&beReq)
	if err != nil {
		glog.Errorf("Could not update deploy, caused by: %s", err.Error())
		ginCtx.JSON(http.StatusNotAcceptable, gin.H{"error": err.Error()})
		ginCtx.Error(err)
		return
	}
	glog.Infof("Deployment updated")
	ginCtx.JSON(http.StatusOK, gin.H{})
}