Skip to content

Commit

Permalink
Kubernetes adapter updates
Browse files Browse the repository at this point in the history
* Rework how configmap is defined for several purposes
  * Support mounting at subPath
  * Support mounting files that might not be defined statically but instead generated in init container
* Add some helper environment variable to both main and init container
  • Loading branch information
treydock committed Mar 16, 2021
1 parent 5372942 commit 6bc9daa
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 46 deletions.
16 changes: 14 additions & 2 deletions lib/ood_core/job/adapters/kubernetes/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,30 @@ def k8s_username
username_prefix.nil? ? username : "#{username_prefix}-#{username}"
end

def user
@user ||= Etc.getpwnam(username)
end

def home_dir
user.dir
end

def run_as_user
Etc.getpwnam(username).uid
user.uid
end

def run_as_group
Etc.getpwnam(username).gid
user.gid
end

def fs_group
run_as_group
end

def group
Etc.getgrgid(run_as_group).name
end

# helper to template resource yml you're going to submit and
# create an id.
def generate_id_yml(script)
Expand Down
3 changes: 1 addition & 2 deletions lib/ood_core/job/adapters/kubernetes/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def configmap_from_native(native, id)

OodCore::Job::Adapters::Kubernetes::Resources::ConfigMap.new(
configmap_name(id),
configmap[:filename],
configmap[:data]
(configmap[:files] || [])
)
end

Expand Down
24 changes: 20 additions & 4 deletions lib/ood_core/job/adapters/kubernetes/resources.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
module OodCore::Job::Adapters::Kubernetes::Resources
require_relative 'batch'

class ConfigMap
attr_accessor :name, :filename, :data
attr_accessor :name, :files

def initialize(name, filename, data)
def initialize(name, files)
@name = name
@filename = filename
@data = data
@files = []
files.each do |f|
@files << ConfigMapFile.new(f)
end
end
end

class ConfigMapFile
attr_accessor :filename, :data, :mount_path, :sub_path, :init_mount_path, :init_sub_path

def initialize(data)
@filename = data[:filename]
@data = data[:data]
@mount_path = data[:mount_path]
@sub_path = data[:sub_path]
@init_mount_path = data[:init_mount_path]
@init_sub_path = data[:init_sub_path]
end
end

Expand Down
65 changes: 57 additions & 8 deletions lib/ood_core/job/adapters/kubernetes/templates/pod.yml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,22 @@ spec:
<%- unless spec.container.working_dir.empty? -%>
workingDir: "<%= spec.container.working_dir %>"
<%- end -%>
<%- unless spec.container.env.empty? -%>
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: USER
value: "<%= username %>"
- name: UID
value: "<%= run_as_user %>"
- name: HOME
value: "<%= home_dir %>"
- name: GROUP
value: "<%= group %>"
- name: GID
value: "<%= run_as_group %>"
<%- unless spec.container.env.empty? -%>
<%- spec.container.env.each do |env| -%>
- name: <%= env[:name] %>
value: "<%= env[:value] %>"
Expand All @@ -58,9 +72,14 @@ spec:
<%- end -%>
volumeMounts:
<%- unless configmap.nil? -%>
<%- configmap.files.each do |file| -%>
- name: configmap-volume
mountPath: <%= configmap_mount_path %>
<%- end -%>
mountPath: <%= file.mount_path || configmap_mount_path %>
<%- unless file.sub_path.nil? -%>
subPath: <%= file.sub_path %>
<%- end # end unless file.sub_path.nil? -%>
<%- end # end configmap.data.each -%>
<%- end # unless configmap -%>
<%- all_mounts.each do |mount| -%>
- name: <%= mount[:name] %>
mountPath: <%= mount[:destination_path] %>
Expand All @@ -83,15 +102,42 @@ spec:
<%- spec.init_containers.each do |ctr| -%>
- name: "<%= ctr.name %>"
image: "<%= ctr.image %>"
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: USER
value: "<%= username %>"
- name: UID
value: "<%= run_as_user %>"
- name: HOME
value: "<%= home_dir %>"
- name: GROUP
value: "<%= group %>"
- name: GID
value: "<%= run_as_group %>"
<%- unless ctr.env.empty? -%>
<%- ctr.env.each do |env| -%>
- name: <%= env[:name] %>
value: "<%= env[:value] %>"
<%- end # for each env -%>
<%- end # unless env is nil -%>
command:
<%- ctr.command.each do |cmd| -%>
- "<%= cmd %>"
<%- end # command loop -%>
volumeMounts:
<%- unless configmap.nil? -%>
<%- configmap.files.each do |file| -%>
<%- next if file.init_mount_path == false -%>
- name: configmap-volume
mountPath: <%= configmap_mount_path %>
<%- end -%>
mountPath: <%= file.init_mount_path || configmap_mount_path %>
<%- unless file.init_sub_path.nil? -%>
subPath: <%= file.init_sub_path %>
<%- end # end unless file.sub_path.nil? -%>
<%- end # end configmap.data.each -%>
<%- end # unless configmap -%>
<%- all_mounts.each do |mount| -%>
- name: <%= mount[:name] %>
mountPath: <%= mount[:destination_path] %>
Expand Down Expand Up @@ -153,6 +199,9 @@ metadata:
labels:
job: <%= id %>
data:
<%= configmap.filename %>: |
<% config_data_lines(configmap.data).each do |line| %><%= line %><% end %>
<%- end # end for configmap -%>
<%- configmap.files.each do |file| -%>
<%- next if file.data.nil? || file.filename.nil? -%>
<%= file.filename %>: |
<% config_data_lines(file.data).each do |line| %><%= line %><% end %>
<%- end # end for configmap files -%>
<%- end # end configmap.nil? %>
31 changes: 29 additions & 2 deletions spec/fixtures/output/k8s/pod_yml_from_all_configs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ spec:
imagePullPolicy: IfNotPresent
workingDir: "/my/home"
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: USER
value: "testuser"
- name: UID
value: "1001"
- name: HOME
value: "/my/home"
- name: GROUP
value: "testgroup"
- name: GID
value: "1002"
- name: PATH
value: "/usr/bin:/usr/local/bin"
command:
Expand All @@ -37,7 +49,7 @@ spec:
- containerPort: 8080
volumeMounts:
- name: configmap-volume
mountPath: /ood
mountPath: /ood
- name: home-dir
mountPath: /home
- name: nfs-dir
Expand All @@ -60,13 +72,28 @@ spec:
initContainers:
- name: "init-1"
image: "busybox:latest"
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: USER
value: "testuser"
- name: UID
value: "1001"
- name: HOME
value: "/my/home"
- name: GROUP
value: "testgroup"
- name: GID
value: "1002"
command:
- "/bin/ls"
- "-lrt"
- "."
volumeMounts:
- name: configmap-volume
mountPath: /ood
mountPath: /ood
- name: home-dir
mountPath: /home
- name: nfs-dir
Expand Down
31 changes: 29 additions & 2 deletions spec/fixtures/output/k8s/pod_yml_from_defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ spec:
imagePullPolicy: IfNotPresent
workingDir: "/my/home"
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: USER
value: "testuser"
- name: UID
value: "1001"
- name: HOME
value: "/my/home"
- name: GROUP
value: "testgroup"
- name: GID
value: "1002"
- name: PATH
value: "/usr/bin:/usr/local/bin"
command:
Expand All @@ -37,7 +49,7 @@ spec:
- containerPort: 8080
volumeMounts:
- name: configmap-volume
mountPath: /ood
mountPath: /ood
- name: ess
mountPath: /fs/ess
resources:
Expand All @@ -56,13 +68,28 @@ spec:
initContainers:
- name: "init-1"
image: "busybox:latest"
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: USER
value: "testuser"
- name: UID
value: "1001"
- name: HOME
value: "/my/home"
- name: GROUP
value: "testgroup"
- name: GID
value: "1002"
command:
- "/bin/ls"
- "-lrt"
- "."
volumeMounts:
- name: configmap-volume
mountPath: /ood
mountPath: /ood
- name: ess
mountPath: /fs/ess
securityContext:
Expand Down
31 changes: 29 additions & 2 deletions spec/fixtures/output/k8s/pod_yml_no_mounts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,20 @@ spec:
imagePullPolicy: IfNotPresent
workingDir: "/my/home"
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: USER
value: "testuser"
- name: UID
value: "1001"
- name: HOME
value: "/my/home"
- name: GROUP
value: "testgroup"
- name: GID
value: "1002"
- name: PATH
value: "/usr/bin:/usr/local/bin"
command:
Expand All @@ -37,7 +49,7 @@ spec:
- containerPort: 8080
volumeMounts:
- name: configmap-volume
mountPath: /ood
mountPath: /ood
resources:
limits:
memory: "6Gi"
Expand All @@ -54,13 +66,28 @@ spec:
initContainers:
- name: "init-1"
image: "busybox:latest"
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: USER
value: "testuser"
- name: UID
value: "1001"
- name: HOME
value: "/my/home"
- name: GROUP
value: "testgroup"
- name: GID
value: "1002"
command:
- "/bin/ls"
- "-lrt"
- "."
volumeMounts:
- name: configmap-volume
mountPath: /ood
mountPath: /ood
securityContext:
allowPrivilegeEscalation: false
capabilities:
Expand Down
Loading

0 comments on commit 6bc9daa

Please sign in to comment.