Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s context can default to cluster if using oidc auth #327

Merged
merged 2 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions lib/ood_core/job/adapters/kubernetes/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ def initialize(options = {})
@config_file = options.fetch(:config_file, Batch.default_config_file)
@bin = options.fetch(:bin, '/usr/bin/kubectl')
@cluster = options.fetch(:cluster, 'open-ondemand')
@context = options.fetch(:context, nil)
@mounts = options.fetch(:mounts, []).map { |m| m.to_h.symbolize_keys }
@all_namespaces = options.fetch(:all_namespaces, false)
@username_prefix = options.fetch(:username_prefix, '')
@namespace_prefix = options.fetch(:namespace_prefix, '')
@auto_supplemental_groups = options.fetch(:auto_supplemental_groups, false)

tmp_ctx = options.fetch(:context, nil)
@context = tmp_ctx.nil? && oidc_auth?(options.fetch(:auth, {})) ? @cluster : tmp_ctx

@helper = OodCore::Job::Adapters::Kubernetes::Helper.new
end

Expand Down Expand Up @@ -308,22 +310,29 @@ def pod_info_from_json(pod)
end

def configure_auth(auth)
type = auth.fetch(:type)
return if managed?(type)

case type
when 'gke'
set_gke_config(auth)
when 'oidc'
set_context
end
if managed_auth?(auth)
return
elsif gke_auth?(auth)
set_gke_config(auth)
elsif oidc_auth?(auth)
set_context
end
johrstrom marked this conversation as resolved.
Show resolved Hide resolved
end

def context?
!@context.nil?
end

def managed?(type)
def gke_auth?(auth = {})
auth.fetch(:type, nil) == 'gke'
end

def oidc_auth?(auth = {})
auth.fetch(:type, nil) == 'oidc'
end

def managed_auth?(auth = {})
type = auth.fetch(:type, nil)
if type.nil?
true # maybe should be false?
else
Oglopf marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
32 changes: 32 additions & 0 deletions spec/job/adapters/kubernetes/batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@

expect { described_class.new }.not_to raise_error
expect { described_class.new(config) }.not_to raise_error
expect { described_class.new({}) }.not_to raise_error
end

it "defaults context to cluster for oidc auth" do
cfg = {
:cluster => 'some-cluster',
:auth => {
:type => 'oidc'
}
}

k = described_class.new(cfg)
expect(k.context).to eq(k.cluster)
expect(k.context).to eq('some-cluster')
expect(k.cluster).to eq('some-cluster')
expect(k.send(:context?)).to eq(true)
end
end

Expand Down Expand Up @@ -1092,6 +1108,22 @@ def not_found_batch(id)
Batch.configure_kube!(config)
end

it 'generates sets oidc context with default context' do
set_cluster = "/usr/bin/wontwork --kubeconfig=~/kube.config config set-cluster test-cluster " \
"--server=https://some.k8s.host --certificate-authority=/etc/some.cert"
set_context = "/usr/bin/wontwork --kubeconfig=~/kube.config config set-context test-cluster " \
"--cluster=test-cluster --namespace=user-jessie --user=dev-jessie"

expect_any_instance_of(Batch).to receive(:call).with(set_cluster)
expect_any_instance_of(Batch).to receive(:username).and_return('jessie')
expect_any_instance_of(Batch).to receive(:username).and_return('jessie')
expect_any_instance_of(Batch).to receive(:call).with(set_context)

# let's make sure --context=#{cluster}
cfg = config.tap { |h| h.delete(:context) }
Batch.configure_kube!(cfg)
end

it 'runs the correct gke commands' do
cfg = {
:auth => {
Expand Down