/
provider.go
616 lines (544 loc) · 19.7 KB
/
provider.go
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
package provider
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
"runtime"
"strings"
pbempty "github.com/golang/protobuf/ptypes/empty"
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
"github.com/moby/patternmatcher"
"github.com/pkg/errors"
"github.com/pulumi/pulumi/pkg/v3/resource/provider"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
rpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
type dockerNativeProvider struct {
rpc.UnimplementedResourceProviderServer
host *provider.HostClient
name string
version string
schemaBytes []byte
config map[string]string
//loginLock sync.Mutex
}
// docker native methods
// Attach sends the engine address to an already running plugin.
func (p *dockerNativeProvider) Attach(context context.Context, req *rpc.PluginAttach) (*emptypb.Empty, error) {
host, err := provider.NewHostClient(req.GetAddress())
if err != nil {
return nil, err
}
p.host = host
return &pbempty.Empty{}, nil
}
// Call dynamically executes a method in the provider associated with a component resource.
func (p *dockerNativeProvider) Call(ctx context.Context, req *rpc.CallRequest) (*rpc.CallResponse, error) {
return nil, status.Error(codes.Unimplemented, "call is not yet implemented")
}
// Construct creates a new component resource.
func (p *dockerNativeProvider) Construct(ctx context.Context, req *rpc.ConstructRequest) (
*rpc.ConstructResponse, error) {
return nil, status.Error(codes.Unimplemented, "construct is not yet implemented")
}
// CheckConfig validates the configuration for this provider.
func (p *dockerNativeProvider) CheckConfig(ctx context.Context, req *rpc.CheckRequest) (*rpc.CheckResponse, error) {
return &rpc.CheckResponse{Inputs: req.GetNews()}, nil
}
// DiffConfig diffs the configuration for this provider.
func (p *dockerNativeProvider) DiffConfig(ctx context.Context, req *rpc.DiffRequest) (*rpc.DiffResponse, error) {
return &rpc.DiffResponse{}, nil
}
// Configure configures the resource provider with "globals" that control its behavior.
func (p *dockerNativeProvider) Configure(_ context.Context, req *rpc.ConfigureRequest) (*rpc.ConfigureResponse, error) {
for key, val := range req.GetVariables() {
p.config[strings.TrimPrefix(key, "docker:config:")] = val
}
return &rpc.ConfigureResponse{
AcceptSecrets: true,
}, nil
}
// Invoke dynamically executes a built-in function in the provider.
func (p *dockerNativeProvider) Invoke(_ context.Context, req *rpc.InvokeRequest) (*rpc.InvokeResponse, error) {
tok := req.GetTok()
return nil, fmt.Errorf("unknown Invoke token '%s'", tok)
}
// StreamInvoke dynamically executes a built-in function in the provider. The result is streamed
// back as a series of messages.
func (p *dockerNativeProvider) StreamInvoke(
req *rpc.InvokeRequest, server rpc.ResourceProvider_StreamInvokeServer) error {
tok := req.GetTok()
return fmt.Errorf("unknown StreamInvoke token '%s'", tok)
}
// Check validates that the given property bag is valid for a resource of the given type and returns
// the inputs that should be passed to successive calls to Diff, Create, or Update for this
// resource. As a rule, the provider inputs returned by a call to Check should preserve the original
// representation of the properties as present in the program inputs. Though this rule is not
// required for correctness, violations thereof can negatively impact the end-user experience, as
// the provider inputs are using for detecting and rendering diffs.
func (p *dockerNativeProvider) Check(ctx context.Context, req *rpc.CheckRequest) (*rpc.CheckResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Create(%s)", p.name, urn)
logging.V(9).Infof("%s executing", label)
inputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
KeepUnknowns: false,
SkipNulls: true,
})
if err != nil {
return nil, err
}
// Set defaults
build, err := marshalBuildAndApplyDefaults(inputs["build"])
if err != nil {
return nil, err
}
// Verify Dockerfile at given location
if _, statErr := os.Stat(build.Dockerfile); statErr != nil {
if filepath.IsAbs(build.Dockerfile) {
return nil, fmt.Errorf("could not open dockerfile at absolute path %s: %v", build.Dockerfile, statErr)
}
relPath := filepath.Join(build.Context, build.Dockerfile)
_, err = os.Stat(relPath)
// In the case of a pulumi project that looks as follows:
// infra/
// app/
// # some content for the Docker build
// Dockerfile
// Pulumi.yaml
//
//
// the user inputs:
// context: "./app"
// dockerfile: "./Dockerfile" # this is in error because it is in "./app/Dockerfile"
//
// we want an error message that tells the user: try "./app/Dockerfile"
if err != nil {
// no clue case
return nil, fmt.Errorf("could not open dockerfile at relative path %s: %v", build.Dockerfile, statErr)
}
// we could open the relative path
return nil, fmt.Errorf("could not open dockerfile at relative path %s. "+
"Try setting `dockerfile` to %q", build.Dockerfile, relPath)
}
// Get the relative path to Dockerfile from docker context
relDockerfile, err := getRelDockerfilePath(build.Context, build.Dockerfile)
if err != nil {
return nil, err
}
// Hash docker build context digest
contextDigest, err := hashContext(build.Context, relDockerfile)
if err != nil {
return nil, err
}
// OS defaults to Linux in all cases
os := "linux"
arch := runtime.GOARCH
hostPlatform := os + "/" + arch
msg := fmt.Sprintf(
"Building your image for %s architecture.\n"+
"To ensure you are building for the correct platform, consider "+
"explicitly setting the `platform` field on ImageBuildOptions.", hostPlatform)
// build options: add implicit resource contextDigest and set default host platform
if inputs["build"].IsNull() {
inputs["build"] = resource.NewObjectProperty(resource.PropertyMap{
"contextDigest": resource.NewStringProperty(contextDigest),
"platform": resource.NewStringProperty(hostPlatform),
})
err = p.host.Log(ctx, "info", urn, msg)
if err != nil {
return nil, err
}
} else {
inputs["build"].ObjectValue()["contextDigest"] = resource.NewStringProperty(contextDigest)
if inputs["build"].ObjectValue()["platform"].IsNull() {
inputs["build"].ObjectValue()["platform"] = resource.NewStringProperty(hostPlatform)
err = p.host.Log(ctx, "info", urn, msg)
if err != nil {
return nil, err
}
}
}
if _, err = marshalCachedImages(inputs["build"]); err != nil {
return nil, err
}
inputStruct, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{
KeepUnknowns: true,
SkipNulls: true,
})
if err != nil {
return nil, err
}
return &rpc.CheckResponse{Inputs: inputStruct, Failures: nil}, nil
}
// Diff checks what impacts a hypothetical update will have on the resource's properties.
func (p *dockerNativeProvider) Diff(ctx context.Context, req *rpc.DiffRequest) (*rpc.DiffResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Diff(%s)", p.name, urn)
logging.V(9).Infof("%s executing", label)
oldState, err := plugin.UnmarshalProperties(req.GetOlds(), plugin.MarshalOptions{
KeepUnknowns: true,
SkipNulls: true,
})
if err != nil {
return nil, err
}
// Extract old inputs from the `__inputs` field of the old state.
oldInputs := parseCheckpointObject(oldState)
news, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
KeepUnknowns: true,
SkipNulls: true,
})
if err != nil {
return nil, err
}
d := oldInputs.Diff(news)
if d == nil {
return &rpc.DiffResponse{
Changes: rpc.DiffResponse_DIFF_NONE,
}, nil
}
diff := map[string]*rpc.PropertyDiff{}
for key := range d.Adds {
diff[string(key)] = &rpc.PropertyDiff{Kind: rpc.PropertyDiff_ADD}
}
for key := range d.Deletes {
diff[string(key)] = &rpc.PropertyDiff{Kind: rpc.PropertyDiff_DELETE}
}
detailedUpdates := diffUpdates(d.Updates)
// merge detailedUpdates into diff
for k, v := range detailedUpdates {
diff[k] = v
}
// if diff is empty, it means we skipped any changes to username and password
if len(diff) == 0 {
return &rpc.DiffResponse{
Changes: rpc.DiffResponse_DIFF_NONE,
}, nil
}
return &rpc.DiffResponse{
Changes: rpc.DiffResponse_DIFF_SOME,
DetailedDiff: diff,
HasDetailedDiff: true,
}, nil
}
func diffUpdates(updates map[resource.PropertyKey]resource.ValueDiff) map[string]*rpc.PropertyDiff {
updateDiff := map[string]*rpc.PropertyDiff{}
for key, valueDiff := range updates {
if string(key) != "registry" {
updateDiff[string(key)] = &rpc.PropertyDiff{
Kind: rpc.PropertyDiff_UPDATE,
}
} else {
// only register a diff on "server" field, but not on "username" or "password",
// as they can change frequently and should not trigger a rebuild.
serverDiff := valueDiff.Object.Updates["server"]
// if serverDiff is not empty, we register a property diff update
if serverDiff != (resource.ValueDiff{}) {
updateDiff[string(key)] = &rpc.PropertyDiff{
Kind: rpc.PropertyDiff_UPDATE,
}
}
}
}
return updateDiff
}
// Create allocates a new instance of the provided resource and returns its unique ID afterwards.
func (p *dockerNativeProvider) Create(ctx context.Context, req *rpc.CreateRequest) (*rpc.CreateResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Create(%s)", p.name, urn)
logging.V(9).Infof("%s executing", label)
// Deserialize RPC inputs.
inputs, err := plugin.UnmarshalProperties(req.GetProperties(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.inputs", label),
KeepUnknowns: true,
RejectAssets: true,
SkipNulls: true,
})
if err != nil {
return nil, errors.Wrapf(err, "malformed resource inputs")
}
id, outputProperties, err := p.dockerBuild(ctx, urn, req.GetProperties())
if err != nil {
return nil, err
}
outputs, err := plugin.UnmarshalProperties(outputProperties, plugin.MarshalOptions{
Label: fmt.Sprintf("%s.outputs", label),
KeepUnknowns: true,
RejectAssets: true,
SkipNulls: true,
})
if err != nil {
return nil, err
}
// Store both outputs and inputs into the state.
checkpoint, err := plugin.MarshalProperties(
checkpointObject(inputs, outputs.Mappable()),
plugin.MarshalOptions{
Label: fmt.Sprintf("%s.checkpoint", label),
KeepUnknowns: true,
SkipNulls: true,
},
)
if err != nil {
return nil, err
}
return &rpc.CreateResponse{
Id: id,
Properties: checkpoint,
}, nil
}
// Read the current live state associated with a resource.
func (p *dockerNativeProvider) Read(ctx context.Context, req *rpc.ReadRequest) (*rpc.ReadResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Read(%s)", p.name, urn)
logging.V(9).Infof("%s executing", label)
id := req.GetId()
inputs := req.GetInputs()
properties := req.GetProperties()
// Return properties as passed, since we do no reconciliation,
return &rpc.ReadResponse{Id: id, Inputs: inputs, Properties: properties}, nil
}
// Update updates an existing resource with new values.
func (p *dockerNativeProvider) Update(ctx context.Context, req *rpc.UpdateRequest) (*rpc.UpdateResponse, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Update(%s)", p.name, urn)
logging.V(9).Infof("%s executing", label)
// Read the inputs to persist them into state.
newInputs, err := plugin.UnmarshalProperties(req.GetNews(), plugin.MarshalOptions{
Label: fmt.Sprintf("%s.newInputs", label),
KeepUnknowns: true,
RejectAssets: true,
SkipNulls: true,
})
if err != nil {
return nil, errors.Wrapf(err, "diff failed because malformed resource inputs")
}
// When the docker image is updated, we build and push again.
_, outputProperties, err := p.dockerBuild(ctx, urn, req.GetNews())
if err != nil {
return nil, err
}
outputs, err := plugin.UnmarshalProperties(outputProperties, plugin.MarshalOptions{
Label: fmt.Sprintf("%s.outputs", label),
KeepUnknowns: true,
RejectAssets: true,
SkipNulls: true,
})
if err != nil {
return nil, err
}
// Store both outputs and inputs into the state and return RPC checkpoint.
checkpoint, err := plugin.MarshalProperties(
checkpointObject(newInputs, outputs.Mappable()),
plugin.MarshalOptions{
Label: fmt.Sprintf("%s.checkpoint", label),
KeepUnknowns: true,
SkipNulls: true,
},
)
if err != nil {
return nil, err
}
return &rpc.UpdateResponse{
Properties: checkpoint,
}, nil
}
// Delete tears down an existing resource with the given ID. If it fails, the resource is assumed
// to still exist.
func (p *dockerNativeProvider) Delete(ctx context.Context, req *rpc.DeleteRequest) (*pbempty.Empty, error) {
urn := resource.URN(req.GetUrn())
label := fmt.Sprintf("%s.Update(%s)", p.name, urn)
logging.V(9).Infof("%s executing", label)
return &pbempty.Empty{}, nil
}
// GetPluginInfo returns generic information about this plugin, like its version.
func (p *dockerNativeProvider) GetPluginInfo(context.Context, *pbempty.Empty) (*rpc.PluginInfo, error) {
return &rpc.PluginInfo{
Version: p.version,
}, nil
}
// GetSchema returns the JSON-serialized schema for the provider.
func (p *dockerNativeProvider) GetSchema(ctx context.Context, req *rpc.GetSchemaRequest) (
*rpc.GetSchemaResponse, error) {
if v := req.GetVersion(); v != 0 {
return nil, fmt.Errorf("unsupported schema version %d", v)
}
return &rpc.GetSchemaResponse{Schema: string(p.schemaBytes)}, nil
}
// Cancel signals the provider to gracefully shut down and abort any ongoing resource operations.
// Operations aborted in this way will return an error (e.g., `Update` and `Create` will either a
// creation error or an initialization error). Since Cancel is advisory and non-blocking, it is up
// to the host to decide how long to wait after Cancel is called before (e.g.)
// hard-closing any gRPC connection.
func (p *dockerNativeProvider) Cancel(context.Context, *pbempty.Empty) (*pbempty.Empty, error) {
// TODO
return &pbempty.Empty{}, nil
}
// checkpointObject puts inputs in the `__inputs` field of the state.
func checkpointObject(inputs resource.PropertyMap, outputs map[string]interface{}) resource.PropertyMap {
object := resource.NewPropertyMapFromMap(outputs)
object["__inputs"] = resource.MakeSecret(resource.NewObjectProperty(inputs))
return object
}
// parseCheckpointObject returns inputs that are saved in the `__inputs` field of the state.
func parseCheckpointObject(obj resource.PropertyMap) resource.PropertyMap {
if inputs, ok := obj["__inputs"]; ok {
if inputs.ContainsSecrets() {
return inputs.SecretValue().Element.ObjectValue()
}
return inputs.ObjectValue()
}
return nil
}
type contextHashAccumulator struct {
dockerContextPath string
input bytes.Buffer // This will hold the file info and content bytes to pass to a hash object
}
func (accumulator *contextHashAccumulator) hashPath(file string, fileMode fs.FileMode) error {
hash := sha256.New()
if fileMode.Type() == fs.ModeSymlink {
// For symlinks, we hash the symlink _path_ instead of the file content.
// This will allow us to:
// a) ignore changes at the symlink target
// b) detect if the symlink _itself_ changes
// c) avoid a panic on io.Copy if the symlink target is a directory
symLinkPath, err := filepath.EvalSymlinks(filepath.Join(accumulator.dockerContextPath, file))
if err != nil {
return fmt.Errorf("could not evaluate symlink at %s: %w", file, err)
}
// Hashed content is the clean, os-agnostic file path:
_, err = io.Copy(hash, strings.NewReader(path.Clean(symLinkPath)))
if err != nil {
return fmt.Errorf("could not copy symlink path %s to hash: %w", file, err)
}
} else {
// For regular files, we can hash their content.
// TODO: consider only hashing file metadata to improve performance
f, err := os.Open(filepath.Join(accumulator.dockerContextPath, file))
if err != nil {
return fmt.Errorf("could not open file %s: %w", file, err)
}
defer f.Close()
_, err = io.Copy(hash, f)
if err != nil {
return fmt.Errorf("could not copy file %s to hash: %w", file, err)
}
}
// Capture all information in the accumulator buffer and add a separator
accumulator.input.Write([]byte(filepath.Clean(file))) // use os-agnostic filepath
accumulator.input.Write([]byte(fileMode.String()))
accumulator.input.Write(hash.Sum(nil))
accumulator.input.WriteByte(0)
return nil
}
func (accumulator *contextHashAccumulator) hexSumContext() string {
h := sha256.New()
_, err := accumulator.input.WriteTo(h)
if err != nil {
return ""
}
return hex.EncodeToString(h.Sum(nil))
}
func hashContext(dockerContextPath string, dockerfile string) (string, error) {
// exclude all files listed in dockerignore
dockerIgnorePath := filepath.Join(dockerContextPath, mapDockerignore(filepath.Base(dockerfile)))
ignorePatterns, err := getIgnore(dockerIgnorePath)
if err != nil {
return "", err
}
ignoreMatcher, err := patternmatcher.New(ignorePatterns)
if err != nil {
return "", fmt.Errorf("unable to load rules from %s: %w", dockerIgnorePath, err)
}
accumulator := contextHashAccumulator{dockerContextPath: dockerContextPath}
err = accumulator.hashPath(dockerfile, 0)
if err != nil {
return "", fmt.Errorf("error hashing dockerfile %q: %w", dockerfile, err)
}
// for each file in the Docker build context, create a hash of its content
err = filepath.WalkDir(dockerContextPath, func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
}
path, err = filepath.Rel(dockerContextPath, path)
if err != nil {
return err
}
if path == "." {
return nil
}
ignore, err := ignoreMatcher.MatchesOrParentMatches(path)
if err != nil {
return fmt.Errorf("%s rule failed: %w", dockerIgnorePath, err)
}
if ignore {
if d.IsDir() {
return filepath.SkipDir
}
return nil
} else if d.IsDir() {
return nil
}
fileInfo, err := d.Info()
if err != nil {
return fmt.Errorf("determining mode for %q: %w", path, err)
}
err = accumulator.hashPath(path, fileInfo.Mode())
if err != nil {
return fmt.Errorf("error while hashing %q: %w", path, err)
}
return nil
})
if err != nil {
return "", fmt.Errorf("unable to hash build context: %w", err)
}
// create a hash of the entire input of the hash accumulator
return accumulator.hexSumContext(), nil
}
func getIgnore(dockerIgnorePath string) ([]string, error) {
var ignorePatterns []string
dockerIgnore, err := os.ReadFile(dockerIgnorePath)
if err != nil {
if os.IsNotExist(err) {
// .dockerignore files are optional
return ignorePatterns, nil
}
return ignorePatterns, fmt.Errorf("unable to read %s file: %w", dockerIgnorePath, err)
}
ignorePatterns, err = dockerignore.ReadAll(bytes.NewReader(dockerIgnore))
if err != nil {
return ignorePatterns, fmt.Errorf("unable to parse %s file: %w", ".dockerignore", err)
}
return ignorePatterns, nil
}
func getRelDockerfilePath(buildContext, dockerfile string) (string, error) {
// if the Pulumi program specifies an absolute path or a path relative to the program's local directory,
// we need to get the Dockerfile's relative path to the context directory for the hash function
if strings.Contains(dockerfile, string(filepath.Separator)) {
absDockerfile, err := filepath.Abs(dockerfile)
if err != nil {
return "", fmt.Errorf("absDockerfile error: %s", err)
}
absBuildpath, err := filepath.Abs(buildContext)
if err != nil {
return "", fmt.Errorf("absBuildPath error: %s", err)
}
dockerfile, err = filepath.Rel(absBuildpath, absDockerfile)
if err != nil {
return "", fmt.Errorf("relDockerfile error: %s", err)
}
}
return dockerfile, nil
}