runtime: add WithVolume options and k8s support (#2032)

* runtime: add WithVolume option

* Fix pointer

* k8s: fix template

* k8s: fix template

* k8s: fix template
This commit is contained in:
ben-toogood
2020-09-30 16:38:29 +01:00
committed by GitHub
parent 02b74a5487
commit a365c51c2b
4 changed files with 61 additions and 0 deletions

View File

@@ -119,6 +119,20 @@ func newService(s *runtime.Service, c runtime.CreateOptions) *service {
kdeploy.Spec.Template.PodSpec.Containers[0].Resources = &client.ResourceRequirements{Limits: resLimits} kdeploy.Spec.Template.PodSpec.Containers[0].Resources = &client.ResourceRequirements{Limits: resLimits}
} }
// mount volumes
var volumes []client.Volume
var mounts []client.VolumeMount
for name, path := range c.Volumes {
volumes = append(volumes, client.Volume{
Name: name,
PersistentVolumeClaim: client.PersistentVolumeClaimVolumeSource{ClaimName: name},
})
mounts = append(mounts, client.VolumeMount{Name: name, MountPath: path})
}
kdeploy.Spec.Template.PodSpec.Volumes = volumes
kdeploy.Spec.Template.PodSpec.Containers[0].VolumeMounts = mounts
return &service{ return &service{
Service: s, Service: s,
kservice: kservice, kservice: kservice,

View File

@@ -86,6 +86,8 @@ type CreateOptions struct {
Secrets map[string]string Secrets map[string]string
// Resources to allocate the service // Resources to allocate the service
Resources *Resources Resources *Resources
// Volumes to mount
Volumes map[string]string
} }
// ReadOptions queries runtime services // ReadOptions queries runtime services
@@ -178,6 +180,17 @@ func WithOutput(out io.Writer) CreateOption {
} }
} }
// WithVolume adds a volume to be mounted
func WithVolume(name, path string) CreateOption {
return func(o *CreateOptions) {
if o.Volumes == nil {
o.Volumes = map[string]string{name: path}
} else {
o.Volumes[name] = path
}
}
}
// ResourceLimits sets the resources for the service to use // ResourceLimits sets the resources for the service to use
func ResourceLimits(r *Resources) CreateOption { func ResourceLimits(r *Resources) CreateOption {
return func(o *CreateOptions) { return func(o *CreateOptions) {

View File

@@ -139,6 +139,21 @@ spec:
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- end }} {{- end }}
volumeMounts:
{{- with .VolumeMounts }}
{{- range . }}
- name: {{ .Name }}
mountPath: {{ .MountPath }}
{{- end }}
{{- end }}
{{- end }}
{{- end }}
volumes:
{{- with .Spec.Template.PodSpec.Volumes }}
{{- range . }}
- name: {{ .Name }}
persistentVolumeClaim:
claimName: {{ .PersistentVolumeClaim.ClaimName }}
{{- end }} {{- end }}
{{- end }} {{- end }}
` `

View File

@@ -43,6 +43,7 @@ type Container struct {
Ports []ContainerPort `json:"ports,omitempty"` Ports []ContainerPort `json:"ports,omitempty"`
ReadinessProbe *Probe `json:"readinessProbe,omitempty"` ReadinessProbe *Probe `json:"readinessProbe,omitempty"`
Resources *ResourceRequirements `json:"resources,omitempty"` Resources *ResourceRequirements `json:"resources,omitempty"`
VolumeMounts []VolumeMount `json:"volumeMounts,omitempty"`
} }
// DeploymentSpec defines micro deployment spec // DeploymentSpec defines micro deployment spec
@@ -110,6 +111,7 @@ type Metadata struct {
type PodSpec struct { type PodSpec struct {
Containers []Container `json:"containers"` Containers []Container `json:"containers"`
ServiceAccountName string `json:"serviceAccountName"` ServiceAccountName string `json:"serviceAccountName"`
Volumes []Volume `json:"volumes"`
} }
// PodList // PodList
@@ -248,3 +250,20 @@ type ResourceLimits struct {
CPU string `json:"cpu,omitempty"` CPU string `json:"cpu,omitempty"`
EphemeralStorage string `json:"ephemeral-storage,omitempty"` EphemeralStorage string `json:"ephemeral-storage,omitempty"`
} }
// Volume describes a volume which can be mounted to a pod
type Volume struct {
Name string `json:"name"`
PersistentVolumeClaim PersistentVolumeClaimVolumeSource `json:"persistentVolumeClaim,omitempty"`
}
// PersistentVolumeClaimVolumeSource represents a reference to a PersistentVolumeClaim in the same namespace
type PersistentVolumeClaimVolumeSource struct {
ClaimName string `json:"claimName"`
}
// VolumeMount describes a mounting of a Volume within a container.
type VolumeMount struct {
Name string `json:"name"`
MountPath string `json:"mountPath"`
}