Skip to content

Commit

Permalink
feat: support secret, cm preset volumes (#1104)
Browse files Browse the repository at this point in the history
  • Loading branch information
cd1989 committed Jun 3, 2019
1 parent 9c17c3b commit 33ec80a
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 13 deletions.
29 changes: 26 additions & 3 deletions pkg/apis/cyclone/v1alpha1/workflow_run.go
Expand Up @@ -49,10 +49,17 @@ type WorkflowRunSpec struct {
type PresetVolume struct {
// Type of the volume
Type PresetVolumeType `json:"type"`
// Path is path in host, or PV.
// Path is path in host, PVC, or key path in Secret, ConfigMap.
Path string `json:"path"`
// MountPath is path in container that this preset volume will be mounted.
MountPath string `json:"mountPath"`
// SubPath is subpath to mount in container, for example, if MountPath is '/etc', SubPath is 'app.config', then
// final file would be '/etc/app.config' in container
SubPath string `json:"subPath"`
// ObjectName is name of the source object if preset volume type is Secret, ConfigMap, PVC
ObjectName *string `json:"objectName,omitempty"`
// Scope defines the which containers to apply the preset volumes
ContainerGroup ContainerGroup `json:"containerGroup"`
}

// PresetVolumeType is type of preset volumes, HostPath, PV supported.
Expand All @@ -61,8 +68,24 @@ type PresetVolumeType string
const (
// PresetVolumeTypeHostPath ...
PresetVolumeTypeHostPath PresetVolumeType = "HostPath"
// PresetVolumeTypePV ...
PresetVolumeTypePV PresetVolumeType = "PV"
// PresetVolumeTypePVC ...
PresetVolumeTypePVC PresetVolumeType = "PVC"
// PresetVolumeTypeSecret ...
PresetVolumeTypeSecret PresetVolumeType = "Secret"
// PresetVolumeTypeConfigMap ...
PresetVolumeTypeConfigMap PresetVolumeType = "ConfigMap"
)

// ContainerGroup defines group of containers in a stage pod, for example, 'sidecar', 'workload', 'initContainer'
type ContainerGroup string

const (
// ContainerGroupAll represents all containers in a pod
ContainerGroupAll ContainerGroup = "All"
// ContainerGroupSidecar represents sidecar containers in a pod
ContainerGroupSidecar ContainerGroup = "Sidecar"
// ContainerGroupWorkload repressents user containers in a pod
ContainerGroupWorkload ContainerGroup = "Workload"
)

// ParameterConfig configures parameters of a resource or a stage.
Expand Down
9 changes: 8 additions & 1 deletion pkg/apis/cyclone/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/server/biz/accelerator/accelerate.go
Expand Up @@ -57,17 +57,17 @@ func (a *Accelerator) Accelerate() {
if a.wfr.Labels != nil && a.wfr.Labels[meta.LabelWorkflowRunAcceleration] == meta.LabelValueTrue {
a.wfr.Spec.PresetVolumes = append(a.wfr.Spec.PresetVolumes, []v1alpha1.PresetVolume{
{
Type: v1alpha1.PresetVolumeTypePV,
Type: v1alpha1.PresetVolumeTypePVC,
Path: fmt.Sprintf("%s/%s/m2", common.CachePrefixPath, a.project),
MountPath: "/root/.m2",
},
{
Type: v1alpha1.PresetVolumeTypePV,
Type: v1alpha1.PresetVolumeTypePVC,
Path: fmt.Sprintf("%s/%s/gradle", common.CachePrefixPath, a.project),
MountPath: "/root/.gradle",
},
{
Type: v1alpha1.PresetVolumeTypePV,
Type: v1alpha1.PresetVolumeTypePVC,
Path: fmt.Sprintf("%s/%s/npm", common.CachePrefixPath, a.project),
MountPath: "/root/.npm",
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/biz/accelerator/accelerate_test.go
Expand Up @@ -33,17 +33,17 @@ func TestAccelerate(t *testing.T) {
Spec: v1alpha1.WorkflowRunSpec{
PresetVolumes: []v1alpha1.PresetVolume{
{
Type: v1alpha1.PresetVolumeTypePV,
Type: v1alpha1.PresetVolumeTypePVC,
Path: fmt.Sprintf("%s/%s/m2", common.CachePrefixPath, project),
MountPath: "/root/.m2",
},
{
Type: v1alpha1.PresetVolumeTypePV,
Type: v1alpha1.PresetVolumeTypePVC,
Path: fmt.Sprintf("%s/%s/gradle", common.CachePrefixPath, project),
MountPath: "/root/.gradle",
},
{
Type: v1alpha1.PresetVolumeTypePV,
Type: v1alpha1.PresetVolumeTypePVC,
Path: fmt.Sprintf("%s/%s/npm", common.CachePrefixPath, project),
MountPath: "/root/.npm",
},
Expand Down
46 changes: 44 additions & 2 deletions pkg/workflow/workload/pod/builder.go
Expand Up @@ -202,7 +202,40 @@ func (m *Builder) CreateVolumes() error {
},
},
})
case v1alpha1.PresetVolumeTypePV:
case v1alpha1.PresetVolumeTypePVC:
// Use the default PVC for preset volume, and the volume already created before
case v1alpha1.PresetVolumeTypeSecret:
m.pod.Spec.Volumes = append(m.pod.Spec.Volumes, corev1.Volume{
Name: common.PresetVolumeName(i),
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: *v.ObjectName,
Items: []corev1.KeyToPath{
{
Key: v.Path,
Path: v.SubPath,
},
},
},
},
})
case v1alpha1.PresetVolumeTypeConfigMap:
m.pod.Spec.Volumes = append(m.pod.Spec.Volumes, corev1.Volume{
Name: common.PresetVolumeName(i),
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
LocalObjectReference: corev1.LocalObjectReference{
Name: *v.ObjectName,
},
Items: []corev1.KeyToPath{
{
Key: v.Path,
Path: v.SubPath,
},
},
},
},
})
default:
log.WithField("type", v.Type).Warning("Unknown preset volume type.")
}
Expand Down Expand Up @@ -590,19 +623,28 @@ func (m *Builder) MountPresetVolumes() error {
var containers []corev1.Container
for _, c := range m.pod.Spec.Containers {
for i, v := range m.wfr.Spec.PresetVolumes {
if !MatchContainerGroup(v.ContainerGroup, c.Name) {
continue
}

switch v.Type {
case v1alpha1.PresetVolumeTypeHostPath:
c.VolumeMounts = append(c.VolumeMounts, corev1.VolumeMount{
Name: common.PresetVolumeName(i),
MountPath: v.MountPath,
ReadOnly: true,
})
case v1alpha1.PresetVolumeTypePV:
case v1alpha1.PresetVolumeTypePVC:
c.VolumeMounts = append(c.VolumeMounts, corev1.VolumeMount{
Name: common.DefaultPvVolumeName,
MountPath: v.MountPath,
SubPath: v.Path,
})
case v1alpha1.PresetVolumeTypeSecret, v1alpha1.PresetVolumeTypeConfigMap:
c.VolumeMounts = append(c.VolumeMounts, corev1.VolumeMount{
Name: common.PresetVolumeName(i),
MountPath: v.MountPath,
})
default:
log.WithField("type", v.Type).Warning("Common volume type not supported")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/workflow/workload/pod/builder_test.go
Expand Up @@ -76,7 +76,7 @@ var wfr = &v1alpha1.WorkflowRun{
},
PresetVolumes: []v1alpha1.PresetVolume{
{
Type: v1alpha1.PresetVolumeTypePV,
Type: v1alpha1.PresetVolumeTypePVC,
Path: "etc",
MountPath: "/tmp",
},
Expand Down
17 changes: 17 additions & 0 deletions pkg/workflow/workload/pod/util.go
Expand Up @@ -2,6 +2,7 @@ package pod

import (
"fmt"
"strings"

"k8s.io/apimachinery/pkg/util/rand"

Expand Down Expand Up @@ -71,3 +72,19 @@ func ResolveRefStringValue(ref string, client clientset.Interface) (string, erro

return strV, nil
}

// MatchContainerGroup matches a container name against a ContainerGroup, if the container belongs to the container group,
// return true, otherwise false. It only tests containers, init containers are not considered here. If input container
// group is empty or invalid, return true.
func MatchContainerGroup(group v1alpha1.ContainerGroup, container string) bool {
switch group {
case v1alpha1.ContainerGroupAll:
return true
case v1alpha1.ContainerGroupSidecar:
return strings.HasPrefix(container, common.CycloneSidecarPrefix)
case v1alpha1.ContainerGroupWorkload:
return !strings.HasPrefix(container, common.CycloneSidecarPrefix)
default:
return true
}
}

0 comments on commit 33ec80a

Please sign in to comment.