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

     1  package itest
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/telepresenceio/telepresence/v2/pkg/dos"
     8  )
     9  
    10  type Image struct {
    11  	Name     string `json:"name,omitempty"`
    12  	Tag      string `json:"tag,omitempty"`
    13  	Registry string `json:"registry,omitempty"`
    14  }
    15  
    16  func (img *Image) FQName() string {
    17  	nb := strings.Builder{}
    18  	if img.Registry != "" {
    19  		nb.WriteString(img.Registry)
    20  		nb.WriteByte('/')
    21  	}
    22  	nb.WriteString(img.Name)
    23  	if img.Tag != "" {
    24  		nb.WriteByte(':')
    25  		nb.WriteString(img.Tag)
    26  	}
    27  	return nb.String()
    28  }
    29  
    30  func ImageFromEnv(ctx context.Context, env, defaultTag, defaultRegistry string) *Image {
    31  	if imgQN, ok := dos.LookupEnv(ctx, env); ok {
    32  		img := new(Image)
    33  		i := strings.LastIndexByte(imgQN, '/')
    34  		if i >= 0 {
    35  			img.Registry = imgQN[:i]
    36  			imgQN = imgQN[i+1:]
    37  		} else {
    38  			img.Registry = defaultRegistry
    39  		}
    40  		if i = strings.IndexByte(imgQN, ':'); i > 0 {
    41  			img.Name = imgQN[:i]
    42  			img.Tag = imgQN[i+1:]
    43  		} else {
    44  			img.Name = imgQN
    45  			img.Tag = defaultTag
    46  		}
    47  		return img
    48  	}
    49  	return nil
    50  }
    51  
    52  type imageContextKey struct{}
    53  
    54  func WithImage(ctx context.Context, image *Image) context.Context {
    55  	return context.WithValue(ctx, imageContextKey{}, image)
    56  }
    57  
    58  func GetImage(ctx context.Context) *Image {
    59  	if image, ok := ctx.Value(imageContextKey{}).(*Image); ok {
    60  		return image
    61  	}
    62  	return nil
    63  }
    64  
    65  type clientImageContextKey struct{}
    66  
    67  func WithClientImage(ctx context.Context, image *Image) context.Context {
    68  	return context.WithValue(ctx, clientImageContextKey{}, image)
    69  }
    70  
    71  func GetClientImage(ctx context.Context) *Image {
    72  	if image, ok := ctx.Value(clientImageContextKey{}).(*Image); ok {
    73  		return image
    74  	}
    75  	return nil
    76  }
    77  
    78  type agentImageContextKey struct{}
    79  
    80  func WithAgentImage(ctx context.Context, image *Image) context.Context {
    81  	return context.WithValue(ctx, agentImageContextKey{}, image)
    82  }
    83  
    84  func GetAgentImage(ctx context.Context) *Image {
    85  	if image, ok := ctx.Value(agentImageContextKey{}).(*Image); ok {
    86  		return image
    87  	}
    88  	return nil
    89  }