github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/integration_test/itest/apply_app.go (about)

     1  package itest
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"path/filepath"
     7  	"strconv"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/datawire/dlib/dtime"
    15  	"github.com/telepresenceio/telepresence/v2/pkg/dos"
    16  )
    17  
    18  func ApplyEchoService(ctx context.Context, name, namespace string, port int) {
    19  	ApplyService(ctx, name, namespace, "jmalloc/echo-server:0.1.0", port, 8080)
    20  }
    21  
    22  func ApplyService(ctx context.Context, name, namespace, image string, port, targetPort int) {
    23  	t := getT(ctx)
    24  	t.Helper()
    25  	require.NoError(t, Kubectl(ctx, namespace, "create", "deploy", name, "--image", image), "failed to create deployment %s", name)
    26  	require.NoError(t, Kubectl(ctx, namespace, "expose", "deploy", name, "--port", strconv.Itoa(port), "--target-port", strconv.Itoa(targetPort)),
    27  		"failed to expose deployment %s", name)
    28  	require.NoError(t, Kubectl(ctx, namespace, "rollout", "status", "-w", "deployment/"+name), "failed to deploy %s", name)
    29  }
    30  
    31  func DeleteSvcAndWorkload(ctx context.Context, workload, name, namespace string) {
    32  	assert.NoError(getT(ctx), Kubectl(ctx, namespace, "delete", "--ignore-not-found", "--grace-period", "3", "svc,"+workload, name),
    33  		"failed to delete service and %s %s", workload, name)
    34  }
    35  
    36  // ApplyApp calls kubectl apply -n <namespace> -f on the given app + .yaml found in testdata/k8s relative
    37  // to the directory returned by GetWorkingDir.
    38  func ApplyApp(ctx context.Context, name, namespace, workload string) {
    39  	t := getT(ctx)
    40  	t.Helper()
    41  	manifest := filepath.Join("testdata", "k8s", name+".yaml")
    42  	require.NoError(t, Kubectl(ctx, namespace, "apply", "-f", manifest), "failed to apply %s", manifest)
    43  	require.NoError(t, RolloutStatusWait(ctx, namespace, workload))
    44  }
    45  
    46  type AppPort struct {
    47  	ServicePortName   string
    48  	ServicePortNumber uint16
    49  	TargetPortName    string
    50  	TargetPortNumber  uint16
    51  	Protocol          string
    52  	AppProtocol       string
    53  }
    54  type AppData struct {
    55  	ServiceName    string
    56  	DeploymentName string
    57  	AppName        string
    58  	ContainerName  string
    59  	Image          string
    60  	PullPolicy     string
    61  	Ports          []AppPort
    62  	Env            map[string]string
    63  }
    64  
    65  // ApplyAppTemplate calls kubectl apply -n <namespace> -f on the given app + .yaml found in testdata/k8s relative
    66  // to the directory returned by GetWorkingDir.
    67  func ApplyAppTemplate(ctx context.Context, namespace string, app *AppData) {
    68  	t := getT(ctx)
    69  	t.Helper()
    70  	r, err := OpenTemplate(WithWorkingDir(ctx, filepath.Join(GetOSSRoot(ctx), "testdata", "k8s")), "svc-deploy.goyaml", app)
    71  	require.NoError(t, err)
    72  	require.NoError(t, Kubectl(dos.WithStdin(ctx, r), namespace, "apply", "-f", "-"), "failed to apply template")
    73  	wl := app.DeploymentName
    74  	if wl == "" {
    75  		wl = app.AppName
    76  	}
    77  	require.NoError(t, RolloutStatusWait(ctx, namespace, "deploy/"+wl))
    78  }
    79  
    80  func RolloutStatusWait(ctx context.Context, namespace, workload string) error {
    81  	ctx, cancel := context.WithTimeout(ctx, PodCreateTimeout(ctx))
    82  	defer cancel()
    83  	switch {
    84  	case strings.HasPrefix(workload, "pod/"):
    85  		return Kubectl(ctx, namespace, "wait", workload, "--for", "condition=ready")
    86  	case strings.HasPrefix(workload, "replicaset/"), strings.HasPrefix(workload, "statefulset/"):
    87  		for {
    88  			status := struct {
    89  				ReadyReplicas int `json:"readyReplicas,omitempty"`
    90  				Replicas      int `json:"replicas,omitempty"`
    91  			}{}
    92  			stdout, err := KubectlOut(ctx, namespace, "get", workload, "-o", "jsonpath={..status}")
    93  			if err != nil {
    94  				return err
    95  			}
    96  			if err = json.Unmarshal([]byte(stdout), &status); err != nil {
    97  				return err
    98  			}
    99  			if status.ReadyReplicas == status.Replicas {
   100  				return nil
   101  			}
   102  			dtime.SleepWithContext(ctx, 3*time.Second)
   103  		}
   104  	}
   105  	return Kubectl(ctx, namespace, "rollout", "status", "-w", workload)
   106  }