forked from vmware-archive/atc
/
cache_identifier.go
130 lines (106 loc) · 3.21 KB
/
cache_identifier.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package resource
import (
"crypto/sha512"
"encoding/json"
"fmt"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc"
"github.com/concourse/atc/db"
"github.com/concourse/atc/worker"
)
const reapExtraVolumeTTL = time.Minute
//go:generate counterfeiter . CacheIdentifier
type CacheIdentifier interface {
FindOn(lager.Logger, worker.Client) (worker.Volume, bool, error)
CreateOn(lager.Logger, worker.Client) (worker.Volume, error)
VolumeIdentifier() worker.VolumeIdentifier
}
type ResourceCacheIdentifier struct {
Type ResourceType
Version atc.Version
Source atc.Source
Params atc.Params
}
func (identifier ResourceCacheIdentifier) FindOn(logger lager.Logger, workerClient worker.Client) (worker.Volume, bool, error) {
volumes, err := workerClient.ListVolumes(logger, identifier.initializedVolumeProperties())
if err != nil {
logger.Error("failed-to-list-volumes", err)
return nil, false, err
}
switch len(volumes) {
case 0:
logger.Debug("no-volumes-found")
return nil, false, nil
case 1:
return volumes[0], true, nil
default:
return selectLowestAlphabeticalVolume(logger, volumes), true, nil
}
}
func (identifier ResourceCacheIdentifier) CreateOn(logger lager.Logger, workerClient worker.Client) (worker.Volume, error) {
ttl := time.Duration(0)
if identifier.Version == nil {
ttl = worker.VolumeTTL
}
return workerClient.CreateVolume(
logger,
worker.VolumeSpec{
Strategy: worker.ResourceCacheStrategy{
ResourceVersion: identifier.Version,
ResourceHash: GenerateResourceHash(identifier.Source, string(identifier.Type)),
},
Properties: identifier.volumeProperties(),
Privileged: true,
TTL: ttl,
},
0,
)
}
func (identifier ResourceCacheIdentifier) volumeProperties() worker.VolumeProperties {
source, _ := json.Marshal(identifier.Source)
version, _ := json.Marshal(identifier.Version)
params, _ := json.Marshal(identifier.Params)
return worker.VolumeProperties{
"resource-type": string(identifier.Type),
"resource-version": string(version),
"resource-source": shastr(source),
"resource-params": shastr(params),
}
}
func (identifier ResourceCacheIdentifier) initializedVolumeProperties() worker.VolumeProperties {
props := identifier.volumeProperties()
props["initialized"] = "yep"
return props
}
func (identifier ResourceCacheIdentifier) VolumeIdentifier() worker.VolumeIdentifier {
return worker.VolumeIdentifier{
ResourceCache: &db.ResourceCacheIdentifier{
ResourceVersion: identifier.Version,
ResourceHash: GenerateResourceHash(identifier.Source, string(identifier.Type)),
},
}
}
func GenerateResourceHash(source atc.Source, resourceType string) string {
sourceJSON, _ := json.Marshal(source)
return resourceType + string(sourceJSON)
}
func shastr(b []byte) string {
return fmt.Sprintf("%x", sha512.Sum512(b))
}
func selectLowestAlphabeticalVolume(logger lager.Logger, volumes []worker.Volume) worker.Volume {
var lowestVolume worker.Volume
for _, v := range volumes {
if lowestVolume == nil {
lowestVolume = v
} else if v.Handle() < lowestVolume.Handle() {
lowestVolume = v
}
}
for _, v := range volumes {
if v != lowestVolume {
v.Release(worker.FinalTTL(reapExtraVolumeTTL))
}
}
return lowestVolume
}