kubernetes二次开发:(发布/滚动升级/回滚) 公共方法的封装

// 发布/回滚/滚动升级 公共方法
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
            }
         }
      }
   })
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值