Repository: GoogleCloudPlatform/cloud-sql-proxy-operator
Pull request #222 (2023-02-24 15:39:37)

feat: Configure --disable-metrics and --disable-telemetry flags.
Allow the user to configure the disable-metrics and disable-telemetry flags on the proxy.

Related to #45 


Patch:
diff --git a/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml b/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml
index fae1a6a..5a25846 100644
--- a/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml
+++ b/config/crd/bases/cloudsql.cloud.google.com_authproxyworkloads.yaml
@@ -925,6 +925,12 @@ spec:
                     telemetry:
                       description: Telemetry specifies how the proxy should expose telemetry. Optional, by default
                       properties:
+                        disableMetrics:
+                          description: DisableMetrics disables Cloud Monitoring testintegration (used with telemetryProject) This sets the proxy container's CLI argument `--disable-metrics`
+                          type: boolean
+                        disableTraces:
+                          description: DisableTraces disables Cloud Trace testintegration (used with telemetryProject) This sets the proxy container's CLI argument `--disable-traces`
+                          type: boolean
                         httpPort:
                           description: HTTPPort the port for Prometheus and health check server. This sets the proxy container's CLI argument `--http-port`
                           format: int32
diff --git a/docs/api.md b/docs/api.md
index 81e7065..771d3ea 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -121,6 +121,8 @@ _Appears in:_
 | Field | Description |
 | --- | --- |
 | `httpPort` _integer_ | HTTPPort the port for Prometheus and health check server. This sets the proxy container's CLI argument `--http-port` |
+| `disableTraces` _boolean_ | DisableTraces disables Cloud Trace testintegration (used with telemetryProject) This sets the proxy container's CLI argument `--disable-traces` |
+| `disableMetrics` _boolean_ | DisableMetrics disables Cloud Monitoring testintegration (used with telemetryProject) This sets the proxy container's CLI argument `--disable-metrics` |
 
 
 #### WorkloadSelectorSpec
diff --git a/installer/cloud-sql-proxy-operator.yaml b/installer/cloud-sql-proxy-operator.yaml
index f8d0e85..b12d277 100644
--- a/installer/cloud-sql-proxy-operator.yaml
+++ b/installer/cloud-sql-proxy-operator.yaml
@@ -943,6 +943,12 @@ spec:
                     telemetry:
                       description: Telemetry specifies how the proxy should expose telemetry. Optional, by default
                       properties:
+                        disableMetrics:
+                          description: DisableMetrics disables Cloud Monitoring testintegration (used with telemetryProject) This sets the proxy container's CLI argument `--disable-metrics`
+                          type: boolean
+                        disableTraces:
+                          description: DisableTraces disables Cloud Trace testintegration (used with telemetryProject) This sets the proxy container's CLI argument `--disable-traces`
+                          type: boolean
                         httpPort:
                           description: HTTPPort the port for Prometheus and health check server. This sets the proxy container's CLI argument `--http-port`
                           format: int32
diff --git a/internal/api/v1alpha1/authproxyworkload_types.go b/internal/api/v1alpha1/authproxyworkload_types.go
index ac9b1fc..eaeabda 100644
--- a/internal/api/v1alpha1/authproxyworkload_types.go
+++ b/internal/api/v1alpha1/authproxyworkload_types.go
@@ -224,6 +224,15 @@ type TelemetrySpec struct {
 	// This sets the proxy container's CLI argument `--http-port`
 	//+kubebuilder:validation:Optional
 	HTTPPort *int32 `json:"httpPort,omitempty"`
+
+	// DisableTraces disables Cloud Trace testintegration (used with telemetryProject)
+	// This sets the proxy container's CLI argument `--disable-traces`
+	//+kubebuilder:validation:Optional
+	DisableTraces *bool `json:"disableTraces,omitempty"`
+	// DisableMetrics disables Cloud Monitoring testintegration (used with telemetryProject)
+	// This sets the proxy container's CLI argument `--disable-metrics`
+	//+kubebuilder:validation:Optional
+	DisableMetrics *bool `json:"disableMetrics,omitempty"`
 }
 
 // InstanceSpec describes the configuration for how the proxy should expose
diff --git a/internal/api/v1alpha1/zz_generated.deepcopy.go b/internal/api/v1alpha1/zz_generated.deepcopy.go
index 4e9e3b5..55666d5 100644
--- a/internal/api/v1alpha1/zz_generated.deepcopy.go
+++ b/internal/api/v1alpha1/zz_generated.deepcopy.go
@@ -252,6 +252,16 @@ func (in *TelemetrySpec) DeepCopyInto(out *TelemetrySpec) {
 		*out = new(int32)
 		**out = **in
 	}
+	if in.DisableTraces != nil {
+		in, out := &in.DisableTraces, &out.DisableTraces
+		*out = new(bool)
+		**out = **in
+	}
+	if in.DisableMetrics != nil {
+		in, out := &in.DisableMetrics, &out.DisableMetrics
+		*out = new(bool)
+		**out = **in
+	}
 }
 
 // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new TelemetrySpec.
diff --git a/internal/workload/podspec_updates.go b/internal/workload/podspec_updates.go
index 6467338..874cff2 100644
--- a/internal/workload/podspec_updates.go
+++ b/internal/workload/podspec_updates.go
@@ -507,6 +507,7 @@ func (s *updateState) updateContainer(p *cloudsqlapi.AuthProxyWorkload, wl Workl
 
 	// always enable http port healthchecks on 0.0.0.0 and structured logs
 	s.addHealthCheck(p, c)
+	s.applyTelemetrySpec(p)
 
 	// enable the proxy's admin service
 	s.addAdminServer(p)
@@ -638,6 +639,24 @@ func (s *updateState) applyContainerSpec(p *cloudsqlapi.AuthProxyWorkload, c *co
 	return
 }
 
+// applyTelemetrySpec applies settings from cloudsqlapi.TelemetrySpec
+// to the container
+func (s *updateState) applyTelemetrySpec(p *cloudsqlapi.AuthProxyWorkload) {
+	if p.Spec.AuthProxyContainer == nil || p.Spec.AuthProxyContainer.Telemetry == nil {
+		return
+	}
+	tel := p.Spec.AuthProxyContainer.Telemetry
+
+	if tel.DisableTraces != nil && *tel.DisableTraces {
+		s.addProxyContainerEnvVar(p, "CSQL_PROXY_DISABLE_TRACES", "true")
+	}
+	if tel.DisableMetrics != nil && *tel.DisableMetrics {
+		s.addProxyContainerEnvVar(p, "CSQL_PROXY_DISABLE_METRICS", "true")
+	}
+
+	return
+}
+
 // updateContainerEnv applies global container state to all containers
 func (s *updateState) updateContainerEnv(c *corev1.Container) {
 	for i := 0; i < len(s.mods.EnvVars); i++ {


Test patch:
diff --git a/internal/workload/podspec_updates_test.go b/internal/workload/podspec_updates_test.go
index 2e6d911..ecea20b 100644
--- a/internal/workload/podspec_updates_test.go
+++ b/internal/workload/podspec_updates_test.go
@@ -628,7 +628,9 @@ func TestProxyCLIArgs(t *testing.T) {
 				AuthProxyContainer: &v1alpha1.AuthProxyContainerSpec{
 					SQLAdminAPIEndpoint: "https://example.com",
 					Telemetry: &v1alpha1.TelemetrySpec{
-						HTTPPort: ptr(int32(9092)),
+						HTTPPort:       ptr(int32(9092)),
+						DisableTraces:  &wantTrue,
+						DisableMetrics: &wantTrue,
 					},
 					AdminServer: &v1alpha1.AdminServerSpec{
 						EnableAPIs: []string{"Debug", "QuitQuitQuit"},
@@ -652,6 +654,8 @@ func TestProxyCLIArgs(t *testing.T) {
 				"CSQL_PROXY_DEBUG":                 "true",
 				"CSQL_PROXY_QUITQUITQUIT":          "true",
 				"CSQL_PROXY_HEALTH_CHECK":          "true",
+				"CSQL_PROXY_DISABLE_TRACES":        "true",
+				"CSQL_PROXY_DISABLE_METRICS":       "true",
 				"CSQL_PROXY_MAX_CONNECTIONS":       "10",
 				"CSQL_PROXY_MAX_SIGTERM_DELAY":     "20",
 			},