Skip to content

Commit

Permalink
add pkg storage: for detect storageclass name (erda-project#3427)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzhuzhen authored and erda-bot committed Dec 24, 2021
1 parent 40ac137 commit a494e29
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 0 deletions.
71 changes: 71 additions & 0 deletions apistructs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,44 @@ type ServicePort struct {

const (
ServiceDiscoveryKindProxy = "PROXY"

// CSI Vendor
CSIVendorAlibaba = "AliCloud"
CSIVendorTencent = "TecentCloud"
CSIVendorHuawei = "HuaweiCloud"
// 对应磁盘类型为 SSD 磁盘
VolumeTypeSSD = "SSD"
// 对应磁盘类型为 NAS (NFS)磁盘
VolumeTypeNAS = "NAS"
// 对应磁盘类型为 OSS 磁盘
VolumeTypeOSS = "OSS"
// 对应磁盘类型为 DICE NAS (DICE NFS)磁盘
VolumeTypeDiceNAS = "DICE-NAS"
// 对应磁盘类型为 DICE LOCAL (DICE LOCAL)磁盘
VolumeTypeDiceLOCAL = "DICE-LOCAL"
// 阿里云 SSD 云盘 storageclass 名称
AlibabaSSDSC = "alicloud-disk-ssd-on-erda"
// 阿里云 NAS 网盘的 storageclass 名称
AlibabaNASSC = "alicloud-nas-subpath-on-erda"
// 阿里云 OSS 网盘的 storageclass 名称
AlibabaOSSSC = "alicloud-nas-oss-on-erda"
// 腾讯云 SSD 云盘 storageclass 名称
TencentSSDSC = "tencentcloud-disk-ssd-on-erda"
// 腾讯云 NAS 网盘的 storageclass 名称
TencentNASSC = "tencentcloud-nas-subpath-on-erda"
// 腾讯云 OSS 网盘的 storageclass 名称
TencentOSSSC = "tencentcloud-nas-oss-on-erda"
// 华为云 SSD 云盘 storageclass 名称
HuaweiSSDSC = "huaweicloud-disk-ssd-on-erda"
// 华为云 NAS 网盘的 storageclass 名称
HuaweiNASSC = "huaweicloud-nas-subpath-on-erda"
// 华为云 OSS 网盘的 storageclass 名称
HuaweiOSSSC = "huaweicloud-nas-oss-on-erda"

DiceLocalVolumeSC = "dice-local-volume"
DiceNFSVolumeSC = "dice-nfs-volume"

CSISnapshotMaxHistory = "pvc.erda.io/snapshot"
)

// One single Service which is the minimum scheduling unit
Expand Down Expand Up @@ -326,6 +364,8 @@ type ServiceBind struct {
Bind
// TODO: refactor it, currently just copy the marathon struct
Persistent *PersistentVolume `json:"persistent,omitempty"`

SCVolume SCVolume `json:"scVolume,omitempty"`
}

type PersistentVolume struct {
Expand Down Expand Up @@ -376,6 +416,37 @@ type Volume struct {
// TODO: k8s.go 需要这个字段,现在对于k8s先不使用其插件中实现的volume相关实现(现在也没有用的地方)
// k8s plugin 重构的时候才去实现 k8s 特定的 volume 逻辑
Storage string `json:"-"`

SCVolume `json:"scVolume,omitempty"`
}

type SCVolume struct {
// Type is the type of volume, it will be supported DICE-NAS, DICE-LOCAL, SSD, NAS, OSS...
// only support DICE-NAS, DICE-LOCAL, SSD currently
Type string `yaml:"type,omitempty" json:"type,omitempty"`
// StorageClassName is the k8s storageclass object which this volume used to create pvc
StorageClassName string `yaml:"storageClassName,omitempty" json:"storageClassName,omitempty"`
// Capacity is the capacity of volume and the default unit is 'GB'
Capacity int32 `yaml:"size" json:"size,omitempty"`
// SourcePath is the volume source path that is used in the local PV or host path
// Default is empty
//SourcePath string `yaml:"sourcePath,omitempty" json:"sourcePath,omitempty"`
// TargetPath indicates will mount the file or directory in the volume to the
// specified location of the container. Default is '/'
TargetPath string `yaml:"targetPath,omitempty" json:"targetPath,omitempty"`
// ReadOnly set the file in the volume allow to be read-only
// Default is false
ReadOnly bool `yaml:"readOnly,omitempty" json:"readOnly,omitempty"`
// Snapshot indicates use can create snapshots of this volume
// if Snapshot field isn't null and the default time interval is 3600 second
// Note: Now, only for Alibaba disk ssd storageclass
Snapshot *VolumeSnapshot `yaml:"snapshot,omitempty" json:"snapshot,omitempty"`
}

type VolumeSnapshot struct {
// MaxHistory indicates the max count of the snapshot can be created
// if the number of snapshots is beyond the max, the earliest one will be deleted
MaxHistory int32 `yaml:"maxHistory,omitempty" json:"maxHistory,omitempty"`
}

type InstanceInfo struct {
Expand Down
92 changes: 92 additions & 0 deletions pkg/k8s/storage/detect_sc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright (c) 2021 Terminus, Inc.
//
// 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 storage

import (
"fmt"

"github.com/pkg/errors"

"github.com/erda-project/erda/apistructs"
)

func VolumeTypeToSCName(diskType string, vendor string) (string, error) {
// 卷类型以 'DICE-' 作为前缀的,表示使用 dice volume 插件,与 vender 无关
switch diskType {
case apistructs.VolumeTypeDiceNAS:
// volume.type == "DICE-NAS" SC="dice-nfs-volume"
return apistructs.DiceNFSVolumeSC, nil

case apistructs.VolumeTypeDiceLOCAL:
// volume.type == "DICE-LOCAL" SC="dice-local-volume"
return apistructs.DiceLocalVolumeSC, nil

// 卷类型不以 'DICE-' 作为前缀的,表示与 vender 有关,结合 vendor 选择 sc
// 'SSD'
case apistructs.VolumeTypeSSD:
switch vendor {
case apistructs.CSIVendorAlibaba:
// vendor = "AliCloud" volume.type="SSD" SC="alicloud-disk-ssd-on-erda"
return apistructs.AlibabaSSDSC, nil

case apistructs.CSIVendorTencent:
// vendor = "TencentCloud" volume.type="SSD" SC="tencent-disk-ssd-on-erda"
return apistructs.TencentSSDSC, nil

case apistructs.CSIVendorHuawei:
// vendor = "HuaweiCloud" volume.type="SSD" SC="huawei-disk-ssd-on-erda"
return apistructs.HuaweiSSDSC, nil
}
// 'NAS'
case apistructs.VolumeTypeNAS:
switch vendor {
case apistructs.CSIVendorAlibaba:
// vendor = "AliCloud" volume.type="SSD" SC="alicloud-disk-ssd-on-erda"
return apistructs.AlibabaNASSC, nil

case apistructs.CSIVendorTencent:
// vendor = "TencentCloud" volume.type="SSD" SC="tencent-disk-ssd-on-erda"
return apistructs.TencentNASSC, nil

case apistructs.CSIVendorHuawei:
// vendor = "HuaweiCloud" volume.type="SSD" SC="huawei-disk-ssd-on-erda"
return apistructs.HuaweiNASSC, nil
}
/*
// 'OSS'
case apistructs.VolumeTypeOSS:
switch vendor {
case apistructs.CSIVendorAlibaba:
// vendor = "AliCloud" volume.type="SSD" SC="alicloud-disk-ssd-on-erda"
return apistructs.AlibabaOSSSC, nil
case apistructs.CSIVendorTencent:
// vendor = "TencentCloud" volume.type="SSD" SC="tencent-disk-ssd-on-erda"
return apistructs.TencentOSSSC, nil
case apistructs.CSIVendorHuawei:
// vendor = "HuaweiCloud" volume.type="SSD" SC="huawei-disk-ssd-on-erda"
return apistructs.HuaweiOSSSC, nil
}
*/
case "":
return apistructs.DiceLocalVolumeSC, nil
//case apistructs.VolumeTypeOSS:
default:
return "", errors.New(fmt.Sprintf("unsupported disk type %s", diskType))
}

return "", errors.New(fmt.Sprintf("can not detect storageclass for disktype [%s] vendor [%s] ", diskType, vendor))
}
137 changes: 137 additions & 0 deletions pkg/k8s/storage/detect_sc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright (c) 2021 Terminus, Inc.
//
// 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 storage

import (
"testing"

"github.com/erda-project/erda/apistructs"
)

func TestVolumeTypeToSCName(t *testing.T) {
type args struct {
diskType string
vendor string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
// TODO: Add test cases.
{
name: "test01",
args: args{
vendor: apistructs.CSIVendorAlibaba,
},
want: apistructs.DiceLocalVolumeSC,
wantErr: false,
},
{
name: "test02",
args: args{
diskType: "DICE-LOCAL",
vendor: apistructs.CSIVendorAlibaba,
},
want: apistructs.DiceLocalVolumeSC,
wantErr: false,
},
{
name: "test03",
args: args{
diskType: "DICE-NAS",
vendor: apistructs.CSIVendorAlibaba,
},
want: apistructs.DiceNFSVolumeSC,
wantErr: false,
},
{
name: "test04",
args: args{
diskType: apistructs.VolumeTypeSSD,
vendor: apistructs.CSIVendorAlibaba,
},
want: apistructs.AlibabaSSDSC,
wantErr: false,
},
{
name: "test05",
args: args{
diskType: apistructs.VolumeTypeNAS,
vendor: apistructs.CSIVendorAlibaba,
},
want: apistructs.AlibabaNASSC,
wantErr: false,
},
{
name: "test06",
args: args{
diskType: apistructs.VolumeTypeSSD,
vendor: apistructs.CSIVendorTencent,
},
want: apistructs.TencentSSDSC,
wantErr: false,
},
{
name: "test07",
args: args{
diskType: apistructs.VolumeTypeNAS,
vendor: apistructs.CSIVendorTencent,
},
want: apistructs.TencentNASSC,
wantErr: false,
},
{
name: "test08",
args: args{
diskType: apistructs.VolumeTypeSSD,
vendor: apistructs.CSIVendorHuawei,
},
want: apistructs.HuaweiSSDSC,
wantErr: false,
},
{
name: "test09",
args: args{
diskType: apistructs.VolumeTypeNAS,
vendor: apistructs.CSIVendorHuawei,
},
want: apistructs.HuaweiNASSC,
wantErr: false,
},
{
name: "test10",
args: args{
diskType: "XXXX",
vendor: apistructs.CSIVendorAlibaba,
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := VolumeTypeToSCName(tt.args.diskType, tt.args.vendor)
if (err != nil) != tt.wantErr {
t.Errorf("VolumeTypeToSCName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("VolumeTypeToSCName() got = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit a494e29

Please sign in to comment.