-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Horizontal Pod Autoscaler (#15)
Co-authored-by: Yusuke Kuoka <[email protected]>
- Loading branch information
Showing
14 changed files
with
208 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,12 @@ jobs: | |
with: | ||
version: v1.61 | ||
|
||
- name: Create kind cluster | ||
uses: helm/[email protected] | ||
with: | ||
node_image: kindest/node:v1.29.2 | ||
version: v0.20.0 | ||
|
||
- name: Run go test | ||
run: go test -v ./... | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{{- if .Values.horizontalPodAutoscaler.enabled }} | ||
apiVersion: autoscaling/v2 | ||
kind: HorizontalPodAutoscaler | ||
metadata: | ||
name: {{ include "surrealdb.fullname" . }} | ||
labels: | ||
{{- include "surrealdb.labels" . | nindent 4 }} | ||
spec: | ||
{{- with .Values.horizontalPodAutoscaler }} | ||
minReplicas: {{ .minReplicas | default 1 }} | ||
maxReplicas: {{ .maxReplicas | default 1 }} | ||
{{- end }} | ||
scaleTargetRef: | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
name: {{ include "surrealdb.fullname" . }} | ||
{{- with .Values.horizontalPodAutoscaler.metrics }} | ||
metrics: | ||
{{- toYaml . | nindent 4 }} | ||
{{- end }} | ||
{{- end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var ( | ||
SurrealDBChartPath = "../charts/surrealdb" | ||
KubectlTimeout = 1 * time.Second | ||
DeploymentReplicasUpdateTimeout = 20 * time.Second | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
var err error | ||
if env := os.Getenv("SURREALDB_CHART_PATH"); env != "" { | ||
SurrealDBChartPath = env | ||
} | ||
if env := os.Getenv("KUBECTL_TIMEOUT"); env != "" { | ||
KubectlTimeout, err = time.ParseDuration(env) | ||
if err != nil { | ||
log.Fatalf("failed to parse KUBECTL_TIMEOUT: %s", err) | ||
} | ||
} | ||
if env := os.Getenv("DEPLOYMENT_REPLICAS_UPDATE_TIMEOUT"); env != "" { | ||
DeploymentReplicasUpdateTimeout, err = time.ParseDuration(env) | ||
if err != nil { | ||
log.Fatalf("failed to parse DEPLOYMENT_REPLICAS_UPDATE_TIMEOUT: %s", err) | ||
} | ||
} | ||
os.Exit(m.Run()) | ||
} | ||
|
||
// This runs a series of helm-upgrade commands to test the HPA enable/disable functionality end-to-end | ||
// It assumes there are a Kubernetes cluster available, and the helm and kubectl commands installed. | ||
func TestHPAEnableDisable(t *testing.T) { | ||
const ( | ||
ReleaseName = "sdb1" | ||
DeploymentName = "sdb1-surrealdb" | ||
) | ||
|
||
t.Cleanup(func() { | ||
helmUninstall(t, ReleaseName) | ||
}) | ||
|
||
helmUpgrade(t, ReleaseName) | ||
waitUntilDeploymentHasReplicas(t, DeploymentName, 1) | ||
|
||
helmUpgrade(t, ReleaseName, "--set", "horizontalPodAutoscaler.enabled=true", "--set", "horizontalPodAutoscaler.minReplicas=2", "--set", "horizontalPodAutoscaler.maxReplicas=3") | ||
waitUntilDeploymentHasReplicas(t, DeploymentName, 2) | ||
|
||
helmUpgrade(t, ReleaseName, "--set", "horizontalPodAutoscaler.enabled=false") | ||
waitUntilDeploymentHasReplicas(t, DeploymentName, 1) | ||
} | ||
|
||
func helmUpgrade(t *testing.T, release string, args ...string) { | ||
c := exec.Command("helm", append([]string{"upgrade", "--install", release, SurrealDBChartPath}, args...)...) | ||
res, err := c.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("helm upgrade failed: %s", string(res)) | ||
} | ||
} | ||
|
||
type deployment struct { | ||
Spec *deploymentSpec `json:"spec"` | ||
} | ||
|
||
type deploymentSpec struct { | ||
Replicas int `json:"replicas"` | ||
} | ||
|
||
func kubectlGetDeployment(t *testing.T, name string) deployment { | ||
ctx, cancel := context.WithTimeout(context.Background(), KubectlTimeout) | ||
defer cancel() | ||
c := exec.CommandContext(ctx, "kubectl", "get", "deployment", name, "-o", "json") | ||
res, err := c.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("kubectl get deployment failed: %s", string(res)) | ||
} | ||
|
||
var deployment deployment | ||
err = json.Unmarshal(res, &deployment) | ||
if err != nil { | ||
t.Fatalf("failed to unmarshal deployment: %s", string(res)) | ||
} | ||
|
||
return deployment | ||
} | ||
|
||
func helmUninstall(t *testing.T, release string) { | ||
c := exec.Command("helm", "uninstall", release) | ||
res, err := c.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("helm uninstall failed: %s", string(res)) | ||
} | ||
} | ||
|
||
func waitUntilDeploymentHasReplicas(t *testing.T, name string, replicas int) { | ||
done := make(chan struct{}) | ||
defer close(done) | ||
go func() { | ||
for { | ||
deployment := kubectlGetDeployment(t, name) | ||
if deployment.Spec.Replicas == replicas { | ||
done <- struct{}{} | ||
return | ||
} | ||
time.Sleep(1 * time.Second) | ||
} | ||
}() | ||
|
||
select { | ||
case <-done: | ||
return | ||
case <-time.After(DeploymentReplicasUpdateTimeout): | ||
t.Fatalf("timed out waiting for deployment to have %d replicas", replicas) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters