/
metadata.go
135 lines (118 loc) · 4.57 KB
/
metadata.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
131
132
133
134
135
package metadata
import (
"context"
"time"
)
// Dynakube stores the necessary info from the Dynakube that is needed to be used during volume mount/unmount.
type Dynakube struct {
Name string `json:"name"`
TenantUUID string `json:"tenantUUID"`
LatestVersion string `json:"latestVersion"`
ImageDigest string `json:"imageDigest"`
MaxFailedMountAttempts int `json:"maxFailedMountAttempts"`
}
// NewDynakube returns a new metadata.Dynakube if all fields are set.
func NewDynakube(dynakubeName, tenantUUID, latestVersion, imageDigest string, maxFailedMountAttempts int) *Dynakube { //nolint:revive // argument-limit doesn't apply to constructors
if tenantUUID == "" || dynakubeName == "" {
return nil
}
return &Dynakube{
Name: dynakubeName,
TenantUUID: tenantUUID,
LatestVersion: latestVersion,
ImageDigest: imageDigest,
MaxFailedMountAttempts: maxFailedMountAttempts,
}
}
type Volume struct {
VolumeID string `json:"volumeID"`
PodName string `json:"podName"`
Version string `json:"version"`
TenantUUID string `json:"tenantUUID"`
MountAttempts int `json:"mountAttempts"`
}
// NewVolume returns a new Volume if all fields (except version) are set.
func NewVolume(id, podName, version, tenantUUID string, mountAttempts int) *Volume { //nolint:revive // argument-limit doesn't apply to constructors
if id == "" || podName == "" || tenantUUID == "" {
return nil
}
if mountAttempts < 0 {
mountAttempts = 0
}
return &Volume{
VolumeID: id,
PodName: podName,
Version: version,
TenantUUID: tenantUUID,
MountAttempts: mountAttempts,
}
}
type OsAgentVolume struct {
VolumeID string `json:"volumeID"`
TenantUUID string `json:"tenantUUID"`
Mounted bool `json:"mounted"`
LastModified *time.Time `json:"lastModified"`
}
// NewOsAgentVolume returns a new volume if all fields are set.
func NewOsAgentVolume(volumeID, tenantUUID string, mounted bool, timeStamp *time.Time) *OsAgentVolume {
if volumeID == "" || tenantUUID == "" || timeStamp == nil {
return nil
}
return &OsAgentVolume{volumeID, tenantUUID, mounted, timeStamp}
}
type Access interface {
Setup(ctx context.Context, path string) error
InsertDynakube(ctx context.Context, dynakube *Dynakube) error
UpdateDynakube(ctx context.Context, dynakube *Dynakube) error
DeleteDynakube(ctx context.Context, dynakubeName string) error
GetDynakube(ctx context.Context, dynakubeName string) (*Dynakube, error)
GetTenantsToDynakubes(ctx context.Context) (map[string]string, error)
GetAllDynakubes(ctx context.Context) ([]*Dynakube, error)
InsertOsAgentVolume(ctx context.Context, volume *OsAgentVolume) error
GetOsAgentVolumeViaVolumeID(ctx context.Context, volumeID string) (*OsAgentVolume, error)
GetOsAgentVolumeViaTenantUUID(ctx context.Context, volumeID string) (*OsAgentVolume, error)
UpdateOsAgentVolume(ctx context.Context, volume *OsAgentVolume) error
GetAllOsAgentVolumes(ctx context.Context) ([]*OsAgentVolume, error)
InsertVolume(ctx context.Context, volume *Volume) error
DeleteVolume(ctx context.Context, volumeID string) error
GetVolume(ctx context.Context, volumeID string) (*Volume, error)
GetAllVolumes(ctx context.Context) ([]*Volume, error)
GetPodNames(ctx context.Context) (map[string]string, error)
GetUsedVersions(ctx context.Context, tenantUUID string) (map[string]bool, error)
GetAllUsedVersions(ctx context.Context) (map[string]bool, error)
GetLatestVersions(ctx context.Context) (map[string]bool, error)
GetUsedImageDigests(ctx context.Context) (map[string]bool, error)
IsImageDigestUsed(ctx context.Context, imageDigest string) (bool, error)
}
type AccessOverview struct {
Volumes []*Volume `json:"volumes"`
Dynakubes []*Dynakube `json:"dynakubes"`
OsAgentVolumes []*OsAgentVolume `json:"osAgentVolumes"`
}
func NewAccessOverview(access Access) (*AccessOverview, error) {
ctx := context.Background()
volumes, err := access.GetAllVolumes(ctx)
if err != nil {
return nil, err
}
dynakubes, err := access.GetAllDynakubes(ctx)
if err != nil {
return nil, err
}
osVolumes, err := access.GetAllOsAgentVolumes(ctx)
if err != nil {
return nil, err
}
return &AccessOverview{
Volumes: volumes,
Dynakubes: dynakubes,
OsAgentVolumes: osVolumes,
}, nil
}
func LogAccessOverview(access Access) {
overview, err := NewAccessOverview(access)
if err != nil {
log.Error(err, "Failed to get an overview of the stored csi metadata")
}
log.Info("The current overview of the csi metadata", "overview", overview)
}