-
Notifications
You must be signed in to change notification settings - Fork 129
/
controller.go
191 lines (164 loc) · 6.9 KB
/
controller.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package healthcheck
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net/http"
"time"
// k8s
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
coreinformersv1 "k8s.io/client-go/informers/core/v1"
coreclientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/klog/v2"
// openshift
operatorsv1 "github.com/openshift/api/operator/v1"
routev1 "github.com/openshift/api/route/v1"
configclientv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
operatorclientv1 "github.com/openshift/client-go/operator/clientset/versioned/typed/operator/v1"
v1 "github.com/openshift/client-go/operator/informers/externalversions/operator/v1"
routeclientv1 "github.com/openshift/client-go/route/clientset/versioned/typed/route/v1"
routesinformersv1 "github.com/openshift/client-go/route/informers/externalversions/route/v1"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/v1helpers"
"github.com/openshift/library-go/pkg/route/routeapihelpers"
// console-operator
"github.com/openshift/console-operator/pkg/api"
"github.com/openshift/console-operator/pkg/console/controllers/util"
"github.com/openshift/console-operator/pkg/console/status"
routesub "github.com/openshift/console-operator/pkg/console/subresource/route"
)
type HealthCheckController struct {
// clients
operatorClient v1helpers.OperatorClient
operatorConfigClient operatorclientv1.ConsoleInterface
ingressClient configclientv1.IngressInterface
routeClient routeclientv1.RoutesGetter
configMapClient coreclientv1.ConfigMapsGetter
}
func NewHealthCheckController(
// top level config
configClient configclientv1.ConfigV1Interface,
// clients
operatorClient v1helpers.OperatorClient,
operatorConfigClient operatorclientv1.ConsoleInterface,
routev1Client routeclientv1.RoutesGetter,
configMapClient coreclientv1.ConfigMapsGetter,
// informers
operatorConfigInformer v1.ConsoleInformer,
coreInformer coreinformersv1.Interface,
routeInformer routesinformersv1.RouteInformer,
// events
recorder events.Recorder,
) factory.Controller {
ctrl := &HealthCheckController{
operatorClient: operatorClient,
operatorConfigClient: operatorConfigClient,
ingressClient: configClient.Ingresses(),
routeClient: routev1Client,
configMapClient: configMapClient,
}
configMapInformer := coreInformer.ConfigMaps()
return factory.New().
WithFilteredEventsInformers( // service
util.IncludeNamesFilter(api.TrustedCAConfigMapName, api.OAuthServingCertConfigMapName),
configMapInformer.Informer(),
).WithFilteredEventsInformers( // route
util.IncludeNamesFilter(api.OpenShiftConsoleRouteName, api.OpenshiftConsoleCustomRouteName),
routeInformer.Informer(),
).ResyncEvery(30*time.Second).WithSync(ctrl.Sync).
ToController("HealthCheckController", recorder.WithComponentSuffix("health-check-controller"))
}
func (c *HealthCheckController) Sync(ctx context.Context, controllerContext factory.SyncContext) error {
operatorConfig, err := c.operatorConfigClient.Get(ctx, api.ConfigResourceName, metav1.GetOptions{})
if err != nil {
return err
}
updatedOperatorConfig := operatorConfig.DeepCopy()
switch updatedOperatorConfig.Spec.ManagementState {
case operatorsv1.Managed:
klog.V(4).Infoln("console-operator is in a managed state: starting health checks")
case operatorsv1.Unmanaged:
klog.V(4).Infoln("console-operator is in an unmanaged state: skipping health checks")
return nil
case operatorsv1.Removed:
klog.V(4).Infoln("console-operator is in a removed state: skipping health checks")
return nil
default:
return fmt.Errorf("unknown state: %v", updatedOperatorConfig.Spec.ManagementState)
}
statusHandler := status.NewStatusHandler(c.operatorClient)
ingressConfig, err := c.ingressClient.Get(ctx, api.ConfigResourceName, metav1.GetOptions{})
if err != nil {
return statusHandler.FlushAndReturn(err)
}
activeRouteName := api.OpenShiftConsoleRouteName
routeConfig := routesub.NewRouteConfig(updatedOperatorConfig, ingressConfig, activeRouteName)
if routeConfig.IsCustomHostnameSet() {
activeRouteName = api.OpenshiftConsoleCustomRouteName
}
activeRoute, activeRouteErr := c.routeClient.Routes(api.OpenShiftConsoleNamespace).Get(ctx, activeRouteName, metav1.GetOptions{})
statusHandler.AddConditions(status.HandleProgressingOrDegraded("RouteHealth", "FailedRouteGet", activeRouteErr))
if activeRouteErr != nil {
statusHandler.FlushAndReturn(activeRouteErr)
}
routeHealthCheckErrReason, routeHealthCheckErr := c.CheckRouteHealth(ctx, updatedOperatorConfig, activeRoute)
statusHandler.AddCondition(status.HandleDegraded("RouteHealth", routeHealthCheckErrReason, routeHealthCheckErr))
statusHandler.AddCondition(status.HandleAvailable("RouteHealth", routeHealthCheckErrReason, routeHealthCheckErr))
return statusHandler.FlushAndReturn(routeHealthCheckErr)
}
func (c *HealthCheckController) CheckRouteHealth(ctx context.Context, operatorConfig *operatorsv1.Console, route *routev1.Route) (string, error) {
url, _, err := routeapihelpers.IngressURI(route, route.Spec.Host)
if err != nil {
return "RouteNotAdmitted", fmt.Errorf("console route is not admitted")
}
caPool, err := c.getCA(ctx, route.Spec.TLS)
if err != nil {
return "FailedLoadCA", fmt.Errorf("failed to read CA to check route health: %v", err)
}
client := clientWithCA(caPool)
req, err := http.NewRequest(http.MethodGet, url.String(), nil)
if err != nil {
return "FailedRequest", fmt.Errorf("failed to build request to route (%s): %v", url, err)
}
resp, err := client.Do(req)
if err != nil {
return "FailedGet", fmt.Errorf("failed to GET route (%s): %v", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "StatusError", fmt.Errorf("route not yet available, %s returns '%s'", url, resp.Status)
}
return "", nil
}
func (c *HealthCheckController) getCA(ctx context.Context, tls *routev1.TLSConfig) (*x509.CertPool, error) {
caCertPool := x509.NewCertPool()
if tls != nil && len(tls.Certificate) != 0 {
if ok := caCertPool.AppendCertsFromPEM([]byte(tls.Certificate)); !ok {
klog.V(4).Infof("failed to parse custom tls.crt")
}
}
for _, cmName := range []string{api.TrustedCAConfigMapName, api.DefaultIngressCertConfigMapName} {
cm, err := c.configMapClient.ConfigMaps(api.OpenShiftConsoleNamespace).Get(ctx, cmName, metav1.GetOptions{})
if err != nil {
klog.V(4).Infof("failed to GET configmap %s / %s ", api.OpenShiftConsoleNamespace, cmName)
return nil, err
}
if ok := caCertPool.AppendCertsFromPEM([]byte(cm.Data["ca-bundle.crt"])); !ok {
klog.V(4).Infof("failed to parse %s ca-bundle.crt", cmName)
}
}
return caCertPool, nil
}
func clientWithCA(caPool *x509.CertPool) *http.Client {
return &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
RootCAs: caPool,
},
},
}
}