/
reconcile.go
87 lines (70 loc) · 2.5 KB
/
reconcile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package user
import (
"context"
"fmt"
"github.com/go-logr/logr"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
openstackv1beta1 "github.com/ianunruh/openstack-operator/api/v1beta1"
"github.com/ianunruh/openstack-operator/pkg/rabbitmq"
"github.com/ianunruh/openstack-operator/pkg/template"
)
func SetupJob(instance *openstackv1beta1.RabbitMQUser, containerImage, databaseHostName, adminSecret string) *batchv1.Job {
labels := template.AppLabels(instance.Name, rabbitmq.AppLabel)
job := template.GenericJob(template.Component{
Namespace: instance.Namespace,
Labels: labels,
Containers: []corev1.Container{
{
Name: "user",
// TODO make configurable
Image: "rabbitmq:3.8.9-management",
Command: []string{
"bash",
"-c",
template.MustReadFile(rabbitmq.AppLabel, "user-setup.sh"),
},
Env: []corev1.EnvVar{
template.SecretEnvVar("RABBITMQ_ADMIN_CONNECTION", adminSecret, "connection"),
template.SecretEnvVar("RABBITMQ_USER_CONNECTION", instance.Spec.Secret, "connection"),
},
},
},
})
job.Name = template.Combine(instance.Spec.Cluster, "user", instance.Name)
return job
}
func Secret(instance *openstackv1beta1.RabbitMQUser) *corev1.Secret {
labels := template.AppLabels(instance.Name, rabbitmq.AppLabel)
secret := template.GenericSecret(instance.Spec.Secret, instance.Namespace, labels)
hostname := instance.Spec.Cluster
username := instance.Spec.Name
password := template.NewPassword()
vhost := instance.Spec.VirtualHost
secret.StringData["connection"] = fmt.Sprintf("rabbit://%s:%s@%s:5672/%s", username, password, hostname, vhost)
secret.StringData["password"] = password
return secret
}
func Ensure(ctx context.Context, c client.Client, instance *openstackv1beta1.RabbitMQUser, log logr.Logger) error {
intended := instance.DeepCopy()
hash, err := template.ObjectHash(intended)
if err != nil {
return fmt.Errorf("error hashing object: %w", err)
}
if err := c.Get(ctx, client.ObjectKeyFromObject(instance), instance); err != nil {
if !errors.IsNotFound(err) {
return err
}
template.SetAppliedHash(instance, hash)
log.Info("Creating RabbitMQUser", "Name", instance.Name)
return c.Create(ctx, instance)
} else if !template.MatchesAppliedHash(instance, hash) {
instance.Spec = intended.Spec
template.SetAppliedHash(instance, hash)
log.Info("Updating RabbitMQUser", "Name", instance.Name)
return c.Update(ctx, instance)
}
return nil
}