Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Rainbond2
Commits
6480a7b6
Commit
6480a7b6
authored
6 years ago
by
GLYASAI
Browse files
Options
Download
Email Patches
Plain Diff
[REV] only share configmap volume template
parent
62bb1d6e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
db/dao/dao.go
+1
-1
db/dao/dao.go
db/mysql/dao/tenants.go
+4
-4
db/mysql/dao/tenants.go
db/volume_db_test.go
+11
-11
db/volume_db_test.go
worker/appm/conversion/version.go
+83
-86
worker/appm/conversion/version.go
with
99 additions
and
102 deletions
+99
-102
db/dao/dao.go
+
1
-
1
View file @
6480a7b6
...
...
@@ -221,7 +221,7 @@ type TenantServiceVolumeDao interface {
type
TenantServiceConfigFileDao
interface
{
Dao
Lis
tByVolume
ID
(
volumeName
string
)
(
[]
*
model
.
TenantServiceConfigFile
,
error
)
Ge
tByVolume
Name
(
volumeName
string
)
(
*
model
.
TenantServiceConfigFile
,
error
)
DelByVolumeID
(
volumeName
string
)
error
}
...
...
This diff is collapsed.
Click to expand it.
db/mysql/dao/tenants.go
+
4
-
4
View file @
6480a7b6
...
...
@@ -969,14 +969,14 @@ func (t *TenantServiceConfigFileDaoImpl) UpdateModel(mo model.Interface) error {
Update
(
configFile
)
.
Error
}
//
Lis
tByVolume
ID lists
config
F
ile
s
by volume
ID
func
(
t
*
TenantServiceConfigFileDaoImpl
)
Lis
tByVolume
ID
(
volumeName
string
)
(
[]
*
model
.
TenantServiceConfigFile
,
error
)
{
var
res
[]
*
model
.
TenantServiceConfigFile
//
Ge
tByVolume
Name get
config
f
ile by volume
name
func
(
t
*
TenantServiceConfigFileDaoImpl
)
Ge
tByVolume
Name
(
volumeName
string
)
(
*
model
.
TenantServiceConfigFile
,
error
)
{
var
res
model
.
TenantServiceConfigFile
if
err
:=
t
.
DB
.
Where
(
"volume_name = ?"
,
volumeName
)
.
Find
(
&
res
)
.
Error
;
err
!=
nil
{
return
nil
,
err
}
return
res
,
nil
return
&
res
,
nil
}
// DelByVolumeID deletes config files according to volume id
...
...
This diff is collapsed.
Click to expand it.
db/volume_db_test.go
+
11
-
11
View file @
6480a7b6
...
...
@@ -19,6 +19,7 @@
package
db
import
(
"github.com/jinzhu/gorm"
dbconfig
"github.com/goodrain/rainbond/db/config"
"github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util"
...
...
@@ -35,32 +36,31 @@ func TestManager_TenantServiceConfigFileDaoImpl_UpdateModel(t *testing.T) {
tx
.
Delete
(
model
.
TenantServiceConfigFile
{})
tx
.
Commit
()
v
id
:=
util
.
NewUUID
()
v
name
:=
util
.
NewUUID
()
cf
:=
&
model
.
TenantServiceConfigFile
{
UUID
:
util
.
NewUUID
(),
VolumeName
:
vid
,
Filename
:
"dummy_filename"
,
VolumeName
:
vname
,
FileContent
:
"dummy file content"
,
}
if
err
:=
GetManager
()
.
TenantServiceConfigFileDao
()
.
AddModel
(
cf
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
cf
s
,
err
:=
GetManager
()
.
TenantServiceConfigFileDao
()
.
Lis
tByVolume
ID
(
vid
)
cf
,
err
:=
GetManager
()
.
TenantServiceConfigFileDao
()
.
Ge
tByVolume
Name
(
vname
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
cf
s
==
nil
||
len
(
cfs
)
!=
1
{
t
.
Errorf
(
"Expected one config file, but returned %v"
,
cf
s
)
if
cf
==
nil
{
t
.
Errorf
(
"Expected one config file, but returned %v"
,
cf
)
}
if
err
:=
GetManager
()
.
TenantServiceConfigFileDao
()
.
DelByVolumeID
(
v
id
);
err
!=
nil
{
if
err
:=
GetManager
()
.
TenantServiceConfigFileDao
()
.
DelByVolumeID
(
v
name
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
cf
s
,
err
=
GetManager
()
.
TenantServiceConfigFileDao
()
.
Lis
tByVolume
ID
(
vid
)
if
err
!=
nil
{
cf
,
err
=
GetManager
()
.
TenantServiceConfigFileDao
()
.
Ge
tByVolume
Name
(
vname
)
if
err
!=
nil
&&
err
!=
gorm
.
ErrRecordNotFound
{
t
.
Fatal
(
err
)
}
if
len
(
cfs
)
!=
0
{
t
.
Errorf
(
"Expected nothing for cfs, but returned %v"
,
cf
s
)
if
cf
!=
nil
{
t
.
Errorf
(
"Expected nothing for cfs, but returned %v"
,
cf
)
}
}
This diff is collapsed.
Click to expand it.
worker/appm/conversion/version.go
+
83
-
86
View file @
6480a7b6
...
...
@@ -22,7 +22,6 @@ import (
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
...
...
@@ -324,7 +323,18 @@ func createVolumes(as *v1.AppService, version *dbmodel.VersionInfo, dbmanager db
if
err
!=
nil
{
return
nil
,
err
}
cmmap
:=
make
(
map
[
string
]
*
corev1
.
ConfigMap
)
// environment variables
configs
:=
make
(
map
[
string
]
string
)
envs
,
err
:=
createEnv
(
as
,
dbmanager
)
if
err
!=
nil
{
logrus
.
Warningf
(
"error creating environment variables: %v"
,
err
)
}
else
{
for
_
,
env
:=
range
*
envs
{
configs
[
env
.
Name
]
=
env
.
Value
}
}
if
vs
!=
nil
&&
len
(
vs
)
>
0
{
for
i
:=
range
vs
{
v
:=
vs
[
i
]
...
...
@@ -336,60 +346,40 @@ func createVolumes(as *v1.AppService, version *dbmodel.VersionInfo, dbmanager db
os
.
Chmod
(
v
.
HostPath
,
0777
)
}
// create a configMap which will be mounted as a volume
name
:=
fmt
.
Sprintf
(
"manual%d"
,
v
.
ID
)
mountPath
:=
v
.
VolumePath
var
cmap
*
corev1
.
ConfigMap
if
v
.
VolumeType
==
dbmodel
.
ConfigFileVolumeType
.
String
()
{
cf
s
,
err
:=
dbmanager
.
TenantServiceConfigFileDao
()
.
Lis
tByVolume
ID
(
v
.
VolumeName
)
cf
,
err
:=
dbmanager
.
TenantServiceConfigFileDao
()
.
Ge
tByVolume
Name
(
v
.
VolumeName
)
if
err
!=
nil
{
logrus
.
Errorf
(
"error
lis
ting config file
s
by volume name(%s): %v"
,
v
.
VolumeName
,
err
)
return
nil
,
fmt
.
Errorf
(
"error
lis
ting config file
s
by volume name(%s): %v"
,
v
.
VolumeName
,
err
)
logrus
.
Errorf
(
"error
get
ting config file by volume name(%s): %v"
,
v
.
VolumeName
,
err
)
return
nil
,
fmt
.
Errorf
(
"error
get
ting config file by volume name(%s): %v"
,
v
.
VolumeName
,
err
)
}
if
cfs
==
nil
||
len
(
cfs
)
==
0
{
logrus
.
Errorf
(
"there is no config files according to volume name(%s)"
,
v
.
VolumeName
)
return
nil
,
fmt
.
Errorf
(
"there is no config files according to volume name(%s)"
,
v
.
VolumeName
)
}
configs
:=
make
(
map
[
string
]
string
)
envs
,
err
:=
createEnv
(
as
,
dbmanager
)
if
err
!=
nil
{
logrus
.
Warningf
(
"error creating environment variables: %v"
,
err
)
}
else
{
for
_
,
env
:=
range
*
envs
{
configs
[
env
.
Name
]
=
env
.
Value
}
}
for
_
,
cf
:=
range
cfs
{
mountPath
=
path
.
Dir
(
v
.
VolumePath
)
name
=
fmt
.
Sprintf
(
"manual%s%s"
,
as
.
ServiceID
,
mountPath
)
name
=
strings
.
Replace
(
name
,
"/"
,
"slash"
,
-
1
)
cmap
,
ok
:=
cmmap
[
name
]
if
!
ok
{
cmap
=
&
corev1
.
ConfigMap
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
name
,
Namespace
:
as
.
TenantID
,
Labels
:
as
.
GetCommonLabels
(),
},
Data
:
make
(
map
[
string
]
string
),
}
cmmap
[
name
]
=
cmap
}
cmap
.
Data
[
filepath
.
Base
(
v
.
VolumePath
)]
=
util
.
ParseVariable
(
cf
.
FileContent
,
configs
)
name
:=
fmt
.
Sprintf
(
"manual%s%s"
,
as
.
ServiceID
,
v
.
VolumePath
)
name
=
strings
.
Replace
(
name
,
"/"
,
"slash"
,
-
1
)
name
=
strings
.
Replace
(
name
,
"."
,
"dot"
,
-
1
)
cmap
=
&
corev1
.
ConfigMap
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
name
,
Namespace
:
as
.
TenantID
,
Labels
:
as
.
GetCommonLabels
(),
},
Data
:
make
(
map
[
string
]
string
),
}
cmap
.
Data
[
path
.
Base
(
v
.
VolumePath
)]
=
util
.
ParseVariable
(
cf
.
FileContent
,
configs
)
as
.
SetConfigMap
(
cmap
)
vd
.
SetVolumeCMap
(
cmap
,
path
.
Base
(
v
.
VolumePath
),
v
.
VolumePath
,
v
.
IsReadOnly
)
continue
// pass codes below
}
if
as
.
GetStatefulSet
()
!=
nil
{
vd
.
SetPV
(
dbmodel
.
VolumeType
(
v
.
VolumeType
),
name
,
mount
Path
,
v
.
IsReadOnly
)
vd
.
SetPV
(
dbmodel
.
VolumeType
(
v
.
VolumeType
),
fmt
.
Sprintf
(
"manual%d"
,
v
.
ID
),
v
.
Volume
Path
,
v
.
IsReadOnly
)
}
else
{
hostPath
:=
v
.
HostPath
if
as
.
IsWindowsService
{
hostPath
=
RewriteHostPathInWindows
(
hostPath
)
}
vd
.
SetVolume
(
dbmodel
.
VolumeType
(
v
.
VolumeType
),
name
,
mount
Path
,
hostPath
,
corev1
.
HostPathDirectoryOrCreate
,
v
.
IsReadOnly
)
vd
.
SetVolume
(
dbmodel
.
VolumeType
(
v
.
VolumeType
),
fmt
.
Sprintf
(
"manual%d"
,
v
.
ID
),
v
.
Volume
Path
,
hostPath
,
corev1
.
HostPathDirectoryOrCreate
,
v
.
IsReadOnly
)
}
}
}
for
_
,
cmap
:=
range
cmmap
{
as
.
SetConfigMap
(
cmap
)
}
//handle Shared storage
logrus
.
Infof
(
"begin handling Shared storage"
)
tsmr
,
err
:=
dbmanager
.
TenantServiceMountRelationDao
()
.
GetTenantServiceMountRelationsByService
(
version
.
ServiceID
)
...
...
@@ -413,16 +403,29 @@ func createVolumes(as *v1.AppService, version *dbmodel.VersionInfo, dbmanager db
case
dbmodel
.
ConfigFileVolumeType
.
String
()
:
tsv
,
err
:=
dbmanager
.
TenantServiceVolumeDao
()
.
GetVolumeByServiceIDAndName
(
t
.
DependServiceID
,
t
.
VolumeName
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error getting TenantServiceVolume according to serviceID(%s) and volumeName(%s): %v"
,
t
.
DependServiceID
,
t
.
VolumeName
,
err
)
return
nil
,
fmt
.
Errorf
(
"error getting TenantServiceVolume according to serviceID(%s) and volumeName(%s): %v"
,
t
.
DependServiceID
,
t
.
VolumeName
,
err
)
}
cf
,
err
:=
dbmanager
.
TenantServiceConfigFileDao
()
.
GetByVolumeName
(
t
.
VolumeName
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error getting TenantServiceConfigFileDao according to volumeName(%s): %v"
,
t
.
VolumeName
,
err
)
}
name
:=
fmt
.
Sprintf
(
"manual%s%s"
,
t
.
DependServiceID
,
path
.
Dir
(
tsv
.
VolumePath
))
name
:=
fmt
.
Sprintf
(
"manual%s%s"
,
as
.
ServiceID
,
t
.
VolumePath
)
name
=
strings
.
Replace
(
name
,
"/"
,
"slash"
,
-
1
)
if
as
.
GetStatefulSet
()
!=
nil
{
vd
.
SetPV
(
dbmodel
.
ConfigFileVolumeType
,
name
,
t
.
VolumePath
,
tsv
.
IsReadOnly
)
}
else
{
vd
.
SetVolume
(
dbmodel
.
ConfigFileVolumeType
,
name
,
t
.
VolumePath
,
""
,
corev1
.
HostPathDirectoryOrCreate
,
tsv
.
IsReadOnly
)
name
=
strings
.
Replace
(
name
,
"."
,
"dot"
,
-
1
)
cmap
:=
&
corev1
.
ConfigMap
{
ObjectMeta
:
metav1
.
ObjectMeta
{
Name
:
name
,
Namespace
:
as
.
TenantID
,
Labels
:
as
.
GetCommonLabels
(),
},
Data
:
make
(
map
[
string
]
string
),
}
cmap
.
Data
[
path
.
Base
(
t
.
VolumePath
)]
=
util
.
ParseVariable
(
cf
.
FileContent
,
configs
)
as
.
SetConfigMap
(
cmap
)
vd
.
SetVolumeCMap
(
cmap
,
path
.
Base
(
t
.
VolumePath
),
t
.
VolumePath
,
tsv
.
IsReadOnly
)
}
}
}
...
...
@@ -470,9 +473,11 @@ type volumeDefine struct {
func
(
v
*
volumeDefine
)
GetVolumes
()
[]
corev1
.
Volume
{
return
v
.
volumes
}
func
(
v
*
volumeDefine
)
GetVolumeMounts
()
[]
corev1
.
VolumeMount
{
return
v
.
volumeMounts
}
func
(
v
*
volumeDefine
)
SetPV
(
VolumeType
dbmodel
.
VolumeType
,
name
,
mountPath
string
,
readOnly
bool
)
{
logrus
.
Info
(
"Set persistence volume for statefuleset."
)
switch
VolumeType
{
...
...
@@ -547,28 +552,9 @@ func (v *volumeDefine) SetPV(VolumeType dbmodel.VolumeType, name, mountPath stri
ReadOnly
:
readOnly
,
})
}
case
dbmodel
.
ConfigFileVolumeType
:
logrus
.
Infof
(
"VolumeType is config-file"
)
if
statefulset
:=
v
.
as
.
GetStatefulSet
();
statefulset
!=
nil
{
cv
:=
corev1
.
Volume
{
Name
:
name
,
VolumeSource
:
corev1
.
VolumeSource
{
ConfigMap
:
&
corev1
.
ConfigMapVolumeSource
{
LocalObjectReference
:
corev1
.
LocalObjectReference
{
Name
:
name
,
},
},
},
}
v
.
volumes
=
append
(
v
.
volumes
,
cv
)
v
.
volumeMounts
=
append
(
v
.
volumeMounts
,
corev1
.
VolumeMount
{
Name
:
name
,
MountPath
:
mountPath
,
ReadOnly
:
readOnly
,
})
}
}
}
func
(
v
*
volumeDefine
)
SetVolume
(
VolumeType
dbmodel
.
VolumeType
,
name
,
mountPath
,
hostPath
string
,
hostPathType
corev1
.
HostPathType
,
readOnly
bool
)
{
logrus
.
Info
(
"Set volume for deployment."
)
for
_
,
m
:=
range
v
.
volumeMounts
{
...
...
@@ -617,25 +603,36 @@ func (v *volumeDefine) SetVolume(VolumeType dbmodel.VolumeType, name, mountPath,
case
dbmodel
.
LocalVolumeType
:
//no support
return
case
dbmodel
.
ConfigFileVolumeType
:
logrus
.
Infof
(
"VolumeType is config-file"
)
vo
:=
corev1
.
Volume
{
Name
:
name
,
}
vo
.
ConfigMap
=
&
corev1
.
ConfigMapVolumeSource
{
LocalObjectReference
:
corev1
.
LocalObjectReference
{
Name
:
name
,
}
}
// SetVolumeCMap sets volumes and volumeMounts. The type of volumes is configMap.
func
(
v
*
volumeDefine
)
SetVolumeCMap
(
cmap
*
corev1
.
ConfigMap
,
k
,
p
string
,
isReadOnly
bool
)
{
vm
:=
corev1
.
VolumeMount
{
MountPath
:
p
,
Name
:
cmap
.
Name
,
ReadOnly
:
isReadOnly
,
SubPath
:
path
.
Base
(
p
),
}
v
.
volumeMounts
=
append
(
v
.
volumeMounts
,
vm
)
vo
:=
corev1
.
Volume
{
Name
:
cmap
.
Name
,
VolumeSource
:
corev1
.
VolumeSource
{
ConfigMap
:
&
corev1
.
ConfigMapVolumeSource
{
LocalObjectReference
:
corev1
.
LocalObjectReference
{
Name
:
cmap
.
Name
,
},
Items
:
[]
corev1
.
KeyToPath
{
{
Key
:
k
,
Path
:
path
.
Base
(
p
),
// subpath
},
},
},
}
v
.
volumes
=
append
(
v
.
volumes
,
vo
)
vm
:=
corev1
.
VolumeMount
{
MountPath
:
mountPath
,
Name
:
name
,
ReadOnly
:
readOnly
,
SubPath
:
""
,
}
v
.
volumeMounts
=
append
(
v
.
volumeMounts
,
vm
)
},
}
v
.
volumes
=
append
(
v
.
volumes
,
vo
)
}
func
createResources
(
as
*
v1
.
AppService
)
corev1
.
ResourceRequirements
{
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help