/
publisher.go
142 lines (118 loc) · 4.89 KB
/
publisher.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
136
137
138
139
140
141
142
/*
Copyright 2021 Dynatrace LLC.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package hostvolumes
import (
"context"
"fmt"
"os"
"time"
csivolumes "github.com/Dynatrace/dynatrace-operator/pkg/controllers/csi/driver/volumes"
"github.com/Dynatrace/dynatrace-operator/pkg/controllers/csi/metadata"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/spf13/afero"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/utils/mount"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func NewHostVolumePublisher(client client.Client, fs afero.Afero, mounter mount.Interface, db metadata.Access, path metadata.PathResolver) csivolumes.Publisher { //nolint:revive // argument-limit doesn't apply to constructors
return &HostVolumePublisher{
client: client,
fs: fs,
mounter: mounter,
db: db,
path: path,
}
}
type HostVolumePublisher struct {
client client.Client
fs afero.Afero
mounter mount.Interface
db metadata.Access
path metadata.PathResolver
}
func (publisher *HostVolumePublisher) PublishVolume(ctx context.Context, volumeCfg *csivolumes.VolumeConfig) (*csi.NodePublishVolumeResponse, error) {
bindCfg, err := csivolumes.NewBindConfig(ctx, publisher.db, volumeCfg)
if err != nil {
return nil, err
}
if err := publisher.mountOneAgent(bindCfg.TenantUUID, volumeCfg); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to mount osagent volume: %s", err.Error()))
}
volume, err := publisher.db.GetOsAgentVolumeViaTenantUUID(ctx, bindCfg.TenantUUID)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to get osagent volume info from database: %s", err.Error()))
}
timestamp := time.Now()
if volume == nil {
storage := metadata.OsAgentVolume{
VolumeID: volumeCfg.VolumeID,
TenantUUID: bindCfg.TenantUUID,
Mounted: true,
LastModified: ×tamp,
}
if err := publisher.db.InsertOsAgentVolume(ctx, &storage); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to insert osagent volume info to database. info: %v err: %s", storage, err.Error()))
}
} else {
volume.VolumeID = volumeCfg.VolumeID
volume.Mounted = true
volume.LastModified = ×tamp
if err := publisher.db.UpdateOsAgentVolume(ctx, volume); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to update osagent volume info to database. info: %v err: %s", volume, err.Error()))
}
}
return &csi.NodePublishVolumeResponse{}, nil
}
func (publisher *HostVolumePublisher) UnpublishVolume(ctx context.Context, volumeInfo *csivolumes.VolumeInfo) (*csi.NodeUnpublishVolumeResponse, error) {
volume, err := publisher.db.GetOsAgentVolumeViaVolumeID(ctx, volumeInfo.VolumeID)
if err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to get osagent volume info from database: %s", err.Error()))
}
if volume == nil {
return nil, nil
}
publisher.umountOneAgent(volumeInfo.TargetPath)
timestamp := time.Now()
volume.Mounted = false
volume.LastModified = ×tamp
if err := publisher.db.UpdateOsAgentVolume(ctx, volume); err != nil {
return nil, status.Error(codes.Internal, fmt.Sprintf("failed to update osagent volume info to database. info: %v err: %s", volume, err.Error()))
}
log.Info("osagent volume has been unpublished", "targetPath", volumeInfo.TargetPath)
return &csi.NodeUnpublishVolumeResponse{}, nil
}
func (publisher *HostVolumePublisher) CanUnpublishVolume(ctx context.Context, volumeInfo *csivolumes.VolumeInfo) (bool, error) {
volume, err := publisher.db.GetOsAgentVolumeViaVolumeID(ctx, volumeInfo.VolumeID)
if err != nil {
return false, status.Error(codes.Internal, fmt.Sprintf("failed to get osagent volume info from database: %s", err.Error()))
}
return volume != nil, nil
}
func (publisher *HostVolumePublisher) mountOneAgent(tenantUUID string, volumeCfg *csivolumes.VolumeConfig) error {
hostDir := publisher.path.OsAgentDir(tenantUUID)
_ = publisher.fs.MkdirAll(hostDir, os.ModePerm)
if err := publisher.fs.MkdirAll(volumeCfg.TargetPath, os.ModePerm); err != nil {
return err
}
if err := publisher.mounter.Mount(hostDir, volumeCfg.TargetPath, "", []string{"bind"}); err != nil {
_ = publisher.mounter.Unmount(hostDir)
return err
}
return nil
}
func (publisher *HostVolumePublisher) umountOneAgent(targetPath string) {
if err := publisher.mounter.Unmount(targetPath); err != nil {
log.Error(err, "Unmount failed", "path", targetPath)
}
}