Skip to content

Commit cb6aea4

Browse files
authored
fix(group): fix missing component in controller revision name (#6060)
1 parent 8ffe30e commit cb6aea4

File tree

7 files changed

+43
-36
lines changed

7 files changed

+43
-36
lines changed

pkg/controllers/common/revision.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,13 @@ func TaskRevision[
115115
var gt GT
116116
w := state.RevisionInitializer()
117117
return task.NameTaskFunc("ContextRevision", func(ctx context.Context) task.Result {
118-
historyCli := history.NewClient(c)
119118
parent := w.Parent()
119+
historyCli := history.NewClient(c, parent.Component())
120120

121121
lbs := w.Labels()
122122
selector := labels.SelectorFromSet(labels.Set(lbs))
123123

124-
revisions, err := historyCli.ListControllerRevisions(gt.To(parent), selector)
124+
revisions, err := historyCli.ListControllerRevisions(ctx, gt.To(parent), selector)
125125
if err != nil {
126126
return task.Fail().With("cannot list controller revisions: %w", err)
127127
}

pkg/controllers/common/revision_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func (f *fakeRevisionStateInitializer[G]) RevisionInitializer() RevisionInitiali
4646
}
4747

4848
const (
49-
fakeOldRevision = "aaa-77554fbd78"
50-
fakeNewRevision = "aaa-77554fbd79"
49+
fakeOldRevision = "aaa-pd-77554fbd78"
50+
fakeNewRevision = "aaa-pd-77554fbd79"
5151
)
5252

5353
func TestTaskRevision(t *testing.T) {
@@ -169,7 +169,7 @@ func TestTaskRevision(t *testing.T) {
169169
fake.FakeObj(fakeOldRevision, fake.Label[appsv1.ControllerRevision]("ccc", "ddd")),
170170
fake.FakeObj(fakeNewRevision, fake.Label[appsv1.ControllerRevision]("ccc", "ddd")),
171171
},
172-
expectedUpdateRevision: "aaa-77554fbd76",
172+
expectedUpdateRevision: "aaa-pd-77554fbd76",
173173
expectedCurrentRevision: fakeOldRevision,
174174
expectedCollisionCount: 2,
175175
},

pkg/controllers/tidbgroup/tasks/state_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func TestState(t *testing.T) {
7979
return obj
8080
}),
8181
},
82-
updateRevision: "aaa-dfb684447",
83-
currentRevision: "aaa-dfb684447",
82+
updateRevision: "aaa-tidb-dfb684447",
83+
currentRevision: "aaa-tidb-dfb684447",
8484
},
8585
},
8686
}

pkg/controllers/tiflashgroup/tasks/state_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ func TestState(t *testing.T) {
7979
return obj
8080
}),
8181
},
82-
updateRevision: "aaa-77554fbd78",
83-
currentRevision: "aaa-77554fbd78",
82+
updateRevision: "aaa-tiflash-77554fbd78",
83+
currentRevision: "aaa-tiflash-77554fbd78",
8484
},
8585
},
8686
}

pkg/utils/k8s/revision/controller_revision.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var encoderMap = map[schema.GroupVersion]kuberuntime.Encoder{}
4747
// a new revision, or modify the Revision of an existing revision if an update to ComponentGroup is detected.
4848
// This method expects that revisions is sorted when supplied.
4949
func GetCurrentAndUpdate(
50-
_ context.Context,
50+
ctx context.Context,
5151
group client.Object,
5252
component string,
5353
labels map[string]string,
@@ -86,13 +86,13 @@ func GetCurrentAndUpdate(
8686
} else if equalCount > 0 {
8787
// if the equivalent revision is not immediately prior we will roll back by incrementing the
8888
// Revision of the equivalent revision
89-
updateRevision, err = cli.UpdateControllerRevision(equalRevisions[equalCount-1], updateRevision.Revision)
89+
updateRevision, err = cli.UpdateControllerRevision(ctx, equalRevisions[equalCount-1], updateRevision.Revision)
9090
if err != nil {
9191
return nil, nil, collisionCount, fmt.Errorf("failed to update a revision: %w", err)
9292
}
9393
} else {
9494
// if there is no equivalent revision we create a new one
95-
updateRevision, err = cli.CreateControllerRevision(group, updateRevision, &collisionCount)
95+
updateRevision, err = cli.CreateControllerRevision(ctx, group, updateRevision, &collisionCount)
9696
if err != nil {
9797
return nil, nil, collisionCount, fmt.Errorf("failed to create a revision: %w", err)
9898
}
@@ -180,6 +180,7 @@ func getPatch(obj client.Object, gvk schema.GroupVersionKind) ([]byte, error) {
180180
// until only RevisionHistoryLimit revisions remain.
181181
// This method expects that revisions is sorted when supplied.
182182
func TruncateHistory[T runtime.Instance](
183+
ctx context.Context,
183184
cli history.Interface,
184185
instances []T,
185186
revisions []*appsv1.ControllerRevision,
@@ -224,7 +225,7 @@ func TruncateHistory[T runtime.Instance](
224225
// delete any non-live history to maintain the revision limit.
225226
hist = hist[:(historyLen - historyLimit)]
226227
for i := 0; i < len(hist); i++ {
227-
if err := cli.DeleteControllerRevision(hist[i]); err != nil {
228+
if err := cli.DeleteControllerRevision(ctx, hist[i]); err != nil {
228229
return fmt.Errorf("failed to delete controller revision %s: %w", hist[i].Name, err)
229230
}
230231
}

pkg/utils/k8s/revision/controller_revision_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type FakeHistoryClient struct {
3838
CreateFunc func(parent client.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error)
3939
}
4040

41-
func (f *FakeHistoryClient) CreateControllerRevision(parent client.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error) {
41+
func (f *FakeHistoryClient) CreateControllerRevision(_ context.Context, parent client.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error) {
4242
if f.CreateFunc != nil {
4343
rev, err := f.CreateFunc(parent, revision, collisionCount)
4444
if err != nil {
@@ -49,7 +49,7 @@ func (f *FakeHistoryClient) CreateControllerRevision(parent client.Object, revis
4949
return nil, nil
5050
}
5151

52-
func (f *FakeHistoryClient) DeleteControllerRevision(revision *appsv1.ControllerRevision) error {
52+
func (f *FakeHistoryClient) DeleteControllerRevision(_ context.Context, revision *appsv1.ControllerRevision) error {
5353
for i, r := range f.Revisions {
5454
if r.Name == revision.Name {
5555
f.Revisions = append(f.Revisions[:i], f.Revisions[i+1:]...)
@@ -59,11 +59,11 @@ func (f *FakeHistoryClient) DeleteControllerRevision(revision *appsv1.Controller
5959
return nil
6060
}
6161

62-
func (f *FakeHistoryClient) ListControllerRevisions(_ client.Object, _ labels.Selector) ([]*appsv1.ControllerRevision, error) {
62+
func (f *FakeHistoryClient) ListControllerRevisions(_ context.Context, _ client.Object, _ labels.Selector) ([]*appsv1.ControllerRevision, error) {
6363
return f.Revisions, nil
6464
}
6565

66-
func (f *FakeHistoryClient) UpdateControllerRevision(revision *appsv1.ControllerRevision, newRevision int64) (*appsv1.ControllerRevision, error) {
66+
func (f *FakeHistoryClient) UpdateControllerRevision(_ context.Context, revision *appsv1.ControllerRevision, newRevision int64) (*appsv1.ControllerRevision, error) {
6767
for _, r := range f.Revisions {
6868
if r.Name == revision.Name {
6969
r.Revision = newRevision
@@ -176,10 +176,11 @@ func TestTruncateHistory(t *testing.T) {
176176
cli := &FakeHistoryClient{
177177
Revisions: tt.revisions,
178178
}
179-
err := TruncateHistory(cli, runtime.FromTiDBSlice(tt.instances), tt.revisions, tt.current.Name, tt.update.Name, tt.limit)
179+
ctx := context.TODO()
180+
err := TruncateHistory(ctx, cli, runtime.FromTiDBSlice(tt.instances), tt.revisions, tt.current.Name, tt.update.Name, tt.limit)
180181
require.NoError(t, err)
181182

182-
remainingRevisions, err := cli.ListControllerRevisions(nil, labels.Everything())
183+
remainingRevisions, err := cli.ListControllerRevisions(ctx, nil, labels.Everything())
183184
require.NoError(t, err)
184185
assert.Equal(t, len(tt.expected), len(remainingRevisions))
185186
m := make(map[string]struct{}, len(remainingRevisions))

third_party/kubernetes/pkg/controller/history/controller_history.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,37 +173,41 @@ type Interface interface {
173173
// ListControllerRevisions lists all ControllerRevisions matching selector and owned by parent or no other
174174
// controller. If the returned error is nil the returned slice of ControllerRevisions is valid. If the
175175
// returned error is not nil, the returned slice is not valid.
176-
ListControllerRevisions(parent client.Object, selector labels.Selector) ([]*appsv1.ControllerRevision, error)
176+
ListControllerRevisions(ctx context.Context, parent client.Object, selector labels.Selector) ([]*appsv1.ControllerRevision, error)
177177
// CreateControllerRevision attempts to create the revision as owned by parent via a ControllerRef. If name
178178
// collision occurs, collisionCount (incremented each time collision occurs except for the first time) is
179179
// added to the hash of the revision, and it is renamed using ControllerRevisionName. Implementations may
180180
// cease to attempt to retry creation after some number of attempts and return an error. If the returned
181181
// error is not nil, creation failed. If the returned error is nil, the returned ControllerRevision has been
182182
// created.
183183
// Callers must make sure that collisionCount is not nil. An error is returned if it is.
184-
CreateControllerRevision(parent client.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error)
184+
CreateControllerRevision(ctx context.Context, parent client.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error)
185185
// DeleteControllerRevision attempts to delete revision. If the returned error is not nil, deletion has failed.
186-
DeleteControllerRevision(revision *appsv1.ControllerRevision) error
186+
DeleteControllerRevision(ctx context.Context, revision *appsv1.ControllerRevision) error
187187
// UpdateControllerRevision updates revision such that its Revision is equal to newRevision. Implementations
188188
// may retry on conflict. If the returned error is nil, the update was successful and returned ControllerRevision
189189
// is valid. If the returned error is not nil, the update failed and the returned ControllerRevision is invalid.
190-
UpdateControllerRevision(revision *appsv1.ControllerRevision, newRevision int64) (*appsv1.ControllerRevision, error)
190+
UpdateControllerRevision(ctx context.Context, revision *appsv1.ControllerRevision, newRevision int64) (*appsv1.ControllerRevision, error)
191191
}
192192

193193
// NewClient returns an instance of Interface that uses client to communicate with the API Server and lister to list
194194
// ControllerRevisions. This method should be used to create an Interface for all scenarios other than testing.
195-
func NewClient(cli client.Client) Interface {
196-
return &realHistory{cli: cli}
195+
func NewClient(cli client.Client, component string) Interface {
196+
return &realHistory{
197+
cli: cli,
198+
component: component,
199+
}
197200
}
198201

199202
type realHistory struct {
200-
cli client.Client
203+
cli client.Client
204+
component string
201205
}
202206

203-
func (rh *realHistory) ListControllerRevisions(parent client.Object, selector labels.Selector) ([]*appsv1.ControllerRevision, error) {
207+
func (rh *realHistory) ListControllerRevisions(ctx context.Context, parent client.Object, selector labels.Selector) ([]*appsv1.ControllerRevision, error) {
204208
// List all revisions in the namespace that match the selector
205209
var list appsv1.ControllerRevisionList
206-
if err := rh.cli.List(context.TODO(), &list, &client.ListOptions{
210+
if err := rh.cli.List(ctx, &list, &client.ListOptions{
207211
Namespace: parent.GetNamespace(),
208212
LabelSelector: selector,
209213
}); err != nil {
@@ -220,21 +224,22 @@ func (rh *realHistory) ListControllerRevisions(parent client.Object, selector la
220224
return owned, nil
221225
}
222226

223-
func (rh *realHistory) CreateControllerRevision(parent client.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error) {
227+
func (rh *realHistory) CreateControllerRevision(ctx context.Context, parent client.Object, revision *appsv1.ControllerRevision, collisionCount *int32) (*appsv1.ControllerRevision, error) {
224228
if collisionCount == nil {
225229
return nil, fmt.Errorf("collisionCount should not be nil")
226230
}
227231

228232
// Clone the input
229233
clone := revision.DeepCopy()
230234

235+
namePrefix := parent.GetName() + "-" + rh.component
231236
// Continue to attempt to create the revision updating the name with a new hash on each iteration
232237
for {
233238
hash := HashControllerRevision(revision, collisionCount)
234239
// Update the revisions name
235-
clone.Name = ControllerRevisionName(parent.GetName(), hash)
240+
clone.Name = ControllerRevisionName(namePrefix, hash)
236241
clone.Namespace = parent.GetNamespace()
237-
err := rh.cli.Create(context.TODO(), clone)
242+
err := rh.cli.Create(ctx, clone)
238243
if errors.IsAlreadyExists(err) {
239244
var exists appsv1.ControllerRevision
240245
if err := rh.cli.Get(context.TODO(), client.ObjectKey{
@@ -253,19 +258,19 @@ func (rh *realHistory) CreateControllerRevision(parent client.Object, revision *
253258
}
254259
}
255260

256-
func (rh *realHistory) UpdateControllerRevision(revision *appsv1.ControllerRevision, newRevision int64) (*appsv1.ControllerRevision, error) {
261+
func (rh *realHistory) UpdateControllerRevision(ctx context.Context, revision *appsv1.ControllerRevision, newRevision int64) (*appsv1.ControllerRevision, error) {
257262
clone := revision.DeepCopy()
258263
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
259264
if clone.Revision == newRevision {
260265
return nil
261266
}
262267
clone.Revision = newRevision
263-
updateErr := rh.cli.Update(context.TODO(), clone)
268+
updateErr := rh.cli.Update(ctx, clone)
264269
if updateErr == nil {
265270
return nil
266271
}
267272
var updated appsv1.ControllerRevision
268-
if err := rh.cli.Get(context.TODO(), client.ObjectKey{
273+
if err := rh.cli.Get(ctx, client.ObjectKey{
269274
Namespace: clone.Namespace,
270275
Name: clone.Name,
271276
}, &updated); err == nil {
@@ -277,6 +282,6 @@ func (rh *realHistory) UpdateControllerRevision(revision *appsv1.ControllerRevis
277282
return clone, err
278283
}
279284

280-
func (rh *realHistory) DeleteControllerRevision(revision *appsv1.ControllerRevision) error {
281-
return rh.cli.Delete(context.TODO(), revision)
285+
func (rh *realHistory) DeleteControllerRevision(ctx context.Context, revision *appsv1.ControllerRevision) error {
286+
return rh.cli.Delete(ctx, revision)
282287
}

0 commit comments

Comments
 (0)