Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add support for custom HTTP headers #1756

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

xorilog
Copy link

@xorilog xorilog commented Nov 13, 2024

Adds support for Generic headers. Addresses #1726

Unfortunately here the following example does not work out of the box:

var headers map[string]string
headers["X-Yolo-Header"] = "Yolo-Value"       # <-- set a custom header in headers var
cfg := &genapi.TransportConfig{
		Schemes:  []string{gURL.Scheme},
		BasePath: "/api",
		Host:     gURL.Host,
		// APIKey is an optional API key or service account token.
		APIKey: credentials.apikey,
		// NumRetries contains the optional number of attempted retries
		NumRetries: 0,
		Client:     client,
		TLSConfig:  tlsConfig,
		HTTPHeaders: headers,         # <-- pass the headers as part of the TransportConfig to build and configure the returned *genapi.GrafanaHTTPAPI
	}

Even tho the TransportConfig.HTTPHeaders exist it is not reused afterwards and this instrumented client is (i did not understood exactly why, if anyone has hints on that i would love to know ! 😃).

I feel this PR does something weird so do not hesitate to point me a cleaner way to achieve the same result.

What does this PR do:

  1. It creates a new headers object under Grafana.spec.client
  2. Adds a headers map[string]string to instrumentedRoundTripper Type
  3. Adds a private method addHeaders on instrumentedRoundTripper to set headers key / values
  4. Modifies the RoundTrip method to loop over the content of the instrumentedRoundTripper Type and Add the present headers to the request.

Here is a Grafana object example using custom Headers to display different allowed ways to set custom headers:

apiVersion: grafana.integreatly.org/v1beta1
kind: Grafana
metadata:
  labels:
    grafana-instance: grafana-kind-localhost
  name: grafana-kind-localhost
spec:
  client:
    headers:
       "X-Titi-Header": "toto"
       X-Yolo-Header: "Yolo-Value"
       x-grafana-org-id: "18"
  external:
    adminPassword:
      key: GF_SECURITY_ADMIN_PASSWORD
      name: grafana-kind-localhost-secrets
    adminUser:
      key: GF_SECURITY_ADMIN_USER
      name: grafana-kind-localhost-secrets
    url: https://grafana.kind.localhost

…trumented Transport object and reuse it in subsequent requests

Signed-off-by: christophe.boucharlat <[email protected]>
api/v1beta1/grafana_types.go Outdated Show resolved Hide resolved
…e to simplify syntax

Signed-off-by: christophe.boucharlat <[email protected]>
@xorilog xorilog requested a review from weisdd November 14, 2024 10:21
Copy link
Collaborator

@weisdd weisdd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After changing the data type, the code became much easier, nice job :)
I've added a few minor suggestions.

api/v1beta1/grafana_types.go Outdated Show resolved Hide resolved
controllers/client/grafana_client.go Outdated Show resolved Hide resolved
controllers/client/round_tripper.go Outdated Show resolved Hide resolved
controllers/client/round_tripper.go Outdated Show resolved Hide resolved
api/v1beta1/grafana_types.go Outdated Show resolved Hide resolved
…, removed useless pointers & follow coding style

Signed-off-by: christophe.boucharlat <[email protected]>
@xorilog xorilog requested a review from weisdd November 14, 2024 11:51
Copy link
Collaborator

@weisdd weisdd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed two commits to your branch (regenerated code after the Headers data type was changed + fixed nil pointer dereference). With those changes in place, I think we should be good to merge the PR, but it would still be handy to have a second look at the code by one of my fellow maintainers.

@weisdd weisdd changed the title Add grafana client generic headers support feat(client): add support for custom HTTP headers Nov 14, 2024
@xorilog
Copy link
Author

xorilog commented Nov 14, 2024

Thank's a lot for those fixes and your time ! 🙏

@MaoMaoCake
Copy link

Hi @weisdd, theres currently an issue with trivy's ghcr where if you run at an unfortunate time you can get rate limited with an error similar to below.

Failed to download artifact repo="ghcr.io/aquasecurity/trivy-db:2" err="OCI repository error: 1 error occurred:\n\t* GET https://ghcr.io/v2/aquasecurity/trivy-db/manifests/2: TOOMANYREQUESTS: retry-after: 189.036µs, allowed: 44000/minute\n\n"
unfortunately you might need to babysit the run to make sure it completes.

@weisdd
Copy link
Collaborator

weisdd commented Nov 15, 2024

@MaoMaoCake No worries, it's something that we've been seeing in every PR for a while. It's not a blocking issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants