Skip to content

Commit fce828f

Browse files
author
Ricardo Lüders
authored
chore: general code cleanup (openshift#776)
* refactor: replace deprecate NewCommand * refactor(clusterconfig): remove deprecated Region when Openstack * refactor: SchemeGroupVersion is deprecated * refactor: AddToScene was moved to SchemeBuilder * refactor: solve variable name collision * chore: fix grammar * refactor: error capitalized * refactor: renames OCMFailureCountThreshold to FailureCountThreshold * refactor: renames OCMFailureCountThreshold to FailureCountThreshold * refactor: renames InsightsRecommendationCollector to Collector * refactor: renames InsightsRecommendationCollector to Collector * chore: fix golang comments * docs: fix typo on README.md * docs: fix typo on README.md * refactor: remove rand.Seed (https://go-review.googlesource.com/c/go/+/443058) * chore: remove golang comment * chore: disable depguard lint * refactor: replace context.TODO with context.Background
1 parent 37673b1 commit fce828f

32 files changed

+74
-77
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ linters:
6666
enable:
6767
- bodyclose
6868
- deadcode
69-
#- depguard
7069
- dogsled
7170
- dupl
7271
- errcheck
@@ -102,6 +101,7 @@ linters:
102101

103102
# don't enable:
104103
# - asciicheck
104+
# - depguard
105105
# - scopelint
106106
# - gochecknoglobals
107107
# - gocognit

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ the body of the commit should describe the why.
3030
```
3131
Add the gitgooks command
3232
33-
This add githooks to the project, in order to run tests
33+
This adds githooks to the project, in order to run tests
3434
and liting and prevent bad commits.
3535
```
3636

STYLEGUIDE.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Directory and File Names
44

55
- Use lowercase dash-separated names for all files (to avoid git issues with case-insensitive file systems)
6-
- Exceptions are files which have their own naming conventions (eg Dockerfile, Makefile, README)
6+
- Exceptions are files which have their own naming conventions (e.g. Dockerfile, Makefile, README)
77
- Automation scripts should be stored into `dotdirs` (eg .githooks, .openshiftci)
88

99
## Go

cmd/changelog/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const gitHubPath = "https://github.com/openshift/insights-operator"
6161
// API reference: https://docs.github.com/en/rest/reference/pulls#get-a-pull-request
6262
const gitHubAPIFormat = "https://api.github.com/repos/%s/%s/pulls/%s" // owner, repo, pull-number
6363

64-
// OpenShift release version helper type
64+
// ReleaseVersion OpenShift release version helper type
6565
type ReleaseVersion struct {
6666
Major int
6767
Minor int
@@ -163,7 +163,7 @@ func readCHANGELOG() map[ReleaseVersion]MarkdownReleaseBlock {
163163
if match := miscSectionRegExp.FindStringSubmatch(versionSection); len(match) > 0 {
164164
releaseBlock.misc = match[1]
165165
}
166-
// We want only found versions - e.g master is not considered as a version
166+
// We want only found versions - e.g. master is not considered as a version
167167
if version != (ReleaseVersion{}) {
168168
releaseBlocks[version] = releaseBlock
169169
}

cmd/gendoc/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func execExampleMethod(methodFullPackage, methodPackage, methodName string) (str
318318
return string(output), err
319319
}
320320

321-
// createRandom creates a random non existing file name in current folder
321+
// createRandom creates a random non-existing file name in current folder
322322
func createRandom() string {
323323
var f string
324324
for {

docs/arch.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
The main goal of the Insights Operator is to periodically gather anonymized data from the OCP cluster (mostly Kubernetes/OpenShift APIs and control plane components) and upload it to `console.redhat.com` for Insights analysis.
44

5-
Insights Operator does not manage any application. As usual with operator applications, most of the code is structured in the `pkg` package and `pkg/controller/operator.go` hosts the operator controller. Typically operator controllers read configuration and start some periodical tasks.
5+
Insights Operator does not manage any application. As usual with operator applications, most of the code is structured in the `pkg` package and `pkg/controller/operator.go` hosts the operator controller. Typically, operator controllers read configuration and start some periodical tasks.
66

77
## How the Insights operator reads configuration
88
The Insights Operator's configuration is a combination of the file [config/pod.yaml](../config/pod.yaml)(basically default configuration hardcoded in the image) and configuration stored in the `support` secret in the `openshift-config` namespace. The secret doesn't exist by default, but when it does, it overrides default settings which IO reads from the `config/pod.yaml`.
@@ -100,27 +100,27 @@ There are these main tasks scheduled:
100100

101101
## Gathering the data
102102

103-
Insights operator defines three types of gatherers (see below). Each of them must implement the [Interface](../pkg/gatherers/interface.go#L11) and they are initialized by calling `gather.CreateAllGatherers` in `operator.go`. The actual gathering is triggered in `Run` method in `pkg/controller/periodic/periodic.go`, but not every gatherer is triggered every time ( for example, see the [CustomPeriodGatherer type](../pkg/gatherers/interface.go#L21)).
103+
Insights operator defines three types of gatherers (see below). Each of them must implement the [Interface,](../pkg/gatherers/interface.go#L11) and they are initialized by calling `gather.CreateAllGatherers` in `operator.go`. The actual gathering is triggered in `Run` method in `pkg/controller/periodic/periodic.go`, but not every gatherer is triggered every time ( for example, see the [CustomPeriodGatherer type](../pkg/gatherers/interface.go#L21)).
104104

105105
Each gatherer includes one or more gathering functions. Gathering functions are defined as a map, where the key is the name of the function and the value is the [GatheringClosure type](../pkg/gatherers/interface.go#L34). They are executed concurrently in the `HandleTasksConcurrently` function in `pkg/gather/task_processing.go`.
106106
One of the attributes of the `GatheringClosure` type is the function that returns the values: `([]record.Record, []error)`. The slice of the records is the result of gathering function. The actual data is in the `Item` attribute of the `Record`. This `Item` is of type `Marshalable` (see the interface in the [record.go](../pkg/record/record.go)) and there are two JSON marshallers used to serialize the data - `JSONMarshaller` and `ResourceMarshaller` which allows you to save few bytes by omitting the `managedFields` during the serialization.
107107
Errors, warnings or panics that occurred during given gathering function are logged in the "metadata" part of the Insights operator archive. See [sample archive example](../docs/insights-archive-sample/insigths-operator/gathers.json)
108108

109109
### Clusterconfig gatherer
110110

111-
Defined in [clusterconfig_gatherer.go](../pkg/gatherers/clusterconfig/clusterconfig_gatherer.go). This gatherer is ran regularly (2h by default) and gathers various data related to cluster config (see [gathered-data doc](../docs/gathered-data.md) for more details).
111+
Defined in [clusterconfig_gatherer.go](../pkg/gatherers/clusterconfig/clusterconfig_gatherer.go). This gatherer is run regularly (2h by default) and gathers various data related to cluster config (see [gathered-data doc](../docs/gathered-data.md) for more details).
112112

113113
The data from this gatherer is stored under `/config` directory in the archive.
114114

115115
### Workloads gatherer
116116

117-
Defined in [workloads_gatherer.go](../pkg/gatherers/workloads/workloads_gatherer.go). This gatherer only runs every 12 hours and the interval is not configurable. This is done because running the gatherer more often would significantly increase data in the archive, that is assumed will not change very often. There is only one gathering function in this gatherer and it gathers workload fingerprint data (SHA of the images, fingerprints of namespaces as number of pods in namespace, fingerprints of containers as first command and first argument).
117+
Defined in [workloads_gatherer.go](../pkg/gatherers/workloads/workloads_gatherer.go). This gatherer only runs every 12 hours and the interval is not configurable. This is done because running the gatherer more often would significantly increase data in the archive, that is assumed will not change very often. There is only one gathering function in this gatherer, and it gathers workload fingerprint data (SHA of the images, fingerprints of namespaces as number of pods in namespace, fingerprints of containers as first command and first argument).
118118

119119
The data from this gatherer is stored in the `/config/workload_info.json` file in the archive, but please note that not every archive contains this data.
120120

121121
### Conditional gatherer
122122

123-
Defined in [conditional_gatherer.go](../pkg/gatherers/conditional/conditional_gatherer.go). This gatherer is ran regularly (2h by default), but it only gathers some data when a corresponding condition is met. The conditions and corresponding gathering functions are defined in an external service (https://console.redhat.com/api/gathering/gathering_rules). A typical example of a condition is when an alert is firing. This also means that this gatherer relies on the availability of Prometheus metrics and alerts.
123+
Defined in [conditional_gatherer.go](../pkg/gatherers/conditional/conditional_gatherer.go). This gatherer is run regularly (2h by default), but it only gathers some data when a corresponding condition is met. The conditions and corresponding gathering functions are defined in an external service (https://console.redhat.com/api/gathering/gathering_rules). A typical example of a condition is when an alert is firing. This also means that this gatherer relies on the availability of Prometheus metrics and alerts.
124124

125125
The data from this gatherer is stored under the `/conditional` directory in the archive.
126126

@@ -166,7 +166,7 @@ health_statuses_insights{metric="total"} 2
166166
167167
### Scheduling and running of Uploader
168168
The `operator.go` starts background task defined in `pkg/insights/insightsuploader/insightsuploader.go`. The insights uploader periodically checks if there is any data to upload. If no data is found, the uploader continues with next cycle.
169-
The uploader triggers the `wait.Until` function, which waits until the configuration changes or it is time to upload. After start of the operator, there is some waiting time before the very first upload. This time is defined by `initialDelay`. If no error occurred while sending the POST request, then the next uploader check is defined as `wait.Jitter(interval, 1.2)`, where interval is the gathering interval.
169+
The uploader triggers the `wait.Until` function, which waits until the configuration changes, or it is time to upload. After start of the operator, there is some waiting time before the very first upload. This time is defined by `initialDelay`. If no error occurred while sending the POST request, then the next uploader check is defined as `wait.Jitter(interval, 1.2)`, where interval is the gathering interval.
170170

171171
## How Uploader authenticates to console.redhat.com
172172
The HTTP communication with the external service (e.g uploading the Insights archive or downloading the Insights analysis) is defined in the [insightsclient package](../pkg/insights/insightsclient/). The HTTP transport is encrypted with TLS (see the `clientTransport()` function defined in the `pkg/insights/insightsclient/insightsclient.go`. This function (and the `prepareRequest` function) uses `pkg/authorizer/clusterauthorizer.go` to respect the proxy settings and to authorize (i.e add the authorization header with respective token value) the requests. The user defined certificates in the `/var/run/configmaps/trusted-ca-bundle/ca-bundle.crt` are taken into account (see the cluster wide proxy setting in the [OCP documentation](https://docs.openshift.com/container-platform/latest/networking/enable-cluster-wide-proxy.html)).
@@ -379,4 +379,4 @@ Such a client is used in [GatherMachineSet](pkg/gather/clusterconfig/clusterconf
379379
380380
## Configuring what to gather
381381
382-
[Insights API](https://github.com/openshift/api/blob/master/config/v1alpha1/types_insights.go) and the corresponding `insightsdatagather.config.openshift.io` CRD allow you to specify a list of `disabledGatherers` to disable specific gatherers or disable data gathering altogether by setting the value to `all`.
382+
[Insights API](https://github.com/openshift/api/blob/master/config/v1alpha1/types_insights.go) and the corresponding `insightsdatagather.config.openshift.io` CRD allow you to specify a list of `disabledGatherers` to disable specific gatherers or disable data gathering altogether by setting the value to `all`.

docs/conditional-gatherer/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Conditional Gatherer
22

33
Conditional gatherer is a special gatherer which uses a set of rules describing which gathering functions to activate.
4-
The conditional rules are defined in the [insights-operator-gathering-conditions GitHub repository](https://github.com/RedHatInsights/insights-operator-gathering-conditions). This content is consumed and exposed by the [insights-operator-gathering-conditions-service](https://github.com/RedHatInsights/insights-operator-gathering-conditions-service). A new version of the `insights-operator-gathering-conditions` requires a new release version of the `insights-operator-gathering-conditions-service`.
4+
The conditional rules are defined in the [insights-operator-gathering-conditions GitHub repository](https://github.com/RedHatInsights/insights-operator-gathering-conditions). This content is consumed and exposed by the [insights-operator-gathering-conditions-service](https://github.com/RedHatInsights/insights-operator-gathering-conditions-service). A new version of the `insights-operator-gathering-conditions` requires a new release version of the `insights-operator-gathering-conditions-service`.
55
The Insights Operator connects to this service and consumes the conditional rules from it. The connection endpoint is defined in the [pod.yaml config file](../../config/pod.yaml) in the `conditionalGathererEndpoint` attribute (the value can be overriden in the `support` secret). Authentication is required and the `pull-secret` token is used for this purpose.
66

77
## Validation of the conditional rules
@@ -10,7 +10,7 @@ The Insights Operator internally validates the conditional rules JSON against th
1010

1111
The following are some examples of validation failures (which will show up in the log):
1212

13-
Non-existing gathering fumction:
13+
Non-existing gathering function:
1414
```
1515
E0808 16:29:51.864716 241084 parsing.go:22] skipping a rule because of an error: unable to create params for conditional.GatheringFunctionName: containers_log {[{alert_is_firing 0xc0004639c0 <nil>}] map[]}
1616
```
@@ -31,7 +31,7 @@ Failed to parse the provided cluster version:
3131
E0809 10:02:16.383643 37430 conditional_gatherer.go:140] error checking conditions for a gathering rule: Could not parse Range "4-11.12": Could not parse version "4-11.12" in "4-11.12": No Major.Minor.Patch elements found
3232
```
3333

34-
One of the common conditions type (see below) is the `alert_is_firing`. This condition depends on availability of Prometheus alerts - i.e connection to the in-cluster Prometheus instance. If the connection is not available, this may manifests in the log as follows for example:
34+
One of the common conditions type (see below) is the `alert_is_firing`. This condition depends on availability of Prometheus alerts - i.e. connection to the in-cluster Prometheus instance. If the connection is not available, this may manifest in the log as follows for example:
3535

3636
```
3737
E0809 11:56:48.491346 46838 conditional_gatherer.go:226] unable to update alerts cache: open /var/run/configmaps/service-ca-bundle/service-ca.crt: no such file or directory

pkg/anonymization/anonymizer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func (anonymizer *Anonymizer) ObfuscateIP(ipStr string) string {
403403
return ipStr
404404
}
405405

406-
// We could use something like https://github.com/yl2chen/cidranger but we shouldn't typically have many networks
406+
// We could use something like https://github.com/yl2chen/cidranger, but we shouldn't typically have many networks,
407407
// so it's fine to just iterate over them
408408
for i := range anonymizer.networks {
409409
networkInfo := &anonymizer.networks[i]
@@ -473,7 +473,7 @@ func (anonymizer *Anonymizer) StoreTranslationTable() *corev1.Secret {
473473
return result
474474
}
475475

476-
// ResetTranslationTable resets the translation table, so that the translation table of multiple gathers wont mix toghater.
476+
// ResetTranslationTable resets the translation table, so that the translation table of multiple gathers won't mix together.
477477
func (anonymizer *Anonymizer) ResetTranslationTable() {
478478
anonymizer.translationTable = make(map[string]string)
479479
}

pkg/cmd/start/start.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package start
33
import (
44
"context"
55
"fmt"
6-
"math/rand"
76
"os"
87
"time"
98

@@ -31,7 +30,7 @@ const (
3130
pbAcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
3231
)
3332

34-
// NewOperator create the commad for running the Insights Operator.
33+
// NewOperator create the command for running the Insights Operator.
3534
func NewOperator() *cobra.Command {
3635
operator := &controller.Operator{
3736
Controller: config.Controller{
@@ -57,12 +56,12 @@ func NewOperator() *cobra.Command {
5756
Short: "Start the operator",
5857
Run: runOperator(operator, cfg),
5958
}
60-
cmd.Flags().AddFlagSet(cfg.NewCommand().Flags())
59+
cmd.Flags().AddFlagSet(cfg.NewCommandWithContext(context.Background()).Flags())
6160

6261
return cmd
6362
}
6463

65-
// NewGather create the commad for running the a single gather.
64+
// NewGather create the command for running a single gather.
6665
func NewGather() *cobra.Command {
6766
operator := &controller.GatherJob{
6867
Controller: config.Controller{
@@ -77,7 +76,7 @@ func NewGather() *cobra.Command {
7776
Short: "Does a single gather, without uploading it",
7877
Run: runGather(operator, cfg),
7978
}
80-
cmd.Flags().AddFlagSet(cfg.NewCommand().Flags())
79+
cmd.Flags().AddFlagSet(cfg.NewCommandWithContext(context.Background()).Flags())
8180

8281
return cmd
8382
}
@@ -196,8 +195,7 @@ func runGather(operator *controller.GatherJob, cfg *controllercmd.ControllerComm
196195
// Boilerplate for running an operator and handling command line arguments.
197196
func runOperator(operator *controller.Operator, cfg *controllercmd.ControllerCommandConfig) func(cmd *cobra.Command, args []string) {
198197
return func(cmd *cobra.Command, args []string) {
199-
// boiler plate for the "normal" command
200-
rand.Seed(time.Now().UTC().UnixNano())
198+
// boilerplate for the "normal" command
201199
defer serviceability.BehaviorOnPanic(os.Getenv("OPENSHIFT_ON_PANIC"), version.Get())()
202200
defer serviceability.Profile(os.Getenv("OPENSHIFT_PROFILE")).Stop()
203201
serviceability.StartProfiler()

pkg/config/configobserver/secretconfigobserver_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func Test_ConfigObserver_ConfigChanged(t *testing.T) {
219219
if err != nil {
220220
t.Fatalf("Unexpected error %s", err)
221221
}
222-
// Check if the event arrived on the channel
222+
// Check if the event arrived at the channel
223223
if len(configCh) != 1 {
224224
t.Fatalf("Config channel has more/less then 1 event on a singal config change. len(configCh)==%d", len(configCh))
225225
}

pkg/config/mock_configurator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type MockSecretConfigurator struct {
1111
Conf *Controller
1212
}
1313

14-
// NewMockConfigurator constructs a new MockConfigurator with default config values
14+
// NewMockSecretConfigurator constructs a new MockConfigurator with default config values
1515
func NewMockSecretConfigurator(conf *Controller) *MockSecretConfigurator {
1616
if conf == nil {
1717
conf = &Controller{}

pkg/controller/operator.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
"github.com/openshift/insights-operator/pkg/recorder/diskrecorder"
4040
)
4141

42-
// Operator is the type responsible for controlling the start up of the Insights Operator
42+
// Operator is the type responsible for controlling the start-up of the Insights Operator
4343
type Operator struct {
4444
config.Controller
4545
}
@@ -201,7 +201,7 @@ func (s *Operator) Run(ctx context.Context, controller *controllercmd.Controller
201201
go periodicGather.PeriodicPrune(ctx)
202202
}
203203

204-
// check we can read IO container status and we are not in crash loop
204+
// check we can read IO container status, and we are not in crash loop
205205
initialCheckTimeout := s.Controller.Interval / 24
206206
initialCheckInterval := 20 * time.Second
207207
baseInitialDelay := s.Controller.Interval / 12

pkg/controller/periodic/periodic_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Test_Controller_CustomPeriodGatherer(t *testing.T) {
4141
mockRecorder.Reset()
4242

4343
c.Gather()
44-
// 5 gatherers + metadata (one is low priority and we need to wait 999 hours to get it
44+
// 5 gatherers + metadata (one is low priority, and we need to wait 999 hours to get it
4545
assert.Len(t, mockRecorder.Records, 6)
4646
mockRecorder.Reset()
4747
}

pkg/controller/status/conditions.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (c *conditions) setCondition(conditionType configv1.ClusterStatusConditionT
7373
status configv1.ConditionStatus, reason, message string) {
7474
originalCondition, ok := c.entryMap[conditionType]
7575
transitionTime := metav1.Now()
76-
// if condition is defined and there is not new status then don't update transition time
76+
// if condition is defined and there is no new status then don't update transition time
7777
if ok && originalCondition.Status == status {
7878
transitionTime = originalCondition.LastTransitionTime
7979
}
@@ -105,7 +105,7 @@ func (c *conditions) findCondition(condition configv1.ClusterStatusConditionType
105105
}
106106

107107
// entries returns a sorted list of status conditions from the mapped values.
108-
// The list is sorted by by type ClusterStatusConditionType to ensure consistent ordering for deep equal checks.
108+
// The list is sorted by type ClusterStatusConditionType to ensure consistent ordering for deep equal checks.
109109
func (c *conditions) entries() []configv1.ClusterOperatorStatusCondition {
110110
var res []configv1.ClusterOperatorStatusCondition
111111
for _, v := range c.entryMap {

0 commit comments

Comments
 (0)