Skip to content
Open
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
34 changes: 23 additions & 11 deletions v1/providers/testkube/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"maps"
"regexp"
"slices"
"strings"
Expand All @@ -29,16 +30,18 @@ const (
labelNameValue = "test-kubernetes"
labelManagedByValue = "brev-cloud-sdk"

annotationRefID = "testkube.brev.dev/ref-id"
annotationCloudCredRefID = "testkube.brev.dev/cloud-cred-ref-id" //nolint:gosec // this is a valid annotation
annotationName = "testkube.brev.dev/name"
annotationLocation = "testkube.brev.dev/location"
annotationSubLocation = "testkube.brev.dev/sub-location"
annotationInstanceType = "testkube.brev.dev/instance-type"
annotationImageID = "testkube.brev.dev/image-id"
annotationCreatedAt = "testkube.brev.dev/created-at"
annotationScenario = "testkube.brev.dev/scenario"
annotationTagsJSON = "testkube.brev.dev/tags-json"
annotationRefID = "testkube.brev.dev/ref-id"
annotationCloudCredRefID = "testkube.brev.dev/cloud-cred-ref-id" //nolint:gosec // this is a valid annotation
annotationName = "testkube.brev.dev/name"
annotationLocation = "testkube.brev.dev/location"
annotationSubLocation = "testkube.brev.dev/sub-location"
annotationInstanceType = "testkube.brev.dev/instance-type"
annotationImageID = "testkube.brev.dev/image-id"
annotationCreatedAt = "testkube.brev.dev/created-at"
annotationScenario = "testkube.brev.dev/scenario"
annotationTagsJSON = "testkube.brev.dev/tags-json"
annotationAWSLoadBalancerConnectionIdleTimeout = "service.beta.kubernetes.io/aws-load-balancer-connection-idle-timeout"
awsLoadBalancerConnectionIdleTimeout = "1800" // 30 minutes

envInstanceType = "TESTKUBE_INSTANCE_TYPE"
envScenario = "TESTKUBE_SCENARIO"
Expand Down Expand Up @@ -79,6 +82,15 @@ func (c *TestKubeClient) createInstanceAsK8sResources(ctx context.Context, attrs
location := c.resourceLocation(attrs)
annotations := c.resourceAnnotations(cloudID, attrs, instanceTypeSpec)

var serviceAnnotations map[string]string
if instanceTypeSpec.serviceType == corev1.ServiceTypeLoadBalancer {
serviceAnnotations = maps.Clone(annotations)
// Setup scripts can run without producing SSH traffic. Keep the load balancer from closing an otherwise healthy, idle SSH connection.
serviceAnnotations[annotationAWSLoadBalancerConnectionIdleTimeout] = awsLoadBalancerConnectionIdleTimeout
} else {
serviceAnnotations = annotations
}

// Create the service.
k8sService, err := c.k8sClient.
CoreV1().
Expand All @@ -88,7 +100,7 @@ func (c *TestKubeClient) createInstanceAsK8sResources(ctx context.Context, attrs
Name: string(cloudID),
Namespace: c.namespace,
Labels: objectLabels(string(cloudID), location),
Annotations: annotations,
Annotations: serviceAnnotations,
},
Spec: corev1.ServiceSpec{
Type: instanceTypeSpec.serviceType,
Expand Down
8 changes: 7 additions & 1 deletion v1/providers/testkube/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCreateInstanceProvisionFailures(t *testing.T) {
}
}

func TestInstanceLifecycle(t *testing.T) {
func TestInstanceLifecycle(t *testing.T) { //nolint:funlen // ok
ctx := context.Background()
client := newTestClient(t)

Expand All @@ -73,6 +73,12 @@ func TestInstanceLifecycle(t *testing.T) {
spec, ok := getInstanceTypeSpec(InstanceTypeOKCPU)
require.True(t, ok)
require.Equal(t, spec.imageID, instance.ImageID)
service, err := client.k8sClient.CoreV1().Services(client.namespace).Get(ctx, string(instance.CloudID), metav1.GetOptions{})
require.NoError(t, err)
require.Equal(t, awsLoadBalancerConnectionIdleTimeout, service.Annotations[annotationAWSLoadBalancerConnectionIdleTimeout])
pod, err := client.k8sClient.CoreV1().Pods(client.namespace).Get(ctx, string(instance.CloudID), metav1.GetOptions{})
require.NoError(t, err)
require.NotContains(t, pod.Annotations, annotationAWSLoadBalancerConnectionIdleTimeout)

listed, err := client.ListInstances(ctx, cloudv1.ListInstancesArgs{
TagFilters: map[string][]string{
Expand Down
Loading