-
Notifications
You must be signed in to change notification settings - Fork 78
/
runtime.go
141 lines (108 loc) · 4.23 KB
/
runtime.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
package runtime
import (
"context"
"k8s.io/apimachinery/pkg/watch"
apiv1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
v1 "kusionstack.io/kusion/pkg/apis/status/v1"
)
const (
Kubernetes apiv1.Type = "Kubernetes"
Terraform apiv1.Type = "Terraform"
)
// Runtime represents an actual infrastructure runtime managed by Kusion and every runtime implements this interface can be orchestrated
// by Kusion like normal K8s resources. All methods in this interface are designed for manipulating one Resource at a time and will be
// invoked in operations like Apply, Preview, Destroy, etc.
type Runtime interface {
// Apply means modify this Resource to the desired state described in the request,
// and it will turn into creating or updating a Resource in most scenarios.
// If the infrastructure runtime already provides an Apply method that conform to this method's semantics meaning,
// like the Kubernetes Runtime, you can directly invoke this method without any conversion.
// PlanResource and priorState are given in this method for the runtime which would make a
// three-way-merge (planState,priorState and live state) when implementing this interface
Apply(ctx context.Context, request *ApplyRequest) *ApplyResponse
// Read the latest state of this Resource
Read(ctx context.Context, request *ReadRequest) *ReadResponse
// Import Resource that already existed in the actual infrastructure
Import(ctx context.Context, request *ImportRequest) *ImportResponse
// Delete this Resource in the actual infrastructure and return success if this Resource is not exist
Delete(ctx context.Context, request *DeleteRequest) *DeleteResponse
// Watch the latest state or event of this Resource.
// This is an optional method for the Runtime to implement,
// but it will be very helpful for us to know what is happening when applying this Resource
Watch(ctx context.Context, request *WatchRequest) *WatchResponse
}
type ApplyRequest struct {
// PriorResource is the last applied resource saved in state storage
PriorResource *apiv1.Resource
// PlanResource is the resource we want to apply in this request
PlanResource *apiv1.Resource
// Stack contains info about where this command is invoked
Stack *apiv1.Stack
// DryRun means this a dry-run request and will not make any changes in actual infra
DryRun bool
}
type ApplyResponse struct {
// Resource is the result returned by Runtime
Resource *apiv1.Resource
// Status contains messages will show to users
Status v1.Status
}
type ReadRequest struct {
// PriorResource is the last applied resource saved in state storage
PriorResource *apiv1.Resource
// PlanResource is the resource we want to apply in this request
PlanResource *apiv1.Resource
// Stack contains info about where this command is invoked
Stack *apiv1.Stack
}
type ReadResponse struct {
// Resource is the result read from the actual infra
Resource *apiv1.Resource
// Status contains messages will show to users
Status v1.Status
}
type ImportRequest struct {
// PlanResource is the resource we want to apply in this request
PlanResource *apiv1.Resource
// Stack contains info about where this command is invoked
Stack *apiv1.Stack
}
type ImportResponse struct {
// Resource is the result returned by Runtime
Resource *apiv1.Resource
// Status contains messages will show to users
Status v1.Status
}
type DeleteRequest struct {
// Resource represents the resource we want to delete from the actual infra
Resource *apiv1.Resource
// Stack contains info about where this command is invoked
Stack *apiv1.Stack
}
type DeleteResponse struct {
// Status contains messages will show to users
Status v1.Status
}
type WatchRequest struct {
// Resource represents the resource we want to watch from the actual infra
Resource *apiv1.Resource
}
type WatchResponse struct {
Watchers *SequentialWatchers
// Status contains messages will show to users
Status v1.Status
}
type SequentialWatchers struct {
IDs []string
Watchers []<-chan watch.Event
}
func NewWatchers() *SequentialWatchers {
return &SequentialWatchers{
IDs: []string{},
Watchers: []<-chan watch.Event{},
}
}
func (w *SequentialWatchers) Insert(id string, watcher <-chan watch.Event) {
w.IDs = append(w.IDs, id)
w.Watchers = append(w.Watchers, watcher)
}