This repository was archived by the owner on Jul 10, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathresources.go
More file actions
162 lines (139 loc) · 4.68 KB
/
Copy pathresources.go
File metadata and controls
162 lines (139 loc) · 4.68 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
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
package rep
import (
"archive/tar"
"bytes"
"fmt"
"io"
"net/url"
"os"
"strings"
bbsmodels "code.cloudfoundry.org/bbs/models"
"code.cloudfoundry.org/executor/containermetrics"
"code.cloudfoundry.org/routing-info/internalroutes"
)
// Scheduling and placement types have moved to code.cloudfoundry.org/bbs/models.
// These aliases maintain backward compatibility for existing callers.
var ErrorIncompatibleRootfs = bbsmodels.ErrorIncompatibleRootfs
const StackVersionFile = bbsmodels.StackVersionFile
type Resource = bbsmodels.Resource
type Resources = bbsmodels.Resources
type PlacementConstraint = bbsmodels.PlacementConstraint
type LRP = bbsmodels.SchedulingLRP
type LRPUpdate = bbsmodels.LRPUpdate
type Task = bbsmodels.SchedulingTask
type Work = bbsmodels.Work
type CellState = bbsmodels.CellState
type InsufficientResourcesError = bbsmodels.InsufficientResourcesError
type InternalRoute = bbsmodels.InternalRoute
type InternalRoutes = bbsmodels.InternalRoutes
var NewResource = bbsmodels.NewResource
var NewResources = bbsmodels.NewResources
var NewPlacementConstraint = bbsmodels.NewPlacementConstraint
var NewLRP = bbsmodels.NewSchedulingLRP
var NewLRPUpdate = bbsmodels.NewLRPUpdate
var NewTask = bbsmodels.NewSchedulingTask
var NewCellState = bbsmodels.NewCellState
// InternalRoutesToInternalRoutes converts bbs/models.InternalRoutes to routing-info.InternalRoutes.
// This conversion is necessary because both types have the same structure but are distinct types in Go.
func InternalRoutesToInternalRoutes(routes InternalRoutes) internalroutes.InternalRoutes {
if routes == nil {
return nil
}
converted := make(internalroutes.InternalRoutes, len(routes))
for i, route := range routes {
converted[i] = internalroutes.InternalRoute{Hostname: route.Hostname}
}
return converted
}
// StackPathMap maps aliases to rootFS paths on the system.
type StackPathMap map[string]string
// ErrPreloadedRootFSNotFound is returned when the given hostname of the
// rootFS could not be resolved if the scheme is the PreloadedRootFSScheme
// or the PreloadedOCIRootFSScheme.
var ErrPreloadedRootFSNotFound = fmt.Errorf("preloaded rootfs path not found")
// PathForRootFS resolves the hostname portion of the RootFS URL to the actual
// path to the preloaded rootFS on the system according to the StackPathMap.
func (m StackPathMap) PathForRootFS(rootFS string) (string, error) {
if rootFS == "" {
return rootFS, nil
}
u, err := url.Parse(rootFS)
if err != nil {
return "", err
}
if u.Scheme == bbsmodels.PreloadedRootFSScheme {
path, ok := m[u.Opaque]
if !ok {
return "", ErrPreloadedRootFSNotFound
}
return path, nil
} else if u.Scheme == bbsmodels.PreloadedOCIRootFSScheme {
path, ok := m[u.Opaque]
if !ok {
return "", ErrPreloadedRootFSNotFound
}
return fmt.Sprintf("%s:%s?%s", u.Scheme, path, u.RawQuery), nil
}
return rootFS, nil
}
func (m StackPathMap) StackVersionList() []string {
type result struct{ name, version string }
ch := make(chan result, len(m))
for name, path := range m {
go func(name, path string) {
ch <- result{name, loadVersionFromPath(path)}
}(name, path)
}
stackVersions := make([]string, 0, len(m))
for range m {
r := <-ch
if r.version != "" {
stackVersions = append(stackVersions, fmt.Sprintf("%s@%s", r.name, r.version))
} else {
stackVersions = append(stackVersions, r.name)
}
}
return stackVersions
}
//go:generate counterfeiter -o auctioncellrep/auctioncellrepfakes/fake_container_metrics_provider.go . ContainerMetricsProvider
type ContainerMetricsProvider interface {
Metrics() map[string]*containermetrics.CachedContainerMetrics
}
type ContainerMetricsCollection struct {
CellID string `json:"cell_id"`
LRPs []LRPMetric `json:"lrps"`
Tasks []TaskMetric `json:"tasks"`
}
type LRPMetric struct {
InstanceGUID string `json:"instance_guid"`
ProcessGUID string `json:"process_guid"`
Index int32 `json:"index"`
containermetrics.CachedContainerMetrics
}
type TaskMetric struct {
TaskGUID string `json:"task_guid"`
containermetrics.CachedContainerMetrics
}
func loadVersionFromPath(path string) string {
file, err := os.Open(path)
if err != nil {
return ""
}
defer file.Close()
tarReader := tar.NewReader(file)
target := strings.TrimLeft(StackVersionFile, "./")
for {
header, err := tarReader.Next()
if err != nil {
break
}
if strings.TrimLeft(header.Name, "./") == target {
buf := new(bytes.Buffer)
if _, err := io.Copy(buf, tarReader); err != nil {
return ""
}
return strings.TrimSpace(buf.String())
}
}
return ""
}