Skip to content

Commit

Permalink
Add policies.rabbitmq.com (#46)
Browse files Browse the repository at this point in the history
* Add policies.rabbitmq.com

- support create, update and delete
- correct minor issues with crds description
  • Loading branch information
ChunyiLyu committed Mar 9, 2021
1 parent 760dc03 commit 3043910
Show file tree
Hide file tree
Showing 27 changed files with 911 additions and 33 deletions.
3 changes: 3 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ resources:
- group: rabbitmq.com
kind: Vhost
version: v1alpha1
- group: rabbitmq.com
kind: Policy
version: v1alpha1
version: "2"
4 changes: 2 additions & 2 deletions api/v1alpha1/binding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type BindingSpec struct {
// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
Arguments *runtime.RawExtension `json:"arguments,omitempty"`
// Reference to the RabbitmqCluster that the exchange will be created in
// Required property
// Reference to the RabbitmqCluster that the binding will be created in.
// Required property.
// +kubebuilder:validation:Required
RabbitmqClusterReference RabbitmqClusterReference `json:"rabbitmqClusterReference"`
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/exchange_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type ExchangeSpec struct {
// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
Arguments *runtime.RawExtension `json:"arguments,omitempty"`
// Reference to the RabbitmqCluster that the exchange will be created in
// Required property
// Reference to the RabbitmqCluster that the exchange will be created in.
// Required property.
// +kubebuilder:validation:Required
RabbitmqClusterReference RabbitmqClusterReference `json:"rabbitmqClusterReference"`
}
Expand Down
66 changes: 66 additions & 0 deletions api/v1alpha1/policy_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// PolicySpec defines the desired state of Policy
// https://www.rabbitmq.com/parameters.html#policies
type PolicySpec struct {
// +kubebuilder:validation:Required
Name string `json:"name"`
// Default to vhost '/'
// +kubebuilder:default:=/
Vhost string `json:"vhost,omitempty"`
// Regular expression pattern used to match queues and exchanges, e.g. "^amq.".
// Required property.
// +kubebuilder:validation:Required
Pattern string `json:"pattern"`
// What this policy applies to: 'queues', 'exchanges', or 'all'.
// Default to 'all'.
// +kubebuilder:validation:Enum=queues;exchanges;all
// +kubebuilder:default:=all
ApplyTo string `json:"applyTo,omitempty"`
// Default to '0'.
// In the event that more than one policy can match a given exchange or queue, the policy with the greatest priority applies.
// +kubebuilder:default:=0
Priority int `json:"priority,omitempty"`
// Policy definition. Required property.
// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
// +kubebuilder:validation:Required
Definition *runtime.RawExtension `json:"definition"`
// Reference to the RabbitmqCluster that the exchange will be created in.
// Required property.
// +kubebuilder:validation:Required
RabbitmqClusterReference RabbitmqClusterReference `json:"rabbitmqClusterReference"`
}

// PolicyStatus defines the observed state of Policy
type PolicyStatus struct {
}

// +kubebuilder:object:root=true

// Policy is the Schema for the policies API
type Policy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec PolicySpec `json:"spec,omitempty"`
Status PolicyStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// PolicyList contains a list of Policy
type PolicyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Policy `json:"items"`
}

func init() {
SchemeBuilder.Register(&Policy{}, &PolicyList{})
}
121 changes: 121 additions & 0 deletions api/v1alpha1/policy_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package v1alpha1

import (
"context"
"k8s.io/apimachinery/pkg/runtime"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("Policy", func() {
var (
namespace = "default"
ctx = context.Background()
)

It("creates a policy with minimal configurations", func() {
policy := Policy{
ObjectMeta: metav1.ObjectMeta{
Name: "test-policy",
Namespace: namespace,
},
Spec: PolicySpec{
Name: "test-policy",
Pattern: "a-queue-name",
Definition: &runtime.RawExtension{
Raw: []byte(`{"key":"value"}`),
},
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "some-cluster",
Namespace: namespace,
},
},
}
Expect(k8sClient.Create(ctx, &policy)).To(Succeed())
fetched := &Policy{}
Expect(k8sClient.Get(ctx, types.NamespacedName{
Name: policy.Name,
Namespace: policy.Namespace,
}, fetched)).To(Succeed())
Expect(fetched.Spec.RabbitmqClusterReference).To(Equal(RabbitmqClusterReference{
Name: "some-cluster",
Namespace: namespace,
}))
Expect(fetched.Spec.Name).To(Equal("test-policy"))
Expect(fetched.Spec.Vhost).To(Equal("/"))
Expect(fetched.Spec.Pattern).To(Equal("a-queue-name"))
Expect(fetched.Spec.ApplyTo).To(Equal("all"))
Expect(fetched.Spec.Priority).To(Equal(0))
Expect(fetched.Spec.Definition.Raw).To(Equal([]byte(`{"key":"value"}`)))
})

It("creates policy with configurations", func() {
policy := Policy{
ObjectMeta: metav1.ObjectMeta{
Name: "random-policy",
Namespace: namespace,
},
Spec: PolicySpec{
Name: "test-policy",
Vhost: "/hello",
Pattern: "*.",
ApplyTo: "exchanges",
Priority: 100,
Definition: &runtime.RawExtension{
Raw: []byte(`{"key":"value"}`),
},
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "random-cluster",
Namespace: namespace,
},
},
}
Expect(k8sClient.Create(ctx, &policy)).To(Succeed())
fetched := &Policy{}
Expect(k8sClient.Get(ctx, types.NamespacedName{
Name: policy.Name,
Namespace: policy.Namespace,
}, fetched)).To(Succeed())

Expect(fetched.Spec.Name).To(Equal("test-policy"))
Expect(fetched.Spec.Vhost).To(Equal("/hello"))
Expect(fetched.Spec.Pattern).To(Equal("*."))
Expect(fetched.Spec.ApplyTo).To(Equal("exchanges"))
Expect(fetched.Spec.Priority).To(Equal(100))
Expect(fetched.Spec.RabbitmqClusterReference).To(Equal(
RabbitmqClusterReference{
Name: "random-cluster",
Namespace: namespace,
}))
Expect(fetched.Spec.Definition.Raw).To(Equal([]byte(`{"key":"value"}`)))
})

When("creating a policy with an invalid 'ApplyTo' value", func() {
It("fails with validation errors", func() {
policy := Policy{
ObjectMeta: metav1.ObjectMeta{
Name: "invalid",
Namespace: namespace,
},
Spec: PolicySpec{
Name: "test-policy",
Pattern: "a-queue-name",
Definition: &runtime.RawExtension{
Raw: []byte(`{"key":"value"}`),
},
ApplyTo: "yo-yo",
RabbitmqClusterReference: RabbitmqClusterReference{
Name: "some-cluster",
Namespace: namespace,
},
},
}
Expect(k8sClient.Create(ctx, &policy)).To(HaveOccurred())
Expect(k8sClient.Create(ctx, &policy)).To(MatchError(`Policy.rabbitmq.com "invalid" is invalid: spec.applyTo: Unsupported value: "yo-yo": supported values: "queues", "exchanges", "all"`))
})
})

})
4 changes: 2 additions & 2 deletions api/v1alpha1/queue_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type QueueSpec struct {
// +kubebuilder:validation:Type=object
// +kubebuilder:pruning:PreserveUnknownFields
Arguments *runtime.RawExtension `json:"arguments,omitempty"`
// Reference to the RabbitmqCluster that the queue will be created in
// Required property
// Reference to the RabbitmqCluster that the queue will be created in.
// Required property.
// +kubebuilder:validation:Required
RabbitmqClusterReference RabbitmqClusterReference `json:"rabbitmqClusterReference"`
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1alpha1/vhost_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type VhostSpec struct {
// +kubebuilder:validation:Required
Name string `json:"name"`
Tracing bool `json:"tracing,omitempty"`
// Reference to the RabbitmqCluster that the vhost will be created in
// Required property
// Reference to the RabbitmqCluster that the vhost will be created in.
// Required property.
// +kubebuilder:validation:Required
RabbitmqClusterReference RabbitmqClusterReference `json:"rabbitmqClusterReference"`
}
Expand Down
95 changes: 95 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

11 changes: 8 additions & 3 deletions config/crd/bases/rabbitmq.com_bindings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ spec:
description: Binding is the Schema for the bindings API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
Expand All @@ -43,7 +47,8 @@ spec:
- queue
type: string
rabbitmqClusterReference:
description: Reference to the RabbitmqCluster that the exchange will be created in Required property
description: Reference to the RabbitmqCluster that the binding will
be created in. Required property.
properties:
name:
type: string
Expand Down
11 changes: 8 additions & 3 deletions config/crd/bases/rabbitmq.com_exchanges.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ spec:
description: Exchange is the Schema for the exchanges API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
Expand All @@ -42,7 +46,8 @@ spec:
name:
type: string
rabbitmqClusterReference:
description: Reference to the RabbitmqCluster that the exchange will be created in Required property
description: Reference to the RabbitmqCluster that the exchange will
be created in. Required property.
properties:
name:
type: string
Expand Down

0 comments on commit 3043910

Please sign in to comment.