forked from fluxcd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeset.go
102 lines (81 loc) · 2.68 KB
/
changeset.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
/*
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"
"sigs.k8s.io/cli-utils/pkg/object"
)
// Action represents the action type performed by the reconciliation process.
type Action string
const (
CreatedAction Action = "created"
ConfiguredAction Action = "configured"
UnchangedAction Action = "unchanged"
DeletedAction Action = "deleted"
UnknownAction Action = "unknown"
)
// ChangeSet holds the result of the reconciliation of an object collection.
type ChangeSet struct {
Entries []ChangeSetEntry
}
// NewChangeSet returns a ChangeSet will an empty slice of entries.
func NewChangeSet() *ChangeSet {
return &ChangeSet{Entries: []ChangeSetEntry{}}
}
// Add appends the given entry 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 end of the slice.
func (c *ChangeSet) Append(e []ChangeSetEntry) {
c.Entries = append(c.Entries, e...)
}
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")
}
func (c *ChangeSet) ToMap() map[string]string {
res := make(map[string]string, len(c.Entries))
for _, entry := range c.Entries {
res[entry.Subject] = entry.Action
}
return res
}
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 string
// Diff contains the YAML diff resulting from server-side apply dry-run.
Diff string
}
func (e ChangeSetEntry) String() string {
return fmt.Sprintf("%s %s", e.Subject, e.Action)
}