/
banner.go
72 lines (61 loc) · 1.92 KB
/
banner.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
package banner
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
"fmt"
consolev1 "github.com/openshift/api/console/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
)
func (r *Reconciler) reconcileBanner(ctx context.Context, instance *arov1alpha1.Cluster) error {
var text string
switch instance.Spec.Banner.Content {
case arov1alpha1.BannerDisabled:
banner := &consolev1.ConsoleNotification{
ObjectMeta: metav1.ObjectMeta{
Name: BannerName,
},
}
err := r.client.Delete(ctx, banner)
if err != nil && kerrors.IsNotFound(err) {
// we don't care if the object doesn't exist
return nil
}
return err
case arov1alpha1.BannerContactSupport:
text = fmt.Sprintf(TextContactSupport, instance.Spec.ResourceID)
default:
return fmt.Errorf("wrong banner setting '%s'", instance.Spec.Banner.Content)
}
return r.createOrUpdate(ctx, text)
}
func (r *Reconciler) createOrUpdate(ctx context.Context, text string) error {
oldBanner := &consolev1.ConsoleNotification{}
err := r.client.Get(ctx, types.NamespacedName{Name: BannerName}, oldBanner)
if err != nil && !kerrors.IsNotFound(err) {
return err
}
// if the object doesn't exist Create
if err != nil && kerrors.IsNotFound(err) {
return r.client.Create(ctx, r.newBanner(text))
}
// if there's no errors, object found then update
oldBanner.Spec.Text = text
return r.client.Update(ctx, oldBanner)
}
func (r *Reconciler) newBanner(text string) *consolev1.ConsoleNotification {
return &consolev1.ConsoleNotification{
ObjectMeta: metav1.ObjectMeta{
Name: BannerName,
},
Spec: consolev1.ConsoleNotificationSpec{
Text: text,
Location: consolev1.BannerTop,
Color: "#000",
BackgroundColor: "#ff0",
},
}
}