Skip to content

Commit

Permalink
feat: add user agent to proxy invocation (#122)
Browse files Browse the repository at this point in the history
Add the operator version to the proxy's user agent, so that we can track how many people
are using the operator.

Fixes #67.
Co-authored-by: Jonathan Hess <[email protected]>
  • Loading branch information
enocom authored Dec 12, 2022
1 parent 891d6d8 commit 803446d
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 23 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ E2E_KUBECTL = $(E2E_KUBECTL_ENV) $(KUBECTL)
# This is the file where Terraform will write the URL to the e2e container registry
E2E_DOCKER_URL_FILE :=$(PWD)/bin/gcloud-docker-repo.url
E2E_DOCKER_URL=$(shell cat $(E2E_DOCKER_URL_FILE) | tr -d '\n')
E2E_PROXY_URL ?= "gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.0.0-preview.2"

# Default value in the Makefile blank. When blank tests will use workload.DefaultProxyImage
E2E_PROXY_URL ?= ""

E2E_WORK_DIR=$(PWD)/bin/e2e
$(E2E_WORK_DIR):
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/authproxyworkload_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func reconciler(p *v1alpha1.AuthProxyWorkload, cb client.Client) (*AuthProxyWork
r := &AuthProxyWorkloadReconciler{
Client: cb,
recentlyDeleted: &recentlyDeletedCache{},
updater: workload.NewUpdater(),
updater: workload.NewUpdater("cloud-sql-proxy-operator/dev"),
}
req := ctrl.Request{
NamespacedName: types.NamespacedName{
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ func InitScheme(scheme *runtime.Scheme) {

// SetupManagers was moved out of ../main.go to here so that it can be invoked
// from the testintegration tests AND from the actual operator.
func SetupManagers(mgr manager.Manager) error {
u := workload.NewUpdater()
func SetupManagers(mgr manager.Manager, userAgent string) error {
u := workload.NewUpdater(userAgent)

setupLog.Info("Configuring reconcilers...")
var err error
Expand Down
2 changes: 1 addition & 1 deletion internal/testintegration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func EnvTestSetup() (func(), error) {
return teardownFunc, fmt.Errorf("unable to start kuberenetes envtest %v", err)
}

err = controller.SetupManagers(mgr)
err = controller.SetupManagers(mgr, "cloud-sql-proxy-operator/dev")
if err != nil {
return teardownFunc, fmt.Errorf("unable to start kuberenetes envtest %v", err)
}
Expand Down
12 changes: 8 additions & 4 deletions internal/workload/podspec_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// package and documented here so that they appear in the godoc. These also
// need to be documented in the CRD
const (
DefaultProxyImage = "gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.0.0-preview.2"
DefaultProxyImage = "gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.0.0-preview.4"

// DefaultFirstPort is the first port number chose for an instance listener by the
// proxy.
Expand All @@ -49,12 +49,14 @@ var l = logf.Log.WithName("internal.workload")

// Updater holds global state used while reconciling workloads.
type Updater struct {
// userAgent is the userAgent of the operator
userAgent string
}

// NewUpdater creates a new instance of Updater with a supplier
// that loads the default proxy impage from the public docker registry
func NewUpdater() *Updater {
return &Updater{}
func NewUpdater(userAgent string) *Updater {
return &Updater{userAgent: userAgent}
}

// ConfigError is an error with extra details about why an AuthProxyWorkload
Expand Down Expand Up @@ -456,7 +458,9 @@ func (s *updateState) updateContainer(p *cloudsqlapi.AuthProxyWorkload, wl Workl
fmt.Sprintf("--http-port=%d", healthcheckPort),
"--http-address=0.0.0.0",
"--health-check",
"--structured-logs")
"--structured-logs",
fmt.Sprintf("--user-agent=%v", s.updater.userAgent),
)

c.Name = ContainerName(p)
c.ImagePullPolicy = "IfNotPresent"
Expand Down
18 changes: 10 additions & 8 deletions internal/workload/podspec_updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func TestUpdatePodWorkload(t *testing.T) {
wantContainerName = "csql-default-" + wantsName
wantsInstanceName = "project:server:db"
wantsInstanceArg = fmt.Sprintf("%s?port=%d", wantsInstanceName, wantsPort)
u = workload.NewUpdater()
u = workload.NewUpdater("cloud-sql-proxy-operator/dev")
)
var err error

Expand Down Expand Up @@ -192,7 +192,7 @@ func TestUpdateWorkloadFixedPort(t *testing.T) {
"DB_HOST": "127.0.0.1",
"DB_PORT": strconv.Itoa(int(wantsPort)),
}
u = workload.NewUpdater()
u = workload.NewUpdater("cloud-sql-proxy-operator/dev")
)

// Create a pod
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestWorkloadNoPortSet(t *testing.T) {
"DB_PORT": strconv.Itoa(int(wantsPort)),
}
)
u := workload.NewUpdater()
u := workload.NewUpdater("cloud-sql-proxy-operator/dev")

// Create a pod
wl := podWorkload()
Expand Down Expand Up @@ -324,7 +324,7 @@ func TestWorkloadUnixVolume(t *testing.T) {
wantWorkloadEnv = map[string]string{
"DB_SOCKET_PATH": wantsUnixDir,
}
u = workload.NewUpdater()
u = workload.NewUpdater("cloud-sql-proxy-operator/dev")
)

// Create a pod
Expand Down Expand Up @@ -399,7 +399,7 @@ func TestContainerImageChanged(t *testing.T) {
var (
wantsInstanceName = "project:server:db"
wantImage = "custom-image:latest"
u = workload.NewUpdater()
u = workload.NewUpdater("cloud-sql-proxy-operator/dev")
)

// Create a pod
Expand Down Expand Up @@ -441,7 +441,7 @@ func TestContainerImageEmpty(t *testing.T) {
var (
wantsInstanceName = "project:server:db"
wantImage = workload.DefaultProxyImage
u = workload.NewUpdater()
u = workload.NewUpdater("cloud-sql-proxy-operator/dev")
)
// Create a AuthProxyWorkload that matches the deployment

Expand Down Expand Up @@ -500,7 +500,7 @@ func TestContainerReplaced(t *testing.T) {
wantContainer = &corev1.Container{
Name: "sample", Image: "debian:latest", Command: []string{"/bin/bash"},
}
u = workload.NewUpdater()
u = workload.NewUpdater("cloud-sql-proxy-operator/dev")
)

// Create a pod
Expand Down Expand Up @@ -570,6 +570,8 @@ func TestProxyCLIArgs(t *testing.T) {
"--structured-logs",
"--health-check",
fmt.Sprintf("--http-port=%d", workload.DefaultHealthCheckPort),
"--http-address=0.0.0.0",
"--user-agent=cloud-sql-proxy-operator/dev",
},
},
{
Expand Down Expand Up @@ -742,7 +744,7 @@ func TestProxyCLIArgs(t *testing.T) {
for i := 0; i < len(testcases); i++ {
tc := &testcases[i]
t.Run(tc.desc, func(t *testing.T) {
u := workload.NewUpdater()
u := workload.NewUpdater("cloud-sql-proxy-operator/dev")

// Create a pod
wl := &workload.PodWorkload{Pod: &corev1.Pod{
Expand Down
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ import (
)

var (
scheme = k8sruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
version = "unknown"
buildID = "unknown"
scheme = k8sruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
version = "unknown"
buildID = "unknown"
userAgent = "cloud-sql-proxy-operator/" + version
)

func init() {
Expand Down Expand Up @@ -86,7 +87,7 @@ func main() {
os.Exit(1)
}

err = controller.SetupManagers(mgr)
err = controller.SetupManagers(mgr, userAgent)
if err != nil {
setupLog.Error(err, "unable to set up the controllers")
os.Exit(1)
Expand Down
3 changes: 2 additions & 1 deletion tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/GoogleCloudPlatform/cloud-sql-proxy-operator/internal/controller"
"github.com/GoogleCloudPlatform/cloud-sql-proxy-operator/internal/testhelpers"
"github.com/GoogleCloudPlatform/cloud-sql-proxy-operator/internal/workload"
"github.com/go-logr/logr"
"go.uber.org/zap/zapcore"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -96,7 +97,7 @@ func setupTests() (func(), error) {
}

// Read e2e test configuration
proxyImageURL = loadValue("PROXY_IMAGE_URL", "../bin/last-proxy-image-url.txt", "gcr.io/cloud-sql-connectors/cloud-sql-proxy:2.0.0-preview.2")
proxyImageURL = loadValue("PROXY_IMAGE_URL", "../bin/last-proxy-image-url.txt", workload.DefaultProxyImage)
operatorURL = loadValue("OPERATOR_IMAGE_URL", "../bin/last-gcloud-operator-url.txt", "operator:latest")
testInfraPath := loadValue("TEST_INFRA_JSON", "", "../bin/testinfra.json")
ti, err := loadTestInfra(testInfraPath)
Expand Down

0 comments on commit 803446d

Please sign in to comment.