-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathclient.go
More file actions
208 lines (178 loc) · 6.03 KB
/
client.go
File metadata and controls
208 lines (178 loc) · 6.03 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package temporalcli
import (
"context"
"fmt"
"os"
"os/user"
"github.com/temporalio/cli/cliext"
"go.temporal.io/api/common/v1"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/workflow"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
// dialClient creates a Temporal client using cliext.ClientOptionsBuilder with CLI-specific customizations.
//
// Note, this call may mutate the ClientOptions.Namespace since it is
// so often used by callers after this call to know the currently configured
// namespace.
func dialClient(cctx *CommandContext, c *cliext.ClientOptions) (client.Client, error) {
if cctx.RootCommand == nil {
return nil, fmt.Errorf("root command unexpectedly missing when dialing client")
}
// Set default identity if not provided
if c.Identity == "" {
hostname, err := os.Hostname()
if err != nil {
hostname = "unknown-host"
}
username := "unknown-user"
if u, err := user.Current(); err == nil {
username = u.Username
}
c.Identity = "temporal-cli:" + username + "@" + hostname
}
if err := applyClientAuthorityFromConfig(cctx, c); err != nil {
return nil, err
}
// Build client options using cliext
builder := &cliext.ClientOptionsBuilder{
CommonOptions: cctx.RootCommand.CommonOptions,
ClientOptions: *c,
EnvLookup: cctx.Options.EnvLookup,
Logger: cctx.Logger,
}
clientOpts, err := builder.Build(cctx)
if err != nil {
return nil, err
}
// We do not put codec on data converter here, it is applied via
// interceptor. Same for failure conversion.
// XXX: If this is altered to be more dynamic, have to also update
// everywhere DataConverterWithRawValue is used.
clientOpts.DataConverter = DataConverterWithRawValue
// Add header propagator.
clientOpts.ContextPropagators = append(clientOpts.ContextPropagators, headerPropagator{})
// Fixed header overrides
clientOpts.ConnectionOptions.DialOptions = append(
clientOpts.ConnectionOptions.DialOptions, grpc.WithChainUnaryInterceptor(fixedHeaderOverrideInterceptor))
// Additional gRPC options
clientOpts.ConnectionOptions.DialOptions = append(
clientOpts.ConnectionOptions.DialOptions, cctx.Options.AdditionalClientGRPCDialOptions...)
// Apply context timeout for dial if configured
dialCtx := context.Context(cctx)
if cctx.RootCommand.CommonOptions.ClientConnectTimeout != 0 {
timeout := cctx.RootCommand.CommonOptions.ClientConnectTimeout.Duration()
var cancel context.CancelFunc
dialCtx, cancel = context.WithTimeoutCause(cctx, timeout, fmt.Errorf("command timed out after %v", timeout))
defer cancel()
}
cl, err := client.DialContext(dialCtx, clientOpts)
if err != nil {
return nil, err
}
// Since this namespace value is used by many commands after this call,
// we are mutating it to be the derived one
c.Namespace = clientOpts.Namespace
return cl, nil
}
func applyClientAuthorityFromConfig(cctx *CommandContext, c *cliext.ClientOptions) error {
if c.ClientAuthority != "" || (c.FlagSet != nil && c.FlagSet.Changed("client-authority")) {
return nil
}
if cctx.RootCommand.DisableConfigFile {
return nil
}
_, additionalProfileFields, err := loadEnvConfigFile(cctx)
if err != nil {
return err
}
if v, ok, err := clientAuthorityFromAdditionalProfileFields(
additionalProfileFields,
envConfigProfileName(cctx),
); err != nil {
return err
} else if ok {
c.ClientAuthority = v
}
return nil
}
func fixedHeaderOverrideInterceptor(
ctx context.Context,
method string, req, reply any,
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption,
) error {
// The SDK sets some values on the outgoing metadata that we can't override
// via normal headers, so we have to replace directly on the metadata
md, _ := metadata.FromOutgoingContext(ctx)
if md == nil {
md = metadata.MD{}
}
md.Set("client-name", "temporal-cli")
md.Set("client-version", Version)
md.Set("supported-server-versions", ">=1.0.0 <2.0.0")
md.Set("caller-type", "operator")
ctx = metadata.NewOutgoingContext(ctx, md)
return invoker(ctx, method, req, reply, cc, opts...)
}
var DataConverterWithRawValue = converter.NewCompositeDataConverter(
rawValuePayloadConverter{},
converter.NewNilPayloadConverter(),
converter.NewByteSlicePayloadConverter(),
converter.NewProtoJSONPayloadConverter(),
converter.NewProtoPayloadConverter(),
converter.NewJSONPayloadConverter(),
)
type RawValue struct{ Payload *common.Payload }
type rawValuePayloadConverter struct{}
func (rawValuePayloadConverter) ToPayload(value any) (*common.Payload, error) {
// Only convert if value is a raw value
if r, ok := value.(RawValue); ok {
return r.Payload, nil
}
return nil, nil
}
func (rawValuePayloadConverter) FromPayload(payload *common.Payload, valuePtr any) error {
return fmt.Errorf("raw value unsupported from payload")
}
func (rawValuePayloadConverter) ToString(p *common.Payload) string {
return fmt.Sprintf("<raw payload %v bytes>", len(p.Data))
}
func (rawValuePayloadConverter) Encoding() string {
// Should never be used
return "raw-value-encoding"
}
type headerPropagator struct{}
type cliHeaderContextKey struct{}
func (headerPropagator) Inject(ctx context.Context, writer workflow.HeaderWriter) error {
if headers, ok := ctx.Value(cliHeaderContextKey{}).(map[string]any); ok {
for k, v := range headers {
p, err := converter.GetDefaultDataConverter().ToPayload(v)
if err != nil {
return err
}
writer.Set(k, p)
}
}
return nil
}
func (headerPropagator) InjectFromWorkflow(ctx workflow.Context, writer workflow.HeaderWriter) error {
return nil
}
func (headerPropagator) Extract(ctx context.Context, _ workflow.HeaderReader) (context.Context, error) {
return ctx, nil
}
func (headerPropagator) ExtractToWorkflow(ctx workflow.Context, _ workflow.HeaderReader) (workflow.Context, error) {
return ctx, nil
}
func contextWithHeaders(ctx context.Context, headers []string) (context.Context, error) {
if len(headers) == 0 {
return ctx, nil
}
out, err := stringKeysJSONValues(headers, false)
if err != nil {
return ctx, err
}
return context.WithValue(ctx, cliHeaderContextKey{}, out), nil
}