-
Notifications
You must be signed in to change notification settings - Fork 40
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
kke
wants to merge
27
commits into
master
Choose a base branch
from
feature/sa_token
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
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"
ab3a108
Revert kubeconfig and move to phase
e9fc165
Transport file fixes to support overwriting and path expansion
32d69bc
Trailing blank
074adb4
Skip installing a copy of /etc/kubernetes/admin.conf to home
3b37658
Add expand option to transport.file
c59ae39
Prefer ~/.kube/config in ConfigureClient
39b7d44
Simpler expand
5be5ced
Why it no work [cluster-e2e]
f70d81f
Cant modify frozen string [cluster-e2e]
dc6e523
Recomplexify [cluster-e2e]
c0b7278
Spec dont like expand [cluster-e2e]
e18ead5
More spec fix [cluster-e2e]
ddd82af
More fix [cluster-e2e]
890788f
Add clarifying comments [cluster-e2e]
b08de30
Update yardoc and retrigger e2e [cluster-e2e]
324c8ed
Use const [cluster-e2e]
888c5b9
Adding log messages to make sense of e2e failure [cluster-e2e]
6a7e4e5
And what does the config look like? [cluster-e2e]
5e4f466
bang [cluster-e2e]
986c2a4
Less noise, retrigger e2e [cluster-e2e]
c23f89c
Slight tweak. I dont know why this fails on drone. [cluster-e2e]
053ffab
Display host env [cluster-e2e]
2701644
Try with --kubeconfig [cluster-e2e]
1792003
Debug [cluster-e2e]
aea1b80
Merge branch 'master' into feature/sa_token
a91e213
Retrigger e2e [cluster-e2e]
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# frozen_string_literal: true | ||
|
||
module Pharos | ||
module Phases | ||
class ConfigureServiceAccount < Pharos::Phase | ||
title "Configure 'pharos-admin' service account" | ||
|
||
ADMIN_USER = 'pharos-admin' | ||
KUBECONFIG_PARAM = '--kubeconfig=/etc/kubernetes/admin.conf' | ||
|
||
def call | ||
logger.info "Creating service account" | ||
create_service_account | ||
logger.info "Creating cluster role binding" | ||
create_cluster_role_binding | ||
|
||
config = build_config | ||
|
||
if config_file.exist? | ||
logger.info "Merging existing configuration" | ||
existing_config = Pharos::Kube::Config.new(config_file.read) | ||
config << existing_config | ||
end | ||
|
||
config_file.write(config.dump, overwrite: true) | ||
config_file.chmod('0600') | ||
|
||
logger.info "Testing new configuration" | ||
validate | ||
end | ||
|
||
def validate | ||
# Validates that "kubectl" without sudo or setting KUBECONFIG / --kubeconfig works on the host | ||
transport.exec!("kubectl get --kubeconfig=/root/.kube/config -n kube-system serviceaccount/#{ADMIN_USER}") | ||
end | ||
|
||
def config_file | ||
@config_file ||= transport.file(File.join(home_kube_dir.path, 'config')) | ||
end | ||
|
||
# Get a real path to ~/.kube and mkdir + chmod it unless exists | ||
def home_kube_dir | ||
transport.file('~/.kube', expand: true).tap do |dir| | ||
transport.exec!("mkdir '#{dir}' && chmod 0700 '#{dir}") unless dir.exist? | ||
end | ||
end | ||
|
||
# Sudo used because /etc/kubernetes/admin.conf is not user-readable | ||
def create_service_account | ||
transport.exec!("sudo kubectl get #{KUBECONFIG_PARAM} -n kube-system serviceaccount/#{ADMIN_USER} || sudo kubectl #{KUBECONFIG_PARAM} -n kube-system create serviceaccount #{ADMIN_USER}") | ||
end | ||
|
||
def create_cluster_role_binding | ||
transport.exec!("sudo kubectl get #{KUBECONFIG_PARAM} -n kube-system clusterrolebinding/pharos-cluster-admin || sudo kubectl create #{KUBECONFIG_PARAM} -n kube-system clusterrolebinding pharos-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:#{ADMIN_USER}") | ||
end | ||
|
||
# @return token_name [String] | ||
def token_name | ||
transport.exec!("sudo kubectl -n kube-system #{KUBECONFIG_PARAM} get serviceaccount/#{ADMIN_USER} -o jsonpath='{.secrets[0].name}'") | ||
end | ||
|
||
# @return token [String] | ||
def token | ||
@token ||= transport.exec!("sudo kubectl -n kube-system #{KUBECONFIG_PARAM} get secret #{token_name} -o jsonpath='{.data.token}' | base64 -d") | ||
end | ||
|
||
# @return [Pharos::Kube::Config] | ||
def build_config | ||
config = Pharos::Kube::Config.new | ||
config.config['clusters'] << { | ||
'cluster' => { | ||
'certificate-authority-data' => certificate_authority_data, | ||
'server' => "https://#{master_host.api_address}:6443" | ||
}, | ||
'name' => @config.name | ||
} | ||
|
||
config.config['users'] << { | ||
'user' => { | ||
'token' => token | ||
}, | ||
'name' => ADMIN_USER | ||
} | ||
|
||
config.config['contexts'] << { | ||
'context' => { | ||
'cluster' => @config.name, | ||
'user' => ADMIN_USER | ||
}, | ||
'name' => context_name | ||
} | ||
|
||
config.config['current-context'] = context_name | ||
|
||
config | ||
end | ||
|
||
# @return [String] | ||
def context_name | ||
@context_name ||= "#{ADMIN_USER}@#{@config.name}" | ||
end | ||
|
||
# @return [String] | ||
def certificate_authority_data | ||
# --flatten expands relative paths, --minify cleans out everything but the stuff needed by current_context | ||
transport.exec!("sudo kubectl config view #{KUBECONFIG_PARAM} --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}'") | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
IMO we don't need sudo for
kubectl
.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.
Ok, maybe we need because this points to root readable kubeconfig?
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.
error: Error loading config file "/etc/kubernetes/admin.conf": open /etc/kubernetes/admin.conf: permission denied