Skip to content

Commit b2309a1

Browse files
committed
fix: state merging
1 parent 4e8c3ed commit b2309a1

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

cmd/deploy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (e *Executor) newDeployCmd() *cobra.Command {
5555
}
5656
}
5757

58-
if opts.Destroy {
58+
if opts.Destroy || opts.SkipAllApps {
5959
opts.SkipBuild = true
6060
}
6161

pkg/actions/deploy_state.go

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func firstPatchLogError(plog diff.PatchLog) error {
3737
return nil
3838
}
3939

40-
func (s *stateDiff) Apply(state *types.StateData) error {
40+
func (s *stateDiff) Apply(state *types.StateData) error { // nolint:gocyclo
4141
if state.Apps == nil {
4242
state.Apps = make(map[string]*apiv1.AppState)
4343
}
@@ -89,7 +89,12 @@ func (s *stateDiff) Apply(state *types.StateData) error {
8989
state.Plugins = make(map[string]*types.PluginState)
9090
}
9191

92-
ret = diff.Patch(s.pluginsState, &state.Plugins)
92+
pluginState, err := pluginsStateWithoutRegistry(state.Plugins)
93+
if err != nil {
94+
return err
95+
}
96+
97+
ret = diff.Patch(s.pluginsState, pluginState)
9398
if ret.HasErrors() {
9499
return merry.Errorf("error applying patch on state.plugins_state: %w", firstPatchLogError(ret))
95100
}
@@ -104,9 +109,28 @@ func (s *stateDiff) Apply(state *types.StateData) error {
104109
return merry.Errorf("error applying patch on state.plugins registry: %w", firstPatchLogError(ret))
105110
}
106111

112+
for k, v := range pluginState {
113+
vals := make(map[string]json.RawMessage, len(v))
114+
115+
if _, ok := state.Plugins[k]; !ok {
116+
state.Plugins[k] = &types.PluginState{}
117+
}
118+
119+
for mapk, mapv := range v {
120+
raw, _ := json.Marshal(mapv)
121+
vals[mapk] = raw
122+
}
123+
124+
state.Plugins[k].Other = vals
125+
}
126+
107127
for k, v := range pluginRegistry {
108128
resources := make([]*registry.ResourceSerialized, 0, len(v))
109129

130+
if _, ok := state.Plugins[k]; !ok {
131+
state.Plugins[k] = &types.PluginState{}
132+
}
133+
110134
for _, r := range v {
111135
resources = append(resources, r)
112136
}
@@ -117,7 +141,7 @@ func (s *stateDiff) Apply(state *types.StateData) error {
117141

118142
out, err := json.Marshal(resources)
119143
if err != nil {
120-
return err
144+
return merry.Errorf("error marshaling plugin registry: %w", err)
121145
}
122146

123147
state.Plugins[k].Registry = out
@@ -216,20 +240,29 @@ func dnsRecordsAsMap(records types.DNSRecordMap) map[string]dnsRecordValue {
216240
return ret
217241
}
218242

219-
func pluginsStateWithoutRegistry(plugins map[string]*types.PluginState) map[string]*types.PluginState {
220-
pluginRegistry := make(map[string]*types.PluginState, len(plugins))
243+
func pluginsStateWithoutRegistry(plugins map[string]*types.PluginState) (map[string]map[string]interface{}, error) {
244+
ret := make(map[string]map[string]interface{}, len(plugins))
221245

222246
for k, v := range plugins {
223-
if v == nil {
247+
if v == nil || len(v.Other) == 0 {
224248
continue
225249
}
226250

227-
pluginRegistry[k] = &types.PluginState{
228-
Other: v.Other,
251+
ret[k] = make(map[string]interface{}, len(v.Other))
252+
253+
for otherk, otherv := range v.Other {
254+
var val interface{}
255+
err := json.Unmarshal(otherv, &val)
256+
257+
if err != nil {
258+
return nil, err
259+
}
260+
261+
ret[k][otherk] = val
229262
}
230263
}
231264

232-
return pluginRegistry
265+
return ret, nil
233266
}
234267

235268
func diffOnlyExported(path []string, parent reflect.Type, field reflect.StructField) bool { // nolint:gocritic
@@ -279,8 +312,15 @@ func computeStateDiff(state1, state2 *types.StateData) (*stateDiff, error) {
279312
return nil, merry.Wrap(err)
280313
}
281314

282-
state1PluginState := pluginsStateWithoutRegistry(state1.Plugins)
283-
state2PluginState := pluginsStateWithoutRegistry(state2.Plugins)
315+
state1PluginState, err := pluginsStateWithoutRegistry(state1.Plugins)
316+
if err != nil {
317+
return nil, merry.Wrap(err)
318+
}
319+
320+
state2PluginState, err := pluginsStateWithoutRegistry(state2.Plugins)
321+
if err != nil {
322+
return nil, merry.Wrap(err)
323+
}
284324

285325
pluginsState, err := computeDiff(state1PluginState, state2PluginState)
286326
if err != nil {

0 commit comments

Comments
 (0)