首页 > 解决方案 > 将部署策略从 RollingUpdate 更改为使用 Golang 重新创建

问题描述

我有一个很大的问题。我们有大量的组件部署,到目前为止,这些组件都使用 RollingUpdate 部署策略类型。

spec:
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate

现在一个新的设计决策需要我们将其转移到一个Recreate策略中,如果我们可以编写一个 shell 脚本来调用kubectl patch和使用文档中描述$retainKeys的方法,这将是相当容易的。

但是,我们使用用 Golang 编写的自定义工具来管理我们的海量部署,我们希望将其集成到我们的常规更新周期中并避免这种黑客攻击。因此,我将代码更改为如下所示:

deploy := &appsv1.Deployment{
        Spec: appsv1.DeploymentSpec{
            Strategy: appsv1.DeploymentStrategy{
                Type: appsv1.RecreateDeploymentStrategyType,
            },

显然,当我们运行更新程序时,该工具失败并出现以下错误:

Error updating *v1.Deployment Deployment.apps "cluster-agent" is invalid: spec.strategy.rollingUpdate: Forbidden: may not be specified when strategy `type` is 'Recreate'

根据上面的链接,retainKeys必须使用该技巧,但我还没有设法从 Golang 中找出如何做到这一点。

我可以在k8s api源中看到补丁策略支持retainKeys方法:

Strategy DeploymentStrategy `json:"strategy,omitempty" patchStrategy:"retainKeys"

但是有人会好心让我知道如何retainKeys在我的 Golang 结构/代码中指定列表吗?非常感谢!

标签: gokubernetesdeploymentpatch

解决方案


推荐阅读