// 发布/回滚/滚动升级 公共方法 func HandlePRR(c *gin.Context, timesince time.Time, action, virtual_name, skyeye, workid string, fn func(*lcmongo.Program, lcmongo.Spec, chan error, string)) { // 更新数据库 发布时间/滚动升级时间/回滚时间 字段 now, err := UpdateTimeField(action, virtual_name, skyeye) if err != nil { MongoDBError(c, Update, err, timesince) return } ProgramQuery(c, skyeye, virtual_name, timesince, func(program *lcmongo.Program, value lcmongo.Spec) { // 针对action = "publish"进行判断,禁止重复发布 if action == "publish" && value.Status == 1 { message := fmt.Sprintf("Project: [%s] has been publish, Please don't republish!", virtual_name) GeneralError(c, conflict, message, timesince) return } ch := make(chan error) ticker, timeout := TickerTimeOut(action) deployment_name := fmt.Sprintf("deployment-%s", value.Name) go func(p *lcmongo.Program, value lcmongo.Spec, ch_ chan error, deployment_name string) { // 针对不同的action(publish/rollingupdate/rollback) // 自己写处理逻辑 fn(p, value, ch_, deployment_name) }(program, value, ch, deployment_name) select { case err := <-ch: close(ch) // kubernetes统一错误错误 if err != nil { KubernetesError(c, Query, err, timesince) return } // 开启定时器,不断轮询查询deployment的pod数量 // 后续改成watche controller,性能更好 for { select { case <-ticker: deployment, err := K8s_Client.GetSpecNameSpaceDeployment(value.NameSpace, deployment_name) if err != nil { KubernetesError(c, Query, err, timesince) return } // pod数量达到预期,发布成功,后期处理 if deployment.Status.UpdatedReplicas == int32(value.Replicas) && deployment.Status.AvailableReplicas == int32(value.Replicas) && deployment.Status.Replicas == int32(value.Replicas){ end_time := strconv.FormatInt(time.Now().Unix(), 10) his := &lcmongo.HistoryLog{ Id: bson.NewObjectId(), WorkdId: workid, Skyeye: skyeye, Virtual_Name: virtual_name, NameSpace: value.NameSpace, Operation_Type: action, Start_Time: now, End_Time: end_time, } if err := lcmongo.InsertDocument(Collection_HisLog, reflect.ValueOf(his)); err != nil { MongoDBError(c, Insert, err, timesince) return } if err := UpdateCountField(action, virtual_name, skyeye); err != nil { MongoDBError(c, Update, err, timesince) return } GeneralAllOk(c, timesince) return } case <-timeout: // 超时处理 defer func() { // 判断action == "publish" && 发布失败 // 则删除kubernetes已发布资源 if action == "publish" { if err := CleanK8sResource(value.Name, value.NameSpace); err != nil { KubernetesError(c, Delete, err, timesince) return } } }() GeneralError(c, internalserver_error, "timeout!", timesince) return } } } }) }
kubernetes二次开发:(发布/滚动升级/回滚) 公共方法的封装
最新推荐文章于 2024-08-24 18:45:00 发布