/
cf_host_init_route_ocp.go
89 lines (77 loc) · 2.71 KB
/
cf_host_init_route_ocp.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
88
89
package cf
import (
"strings"
ar "github.com/Apicurio/apicurio-registry-operator/api/v1"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop/context"
"github.com/Apicurio/apicurio-registry-operator/controllers/svc/resources"
ocp_route "github.com/openshift/api/route/v1"
)
var _ loop.ControlFunction = &HostInitRouteOcpCF{}
type HostInitRouteOcpCF struct {
ctx context.LoopContext
svcResourceCache resources.ResourceCache
isFirstRespond bool
existingHost string
existingRouterCanonicalHostname string
specEntry resources.ResourceCacheEntry
routeEntry resources.ResourceCacheEntry
}
func NewHostInitRouteOcpCF(ctx context.LoopContext) loop.ControlFunction {
return &HostInitRouteOcpCF{
ctx: ctx,
svcResourceCache: ctx.GetResourceCache(),
isFirstRespond: true,
existingHost: "",
existingRouterCanonicalHostname: "",
specEntry: nil,
routeEntry: nil,
}
}
func (this *HostInitRouteOcpCF) Describe() string {
return "HostInitRouteOcpCF"
}
func (this *HostInitRouteOcpCF) Sense() {
// Optimization
if !this.isFirstRespond {
return
}
// Observation #1
// Get spec for patching & the target host
this.existingHost = ""
if specEntry, exists := this.svcResourceCache.Get(resources.RC_KEY_SPEC); exists {
this.specEntry = specEntry
this.existingHost = specEntry.GetValue().(*ar.ApicurioRegistry).Spec.Deployment.Host
}
// Observation #2
this.existingRouterCanonicalHostname = ""
if routeEntry, exists := this.svcResourceCache.Get(resources.RC_KEY_ROUTE_OCP); exists {
this.routeEntry = routeEntry
for _, v := range routeEntry.GetValue().(*ocp_route.Route).Status.Ingress {
if v.Host == this.existingHost {
this.existingRouterCanonicalHostname = v.RouterCanonicalHostname
}
}
this.ctx.SetRequeueDelaySoon()
}
}
func (this *HostInitRouteOcpCF) Compare() bool {
// Condition #1
return this.isFirstRespond && this.existingHost != "" && this.existingRouterCanonicalHostname != ""
}
func (this *HostInitRouteOcpCF) Respond() {
this.isFirstRespond = false
if !strings.HasSuffix(this.existingHost, this.existingRouterCanonicalHostname) {
// Response #1
// Patch the resource
this.specEntry.ApplyPatch(func(value interface{}) interface{} {
spec := value.(*ar.ApicurioRegistry).DeepCopy()
spec.Spec.Deployment.Host = this.existingHost + "." + this.existingRouterCanonicalHostname
return spec
})
}
}
func (this *HostInitRouteOcpCF) Cleanup() bool {
// No cleanup
return true
}