-
Notifications
You must be signed in to change notification settings - Fork 495
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
} | ||
|
||
} | ||
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
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, | ||
}, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.