This guide covers the full developer journey: environment setup, running the operator, making changes, and submitting a pull request. For general contribution policies, see CONTRIBUTING.
- Access to a supported version of an Openshift cluster
- Red Hat OpenShift Local can be used if the monitoring stack is enabled.
- A cluster can also be provisioned here
- A clone of koku-metrics-operator
- Go 1.13 or greater
- Openshift-CLI (preferably a version that matches your Openshift cluster version)
- kubebuilder
- kubectl
- kustomize (before installing this separately, check that it was not already installed along with kubectl)
- Docker Desktop
- quay.io account
- Fork and clone:
git clone https://github.com/<your-username>/koku-metrics-operator.git
cd koku-metrics-operator- Prevent accidental commits to kustomization.yaml:
git update-index --assume-unchanged config/manager/kustomization.yaml- Create a branch:
git checkout -b feature/your-feature-nameBranch naming conventions: feature/, fix/, docs/, refactor/
-
Log into your OCP cluster from a terminal, create an
koku-metrics-operatornamespace, and switch to the new namespace:$ oc login --token=<token> --server=<server> $ oc new-project koku-metrics-operator -
Build the manager binary:
$ make build -
Register the CRD with the Kubernetes apiserver:
$ make install -
Deploy the ServiceAccount:
$ oc apply -f testing/sa.yaml -
Grant monitoring read access to the ServiceAccount:
The local operator uses the
koku-metrics-controller-managerServiceAccount token to query the Prometheus/Thanos endpoint. Grant the ServiceAccountcluster-monitoring-viewpermissions:oc adm policy add-cluster-role-to-user \ cluster-monitoring-view \ system:serviceaccount:koku-metrics-operator:koku-metrics-controller-manager
If this permission is missing, reconciliation can fail with:
prometheus test query failed: client_error: client error: 403 -
Retrieve ServiceAccount Token and CA Certificate:
The operator's local environment needs a serviceAccount token and the cluster's service CA certificate to authenticate with the Kubernetes API. The
get-token-and-certMake command handles this retrieval:make get-token-and-cert
This generates a new token for the
koku-metrics-controller-managerServiceAccount and retrieves the cluster's Service CA certificate from thekube-root-ca.crtConfigMap.These files (
tokenandservice-ca.crt) will be placed in thetestingdirectory but you can specify a different output location by setting theSECRET_ABSPATHenvironment variable:SECRET_ABSPATH=/absolute/path/to/local/secrets make get-token-and-cert
-
Deploy the operator
make run ENABLE_WEBHOOKS=false SECRET_ABSPATH=/absolute/path/to/local/secretsAt this point, you will see the operator spin up in your terminal. After a few seconds, you should see something similar to the following output:
2020-10-21T09:31:37.195-0400 INFO controller-runtime.controller Starting workers {"controller": "kokumetricsconfig", "worker count": 1}The operator is running but is not doing any work. We need to create a CR.
-
Deploy a CR. For local development, uses default token auth. However service-account authentication can be used with the following, which creates the appropriate authentication spec within the CR. This is the
client_idandclient_secretfor your Red Hat Hybrid Cloud Console:$ make deploy-local-cr AUTH=service-account CLIENT_ID=<client_id> CLIENT_SECRET=<client_secret>This command copies
testing/costmanagementmetricsconfig-template.yamltotesting/costmanagement-metrics-cfg_v1beta1_costmanagementmetricsconfig.yaml, adds an external prometheus route, disables TLS verification for the prometheus route, and (whenAUTH=service-account) adds the authentication spec and appliestesting/authentication_secret.yaml. The command then appliestesting/costmanagement-metrics-cfg_v1beta1_costmanagementmetricsconfig.yamlto the cluster.After this CR has been created in the cluster, reconciliation will begin.
Running
make deploy-local-cras-is will create the external prometheus route, disable TLS verification for prometheus, and use token authentication for console.redhat.com. -
To continue development, make code changes. To apply those changes, stop the operator, and redeploy it. If changes are made to the api, the CRD needs to be re-registered, and the operator re-deployed.
- Read existing code in the area you're modifying
- Write failing tests first (TDD)
- Implement the feature or fix
- Run checks:
make test # run all tests
make fmt # format code
make lint # run linters
make verify-manifests # if CRD or RBAC changed- Update documentation if the change is user-facing
For code conventions (Go standards, import ordering, Kubernetes operator patterns, testing patterns), see AGENTS.md.
CRD changes can break existing clusters. If modifying the schema:
- Update
api/v1beta1/metricsconfig_types.go - Add kubebuilder markers for validation
- Run
make manifeststo regenerate - Test backward compatibility
- Document in your PR description
Never remove fields, change field types, or make optional fields required.
Use conventional commit style — keep under 70 characters:
feat: add GPU metrics collection for NVIDIA cardsfix: handle nil pointer in reconciliation loopdocs: update local development prerequisites
## Description
Brief description of changes.
## Motivation
Why is this change needed?
## Testing
- [ ] Unit tests added/updated
- [ ] Integration tests pass
- [ ] Manually tested on OpenShift cluster (if applicable)
- [ ] Verified backward compatibility
## Checklist
- [ ] `make fmt` passes
- [ ] `make lint` passes
- [ ] `make test` passes
- [ ] Documentation updated (if user-facing)
- [ ] No secrets committed- Modify
vendor/manually (usemake vendor) - Skip pre-commit hooks
- Commit secrets or credentials
- Change downstream files when on upstream (or vice versa)
- Automated CI runs (tests, linting, build)
- Code review from maintainers
- Address review comments — push new commits (don't force-push unless requested)
- Approval and merge
| Problem | Solution |
|---|---|
403 from Prometheus |
Grant cluster-monitoring-view to the ServiceAccount (see step 5 above) |
CRD not found |
Run make install |
| Tests fail with nil pointer | Check mock initialization in BeforeEach |
| Build fails with missing package | Run make vendor |
Useful commands:
# Check operator logs
oc logs -n koku-metrics-operator deployment/koku-metrics-operator-controller-manager -f
# Get CR status
oc get costmanagementmetricsconfig -o yaml
# Check RBAC
oc adm policy who-can get prometheuses.monitoring.coreos.com
# Run a specific test
go test ./internal/collector -run TestGenerateReports