/
app.go
66 lines (50 loc) · 1.89 KB
/
app.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
package senlin
import (
"context"
"fmt"
"github.com/go-logr/logr"
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/template"
)
const (
AppLabel = "senlin"
)
var (
appUID = int64(42443)
)
func ConfigMap(instance *openstackv1beta1.Senlin) *corev1.ConfigMap {
labels := template.AppLabels(instance.Name, AppLabel)
cm := template.GenericConfigMap(instance.Name, instance.Namespace, labels)
cfg := template.MustLoadINI(AppLabel, "senlin.conf")
template.MergeINI(cfg, instance.Spec.ExtraConfig)
cm.Data["senlin.conf"] = template.MustOutputINI(cfg).String()
cm.Data["kolla-senlin-api.json"] = template.MustReadFile(AppLabel, "kolla-senlin-api.json")
cm.Data["kolla-senlin-conductor.json"] = template.MustReadFile(AppLabel, "kolla-senlin-conductor.json")
cm.Data["kolla-senlin-engine.json"] = template.MustReadFile(AppLabel, "kolla-senlin-engine.json")
cm.Data["kolla-senlin-health-manager.json"] = template.MustReadFile(AppLabel, "kolla-senlin-health-manager.json")
return cm
}
func EnsureSenlin(ctx context.Context, c client.Client, intended *openstackv1beta1.Senlin, log logr.Logger) error {
hash, err := template.ObjectHash(intended)
if err != nil {
return fmt.Errorf("error hashing object: %w", err)
}
found := &openstackv1beta1.Senlin{}
if err := c.Get(ctx, client.ObjectKeyFromObject(intended), found); err != nil {
if !errors.IsNotFound(err) {
return err
}
template.SetAppliedHash(intended, hash)
log.Info("Creating Senlin", "Name", intended.Name)
return c.Create(ctx, intended)
} else if !template.MatchesAppliedHash(found, hash) {
found.Spec = intended.Spec
template.SetAppliedHash(found, hash)
log.Info("Updating Senlin", "Name", intended.Name)
return c.Update(ctx, found)
}
return nil
}