Skip to content

Commit

Permalink
feat: Configure containerPort on proxy pod when telemetry is enabled
Browse files Browse the repository at this point in the history
feat: Automatically configure graceful exit 0 when the proxy container is terminated.

feat: Assign adminPort, create containerPort for http telemetry ports

feat: Add CSQL_QUIT_URLS envvar to all workload containers so that they can stop the proxy containers when they exit.

test: add prestop hook unit test.
  • Loading branch information
hessjcg committed Sep 13, 2023
1 parent 35e33ff commit 55cf55b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
8 changes: 7 additions & 1 deletion internal/workload/podspec_updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,6 @@ func (s *updateState) applyTelemetrySpec(p *cloudsqlapi.AuthProxyWorkload) {
if tel.QuotaProject != nil {
s.addProxyContainerEnvVar(p, "CSQL_PROXY_QUOTA_PROJECT", *tel.QuotaProject)
}

return
}

Expand Down Expand Up @@ -832,6 +831,7 @@ func (s *updateState) addHealthCheck(p *cloudsqlapi.AuthProxyWorkload, c *corev1
FailureThreshold: 3,
TimeoutSeconds: 10,
}

// Add a port that is associated with the proxy, but not a specific db instance
s.addProxyPort(port, p)
s.addProxyContainerEnvVar(p, "CSQL_PROXY_HTTP_PORT", fmt.Sprintf("%d", port))
Expand All @@ -841,6 +841,12 @@ func (s *updateState) addHealthCheck(p *cloudsqlapi.AuthProxyWorkload, c *corev1
// when it receives a SIGTERM.
s.addProxyContainerEnvVar(p, "CSQL_PROXY_EXIT_ZERO_ON_SIGTERM", "true")

// Add a containerPort declaration for the healthcheck & telemetry port
c.Ports = append(c.Ports, corev1.ContainerPort{
ContainerPort: port,
Protocol: corev1.ProtocolTCP,
})

// Also the operator will enable the /quitquitquit endpoint for graceful exit.
// If the AdminServer.Port is set, use it, otherwise use the default
// admin port.
Expand Down
56 changes: 55 additions & 1 deletion internal/workload/podspec_updates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,6 @@ func TestProxyCLIArgs(t *testing.T) {
}
t.Errorf("got env %v=%v, wants no env var set", dontWantKey, gotEnvVar)
}

})
}

Expand Down Expand Up @@ -915,6 +914,61 @@ func TestPodTemplateAnnotations(t *testing.T) {

}

func TestTelemetryAddsTelemetryContainerPort(t *testing.T) {

var (
u = workload.NewUpdater("cloud-sql-proxy-operator/dev", workload.DefaultProxyImage)
)

// Create a pod
wl := podWorkload()
wl.Pod.Spec.Containers[0].Ports =
[]corev1.ContainerPort{{Name: "http", ContainerPort: 8080}}

// Create a AuthProxyWorkload that matches the deployment
csqls := []*cloudsqlapi.AuthProxyWorkload{
simpleAuthProxy("instance1", "project:server:db"),
simpleAuthProxy("instance2", "project:server2:db2"),
simpleAuthProxy("instance3", "project:server3:db3")}

csqls[0].ObjectMeta.Generation = 1
csqls[1].ObjectMeta.Generation = 2
csqls[2].ObjectMeta.Generation = 3

var wantPorts = map[string]int32{
workload.ContainerName(csqls[0]): workload.DefaultHealthCheckPort,
workload.ContainerName(csqls[1]): workload.DefaultHealthCheckPort + 1,
workload.ContainerName(csqls[2]): workload.DefaultHealthCheckPort + 2,
}

// update the containers
err := configureProxies(u, wl, csqls)
if err != nil {
t.Fatal(err)
}

// test that containerPort values were set properly
for name, port := range wantPorts {
found := false
for _, c := range wl.PodSpec().Containers {
if c.Name == name {
found = true
if len(c.Ports) == 0 {
t.Fatalf("want container port for conatiner %s at port %d, got no containerPort", name, port)
}
if got := c.Ports[0].ContainerPort; got != port {
t.Errorf("want container port for conatiner %s at port %d, got port = %d ", name, port, got)
}
continue
}
}
if !found {
t.Fatalf("want container %s, got no container", name)
}
}

}

func TestQuitUrlEnvVar(t *testing.T) {

var (
Expand Down

0 comments on commit 55cf55b

Please sign in to comment.