Skip to content

Commit cb994fe

Browse files
committed
Refactor availability zone label handling in ResourceRouter and update tests
1 parent aec01f6 commit cb994fe

2 files changed

Lines changed: 33 additions & 21 deletions

File tree

pkg/multicluster/routers.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ func (h HypervisorResourceRouter) Match(obj any, labels map[string]string) (bool
3030
default:
3131
return false, errors.New("object is not a Hypervisor")
3232
}
33-
az, ok := labels["az"]
33+
availabilityZone, ok := labels["availability_zone"]
3434
if !ok {
3535
return false, errors.New("cluster does not have availability zone label")
3636
}
37-
hvAZ, ok := hv.Labels[corev1.LabelTopologyZone]
37+
hvAvailabilityZone, ok := hv.Labels[corev1.LabelTopologyZone]
3838
if !ok {
3939
return false, errors.New("hypervisor does not have availability zone label")
4040
}
41-
return hvAZ == az, nil
41+
return hvAvailabilityZone == availabilityZone, nil
4242
}
4343

4444
// ReservationsResourceRouter routes reservations to clusters based on availability zone.
@@ -54,15 +54,15 @@ func (r ReservationsResourceRouter) Match(obj any, labels map[string]string) (bo
5454
default:
5555
return false, errors.New("object is not a Reservation")
5656
}
57-
az, ok := labels["az"]
57+
availabilityZone, ok := labels["availability_zone"]
5858
if !ok {
59-
return false, errors.New("cluster does not have availability zone label")
59+
return false, errors.New("cluster does not have availability zone in spec")
6060
}
61-
resAZ := res.Spec.AvailabilityZone
62-
if resAZ == "" {
63-
return false, errors.New("reservation does not have availability zone label")
61+
reservationAvailabilityZone := res.Spec.AvailabilityZone
62+
if reservationAvailabilityZone == "" {
63+
return false, errors.New("reservation does not have availability zone in spec")
6464
}
65-
return resAZ == az, nil
65+
return reservationAvailabilityZone == availabilityZone, nil
6666
}
6767

6868
// TODO: Add router for Decision CRD and reservations after their refactoring is done.

pkg/multicluster/routers_test.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestHypervisorResourceRouter_Match(t *testing.T) {
2828
Labels: map[string]string{"topology.kubernetes.io/zone": "qa-de-1a"},
2929
},
3030
},
31-
labels: map[string]string{"az": "qa-de-1a"},
31+
labels: map[string]string{"availability_zone": "qa-de-1a"},
3232
wantMatch: true,
3333
},
3434
{
@@ -38,7 +38,7 @@ func TestHypervisorResourceRouter_Match(t *testing.T) {
3838
Labels: map[string]string{"topology.kubernetes.io/zone": "qa-de-1a"},
3939
},
4040
},
41-
labels: map[string]string{"az": "qa-de-1a"},
41+
labels: map[string]string{"availability_zone": "qa-de-1a"},
4242
wantMatch: true,
4343
},
4444
{
@@ -48,17 +48,17 @@ func TestHypervisorResourceRouter_Match(t *testing.T) {
4848
Labels: map[string]string{"topology.kubernetes.io/zone": "qa-de-1a"},
4949
},
5050
},
51-
labels: map[string]string{"az": "qa-de-1b"},
51+
labels: map[string]string{"availability_zone": "qa-de-1b"},
5252
wantMatch: false,
5353
},
5454
{
5555
name: "not a Hypervisor",
5656
obj: "not-a-hypervisor",
57-
labels: map[string]string{"az": "qa-de-1a"},
57+
labels: map[string]string{"availability_zone": "qa-de-1a"},
5858
wantErr: true,
5959
},
6060
{
61-
name: "cluster missing az label",
61+
name: "cluster missing availability_zone label",
6262
obj: hv1.Hypervisor{
6363
ObjectMeta: metav1.ObjectMeta{
6464
Labels: map[string]string{"topology.kubernetes.io/zone": "qa-de-1a"},
@@ -74,7 +74,13 @@ func TestHypervisorResourceRouter_Match(t *testing.T) {
7474
Labels: map[string]string{},
7575
},
7676
},
77-
labels: map[string]string{"az": "qa-de-1a"},
77+
labels: map[string]string{"availability_zone": "qa-de-1a"},
78+
wantErr: true,
79+
},
80+
{
81+
name: "nil pointer doesn't dereference",
82+
obj: nil,
83+
labels: map[string]string{"availability_zone": "qa-de-1a"},
7884
wantErr: true,
7985
},
8086
}
@@ -112,7 +118,7 @@ func TestReservationsResourceRouter_Match(t *testing.T) {
112118
AvailabilityZone: "qa-de-1a",
113119
},
114120
},
115-
labels: map[string]string{"az": "qa-de-1a"},
121+
labels: map[string]string{"availability_zone": "qa-de-1a"},
116122
wantMatch: true,
117123
},
118124
{
@@ -122,7 +128,7 @@ func TestReservationsResourceRouter_Match(t *testing.T) {
122128
AvailabilityZone: "qa-de-1a",
123129
},
124130
},
125-
labels: map[string]string{"az": "qa-de-1a"},
131+
labels: map[string]string{"availability_zone": "qa-de-1a"},
126132
wantMatch: true,
127133
},
128134
{
@@ -132,17 +138,17 @@ func TestReservationsResourceRouter_Match(t *testing.T) {
132138
AvailabilityZone: "qa-de-1a",
133139
},
134140
},
135-
labels: map[string]string{"az": "qa-de-1b"},
141+
labels: map[string]string{"availability_zone": "qa-de-1b"},
136142
wantMatch: false,
137143
},
138144
{
139145
name: "not a Reservation",
140146
obj: "not-a-reservation",
141-
labels: map[string]string{"az": "qa-de-1a"},
147+
labels: map[string]string{"availability_zone": "qa-de-1a"},
142148
wantErr: true,
143149
},
144150
{
145-
name: "cluster missing az label",
151+
name: "cluster missing availability_zone label",
146152
obj: v1alpha1.Reservation{
147153
Spec: v1alpha1.ReservationSpec{
148154
AvailabilityZone: "qa-de-1a",
@@ -158,7 +164,13 @@ func TestReservationsResourceRouter_Match(t *testing.T) {
158164
AvailabilityZone: "",
159165
},
160166
},
161-
labels: map[string]string{"az": "qa-de-1a"},
167+
labels: map[string]string{"availability_zone": "qa-de-1a"},
168+
wantErr: true,
169+
},
170+
{
171+
name: "nil pointer doesn't dereference",
172+
obj: nil,
173+
labels: map[string]string{"availability_zone": "qa-de-1a"},
162174
wantErr: true,
163175
},
164176
}

0 commit comments

Comments
 (0)