Skip to content

Commit 8b54e38

Browse files
uzabanovdanail-branekov
authored andcommitted
Implement sorting with options for instances
1 parent f9baa25 commit 8b54e38

File tree

6 files changed

+34
-224
lines changed

6 files changed

+34
-224
lines changed

api/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ func main() {
217217
serviceInstanceRepo := repositories.NewServiceInstanceRepo(
218218
spaceScopedKlient,
219219
conditions.NewConditionAwaiter[*korifiv1alpha1.CFServiceInstance, korifiv1alpha1.CFServiceInstanceList](conditionTimeout),
220-
repositories.NewServiceInstanceSorter(),
221220
cfg.RootNamespace,
222221
)
223222
serviceBindingRepo := repositories.NewServiceBindingRepo(

api/repositories/fake/service_instance_sorter.go

Lines changed: 0 additions & 118 deletions
This file was deleted.

api/repositories/service_instance_repository.go

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import (
55
"errors"
66
"fmt"
77
"slices"
8-
"strings"
98
"time"
109

1110
"code.cloudfoundry.org/korifi/api/authorization"
1211
apierrors "code.cloudfoundry.org/korifi/api/errors"
13-
"code.cloudfoundry.org/korifi/api/repositories/compare"
1412
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
1513
"code.cloudfoundry.org/korifi/tools"
1614

@@ -36,59 +34,17 @@ type NamespaceGetter interface {
3634
type ServiceInstanceRepo struct {
3735
klient Klient
3836
awaiter Awaiter[*korifiv1alpha1.CFServiceInstance]
39-
sorter ServiceInstanceSorter
4037
rootNamespace string
4138
}
4239

43-
//counterfeiter:generate -o fake -fake-name ServiceInstanceSorter . ServiceInstanceSorter
44-
type ServiceInstanceSorter interface {
45-
Sort(records []ServiceInstanceRecord, order string) []ServiceInstanceRecord
46-
}
47-
48-
type serviceInstanceSorter struct {
49-
sorter *compare.Sorter[ServiceInstanceRecord]
50-
}
51-
52-
func NewServiceInstanceSorter() *serviceInstanceSorter {
53-
return &serviceInstanceSorter{
54-
sorter: compare.NewSorter(ServiceInstanceComparator),
55-
}
56-
}
57-
58-
func (s *serviceInstanceSorter) Sort(records []ServiceInstanceRecord, order string) []ServiceInstanceRecord {
59-
return s.sorter.Sort(records, order)
60-
}
61-
62-
func ServiceInstanceComparator(fieldName string) func(ServiceInstanceRecord, ServiceInstanceRecord) int {
63-
return func(s1, s2 ServiceInstanceRecord) int {
64-
switch fieldName {
65-
case "created_at":
66-
return tools.CompareTimePtr(&s1.CreatedAt, &s2.CreatedAt)
67-
case "-created_at":
68-
return tools.CompareTimePtr(&s2.CreatedAt, &s1.CreatedAt)
69-
case "updated_at":
70-
return tools.CompareTimePtr(s1.UpdatedAt, s2.UpdatedAt)
71-
case "-updated_at":
72-
return tools.CompareTimePtr(s2.UpdatedAt, s1.UpdatedAt)
73-
case "name":
74-
return strings.Compare(s1.Name, s2.Name)
75-
case "-name":
76-
return strings.Compare(s2.Name, s1.Name)
77-
}
78-
return 0
79-
}
80-
}
81-
8240
func NewServiceInstanceRepo(
8341
klient Klient,
8442
awaiter Awaiter[*korifiv1alpha1.CFServiceInstance],
85-
sorter ServiceInstanceSorter,
8643
rootNamespace string,
8744
) *ServiceInstanceRepo {
8845
return &ServiceInstanceRepo{
8946
klient: klient,
9047
awaiter: awaiter,
91-
sorter: sorter,
9248
rootNamespace: rootNamespace,
9349
}
9450
}
@@ -150,6 +106,9 @@ func (m *ListServiceInstanceMessage) toListOptions() []ListOption {
150106
WithLabelIn(korifiv1alpha1.SpaceGUIDLabelKey, m.SpaceGUIDs),
151107
WithLabelSelector(m.LabelSelector),
152108
WithPaging(m.Pagination),
109+
WithOrdering(m.OrderBy,
110+
"name", "Display Name",
111+
),
153112
}
154113

155114
if m.Type != "" {
@@ -424,7 +383,7 @@ func (r *ServiceInstanceRepo) ListServiceInstances(ctx context.Context, authInfo
424383

425384
return ListResult[ServiceInstanceRecord]{
426385
PageInfo: pageInfo,
427-
Records: r.sorter.Sort(slices.Collect(it.Map(slices.Values(serviceInstanceList.Items), cfServiceInstanceToRecord)), message.OrderBy),
386+
Records: slices.Collect(it.Map(slices.Values(serviceInstanceList.Items), cfServiceInstanceToRecord)),
428387
}, nil
429388
}
430389

api/repositories/service_instance_repository_test.go

Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
. "github.com/onsi/ginkgo/v2"
2222
. "github.com/onsi/gomega"
2323
. "github.com/onsi/gomega/gstruct"
24+
2425
gomega_types "github.com/onsi/gomega/types"
2526
corev1 "k8s.io/api/core/v1"
2627
k8serrors "k8s.io/apimachinery/pkg/api/errors"
@@ -37,7 +38,6 @@ var _ = Describe("ServiceInstanceRepository", func() {
3738
korifiv1alpha1.CFServiceInstanceList,
3839
*korifiv1alpha1.CFServiceInstanceList,
3940
]
40-
sorter *fake.ServiceInstanceSorter
4141

4242
org *korifiv1alpha1.CFOrg
4343
space *korifiv1alpha1.CFSpace
@@ -50,15 +50,10 @@ var _ = Describe("ServiceInstanceRepository", func() {
5050
korifiv1alpha1.CFServiceInstanceList,
5151
*korifiv1alpha1.CFServiceInstanceList,
5252
]{}
53-
sorter = new(fake.ServiceInstanceSorter)
54-
sorter.SortStub = func(records []repositories.ServiceInstanceRecord, _ string) []repositories.ServiceInstanceRecord {
55-
return records
56-
}
5753

5854
serviceInstanceRepo = repositories.NewServiceInstanceRepo(
5955
spaceScopedKlient,
6056
conditionAwaiter,
61-
sorter,
6257
rootNamespace,
6358
)
6459

@@ -716,7 +711,7 @@ var _ = Describe("ServiceInstanceRepository", func() {
716711
}
717712
Expect(k8sClient.Create(ctx, cfServiceInstance1)).To(Succeed())
718713

719-
filters = repositories.ListServiceInstanceMessage{OrderBy: "foo"}
714+
filters = repositories.ListServiceInstanceMessage{OrderBy: "name"}
720715
})
721716

722717
JustBeforeEach(func() {
@@ -740,35 +735,43 @@ var _ = Describe("ServiceInstanceRepository", func() {
740735
))
741736
})
742737

743-
It("sort the service instances", func() {
744-
Expect(sorter.SortCallCount()).To(Equal(1))
745-
sortedServiceInstances, field := sorter.SortArgsForCall(0)
746-
Expect(field).To(Equal("foo"))
747-
Expect(sortedServiceInstances).To(ConsistOf(
748-
MatchFields(IgnoreExtras, Fields{"GUID": Equal(cfServiceInstance1.Name)}),
749-
))
750-
})
738+
DescribeTable("ordering",
739+
func(msg repositories.ListServiceInstanceMessage, match gomega_types.GomegaMatcher) {
740+
fakeKlient := new(fake.Klient)
741+
instancesRepo := repositories.NewServiceInstanceRepo(fakeKlient, conditionAwaiter, rootNamespace)
742+
743+
_, err := instancesRepo.ListServiceInstances(ctx, authInfo, msg)
744+
Expect(err).NotTo(HaveOccurred())
751745

752-
Describe("filtering", func() {
746+
Expect(fakeKlient.ListCallCount()).To(Equal(1))
747+
_, _, listOptions := fakeKlient.ListArgsForCall(0)
748+
Expect(listOptions).To(match)
749+
},
750+
Entry("name", repositories.ListServiceInstanceMessage{OrderBy: "name"}, ContainElement(repositories.SortOpt{By: "Display Name", Desc: false})),
751+
Entry("-name", repositories.ListServiceInstanceMessage{OrderBy: "-name"}, ContainElement(repositories.SortOpt{By: "Display Name", Desc: true})),
752+
)
753+
754+
Describe("list options", func() {
753755
var fakeKlient *fake.Klient
754756

755757
BeforeEach(func() {
756758
fakeKlient = new(fake.Klient)
757-
serviceInstanceRepo = repositories.NewServiceInstanceRepo(fakeKlient, conditionAwaiter, sorter, rootNamespace)
759+
serviceInstanceRepo = repositories.NewServiceInstanceRepo(fakeKlient, conditionAwaiter, rootNamespace)
758760
filters = repositories.ListServiceInstanceMessage{
759761
Names: []string{"instance-1", "instance-2"},
760762
SpaceGUIDs: []string{"space-guid-1", "space-guid-2"},
761763
GUIDs: []string{"guid-1", "guid-2"},
762764
LabelSelector: "a-label=a-label-value",
763765
PlanGUIDs: []string{"plan-guid-1", "plan-guid-2"},
766+
OrderBy: "updated_at",
764767
Pagination: repositories.Pagination{
765768
PerPage: 1,
766769
Page: 10,
767770
},
768771
}
769772
})
770773

771-
It("translates filter parameters to klient list options", func() {
774+
It("translates parameters to klient list options", func() {
772775
Expect(fakeKlient.ListCallCount()).To(Equal(1))
773776
_, _, listOptions := fakeKlient.ListArgsForCall(0)
774777
Expect(listOptions).To(ConsistOf(
@@ -777,6 +780,7 @@ var _ = Describe("ServiceInstanceRepository", func() {
777780
repositories.WithLabelIn(korifiv1alpha1.GUIDLabelKey, []string{"guid-1", "guid-2"}),
778781
repositories.WithLabelSelector("a-label=a-label-value"),
779782
repositories.WithLabelIn(korifiv1alpha1.PlanGUIDLabelKey, []string{"plan-guid-1", "plan-guid-2"}),
783+
repositories.WithOrdering("updated_at"),
780784
repositories.WithPaging(repositories.Pagination{
781785
PerPage: 1,
782786
Page: 10,
@@ -1077,45 +1081,3 @@ var _ = Describe("ServiceInstanceRepository", func() {
10771081
})
10781082
})
10791083
})
1080-
1081-
var _ = DescribeTable("ServiceInstanceSorter",
1082-
func(s1, s2 repositories.ServiceInstanceRecord, field string, match gomega_types.GomegaMatcher) {
1083-
Expect(repositories.ServiceInstanceComparator(field)(s1, s2)).To(match)
1084-
},
1085-
Entry("created_at",
1086-
repositories.ServiceInstanceRecord{CreatedAt: time.UnixMilli(1)},
1087-
repositories.ServiceInstanceRecord{CreatedAt: time.UnixMilli(2)},
1088-
"created_at",
1089-
BeNumerically("<", 0),
1090-
),
1091-
Entry("-created_at",
1092-
repositories.ServiceInstanceRecord{CreatedAt: time.UnixMilli(1)},
1093-
repositories.ServiceInstanceRecord{CreatedAt: time.UnixMilli(2)},
1094-
"-created_at",
1095-
BeNumerically(">", 0),
1096-
),
1097-
Entry("updated_at",
1098-
repositories.ServiceInstanceRecord{UpdatedAt: tools.PtrTo(time.UnixMilli(1))},
1099-
repositories.ServiceInstanceRecord{UpdatedAt: tools.PtrTo(time.UnixMilli(2))},
1100-
"updated_at",
1101-
BeNumerically("<", 0),
1102-
),
1103-
Entry("-updated_at",
1104-
repositories.ServiceInstanceRecord{UpdatedAt: tools.PtrTo(time.UnixMilli(1))},
1105-
repositories.ServiceInstanceRecord{UpdatedAt: tools.PtrTo(time.UnixMilli(2))},
1106-
"-updated_at",
1107-
BeNumerically(">", 0),
1108-
),
1109-
Entry("name",
1110-
repositories.ServiceInstanceRecord{Name: "first-instance"},
1111-
repositories.ServiceInstanceRecord{Name: "second-instance"},
1112-
"name",
1113-
BeNumerically("<", 0),
1114-
),
1115-
Entry("-name",
1116-
repositories.ServiceInstanceRecord{Name: "first-instance"},
1117-
repositories.ServiceInstanceRecord{Name: "second-instance"},
1118-
"-name",
1119-
BeNumerically(">", 0),
1120-
),
1121-
)

controllers/api/v1alpha1/cfserviceinstance_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ type LastOperation struct {
107107

108108
//+kubebuilder:object:root=true
109109
//+kubebuilder:subresource:status
110+
//+kubebuilder:printcolumn:name="Created At",type="string",JSONPath=`.metadata.labels.korifi\.cloudfoundry\.org/created_at`
111+
//+kubebuilder:printcolumn:name="Updated At",type="string",JSONPath=`.metadata.labels.korifi\.cloudfoundry\.org/updated_at`
110112
//+kubebuilder:printcolumn:name="Display Name",type=string,JSONPath=`.spec.displayName`
111113
//+kubebuilder:printcolumn:name="Age",type="date",JSONPath=`.metadata.creationTimestamp`
112114
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

helm/korifi/controllers/crds/korifi.cloudfoundry.org_cfserviceinstances.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ spec:
1515
scope: Namespaced
1616
versions:
1717
- additionalPrinterColumns:
18+
- jsonPath: .metadata.labels.korifi\.cloudfoundry\.org/created_at
19+
name: Created At
20+
type: string
21+
- jsonPath: .metadata.labels.korifi\.cloudfoundry\.org/updated_at
22+
name: Updated At
23+
type: string
1824
- jsonPath: .spec.displayName
1925
name: Display Name
2026
type: string

0 commit comments

Comments
 (0)