Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use patch for sts in periodicity controller #2332

Merged
merged 5 commits into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions pkg/controller/periodicity/periodicity_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,39 @@
package periodicity

import (
"encoding/json"
"time"

"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions"
v1alpha1listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/label"
"github.com/pingcap/tidb-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
kubeinformers "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
eventv1 "k8s.io/client-go/kubernetes/typed/core/v1"
appslisters "k8s.io/client-go/listers/apps/v1"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
)

type Controller struct {
stsLister appslisters.StatefulSetLister
tcLister v1alpha1listers.TidbClusterLister
statefulSetControl controller.StatefulSetControlInterface
kubeCli kubernetes.Interface
stsLister appslisters.StatefulSetLister
tcLister v1alpha1listers.TidbClusterLister
}

func NewController(
kubeCli kubernetes.Interface,
informerFactory informers.SharedInformerFactory,
kubeInformerFactory kubeinformers.SharedInformerFactory) *Controller {

eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartLogging(klog.Infof)
eventBroadcaster.StartRecordingToSink(&eventv1.EventSinkImpl{
Interface: eventv1.New(kubeCli.CoreV1().RESTClient()).Events("")})
recorder := eventBroadcaster.NewRecorder(v1alpha1.Scheme, corev1.EventSource{Component: "periodiciy-controller"})
stsLister := kubeInformerFactory.Apps().V1().StatefulSets().Lister()

return &Controller{
tcLister: informerFactory.Pingcap().V1alpha1().TidbClusters().Lister(),
statefulSetControl: controller.NewRealStatefuSetControl(kubeCli, stsLister, recorder),
stsLister: stsLister,
kubeCli: kubeCli,
tcLister: informerFactory.Pingcap().V1alpha1().TidbClusters().Lister(),
stsLister: stsLister,
}

}
Expand Down Expand Up @@ -94,6 +86,7 @@ func (c *Controller) syncStatefulSetTimeStamp() error {
if err != nil {
return err
}

var errs []error
for _, sts := range stsList {
// If there is any error during our sts annotation updating, we just collect the error
Expand All @@ -102,21 +95,29 @@ func (c *Controller) syncStatefulSetTimeStamp() error {
if !ok {
continue
}
tc, err := c.tcLister.TidbClusters(sts.Namespace).Get(tcRef.Name)
_, err := c.tcLister.TidbClusters(sts.Namespace).Get(tcRef.Name)
if err != nil {
errs = append(errs, err)
continue
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this? there is no need to update statefulsets not owned by tidb clusters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

if sts.Annotations == nil {
sts.Annotations = map[string]string{}
patchAnnotations := map[string]string{}
patchAnnotations[label.AnnStsLastSyncTimestamp] = time.Now().Format(time.RFC3339)
var mergePatch []byte
mergePatch, err = json.Marshal(map[string]interface{}{
"metadata": map[string]interface{}{
"annotations": patchAnnotations,
},
})
Copy link
Contributor

@cofyc cofyc Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using the Patch method is a good idea. It can avoid API conflicts in kube-apiserver.

But the root cause is we should copy before modifying the statefulset if the data in the Lister are touched.

if err != nil {
errs = append(errs, err)
continue
}
sts.Annotations[label.AnnStsLastSyncTimestamp] = time.Now().Format(time.RFC3339)
newSts, err := c.statefulSetControl.UpdateStatefulSet(tc, sts)
_, err = c.kubeCli.AppsV1().StatefulSets(sts.Namespace).Patch(sts.Name, types.MergePatchType, mergePatch)
if err != nil {
klog.Errorf("failed to update statefulset %q, error: %v", sts.Name, err)
klog.Errorf("sts[%s/%s] patch timestamp failed, error: %v", sts.Namespace, sts.Name, err.Error())
errs = append(errs, err)
continue
}
klog.Infof("successfully updated statefulset %q", newSts.Name)
}
return errors.NewAggregate(errs)
}