forked from KusionStack/kusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply.go
208 lines (192 loc) · 4.84 KB
/
apply.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
package api
import (
"fmt"
"io"
"strings"
"sync"
"github.com/liu-hm19/pterm"
apiv1 "kusionstack.io/kusion/pkg/apis/api.kusion.io/v1"
v1 "kusionstack.io/kusion/pkg/apis/status/v1"
"kusionstack.io/kusion/pkg/engine/operation"
"kusionstack.io/kusion/pkg/engine/operation/models"
"kusionstack.io/kusion/pkg/engine/release"
"kusionstack.io/kusion/pkg/log"
)
// The Apply function will apply the resources changes
// through the execution Kusion Engine, and will save
// the state to specified storage.
//
// You can customize the runtime of engine and the state
// storage through `runtime` and `storage` parameters.
func Apply(
o *APIOptions,
storage release.Storage,
rel *apiv1.Release,
changes *models.Changes,
out io.Writer,
) (*apiv1.Release, error) {
// construct the apply operation
ac := &operation.ApplyOperation{
Operation: models.Operation{
Stack: changes.Stack(),
ReleaseStorage: storage,
MsgCh: make(chan models.Message),
IgnoreFields: o.IgnoreFields,
},
}
// line summary
var ls lineSummary
// progress bar, print dag walk detail
progressbar, err := pterm.DefaultProgressbar.
WithMaxWidth(0). // Set to 0, the terminal width will be used
WithTotal(len(changes.StepKeys)).
WithWriter(out).
WithRemoveWhenDone().
Start()
if err != nil {
return nil, err
}
// wait msgCh close
var wg sync.WaitGroup
// receive msg and print detail
go func() {
defer func() {
if p := recover(); p != nil {
log.Errorf("failed to receive msg and print detail as %v", p)
}
}()
wg.Add(1)
for {
select {
case msg, ok := <-ac.MsgCh:
if !ok {
wg.Done()
return
}
changeStep := changes.Get(msg.ResourceID)
switch msg.OpResult {
case models.Success, models.Skip:
var title string
if changeStep.Action == models.UnChanged {
title = fmt.Sprintf("%s %s, %s",
changeStep.Action.String(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(models.Skip)),
)
} else {
title = fmt.Sprintf("%s %s %s",
changeStep.Action.String(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(msg.OpResult)),
)
}
pterm.Success.WithWriter(out).Println(title)
progressbar.UpdateTitle(title)
progressbar.Increment()
ls.Count(changeStep.Action)
case models.Failed:
title := fmt.Sprintf("%s %s %s",
changeStep.Action.String(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(msg.OpResult)),
)
pterm.Error.WithWriter(out).Printf("%s\n", title)
default:
title := fmt.Sprintf("%s %s %s",
changeStep.Action.Ing(),
pterm.Bold.Sprint(changeStep.ID),
strings.ToLower(string(msg.OpResult)),
)
progressbar.UpdateTitle(title)
}
}
}
}()
var upRel *apiv1.Release
if o.DryRun {
for _, r := range rel.Spec.Resources {
ac.MsgCh <- models.Message{
ResourceID: r.ResourceKey(),
OpResult: models.Success,
OpErr: nil,
}
}
close(ac.MsgCh)
} else {
// parse cluster in arguments
rsp, st := ac.Apply(&operation.ApplyRequest{
Request: models.Request{
Project: changes.Project(),
Stack: changes.Stack(),
},
Release: rel,
})
if v1.IsErr(st) {
return nil, fmt.Errorf("apply failed, status:\n%v", st)
}
upRel = rsp.Release
}
// wait for msgCh closed
wg.Wait()
// print summary
pterm.Fprintln(out, fmt.Sprintf("Apply complete! Resources: %d created, %d updated, %d deleted.", ls.created, ls.updated, ls.deleted))
return upRel, nil
}
// Watch function will observe the changes of each resource
// by the execution engine.
//
// Example:
//
// o := NewApplyOptions()
// kubernetesRuntime, err := runtime.NewKubernetesRuntime()
// if err != nil {
// return err
// }
//
// Watch(o, kubernetesRuntime, planResources, changes, os.Stdout)
// if err != nil {
// return err
// }
func Watch(
o *APIOptions,
planResources *apiv1.Spec,
changes *models.Changes,
) error {
if o.DryRun {
fmt.Println("NOTE: Watch doesn't work in DryRun mode")
return nil
}
// filter out unchanged resources
toBeWatched := apiv1.Resources{}
for _, res := range planResources.Resources {
if changes.ChangeOrder.ChangeSteps[res.ResourceKey()].Action != models.UnChanged {
toBeWatched = append(toBeWatched, res)
}
}
// watch operation
wo := &operation.WatchOperation{}
if err := wo.Watch(&operation.WatchRequest{
Request: models.Request{
Project: changes.Project(),
Stack: changes.Stack(),
},
Spec: &apiv1.Spec{Resources: toBeWatched},
}); err != nil {
return err
}
fmt.Println("Watch Finish! All resources have been reconciled.")
return nil
}
type lineSummary struct {
created, updated, deleted int
}
func (ls *lineSummary) Count(op models.ActionType) {
switch op {
case models.Create:
ls.created++
case models.Update:
ls.updated++
case models.Delete:
ls.deleted++
}
}