Skip to content

Create a service account and token for use in ~/.kube/config #1458

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

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ae86c28
Create a service account and token during "pharos kubeconfig"
Aug 13, 2019
ab3a108
Revert kubeconfig and move to phase
Aug 14, 2019
e9fc165
Transport file fixes to support overwriting and path expansion
Aug 14, 2019
32d69bc
Trailing blank
Aug 14, 2019
074adb4
Skip installing a copy of /etc/kubernetes/admin.conf to home
Aug 14, 2019
3b37658
Add expand option to transport.file
Aug 14, 2019
c59ae39
Prefer ~/.kube/config in ConfigureClient
Aug 14, 2019
39b7d44
Simpler expand
Aug 14, 2019
5be5ced
Why it no work [cluster-e2e]
Aug 14, 2019
f70d81f
Cant modify frozen string [cluster-e2e]
Aug 14, 2019
dc6e523
Recomplexify [cluster-e2e]
Aug 14, 2019
c0b7278
Spec dont like expand [cluster-e2e]
Aug 14, 2019
e18ead5
More spec fix [cluster-e2e]
Aug 14, 2019
ddd82af
More fix [cluster-e2e]
Aug 14, 2019
890788f
Add clarifying comments [cluster-e2e]
Aug 14, 2019
b08de30
Update yardoc and retrigger e2e [cluster-e2e]
Aug 14, 2019
324c8ed
Use const [cluster-e2e]
Aug 14, 2019
888c5b9
Adding log messages to make sense of e2e failure [cluster-e2e]
Aug 14, 2019
6a7e4e5
And what does the config look like? [cluster-e2e]
Aug 14, 2019
5e4f466
bang [cluster-e2e]
Aug 14, 2019
986c2a4
Less noise, retrigger e2e [cluster-e2e]
Aug 14, 2019
c23f89c
Slight tweak. I dont know why this fails on drone. [cluster-e2e]
Aug 14, 2019
053ffab
Display host env [cluster-e2e]
Aug 15, 2019
2701644
Try with --kubeconfig [cluster-e2e]
Aug 15, 2019
1792003
Debug [cluster-e2e]
Aug 15, 2019
aea1b80
Merge branch 'master' into feature/sa_token
Aug 21, 2019
a91e213
Retrigger e2e [cluster-e2e]
Aug 21, 2019
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
8 changes: 4 additions & 4 deletions lib/pharos/kube/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ def initialize(content = nil)
def config
@config ||= yaml_content || {
'apiVersion' => 'v1',
'kind' => 'Config',
'clusters' => [],
'contexts' => [],
'current-context' => nil,
'kind' => 'Config',
'users' => [],
'preferences' => {},
'users' => []
'current-context' => nil
}
end
alias to_h config

# Convert to YAML
# @return [String]
def dump
YAML.dump(config)
YAML.dump(JSON.parse(JSON.dump(config))) # dereference to get rid of *1 &1 etc in output
end
alias to_s dump

Expand Down
49 changes: 39 additions & 10 deletions lib/pharos/kubeconfig_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module Pharos
class KubeconfigCommand < Pharos::Command
options :load_config, :tf_json

option ['-n', '--name'], 'NAME', 'overwrite cluster name', attribute_name: :new_name
option ['-C', '--context'], 'CONTEXT', 'overwrite context name', attribute_name: :new_context
option ['-n', '--name'], 'NAME', 'cluster name', attribute_name: :cluster_name, default: 'pharos-cluster'
option ['-C', '--context'], 'CONTEXT', 'context name', attribute_name: :context_name
option ['-u', '--user'], 'USER', 'user name', attribute_name: :user_name, default: 'pharos-admin'
option ['-m', '--merge'], '[FILE]', 'merge with existing configuration file', multivalued: true

REMOTE_FILE = "/etc/kubernetes/admin.conf"
Expand All @@ -14,10 +15,32 @@ def execute
Dir.chdir(config_yaml.dirname) do
transport.connect

config = Pharos::Kube::Config.new(config_file_content)
config.rename_cluster(new_name) if new_name
config.rename_context(new_context) if new_context
config.update_server_address(master_host.api_address)
config = Pharos::Kube::Config.new
config.config['clusters'] << {
'cluster' => {
'certificate-authority-data' => certificate_authority_data,
'server' => "https://#{master_host.api_address}:6443"
},
'name' => cluster_name
}

config.config['users'] << {
'user' => {
'token' => create_or_update_sa_token
},
'name' => user_name
}

config.config['contexts'] << {
'context' => {
'cluster' => cluster_name,
'user' => 'pharos-admin'
},
'name' => context_name || "#{user_name}@#{cluster_name}"
}

config.config['current-context'] = context_name || "#{user_name}@#{cluster_name}"

merge_list.each do |merge|
merge_config = Pharos::Kube::Config.new(File.read(merge))
config << merge_config
Expand All @@ -28,10 +51,16 @@ def execute

private

def config_file_content
file = transport.file(REMOTE_FILE)
signal_usage_error "Remote file #{REMOTE_FILE} not found" unless file.exist?
file.read
# @return token [String]
def create_or_update_sa_token
transport.exec!("kubectl get -n kube-system serviceaccount/#{user_name} || kubectl -n kube-system create serviceaccount #{user_name}")
transport.exec!("kubectl get clusterrolebinding pharos-cluster-admin || kubectl create clusterrolebinding pharos-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:#{user_name}")
token_name = transport.exec!("kubectl -n kube-system get serviceaccount/#{user_name} -o jsonpath='{.secrets[0].name}'")
transport.exec!("kubectl -n kube-system get secret #{token_name} -o jsonpath='{.data.token}' | base64 -d")
end

def certificate_authority_data
transport.exec!("kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}'")
end

def master_host
Expand Down