This repository has been archived by the owner on Apr 29, 2020. It is now read-only.
/
inspect.go
107 lines (93 loc) · 3.41 KB
/
inspect.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
package inspect
import (
"fmt"
"sort"
"github.com/square/p2/pkg/health"
"github.com/square/p2/pkg/launch"
"github.com/square/p2/pkg/store/consul"
"github.com/square/p2/pkg/types"
)
const (
INTENT_SOURCE = iota
REALITY_SOURCE
)
type LaunchableVersion struct {
Location string `json:"location,omitempty"`
Version launch.LaunchableVersion `json:"version,omitempty"`
}
type NodePodStatus struct {
NodeName types.NodeName `json:"node,omitempty"`
PodId types.PodID `json:"pod,omitempty"`
IntentManifestSHA string `json:"intent_manifest_sha"`
RealityManifestSHA string `json:"reality_manifest_sha"`
IntentVersions map[launch.LaunchableID]LaunchableVersion `json:"intent_versions,omitempty"`
RealityVersions map[launch.LaunchableID]LaunchableVersion `json:"reality_versions,omitempty"`
Health health.HealthState `json:"health,omitempty"`
// These fields are kept for backwards compatibility with tools that
// parse the output of p2-inspect. intent_versions and reality_versions
// are preferred since those handle multiple versions of manifest syntax
IntentLocations []string `json:"intent_locations"`
RealityLocations []string `json:"reality_locations"`
}
func AddKVPToMap(result consul.ManifestResult, source int, filterNode types.NodeName, filterPod types.PodID, statuses map[types.PodID]map[types.NodeName]NodePodStatus) error {
if result.PodUniqueKey != "" {
// for now, p2-inspect won't show uuid pods
return nil
}
nodeName := result.PodLocation.Node
podId := result.Manifest.ID()
if filterNode != "" && nodeName != filterNode {
return nil
}
if filterPod != "" && podId != filterPod {
return nil
}
if statuses[podId] == nil {
statuses[podId] = make(map[types.NodeName]NodePodStatus)
}
old := statuses[podId][nodeName]
old.IntentVersions = make(map[launch.LaunchableID]LaunchableVersion)
old.RealityVersions = make(map[launch.LaunchableID]LaunchableVersion)
manifestSHA, err := result.Manifest.SHA()
if err != nil {
return err
}
switch source {
case INTENT_SOURCE:
if old.IntentManifestSHA != "" {
return fmt.Errorf("Two intent manifests for node %s pod %s", nodeName, podId)
}
old.IntentManifestSHA = manifestSHA
for launchableID, launchable := range result.Manifest.GetLaunchableStanzas() {
var version launch.LaunchableVersion
if launchable.Version.ID != "" {
version = launchable.Version
}
old.IntentVersions[launchableID] = LaunchableVersion{
Location: launchable.Location,
Version: version,
}
old.IntentLocations = append(old.IntentLocations, launchable.Location)
}
sort.Strings(old.IntentLocations)
case REALITY_SOURCE:
if old.RealityManifestSHA != "" {
return fmt.Errorf("Two reality manifests for node %s pod %s", nodeName, podId)
}
old.RealityManifestSHA = manifestSHA
for launchableID, launchable := range result.Manifest.GetLaunchableStanzas() {
var version launch.LaunchableVersion
if launchable.Version.ID != "" {
version = launchable.Version
}
old.RealityVersions[launchableID] = LaunchableVersion{
Location: launchable.Location,
Version: version,
}
old.RealityLocations = append(old.RealityLocations, launchable.Location)
}
sort.Strings(old.RealityLocations)
}
statuses[podId][nodeName] = old
return nil
}