-
Notifications
You must be signed in to change notification settings - Fork 9
/
op_waitforproviderresults.go
179 lines (148 loc) · 6.02 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
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
// 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"
"encoding/json"
"fmt"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/dynamicpb"
"google.golang.org/protobuf/types/known/timestamppb"
"namespacelabs.dev/foundation/framework/resources/provider"
"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:")
messageHeader = []byte("namespace.provision.message:")
)
func register_OpWaitForProviderResults() {
execution.RegisterFuncs(execution.Funcs[*internalres.OpWaitForProviderResults]{
EmitStart: func(ctx context.Context, inv *schema.SerializedInvocation, wait *internalres.OpWaitForProviderResults, ch chan *orchpb.Event) {
var label string
if wait.GetResourceClass().GetDescription() != "" {
label = fmt.Sprintf("%s (%s)", wait.GetResourceClass().GetDescription(), wait.ResourceInstanceId)
}
ch <- &orchpb.Event{
ResourceId: wait.ResourceInstanceId,
Category: "Resources deployed",
Ready: orchpb.Event_NOT_READY,
Stage: orchpb.Event_WAITING,
ResourceLabel: label,
}
},
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\n\n >> See the logs above for the provider error. <<\n", 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.message: json` (or message.provision.result previously).
lines := bytes.Split(out.Bytes(), []byte("\n"))
var resultMessage proto.Message
setMessage := func(serialized []byte) error {
if resultMessage != nil {
return fnerrors.ExternalError("invocation produced multiple results")
}
parsedMessage := dynamicpb.NewMessage(msgdesc).Interface()
if err := protojson.Unmarshal(serialized, parsedMessage); err != nil {
return fnerrors.ExternalError("failed to unmarshal provider result: %w", err)
}
resultMessage = parsedMessage
return nil
}
for _, line := range lines {
switch {
case bytes.HasPrefix(line, resultHeader):
original := bytes.TrimPrefix(line, resultHeader)
if err := setMessage(original); err != nil {
return nil, err
}
case bytes.HasPrefix(line, messageHeader):
var msg provider.Message
original := bytes.TrimPrefix(line, messageHeader)
if err := json.Unmarshal(original, &msg); err != nil {
return nil, fnerrors.ExternalError("failed to unmarshal provider message: %w", err)
}
if msg.SerializedInstanceJSON != nil {
if err := setMessage([]byte(*msg.SerializedInstanceJSON)); err != nil {
return nil, err
}
}
}
}
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,
Timestamp: timestamppb.Now(),
}
}
return &execution.HandleResult{
Outputs: []execution.Output{
{InstanceID: wait.ResourceInstanceId, Message: resultMessage},
},
}, nil
})
},
})
}