Skip to content

Commit 702e096

Browse files
authored
opt: move ListGroups to package apicall (#6078)
1 parent 4c25139 commit 702e096

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ _output
22
output
33
.idea/
44
coverage.txt
5+
.vscode/

pkg/action/upgrader.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import (
2020

2121
"github.com/Masterminds/semver/v3"
2222
"github.com/go-logr/logr"
23-
"k8s.io/apimachinery/pkg/api/meta"
24-
kuberuntime "k8s.io/apimachinery/pkg/runtime"
2523

2624
"github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
25+
"github.com/pingcap/tidb-operator/pkg/apicall"
2726
coreutil "github.com/pingcap/tidb-operator/pkg/apiutil/core/v1alpha1"
2827
"github.com/pingcap/tidb-operator/pkg/client"
2928
"github.com/pingcap/tidb-operator/pkg/runtime"
@@ -139,7 +138,7 @@ func checkOneComponentUpgraded[
139138
T runtime.Group,
140139
](ctx context.Context, c client.Client, ns, cluster, version string) (bool, error) {
141140
comp := scope.Component[S]()
142-
groups, err := listGroups[S](ctx, c, ns, cluster)
141+
groups, err := apicall.ListGroups[S](ctx, c, ns, cluster)
143142
if err != nil {
144143
return false, fmt.Errorf("cannot list %s groups: %w", comp, err)
145144
}
@@ -152,35 +151,6 @@ func checkOneComponentUpgraded[
152151
return upgraded, nil
153152
}
154153

155-
func listGroups[
156-
S scope.Group[F, T],
157-
F client.Object,
158-
T runtime.Group,
159-
](ctx context.Context, c client.Client, ns, cluster string) ([]F, error) {
160-
l := scope.NewList[S]()
161-
if err := c.List(ctx, l, client.InNamespace(ns), client.MatchingFields{
162-
"spec.cluster.name": cluster,
163-
}); err != nil {
164-
return nil, err
165-
}
166-
167-
objs := make([]F, 0, meta.LenList(l))
168-
if err := meta.EachListItem(l, func(item kuberuntime.Object) error {
169-
obj, ok := item.(F)
170-
if !ok {
171-
// unreachable
172-
return fmt.Errorf("cannot convert item")
173-
}
174-
objs = append(objs, obj)
175-
return nil
176-
}); err != nil {
177-
// unreachable
178-
return nil, err
179-
}
180-
181-
return objs, nil
182-
}
183-
184154
func isUpgraded[
185155
S scope.Group[F, T],
186156
F client.Object,

pkg/apicall/cluster.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package apicall
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
"k8s.io/apimachinery/pkg/api/meta"
22+
kuberuntime "k8s.io/apimachinery/pkg/runtime"
23+
24+
"github.com/pingcap/tidb-operator/pkg/client"
25+
"github.com/pingcap/tidb-operator/pkg/runtime"
26+
"github.com/pingcap/tidb-operator/pkg/runtime/scope"
27+
)
28+
29+
func ListGroups[
30+
S scope.Group[F, T],
31+
F client.Object,
32+
T runtime.Group,
33+
](ctx context.Context, c client.Client, ns, cluster string) ([]F, error) {
34+
l := scope.NewList[S]()
35+
if err := c.List(ctx, l, client.InNamespace(ns), client.MatchingFields{
36+
"spec.cluster.name": cluster,
37+
}); err != nil {
38+
return nil, err
39+
}
40+
41+
objs := make([]F, 0, meta.LenList(l))
42+
if err := meta.EachListItem(l, func(item kuberuntime.Object) error {
43+
obj, ok := item.(F)
44+
if !ok {
45+
// unreachable
46+
return fmt.Errorf("cannot convert item")
47+
}
48+
objs = append(objs, obj)
49+
return nil
50+
}); err != nil {
51+
// unreachable
52+
return nil, err
53+
}
54+
55+
return objs, nil
56+
}

0 commit comments

Comments
 (0)