Skip to content

Commit b1562fc

Browse files
committed
pr feedback 1
1 parent 765d846 commit b1562fc

27 files changed

+983
-286
lines changed

apis/bases/rabbitmq.openstack.org_rabbitmqpolicies.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ spec:
8585
description: RabbitmqClusterName - the name of the RabbitMQ cluster
8686
type: string
8787
vhostRef:
88-
description: VhostRef - reference to the RabbitMQVhost resource (defaults
89-
to "/")
88+
description: VhostRef - reference to the RabbitMQVhost resource (if
89+
empty, uses default vhost "/")
9090
type: string
9191
required:
9292
- definition

apis/bases/rabbitmq.openstack.org_rabbitmqusers.yaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,18 @@ spec:
6565
properties:
6666
configure:
6767
default: .*
68-
description: Configure - configure permission regex
68+
description: Configure - configure permission regex (default ".*"
69+
allows all, "" denies all)
6970
type: string
7071
read:
7172
default: .*
72-
description: Read - read permission regex
73+
description: Read - read permission regex (default ".*" allows
74+
all, "" denies all)
7375
type: string
7476
write:
7577
default: .*
76-
description: Write - write permission regex
78+
description: Write - write permission regex (default ".*" allows
79+
all, "" denies all)
7780
type: string
7881
type: object
7982
rabbitmqClusterName:

apis/bases/rabbitmq.openstack.org_rabbitmqvhosts.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ spec:
6060
name:
6161
default: /
6262
description: Name - the vhost name in RabbitMQ (defaults to "/")
63+
minLength: 1
6364
type: string
6465
rabbitmqClusterName:
6566
description: RabbitmqClusterName - the name of the RabbitMQ cluster

apis/rabbitmq/v1beta1/conditions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import (
2323
const (
2424
// TransportURLReadyCondition Status=True condition which indicates if TransportURL is configured and operational
2525
TransportURLReadyCondition condition.Type = "TransportURLReady"
26+
27+
// TransportURLFinalizer - finalizer to add to RabbitMQUsers owned by TransportURL
28+
TransportURLFinalizer = "transporturl.rabbitmq.openstack.org/finalizer"
2629
)
2730

2831
// TransportURL Reasons used by API objects.

apis/rabbitmq/v1beta1/rabbitmq_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ const (
4343
CrMaxLengthCorrection = 11
4444
errInvalidOverride = "invalid spec override (%s)"
4545
warnOverrideStatefulSet = "%s: is deprecated and will be removed in a future API version"
46+
47+
// Queue types
48+
// QueueTypeMirrored - mirrored queue type
49+
QueueTypeMirrored = "Mirrored"
50+
// QueueTypeQuorum - quorum queue type
51+
QueueTypeQuorum = "Quorum"
52+
// QueueTypeNone - no special queue type
53+
QueueTypeNone = "None"
4654
)
4755

4856
// PodOverride defines per-pod service configurations

apis/rabbitmq/v1beta1/rabbitmqpolicy_types.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type RabbitMQPolicySpec struct {
2929
RabbitmqClusterName string `json:"rabbitmqClusterName"`
3030

3131
// +kubebuilder:validation:Optional
32-
// VhostRef - reference to the RabbitMQVhost resource (defaults to "/")
32+
// VhostRef - reference to the RabbitMQVhost resource (if empty, uses default vhost "/")
3333
VhostRef string `json:"vhostRef,omitempty"`
3434

3535
// +kubebuilder:validation:Optional
@@ -104,15 +104,15 @@ func (instance RabbitMQPolicy) IsReady() bool {
104104
}
105105

106106
const (
107-
// PolicyReadyCondition indicates that the policy is ready
108-
PolicyReadyCondition condition.Type = "PolicyReady"
107+
// RabbitMQPolicyReadyCondition indicates that the policy is ready
108+
RabbitMQPolicyReadyCondition condition.Type = "RabbitMQPolicyReady"
109109

110-
// PolicyReadyMessage is the message for the PolicyReady condition
111-
PolicyReadyMessage = "RabbitMQ policy is ready"
110+
// RabbitMQPolicyReadyMessage is the message for the RabbitMQPolicyReady condition
111+
RabbitMQPolicyReadyMessage = "RabbitMQ policy is ready"
112112

113-
// PolicyReadyInitMessage is the message for the PolicyReady condition when not started
114-
PolicyReadyInitMessage = "RabbitMQ policy not started"
113+
// RabbitMQPolicyReadyInitMessage is the message for the RabbitMQPolicyReady condition when not started
114+
RabbitMQPolicyReadyInitMessage = "RabbitMQ policy not started"
115115

116-
// PolicyReadyErrorMessage is the message format for the PolicyReady condition when an error occurs
117-
PolicyReadyErrorMessage = "RabbitMQ policy error occurred %s"
116+
// RabbitMQPolicyReadyErrorMessage is the message format for the RabbitMQPolicyReady condition when an error occurs
117+
RabbitMQPolicyReadyErrorMessage = "RabbitMQ policy error occurred %s"
118118
)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import (
20+
"fmt"
21+
22+
apierrors "k8s.io/apimachinery/pkg/api/errors"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/apimachinery/pkg/runtime/schema"
25+
"k8s.io/apimachinery/pkg/util/validation/field"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
logf "sigs.k8s.io/controller-runtime/pkg/log"
28+
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
29+
)
30+
31+
var rabbitmqpolicylog = logf.Log.WithName("rabbitmqpolicy-resource")
32+
33+
//+kubebuilder:webhook:path=/mutate-rabbitmq-openstack-org-v1beta1-rabbitmqpolicy,mutating=true,failurePolicy=fail,sideEffects=None,groups=rabbitmq.openstack.org,resources=rabbitmqpolicies,verbs=create;update,versions=v1beta1,name=mrabbitmqpolicy.kb.io,admissionReviewVersions=v1
34+
35+
// Default implements defaulting for RabbitMQPolicy
36+
func (r *RabbitMQPolicy) Default(_ client.Client) {
37+
rabbitmqpolicylog.Info("default", "name", r.Name)
38+
39+
// Default the policy name to the CR name if not specified
40+
if r.Spec.Name == "" {
41+
r.Spec.Name = r.Name
42+
}
43+
}
44+
45+
//+kubebuilder:webhook:path=/validate-rabbitmq-openstack-org-v1beta1-rabbitmqpolicy,mutating=false,failurePolicy=fail,sideEffects=None,groups=rabbitmq.openstack.org,resources=rabbitmqpolicies,verbs=create;update,versions=v1beta1,name=vrabbitmqpolicy.kb.io,admissionReviewVersions=v1
46+
47+
// ValidateCreate validates the RabbitMQPolicy on creation
48+
func (r *RabbitMQPolicy) ValidateCreate(_ client.Client) (admission.Warnings, error) {
49+
rabbitmqpolicylog.Info("validate create", "name", r.Name)
50+
return nil, nil
51+
}
52+
53+
// ValidateUpdate validates the RabbitMQPolicy on update
54+
func (r *RabbitMQPolicy) ValidateUpdate(_ client.Client, old runtime.Object) (admission.Warnings, error) {
55+
rabbitmqpolicylog.Info("validate update", "name", r.Name)
56+
57+
oldPolicy, ok := old.(*RabbitMQPolicy)
58+
if !ok {
59+
return nil, fmt.Errorf("expected RabbitMQPolicy but got %T", old)
60+
}
61+
62+
// Prevent changing the policy name after creation
63+
if r.Spec.Name != oldPolicy.Spec.Name {
64+
return nil, apierrors.NewInvalid(
65+
schema.GroupKind{Group: "rabbitmq.openstack.org", Kind: "RabbitMQPolicy"},
66+
r.Name,
67+
field.ErrorList{
68+
field.Forbidden(
69+
field.NewPath("spec", "name"),
70+
"policy name cannot be changed after creation",
71+
),
72+
},
73+
)
74+
}
75+
76+
return nil, nil
77+
}
78+
79+
// ValidateDelete validates the RabbitMQPolicy on deletion
80+
func (r *RabbitMQPolicy) ValidateDelete(_ client.Client) (admission.Warnings, error) {
81+
return nil, nil
82+
}

apis/rabbitmq/v1beta1/rabbitmquser_types.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ import (
2525
type RabbitMQUserPermissions struct {
2626
// +kubebuilder:validation:Optional
2727
// +kubebuilder:default=".*"
28-
// Configure - configure permission regex
28+
// Configure - configure permission regex (default ".*" allows all, "" denies all)
2929
Configure string `json:"configure"`
3030

3131
// +kubebuilder:validation:Optional
3232
// +kubebuilder:default=".*"
33-
// Write - write permission regex
33+
// Write - write permission regex (default ".*" allows all, "" denies all)
3434
Write string `json:"write"`
3535

3636
// +kubebuilder:validation:Optional
3737
// +kubebuilder:default=".*"
38-
// Read - read permission regex
38+
// Read - read permission regex (default ".*" allows all, "" denies all)
3939
Read string `json:"read"`
4040
}
4141

@@ -55,7 +55,7 @@ type RabbitMQUserSpec struct {
5555

5656
// +kubebuilder:validation:Optional
5757
// Permissions - user permissions on the vhost
58-
Permissions RabbitMQUserPermissions `json:"permissions,omitempty"`
58+
Permissions RabbitMQUserPermissions `json:"permissions"`
5959

6060
// +kubebuilder:validation:Optional
6161
// Tags - RabbitMQ user tags
@@ -120,15 +120,15 @@ const (
120120
// UserFinalizer - finalizer to protect user from deletion when owned by TransportURL
121121
UserFinalizer = "rabbitmquser.rabbitmq.openstack.org/finalizer"
122122

123-
// UserReadyCondition indicates that the user is ready
124-
UserReadyCondition condition.Type = "UserReady"
123+
// RabbitMQUserReadyCondition indicates that the user is ready
124+
RabbitMQUserReadyCondition condition.Type = "RabbitMQUserReady"
125125

126-
// UserReadyMessage is the message for the UserReady condition
127-
UserReadyMessage = "RabbitMQ user is ready"
126+
// RabbitMQUserReadyMessage is the message for the RabbitMQUserReady condition
127+
RabbitMQUserReadyMessage = "RabbitMQ user is ready"
128128

129-
// UserReadyInitMessage is the message for the UserReady condition when not started
130-
UserReadyInitMessage = "RabbitMQ user not started"
129+
// RabbitMQUserReadyInitMessage is the message for the RabbitMQUserReady condition when not started
130+
RabbitMQUserReadyInitMessage = "RabbitMQ user not started"
131131

132-
// UserReadyErrorMessage is the message format for the UserReady condition when an error occurs
133-
UserReadyErrorMessage = "RabbitMQ user error occurred %s"
132+
// RabbitMQUserReadyErrorMessage is the message format for the RabbitMQUserReady condition when an error occurs
133+
RabbitMQUserReadyErrorMessage = "RabbitMQ user error occurred %s"
134134
)

apis/rabbitmq/v1beta1/rabbitmquser_webhook.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ import (
3131

3232
var rabbitmquserlog = logf.Log.WithName("rabbitmquser-resource")
3333

34+
//+kubebuilder:webhook:path=/mutate-rabbitmq-openstack-org-v1beta1-rabbitmquser,mutating=true,failurePolicy=fail,sideEffects=None,groups=rabbitmq.openstack.org,resources=rabbitmqusers,verbs=create;update,versions=v1beta1,name=mrabbitmquser.kb.io,admissionReviewVersions=v1
35+
36+
// Default implements defaulting for RabbitMQUser
37+
func (r *RabbitMQUser) Default(_ client.Client) {
38+
rabbitmquserlog.Info("default", "name", r.Name)
39+
40+
// Default the username to the CR name if not specified
41+
if r.Spec.Username == "" {
42+
r.Spec.Username = r.Name
43+
}
44+
}
45+
3446
//+kubebuilder:webhook:path=/validate-rabbitmq-openstack-org-v1beta1-rabbitmquser,mutating=false,failurePolicy=fail,sideEffects=None,groups=rabbitmq.openstack.org,resources=rabbitmqusers,verbs=create;update,versions=v1beta1,name=vrabbitmquser.kb.io,admissionReviewVersions=v1
3547

3648
// ValidateCreate validates the RabbitMQUser on creation
@@ -40,8 +52,28 @@ func (r *RabbitMQUser) ValidateCreate(k8sClient client.Client) (admission.Warnin
4052
}
4153

4254
// ValidateUpdate validates the RabbitMQUser on update
43-
func (r *RabbitMQUser) ValidateUpdate(k8sClient client.Client, _ runtime.Object) (admission.Warnings, error) {
55+
func (r *RabbitMQUser) ValidateUpdate(k8sClient client.Client, old runtime.Object) (admission.Warnings, error) {
4456
rabbitmquserlog.Info("validate update", "name", r.Name)
57+
58+
oldUser, ok := old.(*RabbitMQUser)
59+
if !ok {
60+
return nil, fmt.Errorf("expected RabbitMQUser but got %T", old)
61+
}
62+
63+
// Prevent changing the username after creation
64+
if r.Spec.Username != oldUser.Spec.Username {
65+
return nil, apierrors.NewInvalid(
66+
schema.GroupKind{Group: "rabbitmq.openstack.org", Kind: "RabbitMQUser"},
67+
r.Name,
68+
field.ErrorList{
69+
field.Forbidden(
70+
field.NewPath("spec", "username"),
71+
"username cannot be changed after creation",
72+
),
73+
},
74+
)
75+
}
76+
4577
return nil, r.validateUniqueUsername(k8sClient)
4678
}
4779

@@ -52,16 +84,6 @@ func (r *RabbitMQUser) ValidateDelete(client.Client) (admission.Warnings, error)
5284

5385
// validateUniqueUsername checks that no other RabbitMQUser exists with the same username, vhost, and cluster
5486
func (r *RabbitMQUser) validateUniqueUsername(k8sClient client.Client) error {
55-
if k8sClient == nil {
56-
return nil
57-
}
58-
59-
// Determine the username that will be used
60-
username := r.Spec.Username
61-
if username == "" {
62-
username = r.Name
63-
}
64-
6587
// List all RabbitMQUsers in the same namespace
6688
userList := &RabbitMQUserList{}
6789
if err := k8sClient.List(context.TODO(), userList, client.InNamespace(r.Namespace)); err != nil {
@@ -85,22 +107,16 @@ func (r *RabbitMQUser) validateUniqueUsername(k8sClient client.Client) error {
85107
continue
86108
}
87109

88-
// Determine the other user's username
89-
otherUsername := user.Spec.Username
90-
if otherUsername == "" {
91-
otherUsername = user.Name
92-
}
93-
94110
// If usernames match, reject
95-
if username == otherUsername {
111+
if r.Spec.Username == user.Spec.Username {
96112
return apierrors.NewInvalid(
97113
schema.GroupKind{Group: "rabbitmq.openstack.org", Kind: "RabbitMQUser"},
98114
r.Name,
99115
field.ErrorList{
100116
field.Duplicate(
101117
field.NewPath("spec", "username"),
102118
fmt.Sprintf("username %q already exists in vhost %q on cluster %q (existing RabbitMQUser: %s)",
103-
username, r.Spec.VhostRef, r.Spec.RabbitmqClusterName, user.Name),
119+
r.Spec.Username, r.Spec.VhostRef, r.Spec.RabbitmqClusterName, user.Name),
104120
),
105121
},
106122
)

apis/rabbitmq/v1beta1/rabbitmqvhost_types.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type RabbitMQVhostSpec struct {
2828
RabbitmqClusterName string `json:"rabbitmqClusterName"`
2929

3030
// +kubebuilder:validation:Optional
31+
// +kubebuilder:validation:MinLength=1
3132
// +kubebuilder:default="/"
3233
// Name - the vhost name in RabbitMQ (defaults to "/")
3334
Name string `json:"name"`
@@ -81,15 +82,15 @@ const (
8182
// VhostFinalizer - finalizer to protect vhost from deletion when owned by TransportURL
8283
VhostFinalizer = "rabbitmqvhost.rabbitmq.openstack.org/finalizer"
8384

84-
// VhostReadyCondition indicates that the vhost is ready
85-
VhostReadyCondition condition.Type = "VhostReady"
85+
// RabbitMQVhostReadyCondition indicates that the vhost is ready
86+
RabbitMQVhostReadyCondition condition.Type = "RabbitMQVhostReady"
8687

87-
// VhostReadyMessage is the message for the VhostReady condition
88-
VhostReadyMessage = "RabbitMQ vhost is ready"
88+
// RabbitMQVhostReadyMessage is the message for the RabbitMQVhostReady condition
89+
RabbitMQVhostReadyMessage = "RabbitMQ vhost is ready"
8990

90-
// VhostReadyInitMessage is the message for the VhostReady condition when not started
91-
VhostReadyInitMessage = "RabbitMQ vhost not started"
91+
// RabbitMQVhostReadyInitMessage is the message for the RabbitMQVhostReady condition when not started
92+
RabbitMQVhostReadyInitMessage = "RabbitMQ vhost not started"
9293

93-
// VhostReadyErrorMessage is the message format for the VhostReady condition when an error occurs
94-
VhostReadyErrorMessage = "RabbitMQ vhost error occurred %s"
94+
// RabbitMQVhostReadyErrorMessage is the message format for the RabbitMQVhostReady condition when an error occurs
95+
RabbitMQVhostReadyErrorMessage = "RabbitMQ vhost error occurred %s"
9596
)

0 commit comments

Comments
 (0)