<p>We have created our own custom resource a.k.a CRD and we need to add support for rolling update, as K8s is supporting it for deployments etc we want to reuse such logic, is there any lib which we can use (maybe partially) which we can use to support it? Or maybe learn and follow the logic as we don't want to re-invent the wheel? Any reference/lib would be helpful.</p>
<p>I've struggled to find this <a href="https://github.com/kubernetes/kubernetes" rel="nofollow noreferrer">here</a>.</p>

<p>Posted community wiki answer to summarise the problem.</p>
<p><a href="https://stackoverflow.com/users/13906951/clark-mccauley" title="1,093 reputation">Clark McCauley</a> well suggested:</p>
<blockquote>
<p>You're probably looking for the logic contained <a href="https://github.com/kubernetes/kubernetes/blob/master/pkg/controller/deployment/rolling.go#L32-L67" rel="nofollow noreferrer">here</a>.</p>
</blockquote>
<p>This is a reference to the k8s source code, so you probably won't find a better source of ideas :)</p>
<pre><code>// rolloutRolling implements the logic for rolling a new replica set.
func (dc *DeploymentController) rolloutRolling(ctx context.Context, d *apps.Deployment, rsList []*apps.ReplicaSet) error {
    newRS, oldRSs, err := dc.getAllReplicaSetsAndSyncRevision(ctx, d, rsList, true)
    if err != nil {
        return err
    }
    allRSs := append(oldRSs, newRS)

    // Scale up, if we can.
    scaledUp, err := dc.reconcileNewReplicaSet(ctx, allRSs, newRS, d)
    if err != nil {
        return err
    }
    if scaledUp {
        // Update DeploymentStatus
        return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
    }

    // Scale down, if we can.
    scaledDown, err := dc.reconcileOldReplicaSets(ctx, allRSs, controller.FilterActiveReplicaSets(oldRSs), newRS, d)
    if err != nil {
        return err
    }
    if scaledDown {
        // Update DeploymentStatus
        return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
    }

    if deploymentutil.DeploymentComplete(d, &amp;d.Status) {
        if err := dc.cleanupDeployment(ctx, oldRSs, d); err != nil {
            return err
        }
    }

    // Sync deployment status
    return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
}
</code></pre>
