-
Notifications
You must be signed in to change notification settings - Fork 9
/
op_waitforproviderresults.go
146 lines (119 loc) · 4.89 KB
/
op_waitforproviderresults.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package deploy
import (
"bytes"
"context"
"fmt"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/dynamicpb"
"namespacelabs.dev/foundation/internal/codegen/protos"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
internalres "namespacelabs.dev/foundation/internal/resources"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
orchpb "namespacelabs.dev/foundation/schema/orchestration"
"namespacelabs.dev/foundation/std/execution"
"namespacelabs.dev/foundation/std/tasks"
)
var GarbageCollectProviders = true
var resultHeader = []byte("namespace.provision.result:")
func register_OpWaitForProviderResults() {
execution.RegisterFuncs(execution.Funcs[*internalres.OpWaitForProviderResults]{
EmitStart: func(ctx context.Context, inv *schema.SerializedInvocation, wait *internalres.OpWaitForProviderResults, ch chan *orchpb.Event) {
ch <- &orchpb.Event{
ResourceId: wait.ResourceInstanceId,
Category: "Resources deployed",
Ready: orchpb.Event_NOT_READY,
Stage: orchpb.Event_WAITING,
}
},
HandleWithEvents: func(ctx context.Context, inv *schema.SerializedInvocation, wait *internalres.OpWaitForProviderResults, ch chan *orchpb.Event) (*execution.HandleResult, error) {
action := tasks.Action("resource.complete-invocation").
Scope(wait.Deployable.GetPackageRef().AsPackageName()).
Arg("resource_instance_id", wait.ResourceInstanceId).
HumanReadablef(inv.Description)
return tasks.Return(ctx, action, func(ctx context.Context) (*execution.HandleResult, error) {
cluster, err := execution.Get(ctx, runtime.ClusterNamespaceInjection)
if err != nil {
return nil, err
}
if GarbageCollectProviders {
defer func() {
if err := cluster.DeleteDeployable(ctx, wait.Deployable); err != nil {
fmt.Fprintf(console.Errors(ctx), "Deleting %s failed: %v\n", wait.Deployable.Name, err)
}
}()
}
if ch != nil {
ch <- &orchpb.Event{
ResourceId: wait.ResourceInstanceId,
Category: "Resources deployed",
Ready: orchpb.Event_NOT_READY,
Stage: orchpb.Event_WAITING,
WaitStatus: []*orchpb.Event_WaitStatus{{Description: "Waiting for provider..."}},
}
}
// XXX add a maximum time we're willing to wait.
containers, err := cluster.WaitForTermination(ctx, wait.Deployable)
if err != nil {
return nil, err
}
if len(containers) != 1 {
return nil, fnerrors.InternalError("expected exactly one container, got %d", len(containers))
}
main := containers[0]
var out bytes.Buffer
if err := cluster.Cluster().FetchLogsTo(ctx, main.Reference, runtime.FetchLogsOpts{}, runtime.WriteToWriter(&out)); err != nil {
return nil, fnerrors.InternalError("failed to retrieve output of provider invocation: %w", err)
}
if main.TerminationError != nil {
fmt.Fprintf(console.Errors(ctx), "%s provision failure:\n%s\n", wait.ResourceInstanceId, out.Bytes())
return nil, fnerrors.ExternalError("provider failed: %w", main.TerminationError)
}
_, msgdesc, err := protos.LoadMessageByName(wait.InstanceTypeSource, wait.ResourceClass.InstanceType.ProtoType)
if err != nil {
return nil, err
}
tasks.Attachments(ctx).Attach(tasks.Output("invocation-output.log", "text/plain"), out.Bytes())
// The protocol is that a provision tool must emit a line `namespace.provision.result: json`
lines := bytes.Split(out.Bytes(), []byte("\n"))
var resultMessage proto.Message
for _, line := range lines {
if !bytes.HasPrefix(line, resultHeader) {
continue
}
if resultMessage != nil {
return nil, fnerrors.ExternalError("invocation produced multiple results")
}
parsedMessage := dynamicpb.NewMessage(msgdesc).Interface()
original := bytes.TrimPrefix(line, resultHeader)
if err := protojson.Unmarshal(original, parsedMessage); err != nil {
return nil, fnerrors.ExternalError("failed to unmarshal provision result: %w", err)
}
resultMessage = parsedMessage
}
if resultMessage == nil {
return nil, fnerrors.ExternalError("provision did not produce a result")
}
_ = tasks.Attachments(ctx).AttachSerializable("instance.json", "", resultMessage)
if ch != nil {
ch <- &orchpb.Event{
ResourceId: wait.ResourceInstanceId,
Category: "Resources deployed",
Ready: orchpb.Event_READY,
Stage: orchpb.Event_DONE,
}
}
return &execution.HandleResult{
Outputs: []execution.Output{
{InstanceID: wait.ResourceInstanceId, Message: resultMessage},
},
}, nil
})
},
})
}