Files
kubevela/pkg/controller/common/rollout/workloads/statefulset_controller.go
Jianbo Sun aa87d3da24 upgrade K8s dependency lib to v0.21 (#1985)
* upgrade K8s dependency to v0.21

* update CRD for new version of controller-runtime

* fix ci component revision create must fill Raw in runtime.RawExtension

* try fix test

* start control plane timeout set to 1min and fix tests

* add timeout for env test start and stop

* longer timeout time for BeforeSuit function

* upgrade kubebuilder and k8s cluster version to match with v1.21.2 in github action

* fix test

* fix resource tracker ownerRef override

* update developer guides
2021-08-03 11:30:02 +08:00

112 lines
4.0 KiB
Go

/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package workloads
import (
"context"
"fmt"
"github.com/crossplane/crossplane-runtime/pkg/event"
apps "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1"
)
type statefulSetController struct {
workloadController
targetNamespacedName types.NamespacedName
}
// add the parent controller to the owner of the StatefulSet, and initialize the size
// before kicking start the update and start from every pod in the old version
func (c *statefulSetController) claimStatefulSet(ctx context.Context, statefulSet *apps.StatefulSet, initSize *int32) (bool, error) {
if controller := metav1.GetControllerOf(statefulSet); controller != nil &&
controller.Kind == v1beta1.AppRolloutKind && controller.APIVersion == v1beta1.SchemeGroupVersion.String() {
// it's already there
return true, nil
}
statefulSetPatch := client.MergeFrom(statefulSet.DeepCopy())
// add the parent controller to the owner of the StatefulSet
ref := metav1.NewControllerRef(c.parentController, v1beta1.AppRolloutKindVersionKind)
statefulSet.SetOwnerReferences(append(statefulSet.GetOwnerReferences(), *ref))
if initSize != nil {
statefulSet.Spec.Replicas = initSize
}
// patch the StatefulSet
if err := c.client.Patch(ctx, statefulSet, statefulSetPatch, client.FieldOwner(c.parentController.GetUID())); err != nil {
c.recorder.Event(c.parentController, event.Warning("Failed to the start the StatefulSet update", err))
c.rolloutStatus.RolloutRetry(err.Error())
return false, err
}
return false, nil
}
// scale the StatefulSet
func (c *statefulSetController) scaleStatefulSet(ctx context.Context, statefulSet *apps.StatefulSet, size int32) error {
statefulSetPatch := client.MergeFrom(statefulSet.DeepCopy())
statefulSet.Spec.Replicas = pointer.Int32Ptr(size)
// patch the StatefulSet
if err := c.client.Patch(ctx, statefulSet, statefulSetPatch, client.FieldOwner(c.parentController.GetUID())); err != nil {
c.recorder.Event(c.parentController, event.Warning(event.Reason(fmt.Sprintf(
"Failed to update the StatefulSet %s to the correct target %d", statefulSet.GetName(), size)), err))
c.rolloutStatus.RolloutRetry(err.Error())
return err
}
klog.InfoS("Submitted upgrade quest for StatefulSet", "StatefulSet",
statefulSet.GetName(), "target replica size", size, "batch", c.rolloutStatus.CurrentBatch)
return nil
}
// remove the parent controller from the StatefulSet's owner list
func (c *statefulSetController) releaseStatefulSet(ctx context.Context, statefulSet *apps.StatefulSet) (bool, error) {
statefulSetPatch := client.MergeFrom(statefulSet.DeepCopy())
var newOwnerList []metav1.OwnerReference
found := false
for _, owner := range statefulSet.GetOwnerReferences() {
if owner.Kind == v1beta1.AppRolloutKind && owner.APIVersion == v1beta1.SchemeGroupVersion.String() {
found = true
continue
}
newOwnerList = append(newOwnerList, owner)
}
if !found {
klog.InfoS("the StatefulSet is already released", "StatefulSet", statefulSet.Name)
return true, nil
}
statefulSet.SetOwnerReferences(newOwnerList)
// patch the StatefulSet
if err := c.client.Patch(ctx, statefulSet, statefulSetPatch, client.FieldOwner(c.parentController.GetUID())); err != nil {
c.recorder.Event(c.parentController, event.Warning("Failed to the release the StatefulSet", err))
c.rolloutStatus.RolloutRetry(err.Error())
return false, err
}
return false, nil
}