Skip to content

Commit

Permalink
Merge pull request #1027 from rabbitmq/examples-namespace
Browse files Browse the repository at this point in the history
Document `examples` namespace in examples and small refactors
  • Loading branch information
ChunyiLyu authored Apr 28, 2022
2 parents 9b84a28 + 8b3b56e commit a64360d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion controllers/reconcile_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (r *RabbitmqClusterReconciler) reconcilePVC(ctx context.Context, rmq *rabbi
err := scaling.NewPersistenceScaler(r.Clientset).Scale(ctx, *rmq, desiredCapacity)
if err != nil {
msg := fmt.Sprintf("Failed to scale PVCs: %s", err.Error())
logger.Error(fmt.Errorf("Hit an error while scaling PVC capacity: %w", err), msg)
logger.Error(fmt.Errorf("hit an error while scaling PVC capacity: %w", err), msg)
r.Recorder.Event(rmq, corev1.EventTypeWarning, "FailedReconcilePersistence", msg)
}
return err
Expand Down
9 changes: 9 additions & 0 deletions docs/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ The test and setup scripts can assume that [Cert Manager](https://cert-manager.i
There is also a cluster issuer to produce self-signed certificates, named `selfsigned-issuer`. It is also
acceptable to create local `Issuer`s when needed.

### Namespace

Some examples in this folder default to namespace `examples`, which can be created by:

```shell
kubectl create ns examples
```

You can also replace the namespace with any existing namespace in your environment.
10 changes: 9 additions & 1 deletion docs/examples/vault-default-user/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ This example requires:
4. The RabbitMQ admin credentials were already written to Vault to path `spec.secretBackend.vault.defaultUserPath` with keys `username` and `password` (by some cluster-operator external mechanism. The cluster-operator will never write admin credentials to Vault).
5. Role `spec.secretBackend.vault.role` is configured in Vault with a policy to read from `defaultUserPath`.

Run script [setup.sh](./setup.sh) to get started with a Vault server in [dev mode](https://www.vaultproject.io/docs/concepts/dev-server) fullfilling above requirements. (This script is not production-ready. It is only meant to get you started experiencing end-to-end how RabbitMQ integrates with Vault.)
Run script [setup.sh](./setup.sh) to get started with a Vault server in [dev mode](https://www.vaultproject.io/docs/concepts/dev-server) fulfilling above requirements.
[setup.sh](./setup.sh) assumes you are using namespace `examples`, which can be created by:

```shell
kubectl create ns examples
```

If you want to deploy this example in a different existing namespace, you can set environment variable `RABBITMQ_NAMESPACE` when you run the script.
(This script is not production-ready. It is only meant to get you started experiencing end-to-end how RabbitMQ integrates with Vault.)

You can deploy this example like this:

Expand Down
6 changes: 3 additions & 3 deletions internal/metadata/annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package metadata_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
internal_metadata "github.com/rabbitmq/cluster-operator/internal/metadata"
internalmetadata "github.com/rabbitmq/cluster-operator/internal/metadata"
)

var _ = Describe("Annotation", func() {
Expand All @@ -16,7 +16,7 @@ var _ = Describe("Annotation", func() {

DescribeTable("Reconcile annotations",
func(expectedAnnotations map[string]string, existingAnnotations map[string]string, defaultAnnotations ...map[string]string) {
reconciledAnnotations := internal_metadata.ReconcileAnnotations(existingAnnotations, defaultAnnotations...)
reconciledAnnotations := internalmetadata.ReconcileAnnotations(existingAnnotations, defaultAnnotations...)
Expect(reconciledAnnotations).To(Equal(expectedAnnotations))
},

Expand Down Expand Up @@ -55,7 +55,7 @@ var _ = Describe("Annotation", func() {

DescribeTable("Reconcile and filter annotations",
func(expectedAnnotations map[string]string, existingAnnotations map[string]string, defaultAnnotations ...map[string]string) {
reconciledAnnotations := internal_metadata.ReconcileAndFilterAnnotations(existingAnnotations, defaultAnnotations...)
reconciledAnnotations := internalmetadata.ReconcileAndFilterAnnotations(existingAnnotations, defaultAnnotations...)
Expect(reconciledAnnotations).To(Equal(expectedAnnotations))
},

Expand Down
10 changes: 5 additions & 5 deletions internal/scaling/scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (p PersistenceScaler) Scale(ctx context.Context, rmq rabbitmqv1beta1.Rabbit

existingCapacity, err := p.existingCapacity(ctx, rmq)
if client.IgnoreNotFound(err) != nil {
logErr := fmt.Errorf("Failed to determine existing STS capactiy: %w", err)
logErr := fmt.Errorf("failed to determine existing STS capactiy: %w", err)
logger.Error(logErr, "Could not read sts")
return logErr
}
Expand Down Expand Up @@ -65,7 +65,7 @@ func (p PersistenceScaler) Scale(ctx context.Context, rmq rabbitmqv1beta1.Rabbit
logger.Info("Scaling up PVCs", "RabbitmqCluster", rmq.Name, "pvcsToBeScaled", pvcsToBeScaled)

if err := p.deleteSts(ctx, rmq); err != nil {
logErr := fmt.Errorf("Failed to delete Statefulset from Kubernetes API: %w", err)
logErr := fmt.Errorf("failed to delete Statefulset from Kubernetes API: %w", err)
logger.Error(logErr, "Could not delete existing sts")
return logErr
}
Expand All @@ -82,7 +82,7 @@ func (p PersistenceScaler) getClusterPVCs(ctx context.Context, rmq rabbitmqv1bet
for i = 0; i < pointer.Int32Deref(rmq.Spec.Replicas, 1); i++ {
pvc, err := p.Client.CoreV1().PersistentVolumeClaims(rmq.Namespace).Get(ctx, rmq.PVCName(int(i)), metav1.GetOptions{})
if client.IgnoreNotFound(err) != nil {
logErr := fmt.Errorf("Failed to get PVC from Kubernetes API: %w", err)
logErr := fmt.Errorf("failed to get PVC from Kubernetes API: %w", err)
logger.Error(logErr, "Could not read existing PVC")
return nil, logErr
}
Expand Down Expand Up @@ -137,7 +137,7 @@ func (p PersistenceScaler) deleteSts(ctx context.Context, rmq rabbitmqv1beta1.Ra

sts, err := p.getSts(ctx, rmq)
if client.IgnoreNotFound(err) != nil {
logErr := fmt.Errorf("Failed to get statefulset from Kubernetes API: %w", err)
logErr := fmt.Errorf("failed to get statefulset from Kubernetes API: %w", err)
logger.Error(logErr, "Could not read existing statefulset")
return logErr
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (p PersistenceScaler) scaleUpPVCs(ctx context.Context, rmq rabbitmqv1beta1.
// To minimise any timing windows, retrieve the latest version of this PVC before updating
pvc, err := p.Client.CoreV1().PersistentVolumeClaims(pvc.Namespace).Get(ctx, pvc.Name, metav1.GetOptions{})
if err != nil {
logErr := fmt.Errorf("Failed to get PVC from Kubernetes API: %w", err)
logErr := fmt.Errorf("failed to get PVC from Kubernetes API: %w", err)
logger.Error(logErr, "Could not read existing PVC")
return logErr
}
Expand Down
4 changes: 2 additions & 2 deletions internal/scaling/scaling_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
ephemeralStorage = k8sresource.MustParse("0")
)

func generatePVCTemplate(rmq rabbitmqv1beta1.RabbitmqCluster, size k8sresource.Quantity) corev1.PersistentVolumeClaim {
func generatePVCTemplate(size k8sresource.Quantity) corev1.PersistentVolumeClaim {
return corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "persistence",
Expand Down Expand Up @@ -201,7 +201,7 @@ func (matcher *UpdateActionMatcher) Match(actual interface{}) (bool, error) {
updatedObject := reflect.ValueOf(action.GetObject()).Elem()
objMeta, ok := updatedObject.FieldByName("ObjectMeta").Interface().(metav1.ObjectMeta)
if !ok {
return false, fmt.Errorf("Object of action was not an object with ObjectMeta")
return false, fmt.Errorf("object of action was not an object with ObjectMeta")
}

// Check the object's Name, Namespace, resource type and the verb of the action first. If this fails, there's
Expand Down
2 changes: 1 addition & 1 deletion internal/scaling/scaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ = Describe("Scaling", func() {
Namespace: namespace,
},
Spec: appsv1.StatefulSetSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{generatePVCTemplate(rmq, tenG)},
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{generatePVCTemplate(tenG)},
},
}
})
Expand Down

0 comments on commit a64360d

Please sign in to comment.