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: set proper User-Agent for each request #387

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
- [v0.1.1](#v011)
- [v0.1.0](#v010)

## Unreleased

> Release date: TBD

### Added

- Proper `User-Agent` header is now set on outgoing HTTP requests.
[#387](https://github.com/Kong/gateway-operator/pull/387)

## [v1.3.0]

> Release date: 2024-06-24
Expand Down
29 changes: 24 additions & 5 deletions modules/manager/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
// Package metadata includes metadata variables for logging and reporting.
package metadata

import (
"fmt"
"runtime"
)

// -----------------------------------------------------------------------------
// Controller Manager - Versioning Information
// -----------------------------------------------------------------------------

// WARNING: moving any of these variables requires changes to both the Makefile
// and the Dockerfile which modify them during the link step with -X

// BuildFlavor is the flavor of the build.
type BuildFlavor string

const (
// OSSFlavor is the open-source flavor.
OSSFlavor BuildFlavor = "oss"
// EEFlavor is the enterprise flavor.
EEFlavor BuildFlavor = "enterprise"
)

// Info is a struct type that holds the metadata for the controller manager.
type Info struct {
// Release returns the release version, generally a semver like v1.0.0.
Expand All @@ -29,7 +44,14 @@ type Info struct {
Organization string

// Flavor is the flavor of the build.
Flavor string
Flavor BuildFlavor
}

// UserAgent returns the User-Agent string to use in all HTTP requests made by KGO.
func (inf Info) UserAgent() string {
return fmt.Sprintf("%s/%s (%s/%s) (%s)",
inf.ProjectName, inf.Release, runtime.GOOS, runtime.GOARCH, inf.Flavor,
)
}

var (
Expand All @@ -50,9 +72,6 @@ var (

// Organization is the Kong organization
organization = "Kong"

// Flavor is the flavor of the build.
flavor = "oss"
)

// Metadata returns the metadata for the controller manager.
Expand All @@ -64,6 +83,6 @@ func Metadata() Info {
Commit: commit,
ProjectName: projectName,
Organization: organization,
Flavor: flavor,
Flavor: OSSFlavor,
}
}
30 changes: 8 additions & 22 deletions modules/manager/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
Expand Down Expand Up @@ -82,7 +81,7 @@ type Config struct {
DataPlaneControllerEnabled bool
DataPlaneBlueGreenControllerEnabled bool

// Controllers for speciality APIs and experimental features.
// Controllers for specialty APIs and experimental features.
AIGatewayControllerEnabled bool

// webhook and validation options
Expand Down Expand Up @@ -156,7 +155,10 @@ func Run(
setupLog.Info("leader election disabled")
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
restCfg := ctrl.GetConfigOrDie()
restCfg.UserAgent = metadata.UserAgent()

mgr, err := ctrl.NewManager(restCfg, ctrl.Options{
Scheme: scheme,
Metrics: server.Options{
BindAddress: cfg.MetricsAddr,
Expand Down Expand Up @@ -244,7 +246,7 @@ func Run(
// Enable anonnymous reporting when configured but not for development builds
// to reduce the noise.
if cfg.AnonymousReports && !cfg.DevelopmentMode {
stopAnonymousReports, err := setupAnonymousReports(context.Background(), cfg, setupLog, metadata)
stopAnonymousReports, err := setupAnonymousReports(context.Background(), restCfg, setupLog, metadata)
if err != nil {
setupLog.Error(err, "failed setting up anonymous reports")
} else {
Expand Down Expand Up @@ -353,35 +355,19 @@ func (m *caManager) maybeCreateCACertificate(ctx context.Context) error {
return nil
}

func getKubeconfig(apiServerPath string, kubeconfig string) (*rest.Config, error) {
config, err := clientcmd.BuildConfigFromFlags(apiServerPath, kubeconfig)
if err != nil {
// Fall back to default client loading rules.
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// if you want to change the loading rules (which files in which order), you can do so here
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, nil)
return kubeConfig.ClientConfig()
}
return config, nil
}

// setupAnonymousReports sets up and starts the anonymous reporting and returns
// a cleanup function and an error.
// The caller is responsible to call the returned function - when the returned
// error is not nil - to stop the reports sending.
func setupAnonymousReports(ctx context.Context, cfg Config, logger logr.Logger, metadata metadata.Info) (func(), error) {
func setupAnonymousReports(ctx context.Context, restCfg *rest.Config, logger logr.Logger, metadata metadata.Info) (func(), error) {
logger.Info("starting anonymous reports")
restConfig, err := getKubeconfig(cfg.APIServerPath, cfg.KubeconfigPath)
if err != nil {
return nil, fmt.Errorf("failed to get kubeconfig: %w", err)
}

payload := telemetry.Payload{
"v": metadata.Release,
"flavor": metadata.Flavor,
}

tMgr, err := telemetry.CreateManager(telemetry.SignalPing, restConfig, logger, payload)
tMgr, err := telemetry.CreateManager(telemetry.SignalPing, restCfg, logger, payload)
if err != nil {
return nil, fmt.Errorf("failed to create anonymous reports manager: %w", err)
}
Expand Down
Loading