-
Notifications
You must be signed in to change notification settings - Fork 731
/
conflictdetector.go
73 lines (62 loc) 路 1.53 KB
/
conflictdetector.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
package reader
import (
"fmt"
"log"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
const warningMsg = "WARNING - Resource named %q (from %s) is already defined in %s"
type source struct {
filename string
image string
stdin bool
objs []*unstructured.Unstructured
}
type gknn struct {
schema.GroupKind
name string
namespace string
}
type conflict struct {
id gknn
a *source
b *source
}
func detectConflicts(sources []*source) []conflict {
var conflicts []conflict
cmap := make(map[gknn]*source)
for _, s := range sources {
for _, obj := range s.objs {
key := gknn{
GroupKind: schema.GroupKind{Group: obj.GroupVersionKind().Group, Kind: obj.GetKind()},
name: obj.GetName(),
namespace: obj.GetNamespace(),
}
if dupe, exists := cmap[key]; exists {
conflicts = append(conflicts, conflict{
id: key,
a: s,
b: dupe,
})
}
cmap[key] = s
}
}
return conflicts
}
func logConflict(c *conflict) {
log.Printf(warningMsg+"\n", c.id.name, sourceDebugInfo(c.a), sourceDebugInfo(c.b))
}
// sourceDebugInfo returns a string identifying the source.
// For sources pulled from stdin: "stdin".
// For sources pulled from a file: "file: <filename>".
// For sources pulled from an image: "file: <filename>, image: <imgURL>".
func sourceDebugInfo(s *source) string {
if s.stdin {
return "stdin"
}
if s.image != "" {
return fmt.Sprintf("file: %q, image: %q", s.filename, s.image)
}
return fmt.Sprintf("file: %q", s.filename)
}