-
Notifications
You must be signed in to change notification settings - Fork 37
/
cf_initializing.go
139 lines (119 loc) · 4.21 KB
/
cf_initializing.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package condition
import (
"crypto/tls"
"github.com/Apicurio/apicurio-registry-operator/controllers/cf"
c "github.com/Apicurio/apicurio-registry-operator/controllers/common"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop/context"
"github.com/Apicurio/apicurio-registry-operator/controllers/loop/services"
"github.com/Apicurio/apicurio-registry-operator/controllers/svc/resources"
"go.uber.org/zap"
core "k8s.io/api/core/v1"
"net/http"
"os"
"strings"
"time"
)
var _ loop.ControlFunction = &InitializingCF{}
type InitializingCF struct {
ctx context.LoopContext
log *zap.SugaredLogger
services services.LoopServices
httpClient http.Client
initializing bool
targetType core.ServiceType
targetIP string
requestOk bool
disabled bool
}
func NewInitializingCF(ctx context.LoopContext, services services.LoopServices) loop.ControlFunction {
res := &InitializingCF{
ctx: ctx,
services: services,
httpClient: http.Client{
Timeout: 3 * time.Second,
Transport: &http.Transport{
// ignore expired SSL certificates for health checks
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
initializing: true,
requestOk: false,
}
res.log = ctx.GetLog().Sugar().With("cf", res.Describe())
return res
}
func (this *InitializingCF) Describe() string {
return "InitializingCF"
}
func (this *InitializingCF) Sense() {
// This CF runs only at the initialization
// Improve speed by avoiding unnecessary HTTP requests
if !this.initializing || this.ctx.GetAttempts() > 0 {
return
}
this.disabled = false
if entry, exists := this.ctx.GetEnvCache().Get(cf.ENV_REGISTRY_AUTH_ENABLED); exists {
this.disabled = strings.ToLower(entry.GetValue().Value) == "true"
}
if !this.disabled {
// The application is initialized if we can make an HTTP request to the app via the Service
// (as Ingress/Route might not work on some systems, or without additional config).
var port string = "8080"
var scheme string = "http://"
if serviceEntry, exists := this.ctx.GetResourceCache().Get(resources.RC_KEY_SERVICE); exists {
this.targetType = serviceEntry.GetValue().(*core.Service).Spec.Type
this.targetIP = serviceEntry.GetValue().(*core.Service).Spec.ClusterIP
if c.HasPort("https", serviceEntry.GetValue().(*core.Service).Spec.Ports) {
port = "8443"
scheme = "https://"
}
}
this.requestOk = false
if this.ctx.GetTestingSupport().IsEnabled() {
this.requestOk = this.ctx.GetTestingSupport().GetMockCanMakeHTTPRequestToOperand(this.ctx.GetAppNamespace().Str())
} else {
if this.targetType == core.ServiceTypeClusterIP && this.targetIP != "" {
url := scheme + this.targetIP + ":" + port
res, err := this.httpClient.Get(url)
if err == nil {
defer res.Body.Close()
if res.StatusCode >= 200 && res.StatusCode < 300 {
this.requestOk = true
} else {
this.log.Warnw("request to check that Apicurio Registry instance is available has failed with a status", "url", url, "status", res.StatusCode)
}
} else if os.IsTimeout(err) {
this.log.Warnw("request to check that Apicurio Registry instance is available has timed out", "url", url, "timeout", this.httpClient.Timeout)
} else {
this.log.Warnw("request to check that Apicurio Registry instance is available has failed", "url", url)
}
}
}
} else {
this.log.Infow("initializing health check is disabled because auth is enabled")
this.initializing = false
}
}
func (this *InitializingCF) Compare() bool {
// Executing only when initializing
// Prevent loop from getting stable by only executing once
return !this.disabled && this.initializing && this.ctx.GetAttempts() == 0
}
func (this *InitializingCF) Respond() {
if !this.requestOk {
this.services.GetConditionManager().GetReadyCondition().TransitionInitializing()
this.ctx.SetRequeueDelaySoon()
} else {
this.initializing = false
this.httpClient.CloseIdleConnections()
// The condition is reset automatically
}
if this.ctx.GetTestingSupport().IsEnabled() {
this.ctx.SetRequeueNow() // Ensure the reconciler is executed again very soon
}
}
func (this *InitializingCF) Cleanup() bool {
// No cleanup
return true
}