Skip to content

Commit 37a8a23

Browse files
author
Serhii Zakharov
committed
implemented fetching rules
1 parent 3932154 commit 37a8a23

File tree

24 files changed

+923
-554
lines changed

24 files changed

+923
-554
lines changed

README.md

+47-36
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Insights Operator
22

3-
This cluster operator gathers anonymized system configuration and reports it to Red Hat Insights. It is a part of the
4-
standard OpenShift distribution. The data collected allows for debugging in the event of cluster failures or
3+
This cluster operator gathers anonymized system configuration and reports it to Red Hat Insights. It is a part of the
4+
standard OpenShift distribution. The data collected allows for debugging in the event of cluster failures or
55
unanticipated errors.
66

77
# Table of Contents
@@ -12,26 +12,27 @@ unanticipated errors.
1212
- [Testing](#testing)
1313
- [Documentation](#documentation)
1414
- [Getting metrics from Prometheus](#getting-metrics-from-prometheus)
15-
- [Generate the certificate and key](#generate-the-certificate-and-key)
16-
- [Prometheus metrics provided by Insights Operator](#prometheus-metrics-provided-by-insights-operator)
17-
- [Running IO locally](#running-io-locally)
18-
- [Running IO on K8s](#running-io-on-k8s)
19-
- [Getting the data directly from Prometheus](#getting-the-data-directly-from-prometheus)
20-
- [Debugging Prometheus metrics without valid CA](#debugging-prometheus-metrics-without-valid-ca)
15+
- [Generate the certificate and key](#generate-the-certificate-and-key)
16+
- [Prometheus metrics provided by Insights Operator](#prometheus-metrics-provided-by-insights-operator)
17+
- [Running IO locally](#running-io-locally)
18+
- [Running IO on K8s](#running-io-on-k8s)
19+
- [Getting the data directly from Prometheus](#getting-the-data-directly-from-prometheus)
20+
- [Debugging Prometheus metrics without valid CA](#debugging-prometheus-metrics-without-valid-ca)
2121
- [Debugging](#debugging)
22-
- [Using the profiler](#using-the-profiler)
23-
- [Starting IO with the profiler](#starting-io-with-the-profiler)
24-
- [Collect profiling data](#collect-profiling-data)
25-
- [Analyzing profiling data](#analyzing-profiling-data)
22+
- [Using the profiler](#using-the-profiler)
23+
- [Starting IO with the profiler](#starting-io-with-the-profiler)
24+
- [Collect profiling data](#collect-profiling-data)
25+
- [Analyzing profiling data](#analyzing-profiling-data)
2626
- [Changelog](#changelog)
27-
- [Updating the changelog](#updating-the-changelog)
27+
- [Updating the changelog](#updating-the-changelog)
2828
- [Reported data](#reported-data)
29-
- [Insights Operator Archive](#insights-operator-archive)
30-
- [Sample IO archive](#sample-io-archive)
31-
- [Generating a sample archive](#generating-a-sample-archive)
32-
- [Formatting archive json files](#formatting-archive-json-files)
33-
- [Obfuscating an archive](#obfuscating-an-archive)
34-
- [Updating the sample archive](#updating-the-sample-archive)
29+
- [Insights Operator Archive](#insights-operator-archive)
30+
- [Sample IO archive](#sample-io-archive)
31+
- [Generating a sample archive](#generating-a-sample-archive)
32+
- [Formatting archive json files](#formatting-archive-json-files)
33+
- [Obfuscating an archive](#obfuscating-an-archive)
34+
- [Updating the sample archive](#updating-the-sample-archive)
35+
- [Conditional Gathering](#conditional-gathering)
3536
- [Contributing](#contributing)
3637
- [Support](#support)
3738
- [License](#license)
@@ -60,7 +61,7 @@ Unit tests can be started by the following command:
6061
make test
6162
```
6263

63-
It is also possible to specify CLI options for Go test. For example, if you need to disable test results caching,
64+
It is also possible to specify CLI options for Go test. For example, if you need to disable test results caching,
6465
use the following command:
6566

6667
```shell script
@@ -72,8 +73,8 @@ VERBOSE=-count=1 make test
7273
# Documentation
7374

7475

75-
The document [docs/gathered-data](docs/gathered-data.md) contains the list of collected data and the API that is used
76-
to collect it. This documentation is generated by the command bellow, by collecting the comment tags located above
76+
The document [docs/gathered-data](docs/gathered-data.md) contains the list of collected data and the API that is used
77+
to collect it. This documentation is generated by the command bellow, by collecting the comment tags located above
7778
each Gather method.
7879

7980
To start generating the document run:
@@ -86,12 +87,12 @@ make docs
8687

8788
## Generate the certificate and key
8889

89-
Certificate and key are required to access Prometheus metrics (instead 404 Forbidden is returned). It is possible
90-
to generate these two files from Kubernetes config file. Certificate is stored in `users/admin/client-cerfificate-data`
91-
and key in `users/admin/client-key-data`. Please note that these values are encoded by using Base64 encoding,
90+
Certificate and key are required to access Prometheus metrics (instead 404 Forbidden is returned). It is possible
91+
to generate these two files from Kubernetes config file. Certificate is stored in `users/admin/client-cerfificate-data`
92+
and key in `users/admin/client-key-data`. Please note that these values are encoded by using Base64 encoding,
9293
so it is needed to decode them, for example by `base64 -d`.
9394

94-
There's a tool named `gen_cert_key.py` that can be used to automatically generate both files. It is stored in `tools`
95+
There's a tool named `gen_cert_key.py` that can be used to automatically generate both files. It is stored in `tools`
9596
subdirectory.
9697

9798
```shell script
@@ -100,10 +101,10 @@ gen_cert_file.py kubeconfig.yaml
100101

101102
## Prometheus metrics provided by Insights Operator
102103

103-
It is possible to read Prometheus metrics provided by Insights Operator. Example of metrics exposed by
104+
It is possible to read Prometheus metrics provided by Insights Operator. Example of metrics exposed by
104105
Insights Operator can be found at [metrics.txt](docs/metrics.txt)
105106

106-
Depending on how or where the IO is running you may have different ways to retrieve the metrics.
107+
Depending on how or where the IO is running you may have different ways to retrieve the metrics.
107108
Here is a list of some options, so you can find the one that fits you:
108109

109110
### Running IO locally
@@ -185,7 +186,7 @@ go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
185186
go tool pprof http://localhost:6060/debug/pprof/heap
186187
```
187188

188-
These commands will create a compressed file that can be visualized using a variety of tools, one of them is
189+
These commands will create a compressed file that can be visualized using a variety of tools, one of them is
189190
the `pprof` tool.
190191

191192
### Analyzing profiling data
@@ -213,7 +214,7 @@ It uses both the local git and GitHub`s API to update the file so:
213214

214215
It can be used 2 ways:
215216

216-
1. Providing no command line arguments the script will update the current `CHANGELOG.md` with the latest changes
217+
1. Providing no command line arguments the script will update the current `CHANGELOG.md` with the latest changes
217218
2. according to the local git state.
218219

219220
> 🚨 IMPORTANT: It will only work with changelogs created with this script
@@ -222,7 +223,7 @@ It can be used 2 ways:
222223
go run cmd/changelog/main.go
223224
```
224225

225-
2. Providing 2 command line arguments, `AFTER` and `UNTIL` dates the script will generate a new `CHANGELOG.md` within
226+
2. Providing 2 command line arguments, `AFTER` and `UNTIL` dates the script will generate a new `CHANGELOG.md` within
226227
the provided time frame.
227228

228229
```shell script
@@ -235,17 +236,17 @@ go run cmd/changelog/main.go 2021-01-10 2021-01-20
235236
* ClusterOperator objects
236237
* All non-secret global config (hostnames and URLs anonymized)
237238

238-
The list of all collected data with description, location in produced archive and link to Api and some examples is
239+
The list of all collected data with description, location in produced archive and link to Api and some examples is
239240
at [docs/gathered-data.md](docs/gathered-data.md)
240241

241-
The resulting data is packed in `.tar.gz` archive with folder structure indicated in the document. Example of such
242+
The resulting data is packed in `.tar.gz` archive with folder structure indicated in the document. Example of such
242243
archive is at [docs/insights-archive-sample](docs/insights-archive-sample).
243244

244245
## Insights Operator Archive
245246

246247
### Sample IO archive
247248

248-
There is a sample IO archive maintained in this repo to use as a quick reference. (can be found
249+
There is a sample IO archive maintained in this repo to use as a quick reference. (can be found
249250
at [docs/insights-archive-sample](https://github.com/openshift/insights-operator/tree/master/docs/insights-archive-sample))
250251

251252
To keep it up-to-date it is **required** to update this manually when developing a new data enhancement.
@@ -311,8 +312,18 @@ the `managedFields` field when it was removed from the IO archive to save space:
311312
./scripts/update_sample_archive.sh <Path of directory with the NEW extracted IO archive> '"managedFields":'
312313
```
313314

314-
The path of the sample archive directory should be constant relative to
315-
the path of the script and therefore does not have to be specified explicitly.
315+
The path of the sample archive directory should be constant relative to the path of the script and therefore does not
316+
have to be specified explicitly.
317+
318+
# Conditional Gathering
319+
320+
Conditional Gatherer fetches its config from remote URL which is set in the config, the default one for local
321+
development is set to `http://localhost:8000/`. You can start a mock server following the next steps:
322+
323+
- `git clone https://github.com/RedHatInsights/insights-operator-gathering-conditions.git`
324+
- `cd insights-operator-gathering-conditions/`
325+
- `./build.sh`
326+
- `python3 -m http.server --directory build/`
316327

317328
# Contributing
318329

config/local.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ leaderElection:
55
interval: "5m"
66
storagePath: /tmp/insights-operator
77
endpoint: http://[::1]:8081
8+
conditionalGathererEndpoint: http://localhost:8081/api/gathering/gathering_rules
89
impersonate: system:serviceaccount:openshift-insights:gather
910
gather:
1011
- ALL

config/pod.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ leaderElection:
55
interval: "2h"
66
storagePath: /var/lib/insights-operator
77
endpoint: https://cloud.redhat.com/api/ingress/v1/upload
8+
conditionalGathererEndpoint: https://console.stage.redhat.com/api/gathering/gathering_rules
89
impersonate: system:serviceaccount:openshift-insights:gather
910
pull_report:
1011
endpoint: https://cloud.redhat.com/api/insights-results-aggregator/v1/clusters/%s/report

docs/conditional-gatherer/README.md

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# Conditional Gatherer
2+
3+
Conditional gatherer is a special gatherer which uses a set of rules describing which gathering functions to activate.
4+
More details can be found in `pkg/gatherers/conditional/conditional_gatherer.go`.
5+
6+
## Manual Testing
7+
8+
To test that conditional gatherer provides some data, follow the next steps:
9+
10+
1. Downscale CVO:
11+
12+
```bash
13+
oc scale deployment -n openshift-cluster-version cluster-version-operator --replicas=0
14+
```
15+
16+
2. Backup prometheus rules:
17+
18+
```bash
19+
oc get prometheusrule -n openshift-cluster-samples-operator samples-operator-alerts -o json > prometheus-rules.back.json
20+
```
21+
22+
3. Make SamplesImagestreamImportFailing alert to fire by setting `SamplesImagestreamImportFailing`'s
23+
`expr` value to `1 > bool 0` and `for` to `1s`:
24+
25+
```bash
26+
echo '{
27+
"apiVersion": "monitoring.coreos.com/v1",
28+
"kind": "PrometheusRule",
29+
"metadata": {
30+
"name": "samples-operator-alerts",
31+
"namespace": "openshift-cluster-samples-operator"
32+
},
33+
"spec": {
34+
"groups": [
35+
{
36+
"name": "SamplesOperator",
37+
"rules": [
38+
{
39+
"alert": "SamplesImagestreamImportFailing",
40+
"annotations": {
41+
"message": "Always firing"
42+
},
43+
"expr": "1 > bool 0",
44+
"for": "1s",
45+
"labels": {
46+
"severity": "warning"
47+
}
48+
}
49+
]
50+
}
51+
]
52+
}
53+
}' | oc apply -f -
54+
```
55+
56+
4. Wait for the alert to fire:
57+
58+
```
59+
export ALERT_MANAGER_HOST=(oc get route alertmanager-main -n openshift-monitoring -o jsonpath='{@.spec.host}')
60+
export INSECURE_PROMETHEUS_TOKEN=(oc sa get-token prometheus-k8s -n openshift-monitoring)
61+
curl -k -H "Authorization: Bearer $INSECURE_PROMETHEUS_TOKEN" https://$ALERT_MANAGER_HOST/api/v1/alerts | \
62+
jq '.data[] | select(.labels.alertname == "SamplesImagestreamImportFailing")'
63+
```
64+
65+
5. Make metrics work by forwarding the endpoint and setting INSECURE_PROMETHEUS_TOKEN environment variable:
66+
67+
```bash
68+
export INSECURE_PROMETHEUS_TOKEN=(oc sa get-token prometheus-k8s -n openshift-monitoring)
69+
```
70+
71+
```bash
72+
# run this command in a separate terminal
73+
sudo kubefwd svc -n openshift-monitoring -d openshift-monitoring.svc -l prometheus=k8s --kubeconfig $KUBECONFIG
74+
```
75+
76+
6. Run the operator and wait for an archive containing `conditional/` directory.
77+
78+
7. Restore the backup:
79+
80+
```bash
81+
oc apply -f prometheus-rules.back.json
82+
```
83+
84+
8. Fix CVO back
85+
```bash
86+
oc scale deployment -n openshift-cluster-version cluster-version-operator --replicas=1
87+
```
88+
89+
## Using Locally Started Service
90+
91+
1. Run the service following the instructions here
92+
https://github.com/RedHatInsights/insights-operator-gathering-conditions-service
93+
2. Set `conditionalGathererEndpoint` in `config/local.yaml` to http://localhost:8081/api/gathering/gathering_rules
94+
3. Enjoy your conditional rules from the local service
95+
96+
## Using Stage Endpoint
97+
98+
0. Be connected to Red Hat network or configure a proxy for `console.stage.redhat.com`
99+
1. Set up the stage endpoint in `config/local.yaml`
100+
2. Configure authentication through support secret
101+
102+
```bash
103+
echo '{
104+
"apiVersion": "v1",
105+
"kind": "Secret",
106+
"metadata": {
107+
"namespace": "openshift-config",
108+
"name": "support"
109+
},
110+
"type": "Opaque",
111+
"data": {
112+
"username": "'(echo $STAGE_USERNAME | base64 --wrap=0)'",
113+
"password": "'(echo $STAGE_PASSWORD | base64 --wrap=0)'"
114+
}
115+
}' | oc apply -f -
116+
```
117+
118+
3. Enjoy your conditional rules from the stage endpoint

pkg/authorizer/clusterauthorizer/clusterauthorizer.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@ import (
66
"net/url"
77
"strings"
88

9-
"github.com/openshift/insights-operator/pkg/config"
109
"golang.org/x/net/http/httpproxy"
1110
knet "k8s.io/apimachinery/pkg/util/net"
12-
)
1311

14-
type Configurator interface {
15-
Config() *config.Controller
16-
}
12+
"github.com/openshift/insights-operator/pkg/config/configobserver"
13+
)
1714

1815
type Authorizer struct {
19-
configurator Configurator
16+
configurator configobserver.Configurator
2017
// exposed for tests
2118
proxyFromEnvironment func(*http.Request) (*url.URL, error)
2219
}
2320

2421
// New creates a new Authorizer, whose purpose is to auth requests for outgoing traffic.
25-
func New(configurator Configurator) *Authorizer {
22+
func New(configurator configobserver.Configurator) *Authorizer {
2623
return &Authorizer{
2724
configurator: configurator,
2825
proxyFromEnvironment: http.ProxyFromEnvironment,
@@ -32,18 +29,23 @@ func New(configurator Configurator) *Authorizer {
3229
// Authorize adds the necessary auth header to the request, depending on the config. (BasicAuth/Token)
3330
func (a *Authorizer) Authorize(req *http.Request) error {
3431
cfg := a.configurator.Config()
32+
33+
if req.Header == nil {
34+
req.Header = make(http.Header)
35+
}
36+
3537
if len(cfg.Username) > 0 || len(cfg.Password) > 0 {
3638
req.SetBasicAuth(cfg.Username, cfg.Password)
3739
return nil
3840
}
41+
3942
token, err := a.Token()
4043
if err != nil {
4144
return err
4245
}
43-
if req.Header == nil {
44-
req.Header = make(http.Header)
45-
}
46+
4647
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
48+
4749
return nil
4850
}
4951

0 commit comments

Comments
 (0)