-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathjd.go
More file actions
181 lines (169 loc) · 5.87 KB
/
jd.go
File metadata and controls
181 lines (169 loc) · 5.87 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package jd
import (
"context"
"fmt"
"os"
"time"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
tc "github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
tcwait "github.com/testcontainers/testcontainers-go/wait"
"github.com/smartcontractkit/chainlink-testing-framework/framework"
"github.com/smartcontractkit/chainlink-testing-framework/framework/components/postgres"
"github.com/smartcontractkit/chainlink-testing-framework/framework/pods"
)
const (
TmpImageName = "jd-local"
GRPCPort string = "14231"
CSAEncryptionKey string = "!PASsword000!"
WSRPCPort string = "8080"
WSRPCHealthPort string = "8081"
)
type Input struct {
Image string `toml:"image"`
GRPCPort string `toml:"grpc_port"`
WSRPCPort string `toml:"wsrpc_port"`
CSAEncryptionKey string `toml:"csa_encryption_key"`
DockerFilePath string `toml:"docker_file"`
DockerContext string `toml:"docker_ctx"`
JDSQLDumpPath string `toml:"jd_sql_dump_path"`
// DisableDNSIsolation keeps Docker's embedded DNS (127.0.0.11).
// Leave false (default) to preserve historical isolation behavior.
// Set true when JD must resolve peer service names (for example jd-db)
// on a shared Docker network.
DisableDNSIsolation bool `toml:"disable_dns_isolation"`
DBInput *postgres.Input `toml:"db"`
Out *Output `toml:"out"`
}
type Output struct {
UseCache bool `toml:"use_cache"`
ContainerName string `toml:"container_name"`
DBContainerName string `toml:"db_container_name"`
ExternalGRPCUrl string `toml:"grpc_url"`
InternalGRPCUrl string `toml:"internal_grpc_url"`
ExternalWSRPCUrl string `toml:"wsrpc_url"`
InternalWSRPCUrl string `toml:"internal_wsrpc_url"`
}
func defaults(in *Input) {
if in.GRPCPort == "" {
in.GRPCPort = GRPCPort
}
if in.WSRPCPort == "" {
in.WSRPCPort = WSRPCPort
}
if in.CSAEncryptionKey == "" {
in.CSAEncryptionKey = CSAEncryptionKey
}
}
func defaultJDDB() *postgres.Input {
return &postgres.Input{
Image: "postgres:16",
Port: 14000,
Name: "jd-db",
VolumeName: "jd",
JDDatabase: true,
}
}
func NewJD(in *Input) (*Output, error) {
return NewWithContext(context.Background(), in)
}
func NewWithContext(ctx context.Context, in *Input) (*Output, error) {
if in.Out != nil && in.Out.UseCache {
return in.Out, nil
}
defaults(in)
jdImg := os.Getenv("CTF_JD_IMAGE")
if jdImg != "" {
// unset docker build context and file path to avoid conflicts, image provided via env var takes precedence
in.Image = jdImg
in.DockerContext = ""
in.DockerFilePath = ""
}
if in.WSRPCPort == WSRPCHealthPort {
return nil, fmt.Errorf("wsrpc port cannot be the same as wsrpc health port")
}
if in.DBInput == nil {
in.DBInput = defaultJDDB()
suffix := fmt.Sprintf("%d", time.Now().UnixNano())
in.DBInput.Name = fmt.Sprintf("%s-%s", in.DBInput.Name, suffix)
in.DBInput.VolumeName = fmt.Sprintf("%s-%s", in.DBInput.VolumeName, suffix)
}
in.DBInput.JDSQLDumpPath = in.JDSQLDumpPath
pgOut, err := postgres.NewWithContext(ctx, in.DBInput)
if err != nil {
return nil, err
}
containerName := framework.DefaultTCName("jd")
grpcPort := fmt.Sprintf("%s/tcp", in.GRPCPort)
wsrpcPort := fmt.Sprintf("%s/tcp", in.WSRPCPort)
wsHealthPort := fmt.Sprintf("%s/tcp", WSRPCHealthPort)
if pods.K8sEnabled() {
return nil, fmt.Errorf("K8s support is not yet implemented")
}
if err := framework.DefaultNetwork(nil); err != nil {
return nil, fmt.Errorf("failed to ensure default docker network %q: %w", framework.DefaultNetworkName, err)
}
req := tc.ContainerRequest{
Name: containerName,
Image: in.Image,
Labels: framework.DefaultTCLabels(),
Networks: []string{framework.DefaultNetworkName},
NetworkAliases: map[string][]string{
framework.DefaultNetworkName: {containerName},
},
ExposedPorts: []string{grpcPort, wsrpcPort, wsHealthPort},
HostConfigModifier: func(h *container.HostConfig) {
// Default behavior keeps DNS isolation enabled for backwards compatibility.
// Disable only when JD needs Docker service-name resolution (for example jd-db).
framework.NoDNS(!in.DisableDNSIsolation, h)
h.PortBindings = framework.MapTheSamePort(grpcPort, wsrpcPort)
h.ExtraHosts = append(h.ExtraHosts, "host.docker.internal:host-gateway")
},
Env: map[string]string{
"DATABASE_URL": pgOut.JDInternalURL,
"PORT": in.GRPCPort,
"NODE_RPC_PORT": in.WSRPCPort,
"CSA_KEY_ENCRYPTION_SECRET": in.CSAEncryptionKey,
},
WaitingFor: tcwait.ForAll(
tcwait.ForListeningPort(nat.Port(fmt.Sprintf("%s/tcp", in.GRPCPort))),
wait.ForHTTP("/healthz").
WithPort(nat.Port(fmt.Sprintf("%s/tcp", WSRPCHealthPort))). // WSRPC health endpoint uses different port than WSRPC
WithStartupTimeout(1*time.Minute).
WithPollInterval(200*time.Millisecond),
NewGRPCHealthStrategy(nat.Port(fmt.Sprintf("%s/tcp", in.GRPCPort))).
WithTimeout(1*time.Minute).
WithPollInterval(200*time.Millisecond),
),
}
if req.Image == "" {
req.Image = TmpImageName
if err := framework.BuildImage(in.DockerContext, in.DockerFilePath, req.Image, nil); err != nil {
return nil, err
}
req.KeepImage = false
}
c, err := tc.GenericContainer(ctx, tc.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
if err != nil {
return nil, err
}
host, err := framework.GetHostWithContext(ctx, c)
if err != nil {
return nil, err
}
out := &Output{
UseCache: true,
ContainerName: containerName,
DBContainerName: pgOut.ContainerName,
ExternalGRPCUrl: fmt.Sprintf("%s:%s", host, in.GRPCPort),
InternalGRPCUrl: fmt.Sprintf("%s:%s", containerName, in.GRPCPort),
ExternalWSRPCUrl: fmt.Sprintf("%s:%s", host, in.WSRPCPort),
InternalWSRPCUrl: fmt.Sprintf("%s:%s", containerName, in.WSRPCPort),
}
in.Out = out
return out, nil
}