-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_change_set.go
169 lines (133 loc) · 4.61 KB
/
cluster_change_set.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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package clusterapply
import (
"fmt"
ctlconf "github.com/k14s/kapp/pkg/kapp/config"
ctldiff "github.com/k14s/kapp/pkg/kapp/diff"
ctldgraph "github.com/k14s/kapp/pkg/kapp/diffgraph"
"github.com/k14s/kapp/pkg/kapp/logger"
)
type ClusterChangeSetOpts struct {
ApplyingChangesOpts
WaitingChangesOpts
}
type ClusterChangeSet struct {
changes []ctldiff.Change
opts ClusterChangeSetOpts
clusterChangeFactory ClusterChangeFactory
changeGroupBindings []ctlconf.ChangeGroupBinding
changeRuleBindings []ctlconf.ChangeRuleBinding
ui UI
logger logger.Logger
}
func NewClusterChangeSet(changes []ctldiff.Change, opts ClusterChangeSetOpts,
clusterChangeFactory ClusterChangeFactory, changeGroupBindings []ctlconf.ChangeGroupBinding,
changeRuleBindings []ctlconf.ChangeRuleBinding, ui UI, logger logger.Logger) ClusterChangeSet {
return ClusterChangeSet{changes, opts, clusterChangeFactory,
changeGroupBindings, changeRuleBindings, ui, logger.NewPrefixed("ClusterChangeSet")}
}
func (c ClusterChangeSet) Calculate() ([]*ClusterChange, *ctldgraph.ChangeGraph, error) {
var wrappedClusterChanges []ctldgraph.ActualChange
for _, change := range c.changes {
clusterChange := c.clusterChangeFactory.NewClusterChange(change)
wrappedClusterChanges = append(wrappedClusterChanges, wrappedClusterChange{clusterChange})
}
changesGraph, err := ctldgraph.NewChangeGraph(wrappedClusterChanges,
c.changeGroupBindings, c.changeRuleBindings, c.logger)
if err != nil {
// Return graph for inspection
return nil, changesGraph, err
}
changesGraph.AllMatching(func(change *ctldgraph.Change) bool {
c.markChangesToWait(change)
return false
})
// Prune out changes that are not involved with anything
changesGraph.RemoveMatching(func(change *ctldgraph.Change) bool {
clusterChange := change.Change.(wrappedClusterChange).ClusterChange
return clusterChange.ApplyOp() == ClusterChangeApplyOpNoop &&
clusterChange.WaitOp() == ClusterChangeWaitOpNoop
})
var clusterChanges []*ClusterChange
for _, change := range changesGraph.All() {
clusterChange := change.Change.(wrappedClusterChange).ClusterChange
clusterChanges = append(clusterChanges, clusterChange)
}
return clusterChanges, changesGraph, nil
}
func (c ClusterChangeSet) markChangesToWait(change *ctldgraph.Change) bool {
var needsWaiting bool
for _, ch := range change.WaitingFor {
if c.markChangesToWait(ch) {
needsWaiting = true
break
}
}
if needsWaiting {
change.Change.(wrappedClusterChange).MarkNeedsWaiting()
return true
}
return change.Change.(wrappedClusterChange).WaitOp() != ClusterChangeWaitOpNoop
}
func (c ClusterChangeSet) Apply(changesGraph *ctldgraph.ChangeGraph) error {
defer c.logger.DebugFunc("Apply").Finish()
expectedNumChanges := len(changesGraph.All())
blockedChanges := ctldgraph.NewBlockedChanges(changesGraph)
applyingChanges := NewApplyingChanges(
expectedNumChanges, c.opts.ApplyingChangesOpts, c.clusterChangeFactory, c.ui)
waitingChanges := NewWaitingChanges(expectedNumChanges, c.opts.WaitingChangesOpts, c.ui)
for {
appliedChanges, err := applyingChanges.Apply(blockedChanges.Unblocked())
if err != nil {
return err
}
waitingChanges.Track(appliedChanges)
if waitingChanges.IsEmpty() {
err := applyingChanges.Complete()
if err != nil {
c.ui.Notify([]string{fmt.Sprintf("Blocked changes:\n%s\n", blockedChanges.WhyBlocked(blockedChanges.Blocked()))})
return err
}
return waitingChanges.Complete()
}
doneChanges, err := waitingChanges.WaitForAny()
if err != nil {
return err
}
for _, change := range doneChanges {
blockedChanges.Unblock(change.Graph)
}
}
}
func ClusterChangesAsChangeViews(changes []*ClusterChange) []ChangeView {
var result []ChangeView
for _, change := range changes {
result = append(result, change)
}
return result
}
type wrappedClusterChange struct {
*ClusterChange
}
func (c wrappedClusterChange) Op() ctldgraph.ActualChangeOp {
op := c.ApplyOp()
switch op {
case ClusterChangeApplyOpAdd, ClusterChangeApplyOpUpdate:
return ctldgraph.ActualChangeOpUpsert
case ClusterChangeApplyOpDelete:
return ctldgraph.ActualChangeOpDelete
case ClusterChangeApplyOpNoop:
return ctldgraph.ActualChangeOpNoop
case ClusterChangeApplyOpExists:
return ctldgraph.ActualChangeOpUpsert
default:
panic(fmt.Sprintf("Unknown change apply operation: %s", op))
}
}
func (c wrappedClusterChange) WaitOp() ClusterChangeWaitOp {
return c.ClusterChange.WaitOp()
}
func (c wrappedClusterChange) MarkNeedsWaiting() {
c.ClusterChange.MarkNeedsWaiting()
}