-
Notifications
You must be signed in to change notification settings - Fork 139
Expand file tree
/
Copy pathchangeset.go
More file actions
130 lines (108 loc) · 3.99 KB
/
Copy pathchangeset.go
File metadata and controls
130 lines (108 loc) · 3.99 KB
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
/*
Copyright 2021 Stefan Prodan
Copyright 2021 The Flux authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ssa
import (
"fmt"
"strings"
"github.com/fluxcd/cli-utils/pkg/object"
)
// Action represents the action type performed by the reconciliation process.
type Action string
// String returns the string representation of the action.
func (a Action) String() string {
return string(a)
}
const (
// CreatedAction represents the creation of a new object.
CreatedAction Action = "created"
// ConfiguredAction represents the update of an existing object.
ConfiguredAction Action = "configured"
// UnchangedAction represents the absence of any action to an object.
UnchangedAction Action = "unchanged"
// DeletedAction represents the deletion of an object.
DeletedAction Action = "deleted"
// SkippedAction represents the fact that no action was performed on an object
// due to the object being excluded from the reconciliation.
SkippedAction Action = "skipped"
// UnknownAction represents an unknown action.
UnknownAction Action = "unknown"
)
// ChangeSet holds the result of the reconciliation of an object collection.
type ChangeSet struct {
Entries []ChangeSetEntry
}
// NewChangeSet returns a ChangeSet with an empty slice of entries.
func NewChangeSet() *ChangeSet {
return &ChangeSet{Entries: []ChangeSetEntry{}}
}
// Add appends the given ChangeSetEntry to the end of the slice.
func (c *ChangeSet) Add(e ChangeSetEntry) {
c.Entries = append(c.Entries, e)
}
// Append adds the given ChangeSet entries to the end of the slice.
func (c *ChangeSet) Append(e []ChangeSetEntry) {
c.Entries = append(c.Entries, e...)
}
// String formats and returns the string representation of the ChangeSet
// by concatenating the string output of each entry.
func (c *ChangeSet) String() string {
var b strings.Builder
for _, entry := range c.Entries {
b.WriteString(entry.String() + "\n")
}
return strings.TrimSuffix(b.String(), "\n")
}
// ToGroupedMap converts ChangeSet entries into a map grouped by Action,
// where keys are actions and values are subject slices.
func (c *ChangeSet) ToGroupedMap() map[Action][]string {
res := make(map[Action][]string)
for _, entry := range c.Entries {
res[entry.Action] = append(res[entry.Action], entry.Subject)
}
return res
}
// ToMap converts the ChangeSet entries into a map where the keys
// are subjects and the values are the corresponding actions.
func (c *ChangeSet) ToMap() map[string]Action {
res := make(map[string]Action, len(c.Entries))
for _, entry := range c.Entries {
res[entry.Subject] = entry.Action
}
return res
}
// ToObjMetadataSet converts the ChangeSet entries to an ObjMetadataSet
// by extracting the ObjMetadata from each entry.
func (c *ChangeSet) ToObjMetadataSet() object.ObjMetadataSet {
var res []object.ObjMetadata
for _, entry := range c.Entries {
res = append(res, entry.ObjMetadata)
}
return res
}
// ChangeSetEntry defines the result of an action performed on an object.
type ChangeSetEntry struct {
// ObjMetadata holds the unique identifier of this entry.
ObjMetadata object.ObjMetadata
// GroupVersion holds the API group version of this entry.
GroupVersion string
// Subject represents the Object ID in the format 'kind/namespace/name'.
Subject string
// Action represents the action type taken by the reconciler for this object.
Action Action
}
// String returns a string representation of the ChangeSetEntry
// by combining its Subject and Action fields.
func (e ChangeSetEntry) String() string {
return fmt.Sprintf("%s %s", e.Subject, e.Action)
}