-
Notifications
You must be signed in to change notification settings - Fork 25
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
Bugfix/k8s context #324
Merged
Merged
Bugfix/k8s context #324
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,32 +11,25 @@ class OodCore::Job::Adapters::Kubernetes::Batch | |
class Error < StandardError; end | ||
class NotFoundError < StandardError; end | ||
|
||
attr_reader :config_file, :bin, :cluster, :mounts | ||
attr_reader :all_namespaces, :using_context, :helper | ||
attr_reader :config_file, :bin, :cluster, :context, :mounts | ||
attr_reader :all_namespaces, :helper | ||
attr_reader :username_prefix, :namespace_prefix | ||
attr_reader :auto_supplemental_groups | ||
|
||
def initialize(options = {}) | ||
options = options.to_h.symbolize_keys | ||
|
||
@config_file = options.fetch(:config_file, default_config_file) | ||
@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) | ||
|
||
@using_context = false | ||
@helper = OodCore::Job::Adapters::Kubernetes::Helper.new | ||
|
||
begin | ||
make_kubectl_config(options) | ||
rescue | ||
# FIXME could use a log here | ||
# means you couldn't 'kubectl set config' | ||
end | ||
end | ||
|
||
def resource_file(resource_type = 'pod') | ||
|
@@ -117,6 +110,32 @@ def delete(id) | |
safe_call("delete", "configmap", configmap_name(id)) | ||
end | ||
|
||
class << self | ||
def default_config_file | ||
(ENV['KUBECONFIG'] || "#{Dir.home}/.kube/config") | ||
end | ||
|
||
def default_auth | ||
{ | ||
type: 'managed' | ||
}.symbolize_keys | ||
end | ||
|
||
def default_server | ||
{ | ||
endpoint: 'https://localhost:8080', | ||
cert_authority_file: nil | ||
}.symbolize_keys | ||
end | ||
|
||
def configure_kube!(config) | ||
k = self.new(config) | ||
# TODO: probably shouldn't be using send here | ||
k.send(:set_cluster, config.fetch(:server, default_server).to_h.symbolize_keys) | ||
k.send(:configure_auth, config.fetch(:auth, default_auth).to_h.symbolize_keys) | ||
end | ||
end | ||
|
||
private | ||
|
||
def safe_call(verb, resource, id) | ||
|
@@ -250,27 +269,6 @@ def namespace | |
"#{namespace_prefix}#{username}" | ||
end | ||
|
||
def context | ||
cluster | ||
end | ||
|
||
def default_config_file | ||
(ENV['KUBECONFIG'] || "#{Dir.home}/.kube/config") | ||
end | ||
|
||
def default_auth | ||
{ | ||
type: 'managaged' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. managed was misspelled here. fixed in this pr. |
||
}.symbolize_keys | ||
end | ||
|
||
def default_server | ||
{ | ||
endpoint: 'https://localhost:8080', | ||
cert_authority_file: nil | ||
}.symbolize_keys | ||
end | ||
|
||
def formatted_ns_cmd | ||
"#{namespaced_cmd} -o json" | ||
end | ||
|
@@ -281,7 +279,7 @@ def namespaced_cmd | |
|
||
def base_cmd | ||
base = "#{bin} --kubeconfig=#{config_file}" | ||
base << " --context=#{context}" if using_context | ||
base << " --context=#{context}" if context? | ||
base | ||
end | ||
|
||
|
@@ -309,11 +307,6 @@ def pod_info_from_json(pod) | |
nil | ||
end | ||
|
||
def make_kubectl_config(config) | ||
set_cluster(config.fetch(:server, default_server).to_h.symbolize_keys) | ||
configure_auth(config.fetch(:auth, default_auth).to_h.symbolize_keys) | ||
end | ||
|
||
def configure_auth(auth) | ||
type = auth.fetch(:type) | ||
return if managed?(type) | ||
|
@@ -326,8 +319,8 @@ def configure_auth(auth) | |
end | ||
end | ||
|
||
def use_context | ||
@using_context = true | ||
def context? | ||
!@context.nil? | ||
end | ||
|
||
def managed?(type) | ||
|
@@ -359,23 +352,24 @@ def set_gke_credentials(auth) | |
# gke cluster name can probably can differ from what ood calls the cluster | ||
cmd = "gcloud container clusters get-credentials #{locale} #{cluster}" | ||
env = { 'KUBECONFIG' => config_file } | ||
call(cmd, env) | ||
call(cmd, env: env) | ||
end | ||
|
||
def set_context | ||
cmd = "#{base_cmd} config set-context #{cluster}" | ||
# can't really use base_cmd, bc it may use --context flag | ||
cmd = "#{bin} --kubeconfig=#{config_file} config set-context #{context}" | ||
cmd << " --cluster=#{cluster} --namespace=#{namespace}" | ||
cmd << " --user=#{k8s_username}" | ||
|
||
call(cmd) | ||
use_context | ||
end | ||
|
||
def set_cluster(config) | ||
server = config.fetch(:endpoint) | ||
cert = config.fetch(:cert_authority_file, nil) | ||
|
||
cmd = "#{base_cmd} config set-cluster #{cluster}" | ||
# shouldn't use context here either | ||
cmd = "#{bin} --kubeconfig=#{config_file} config set-cluster #{cluster}" | ||
cmd << " --server=#{server}" | ||
cmd << " --certificate-authority=#{cert}" unless cert.nil? | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@johrstrom Should this default to
@cluster
to maintain past behavior? I think forcing a context makes sense since not having forced context can cause a lot of problems and lead to sites easily having weird behavior. If we don't force a context then this will require a Puppet change so OSC side doesn't break. It would still require a Puppet change to expose all options but not required if we have sane default.