Skip to content

Commit 9d45712

Browse files
committed
Use pointer to store user limits to allow for unspecified limits.
1 parent 47fe808 commit 9d45712

File tree

8 files changed

+68
-51
lines changed

8 files changed

+68
-51
lines changed

api/v1beta1/user_types.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type UserSpec struct {
4141
// the user can create. These limits can be used as guard rails in environments
4242
// where applications cannot be trusted and monitored in detail, for example,
4343
// when RabbitMQ clusters are offered as a service. See https://www.rabbitmq.com/docs/user-limits.
44-
UserLimits UserLimits `json:"limits,omitempty"`
44+
UserLimits *UserLimits `json:"limits,omitempty"`
4545
}
4646

4747
// UserStatus defines the observed state of User.
@@ -67,9 +67,9 @@ type UserTag string
6767
// when RabbitMQ clusters are offered as a service. See https://www.rabbitmq.com/docs/user-limits.
6868
type UserLimits struct {
6969
// Limits how many connections the user can open.
70-
Connections int32 `json:"connections,omitempty"`
70+
Connections *int32 `json:"connections,omitempty"`
7171
// Limits how many AMQP 0.9.1 channels the user can open.
72-
Channels int32 `json:"channels,omitempty"`
72+
Channels *int32 `json:"channels,omitempty"`
7373
}
7474

7575
// +genclient

api/v1beta1/user_types_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ var _ = Describe("user spec", func() {
9696
var user User
9797
var username string
9898
var userLimits UserLimits
99+
var connections, channels int32
99100

100101
JustBeforeEach(func() {
101102
user = User{
@@ -107,17 +108,19 @@ var _ = Describe("user spec", func() {
107108
RabbitmqClusterReference: RabbitmqClusterReference{
108109
Name: "some-cluster",
109110
},
110-
UserLimits: userLimits,
111+
UserLimits: &userLimits,
111112
},
112113
}
113114
})
114115

115116
When("creating a user with valid limits", func() {
116117
BeforeEach(func() {
117118
username = "limits-user"
119+
connections = 5
120+
channels = 10
118121
userLimits = UserLimits{
119-
Connections: 5,
120-
Channels: 10,
122+
Connections: &connections,
123+
Channels: &channels,
121124
}
122125
})
123126
It("successfully creates the user", func() {
@@ -130,7 +133,9 @@ var _ = Describe("user spec", func() {
130133
Expect(fetchedUser.Spec.RabbitmqClusterReference).To(Equal(RabbitmqClusterReference{
131134
Name: "some-cluster",
132135
}))
133-
Expect(fetchedUser.Spec.UserLimits).To(Equal(UserLimits{Connections: 5, Channels: 10}))
136+
Expect(fetchedUser.Spec.UserLimits).NotTo(BeNil())
137+
Expect(*fetchedUser.Spec.UserLimits.Connections).To(Equal(connections))
138+
Expect(*fetchedUser.Spec.UserLimits.Channels).To(Equal(channels))
134139
})
135140
})
136141
})

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controllers/user_controller.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,5 @@ func (r *UserReconciler) DeleteFunc(ctx context.Context, client rabbitmqclient.C
255255
} else if err != nil {
256256
return err
257257
}
258-
259-
userLimits := []string{"max-connections", "max-channels"}
260-
err = validateResponseForDeletion(client.DeleteUserLimits(user.Status.Username, userLimits))
261-
if errors.Is(err, NotFound) {
262-
logger.Info("cannot find user limits in rabbitmq server; already deleted", "user", user.Name, "limits", userLimits)
263-
} else if err != nil {
264-
return err
265-
}
266258
return nil
267259
}

controllers/user_controller_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ var _ = Describe("UserController", func() {
3434
managerCancel context.CancelFunc
3535
k8sClient runtimeClient.Client
3636
userLimits topology.UserLimits
37+
connections int32
38+
channels int32
3739
)
3840

3941
BeforeEach(func() {
@@ -92,7 +94,7 @@ var _ = Describe("UserController", func() {
9294
RabbitmqClusterReference: topology.RabbitmqClusterReference{
9395
Name: "example-rabbit",
9496
},
95-
UserLimits: userLimits,
97+
UserLimits: &userLimits,
9698
},
9799
}
98100
})
@@ -160,9 +162,11 @@ var _ = Describe("UserController", func() {
160162
When("the user has limits defined", func() {
161163
BeforeEach(func() {
162164
userName = "test-user-limits"
165+
connections = 5
166+
channels = 10
163167
userLimits = topology.UserLimits{
164-
Connections: 5,
165-
Channels: 10,
168+
Connections: &connections,
169+
Channels: &channels,
166170
}
167171
fakeRabbitMQClient.PutUserReturns(&http.Response{
168172
Status: "201 Created",
@@ -195,12 +199,8 @@ var _ = Describe("UserController", func() {
195199
By("calling PutUserLimits with the correct user limits")
196200
Expect(fakeRabbitMQClient.PutUserLimitsCallCount()).To(BeNumerically(">", 0))
197201
_, userLimitsValues := fakeRabbitMQClient.PutUserLimitsArgsForCall(0)
198-
connectionLimit, ok := userLimitsValues["max-connections"]
199-
Expect(ok).To(BeTrue())
200-
Expect(connectionLimit).To(Equal(5))
201-
channelLimit, ok := userLimitsValues["max-channels"]
202-
Expect(ok).To(BeTrue())
203-
Expect(channelLimit).To(Equal(10))
202+
Expect(userLimitsValues).To(HaveKeyWithValue("max-connections", int(connections)))
203+
Expect(userLimitsValues).To(HaveKeyWithValue("max-channels", (int(channels))))
204204
})
205205
})
206206
})

internal/user.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ func GenerateUserSettings(credentials *corev1.Secret, tags []topology.UserTag) (
6363
}, nil
6464
}
6565

66-
func GenerateUserLimits(userLimits topology.UserLimits) rabbithole.UserLimitsValues {
66+
func GenerateUserLimits(userLimits *topology.UserLimits) rabbithole.UserLimitsValues {
6767
userLimitsValues := rabbithole.UserLimitsValues{}
68-
if userLimits.Connections > 0 {
69-
userLimitsValues["max-connections"] = int(userLimits.Connections)
70-
}
71-
if userLimits.Channels > 0 {
72-
userLimitsValues["max-channels"] = int(userLimits.Channels)
68+
if userLimits != nil {
69+
if userLimits.Connections != nil {
70+
userLimitsValues["max-connections"] = int(*userLimits.Connections)
71+
}
72+
if userLimits.Channels != nil {
73+
userLimitsValues["max-channels"] = int(*userLimits.Channels)
74+
}
7375
}
7476
return userLimitsValues
7577
}

internal/user_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,28 +65,33 @@ var _ = Describe("GenerateUserSettings", func() {
6565
})
6666

6767
When("user limits are provided", func() {
68+
var connections, channels int32
69+
6870
It("uses the limits to generate the expected rabbithole.UserLimits", func() {
69-
limits := internal.GenerateUserLimits(topology.UserLimits{
70-
Connections: 3,
71-
Channels: 7,
71+
connections = 3
72+
channels = 7
73+
limits := internal.GenerateUserLimits(&topology.UserLimits{
74+
Connections: &connections,
75+
Channels: &channels,
7276
})
73-
Expect(limits["max-connections"]).To(Equal(3))
74-
Expect(limits["max-channels"]).To(Equal(7))
77+
Expect(limits).To(HaveLen(2))
78+
Expect(limits).To(HaveKeyWithValue("max-connections", int(connections)))
79+
Expect(limits).To(HaveKeyWithValue("max-channels", int(channels)))
7580
})
7681

7782
It("does not create unspecified limits", func() {
78-
limits := internal.GenerateUserLimits(topology.UserLimits{
79-
Connections: 5,
83+
connections = 5
84+
limits := internal.GenerateUserLimits(&topology.UserLimits{
85+
Connections: &connections,
8086
})
81-
Expect(limits["max-connections"]).To(Equal(5))
82-
_, ok := limits["max-channels"]
83-
Expect(ok).To(BeFalse())
87+
Expect(limits).To(HaveKeyWithValue("max-connections", int(connections)))
88+
Expect(limits).NotTo(HaveKey("max-channels"))
8489
})
8590
})
8691

8792
When("no user limits are provided", func() {
8893
It("does not specify limits", func() {
89-
limits := internal.GenerateUserLimits(topology.UserLimits{})
94+
limits := internal.GenerateUserLimits(&topology.UserLimits{})
9095
Expect(limits).To(BeEmpty())
9196
})
9297
})

system_tests/user_system_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ var _ = Describe("Users", func() {
396396
When("providing user limits", func() {
397397
const username = "limits-user"
398398
var credentialSecret corev1.Secret
399+
var connections, channels int32
399400
BeforeEach(func() {
400401
credentialSecret = corev1.Secret{
401402
ObjectMeta: metav1.ObjectMeta{
@@ -409,6 +410,8 @@ var _ = Describe("Users", func() {
409410
}
410411
Expect(k8sClient.Create(ctx, &credentialSecret, &client.CreateOptions{})).To(Succeed())
411412

413+
connections = 4
414+
channels = 6
412415
user = &topology.User{
413416
ObjectMeta: metav1.ObjectMeta{
414417
Name: username,
@@ -421,9 +424,9 @@ var _ = Describe("Users", func() {
421424
ImportCredentialsSecret: &corev1.LocalObjectReference{
422425
Name: credentialSecret.Name,
423426
},
424-
UserLimits: topology.UserLimits{
425-
Connections: 4,
426-
Channels: 6,
427+
UserLimits: &topology.UserLimits{
428+
Connections: &connections,
429+
Channels: &channels,
427430
},
428431
},
429432
}
@@ -446,12 +449,8 @@ var _ = Describe("Users", func() {
446449
}, 30, 2).Should(BeNil())
447450
Expect(userLimitsInfo).To(HaveLen(1))
448451
Expect(userLimitsInfo[0].User).To(Equal(username))
449-
connectionLimit, ok := userLimitsInfo[0].Value["max-connections"]
450-
Expect(ok).To(BeTrue())
451-
Expect(connectionLimit).To(Equal(4))
452-
channelLimit, ok := userLimitsInfo[0].Value["max-channels"]
453-
Expect(ok).To(BeTrue())
454-
Expect(channelLimit).To(Equal(6))
452+
Expect(userLimitsInfo[0].Value).To(HaveKeyWithValue("max-connections", int(connections)))
453+
Expect(userLimitsInfo[0].Value).To(HaveKeyWithValue("max-channels", int(channels)))
455454

456455
By("deleting user")
457456
Expect(k8sClient.Delete(ctx, user)).To(Succeed())

0 commit comments

Comments
 (0)