Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schema validation for rabbitmq.additionalPlugins #87

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion api/v1beta1/rabbitmqcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ type RabbitmqClusterSpec struct {
Rabbitmq RabbitmqClusterConfigurationSpec `json:"rabbitmq,omitempty"`
}

// kubebuilder validating tags 'Pattern' and 'MaxLength' must be specified on string type.
// Alias type 'string' as 'Plugin' to specify schema validation on items of the list 'AdditionalPlugins'
// +kubebuilder:validation:Pattern:="^\\w+$"
// +kubebuilder:validation:MaxLength=100
type Plugin string

// Rabbitmq related configurations
type RabbitmqClusterConfigurationSpec struct {
// List of plugins to enable in addition to essential plugins: rabbitmq_management, rabbitmq_prometheus, and rabbitmq_peer_discovery_k8s.
AdditionalPlugins []string `json:"additionalPlugins,omitempty"`
// +kubebuilder:validation:MaxItems:=100
AdditionalPlugins []Plugin `json:"additionalPlugins,omitempty"`
}

// The settings for the persistent storage desired for each Pod in the RabbitmqCluster.
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/rabbitmqcluster_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ var _ = Describe("RabbitmqCluster", func() {
},
},
Rabbitmq: RabbitmqClusterConfigurationSpec{
AdditionalPlugins: []string{
AdditionalPlugins: []Plugin{
"my-plugins",
},
},
Expand Down
2 changes: 1 addition & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions charts/rabbitmq/expected-template-output
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ metadata:
labels:
label1: foo
label2: bar

annotations:
annotation1: foo
annotation2: bar


spec:
image: rabbitmq:3.8.1
Expand All @@ -22,7 +20,6 @@ spec:
type: LoadBalancer
annotations:
cloud.google.com/load-balancer-type: Internal

persistence:
storageClassName: foo
storage: 30Gi
Expand All @@ -31,7 +28,6 @@ spec:
key: dedicated
operator: Equal
value: rabbitmq

affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
Expand All @@ -42,15 +38,13 @@ spec:
values:
- e2e-az1
- e2e-az2

resources:
limits:
cpu: 888m
memory: 1Gi
requests:
cpu: 777m
memory: 1Gi

rabbitmq:
additionalPlugins:
- rabbitmq_shovel
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/rabbitmq.pivotal.io_rabbitmqclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,13 @@ spec:
description: 'List of plugins to enable in addition to essential
plugins: rabbitmq_management, rabbitmq_prometheus, and rabbitmq_peer_discovery_k8s.'
items:
description: kubebuilder validating tags 'Pattern' and 'MaxLength'
must be specified on string type. Alias type 'string' as 'Plugin'
to specify schema validation on items of the list 'AdditionalPlugins'
maxLength: 100
pattern: ^\w+$
type: string
maxItems: 100
type: array
type: object
replicas:
Expand Down
15 changes: 1 addition & 14 deletions controllers/rabbitmqcluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (r *RabbitmqClusterReconciler) enablePlugins(rmq *rabbitmqv1beta1.RabbitmqC
for i := 0; i < int(rmq.Spec.Replicas); i++ {
podName := fmt.Sprintf("%s-%d", rmq.ChildResourceName("server"), i)
rabbitCommand := fmt.Sprintf("rabbitmq-plugins set %s",
strings.Join(appendIfUnique(resource.RequiredPlugins, rmq.Spec.Rabbitmq.AdditionalPlugins), " "))
strings.Join(resource.AppendIfUnique(resource.RequiredPlugins, rmq.Spec.Rabbitmq.AdditionalPlugins), " "))

output, err := r.exec(rmq.Namespace, podName, "rabbitmq", "sh", "-c", rabbitCommand)

Expand Down Expand Up @@ -334,19 +334,6 @@ func containsString(slice []string, s string) bool {
return false
}

func appendIfUnique(a, b []string) []string {
check := make(map[string]bool)
list := append(a, b...)
set := make([]string, 0)
for _, s := range list {
if _, value := check[s]; !value {
check[s] = true
set = append(set, s)
}
}
return set
}

func (r *RabbitmqClusterReconciler) prepareForDeletion(ctx context.Context, rabbitmqCluster *rabbitmqv1beta1.RabbitmqCluster) error {
if containsString(rabbitmqCluster.ObjectMeta.Finalizers, deletionFinalizer) {
if err := clientretry.RetryOnConflict(clientretry.DefaultRetry, func() error {
Expand Down
11 changes: 8 additions & 3 deletions internal/resource/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,19 @@ func (builder *ServerConfigMapBuilder) Build() (runtime.Object, error) {
},
Data: map[string]string{
"rabbitmq.conf": defaultRabbitmqConf,
"enabled_plugins": "[" + strings.Join(appendIfUnique(RequiredPlugins, builder.Instance.Spec.Rabbitmq.AdditionalPlugins), ",") + "].",
"enabled_plugins": "[" + strings.Join(AppendIfUnique(RequiredPlugins, builder.Instance.Spec.Rabbitmq.AdditionalPlugins), ",") + "].",
},
}, nil
}

func appendIfUnique(a, b []string) []string {
func AppendIfUnique(a []string, b []rabbitmqv1beta1.Plugin) []string {
data := make([]string, len(b))
for i := range data {
data[i] = string(b[i])
}

check := make(map[string]bool)
list := append(a, b...)
list := append(a, data...)
set := make([]string, 0)
for _, s := range list {
if _, value := check[s]; !value {
Expand Down
2 changes: 1 addition & 1 deletion internal/resource/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var _ = Describe("GenerateServerConfigMap", func() {

When("additionalPlugins are provided in instance spec", func() {
It("appends provided plugins to a list of required ones and removes duplicates", func() {
instance.Spec.Rabbitmq.AdditionalPlugins = []string{"rabbitmq_management", "rabbitmq_management", "rabbitmq_shovel", "rabbitmq_top", "my_great_plugin"}
instance.Spec.Rabbitmq.AdditionalPlugins = []rabbitmqv1beta1.Plugin{"rabbitmq_management", "rabbitmq_management", "rabbitmq_shovel", "rabbitmq_top", "my_great_plugin"}

expectedEnabledPlugins := "[" +
"rabbitmq_peer_discovery_k8s," +
Expand Down
2 changes: 1 addition & 1 deletion system_tests/system_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var _ = Describe("Operator", func() {
// modify rabbitmqcluster.spec.rabbitmq.additionalPlugins
fetchedRabbit := &rabbitmqv1beta1.RabbitmqCluster{}
Expect(rmqClusterClient.Get(context.Background(), types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.Name}, fetchedRabbit)).NotTo(HaveOccurred())
fetchedRabbit.Spec.Rabbitmq.AdditionalPlugins = []string{"rabbitmq_top"}
fetchedRabbit.Spec.Rabbitmq.AdditionalPlugins = []rabbitmqv1beta1.Plugin{"rabbitmq_top"}
Expect(rmqClusterClient.Update(context.TODO(), fetchedRabbit)).To(Succeed())

// wait for pod being restarted and becoming not ready
Expand Down