summaryrefslogtreecommitdiff
path: root/vendor/github.com/testcontainers/testcontainers-go/internal/core/labels.go
blob: 198fdae7c8afa3752950b680dc3f71b6216a16c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package core

import (
	"errors"
	"fmt"
	"maps"
	"strings"

	"github.com/testcontainers/testcontainers-go/internal"
	"github.com/testcontainers/testcontainers-go/internal/config"
)

const (
	// LabelBase is the base label for all testcontainers labels.
	LabelBase = "org.testcontainers"

	// LabelLang specifies the language which created the test container.
	LabelLang = LabelBase + ".lang"

	// LabelReaper identifies the container as a reaper.
	LabelReaper = LabelBase + ".reaper"

	// LabelRyuk identifies the container as a ryuk.
	LabelRyuk = LabelBase + ".ryuk"

	// LabelSessionID specifies the session ID of the container.
	LabelSessionID = LabelBase + ".sessionId"

	// LabelVersion specifies the version of testcontainers which created the container.
	LabelVersion = LabelBase + ".version"

	// LabelReap specifies the container should be reaped by the reaper.
	LabelReap = LabelBase + ".reap"
)

// DefaultLabels returns the standard set of labels which
// includes LabelSessionID if the reaper is enabled.
func DefaultLabels(sessionID string) map[string]string {
	labels := map[string]string{
		LabelBase:      "true",
		LabelLang:      "go",
		LabelVersion:   internal.Version,
		LabelSessionID: sessionID,
	}

	if !config.Read().RyukDisabled {
		labels[LabelReap] = "true"
	}

	return labels
}

// AddDefaultLabels adds the default labels for sessionID to target.
func AddDefaultLabels(sessionID string, target map[string]string) {
	maps.Copy(target, DefaultLabels(sessionID))
}

// MergeCustomLabels sets labels from src to dst.
// If a key in src has [LabelBase] prefix returns an error.
// If dst is nil returns an error.
func MergeCustomLabels(dst, src map[string]string) error {
	if dst == nil {
		return errors.New("destination map is nil")
	}
	for key, value := range src {
		if strings.HasPrefix(key, LabelBase) {
			return fmt.Errorf("key %q has %q prefix", key, LabelBase)
		}
		dst[key] = value
	}
	return nil
}