-
Notifications
You must be signed in to change notification settings - Fork 8
/
resourceprovider.go
147 lines (123 loc) · 4.73 KB
/
resourceprovider.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
// 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 (
"context"
"encoding/json"
"fmt"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"k8s.io/utils/strings/slices"
"namespacelabs.dev/foundation/internal/artifacts/oci"
protos2 "namespacelabs.dev/foundation/internal/codegen/protos"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/protos"
internalres "namespacelabs.dev/foundation/internal/resources"
"namespacelabs.dev/foundation/internal/runtime"
"namespacelabs.dev/foundation/schema"
runtimepb "namespacelabs.dev/foundation/schema/runtime"
"namespacelabs.dev/foundation/std/execution"
"namespacelabs.dev/foundation/std/resources"
"namespacelabs.dev/foundation/std/tasks"
"namespacelabs.dev/go-ids"
)
type InvokeResourceProvider struct {
ResourceInstanceId string
SerializedIntentJson []byte
BinaryRef *schema.PackageRef
BinaryImageId oci.ImageID
BinaryConfig *schema.BinaryConfig
ResourceClass *schema.ResourceClass
ResourceProvider *schema.ResourceProvider
InstanceTypeSource *protos2.FileDescriptorSetAndDeps
ResourceDependencies []*resources.ResourceDependency
SecretResources []runtime.SecretResourceDependency
}
func PlanResourceProviderInvocation(ctx context.Context, planner runtime.Planner, invoke *InvokeResourceProvider) ([]*schema.SerializedInvocation, error) {
args := append(slices.Clone(invoke.BinaryConfig.Args), fmt.Sprintf("--intent=%s", invoke.SerializedIntentJson))
spec := runtime.DeployableSpec{
// ErrorLocation: resource.ProviderPackage.Location,
PackageRef: invoke.BinaryRef,
Class: schema.DeployableClass_MANUAL, // Don't emit deployment events.
Id: ids.NewRandomBase32ID(8),
Name: "provider",
Description: fmt.Sprintf("Ensure resource: %s", invoke.ResourceInstanceId),
Resources: invoke.ResourceDependencies,
SecretResources: invoke.SecretResources,
SetContainerField: []*runtimepb.SetContainerField{
// Resources are passed in as flags to minimize the number of k8s resources that are created.
// XXX security validate this.
{SetArg: []*runtimepb.SetContainerField_SetValue{{Key: "--resources", Value: runtimepb.SetContainerField_RESOURCE_CONFIG}}},
},
MainContainer: runtime.ContainerRunOpts{
Image: invoke.BinaryImageId,
Command: invoke.BinaryConfig.Command,
Args: args,
Env: invoke.BinaryConfig.Env,
},
}
plan, err := planner.PlanDeployment(ctx, runtime.DeploymentSpec{
Specs: []runtime.DeployableSpec{spec},
})
if err != nil {
return nil, err
}
var ops []*schema.SerializedInvocation
ops = append(ops, plan.Definitions...)
ops = append(ops, &schema.SerializedInvocation{
Description: fmt.Sprintf("Wait for Resource (%s:%s)", invoke.ResourceClass.PackageName, invoke.ResourceClass.Name),
Impl: protos.WrapAnyOrDie(&internalres.OpWaitForProviderResults{
ResourceInstanceId: invoke.ResourceInstanceId,
Deployable: runtime.DeployableToProto(spec),
ResourceClass: invoke.ResourceClass,
InstanceTypeSource: invoke.InstanceTypeSource,
}),
Order: &schema.ScheduleOrder{
SchedCategory: []string{resources.ResourceInstanceCategory(invoke.ResourceInstanceId)},
SchedAfterCategory: []string{runtime.DeployableCategory(spec)},
},
})
return ops, nil
}
type RawJSONObject map[string]any
func BuildResourceMap(ctx context.Context, dependencies []*resources.ResourceDependency) (map[string]RawJSONObject, error) {
if len(dependencies) == 0 {
return nil, nil
}
return tasks.Return(ctx, tasks.Action("deploy.build-resource-map"), func(ctx context.Context) (map[string]RawJSONObject, error) {
inputs, err := execution.Get(ctx, execution.InputsInjection)
if err != nil {
return nil, err
}
resourceData := map[string]RawJSONObject{}
var missing []string
for _, dep := range dependencies {
input, ok := inputs[dep.ResourceInstanceId]
if ok {
raw, err := protoAsGenericJson(input.Message)
if err != nil {
return nil, err
}
resourceData[dep.GetResourceRef().Canonical()] = raw
} else {
missing = append(missing, dep.GetResourceRef().Canonical())
}
}
if len(missing) > 0 {
return nil, fnerrors.InternalError("missing required resources: %v", missing)
}
return resourceData, nil
})
}
func protoAsGenericJson(msg proto.Message) (RawJSONObject, error) {
serialized, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(msg)
if err != nil {
return nil, err
}
var m RawJSONObject
if err := json.Unmarshal(serialized, &m); err != nil {
return nil, err
}
return m, nil
}