Skip to content

Commit 6a7e437

Browse files
authored
add pkg storage: for detect storageclass name (#3427)
1 parent ab72d9d commit 6a7e437

File tree

3 files changed

+300
-0
lines changed

3 files changed

+300
-0
lines changed

apistructs/service.go

+71
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,44 @@ type ServicePort struct {
218218

219219
const (
220220
ServiceDiscoveryKindProxy = "PROXY"
221+
222+
// CSI Vendor
223+
CSIVendorAlibaba = "AliCloud"
224+
CSIVendorTencent = "TecentCloud"
225+
CSIVendorHuawei = "HuaweiCloud"
226+
// 对应磁盘类型为 SSD 磁盘
227+
VolumeTypeSSD = "SSD"
228+
// 对应磁盘类型为 NAS (NFS)磁盘
229+
VolumeTypeNAS = "NAS"
230+
// 对应磁盘类型为 OSS 磁盘
231+
VolumeTypeOSS = "OSS"
232+
// 对应磁盘类型为 DICE NAS (DICE NFS)磁盘
233+
VolumeTypeDiceNAS = "DICE-NAS"
234+
// 对应磁盘类型为 DICE LOCAL (DICE LOCAL)磁盘
235+
VolumeTypeDiceLOCAL = "DICE-LOCAL"
236+
// 阿里云 SSD 云盘 storageclass 名称
237+
AlibabaSSDSC = "alicloud-disk-ssd-on-erda"
238+
// 阿里云 NAS 网盘的 storageclass 名称
239+
AlibabaNASSC = "alicloud-nas-subpath-on-erda"
240+
// 阿里云 OSS 网盘的 storageclass 名称
241+
AlibabaOSSSC = "alicloud-nas-oss-on-erda"
242+
// 腾讯云 SSD 云盘 storageclass 名称
243+
TencentSSDSC = "tencentcloud-disk-ssd-on-erda"
244+
// 腾讯云 NAS 网盘的 storageclass 名称
245+
TencentNASSC = "tencentcloud-nas-subpath-on-erda"
246+
// 腾讯云 OSS 网盘的 storageclass 名称
247+
TencentOSSSC = "tencentcloud-nas-oss-on-erda"
248+
// 华为云 SSD 云盘 storageclass 名称
249+
HuaweiSSDSC = "huaweicloud-disk-ssd-on-erda"
250+
// 华为云 NAS 网盘的 storageclass 名称
251+
HuaweiNASSC = "huaweicloud-nas-subpath-on-erda"
252+
// 华为云 OSS 网盘的 storageclass 名称
253+
HuaweiOSSSC = "huaweicloud-nas-oss-on-erda"
254+
255+
DiceLocalVolumeSC = "dice-local-volume"
256+
DiceNFSVolumeSC = "dice-nfs-volume"
257+
258+
CSISnapshotMaxHistory = "pvc.erda.io/snapshot"
221259
)
222260

223261
// One single Service which is the minimum scheduling unit
@@ -326,6 +364,8 @@ type ServiceBind struct {
326364
Bind
327365
// TODO: refactor it, currently just copy the marathon struct
328366
Persistent *PersistentVolume `json:"persistent,omitempty"`
367+
368+
SCVolume SCVolume `json:"scVolume,omitempty"`
329369
}
330370

331371
type PersistentVolume struct {
@@ -376,6 +416,37 @@ type Volume struct {
376416
// TODO: k8s.go 需要这个字段,现在对于k8s先不使用其插件中实现的volume相关实现(现在也没有用的地方)
377417
// k8s plugin 重构的时候才去实现 k8s 特定的 volume 逻辑
378418
Storage string `json:"-"`
419+
420+
SCVolume `json:"scVolume,omitempty"`
421+
}
422+
423+
type SCVolume struct {
424+
// Type is the type of volume, it will be supported DICE-NAS, DICE-LOCAL, SSD, NAS, OSS...
425+
// only support DICE-NAS, DICE-LOCAL, SSD currently
426+
Type string `yaml:"type,omitempty" json:"type,omitempty"`
427+
// StorageClassName is the k8s storageclass object which this volume used to create pvc
428+
StorageClassName string `yaml:"storageClassName,omitempty" json:"storageClassName,omitempty"`
429+
// Capacity is the capacity of volume and the default unit is 'GB'
430+
Capacity int32 `yaml:"size" json:"size,omitempty"`
431+
// SourcePath is the volume source path that is used in the local PV or host path
432+
// Default is empty
433+
//SourcePath string `yaml:"sourcePath,omitempty" json:"sourcePath,omitempty"`
434+
// TargetPath indicates will mount the file or directory in the volume to the
435+
// specified location of the container. Default is '/'
436+
TargetPath string `yaml:"targetPath,omitempty" json:"targetPath,omitempty"`
437+
// ReadOnly set the file in the volume allow to be read-only
438+
// Default is false
439+
ReadOnly bool `yaml:"readOnly,omitempty" json:"readOnly,omitempty"`
440+
// Snapshot indicates use can create snapshots of this volume
441+
// if Snapshot field isn't null and the default time interval is 3600 second
442+
// Note: Now, only for Alibaba disk ssd storageclass
443+
Snapshot *VolumeSnapshot `yaml:"snapshot,omitempty" json:"snapshot,omitempty"`
444+
}
445+
446+
type VolumeSnapshot struct {
447+
// MaxHistory indicates the max count of the snapshot can be created
448+
// if the number of snapshots is beyond the max, the earliest one will be deleted
449+
MaxHistory int32 `yaml:"maxHistory,omitempty" json:"maxHistory,omitempty"`
379450
}
380451

381452
type InstanceInfo struct {

pkg/k8s/storage/detect_sc.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) 2021 Terminus, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package storage
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/pkg/errors"
21+
22+
"github.com/erda-project/erda/apistructs"
23+
)
24+
25+
func VolumeTypeToSCName(diskType string, vendor string) (string, error) {
26+
// 卷类型以 'DICE-' 作为前缀的,表示使用 dice volume 插件,与 vender 无关
27+
switch diskType {
28+
case apistructs.VolumeTypeDiceNAS:
29+
// volume.type == "DICE-NAS" SC="dice-nfs-volume"
30+
return apistructs.DiceNFSVolumeSC, nil
31+
32+
case apistructs.VolumeTypeDiceLOCAL:
33+
// volume.type == "DICE-LOCAL" SC="dice-local-volume"
34+
return apistructs.DiceLocalVolumeSC, nil
35+
36+
// 卷类型不以 'DICE-' 作为前缀的,表示与 vender 有关,结合 vendor 选择 sc
37+
// 'SSD'
38+
case apistructs.VolumeTypeSSD:
39+
switch vendor {
40+
case apistructs.CSIVendorAlibaba:
41+
// vendor = "AliCloud" volume.type="SSD" SC="alicloud-disk-ssd-on-erda"
42+
return apistructs.AlibabaSSDSC, nil
43+
44+
case apistructs.CSIVendorTencent:
45+
// vendor = "TencentCloud" volume.type="SSD" SC="tencent-disk-ssd-on-erda"
46+
return apistructs.TencentSSDSC, nil
47+
48+
case apistructs.CSIVendorHuawei:
49+
// vendor = "HuaweiCloud" volume.type="SSD" SC="huawei-disk-ssd-on-erda"
50+
return apistructs.HuaweiSSDSC, nil
51+
}
52+
// 'NAS'
53+
case apistructs.VolumeTypeNAS:
54+
switch vendor {
55+
case apistructs.CSIVendorAlibaba:
56+
// vendor = "AliCloud" volume.type="SSD" SC="alicloud-disk-ssd-on-erda"
57+
return apistructs.AlibabaNASSC, nil
58+
59+
case apistructs.CSIVendorTencent:
60+
// vendor = "TencentCloud" volume.type="SSD" SC="tencent-disk-ssd-on-erda"
61+
return apistructs.TencentNASSC, nil
62+
63+
case apistructs.CSIVendorHuawei:
64+
// vendor = "HuaweiCloud" volume.type="SSD" SC="huawei-disk-ssd-on-erda"
65+
return apistructs.HuaweiNASSC, nil
66+
}
67+
/*
68+
// 'OSS'
69+
case apistructs.VolumeTypeOSS:
70+
switch vendor {
71+
case apistructs.CSIVendorAlibaba:
72+
// vendor = "AliCloud" volume.type="SSD" SC="alicloud-disk-ssd-on-erda"
73+
return apistructs.AlibabaOSSSC, nil
74+
75+
case apistructs.CSIVendorTencent:
76+
// vendor = "TencentCloud" volume.type="SSD" SC="tencent-disk-ssd-on-erda"
77+
return apistructs.TencentOSSSC, nil
78+
79+
case apistructs.CSIVendorHuawei:
80+
// vendor = "HuaweiCloud" volume.type="SSD" SC="huawei-disk-ssd-on-erda"
81+
return apistructs.HuaweiOSSSC, nil
82+
}
83+
*/
84+
case "":
85+
return apistructs.DiceLocalVolumeSC, nil
86+
//case apistructs.VolumeTypeOSS:
87+
default:
88+
return "", errors.New(fmt.Sprintf("unsupported disk type %s", diskType))
89+
}
90+
91+
return "", errors.New(fmt.Sprintf("can not detect storageclass for disktype [%s] vendor [%s] ", diskType, vendor))
92+
}

pkg/k8s/storage/detect_sc_test.go

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright (c) 2021 Terminus, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package storage
16+
17+
import (
18+
"testing"
19+
20+
"github.com/erda-project/erda/apistructs"
21+
)
22+
23+
func TestVolumeTypeToSCName(t *testing.T) {
24+
type args struct {
25+
diskType string
26+
vendor string
27+
}
28+
tests := []struct {
29+
name string
30+
args args
31+
want string
32+
wantErr bool
33+
}{
34+
// TODO: Add test cases.
35+
{
36+
name: "test01",
37+
args: args{
38+
vendor: apistructs.CSIVendorAlibaba,
39+
},
40+
want: apistructs.DiceLocalVolumeSC,
41+
wantErr: false,
42+
},
43+
{
44+
name: "test02",
45+
args: args{
46+
diskType: "DICE-LOCAL",
47+
vendor: apistructs.CSIVendorAlibaba,
48+
},
49+
want: apistructs.DiceLocalVolumeSC,
50+
wantErr: false,
51+
},
52+
{
53+
name: "test03",
54+
args: args{
55+
diskType: "DICE-NAS",
56+
vendor: apistructs.CSIVendorAlibaba,
57+
},
58+
want: apistructs.DiceNFSVolumeSC,
59+
wantErr: false,
60+
},
61+
{
62+
name: "test04",
63+
args: args{
64+
diskType: apistructs.VolumeTypeSSD,
65+
vendor: apistructs.CSIVendorAlibaba,
66+
},
67+
want: apistructs.AlibabaSSDSC,
68+
wantErr: false,
69+
},
70+
{
71+
name: "test05",
72+
args: args{
73+
diskType: apistructs.VolumeTypeNAS,
74+
vendor: apistructs.CSIVendorAlibaba,
75+
},
76+
want: apistructs.AlibabaNASSC,
77+
wantErr: false,
78+
},
79+
{
80+
name: "test06",
81+
args: args{
82+
diskType: apistructs.VolumeTypeSSD,
83+
vendor: apistructs.CSIVendorTencent,
84+
},
85+
want: apistructs.TencentSSDSC,
86+
wantErr: false,
87+
},
88+
{
89+
name: "test07",
90+
args: args{
91+
diskType: apistructs.VolumeTypeNAS,
92+
vendor: apistructs.CSIVendorTencent,
93+
},
94+
want: apistructs.TencentNASSC,
95+
wantErr: false,
96+
},
97+
{
98+
name: "test08",
99+
args: args{
100+
diskType: apistructs.VolumeTypeSSD,
101+
vendor: apistructs.CSIVendorHuawei,
102+
},
103+
want: apistructs.HuaweiSSDSC,
104+
wantErr: false,
105+
},
106+
{
107+
name: "test09",
108+
args: args{
109+
diskType: apistructs.VolumeTypeNAS,
110+
vendor: apistructs.CSIVendorHuawei,
111+
},
112+
want: apistructs.HuaweiNASSC,
113+
wantErr: false,
114+
},
115+
{
116+
name: "test10",
117+
args: args{
118+
diskType: "XXXX",
119+
vendor: apistructs.CSIVendorAlibaba,
120+
},
121+
want: "",
122+
wantErr: true,
123+
},
124+
}
125+
for _, tt := range tests {
126+
t.Run(tt.name, func(t *testing.T) {
127+
got, err := VolumeTypeToSCName(tt.args.diskType, tt.args.vendor)
128+
if (err != nil) != tt.wantErr {
129+
t.Errorf("VolumeTypeToSCName() error = %v, wantErr %v", err, tt.wantErr)
130+
return
131+
}
132+
if got != tt.want {
133+
t.Errorf("VolumeTypeToSCName() got = %v, want %v", got, tt.want)
134+
}
135+
})
136+
}
137+
}

0 commit comments

Comments
 (0)