Skip to content

Commit

Permalink
improve iam role infer_cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
tongueroo committed Feb 16, 2022
1 parent 2f11fad commit a906b66
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/kubes_aws/iam_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class IamRole

# public method to keep: role_name
attr_reader :role_name
def initialize(app:, cluster:, namespace:nil, managed_policies: [], inline_policies: [], role_name: nil, ksa: nil)
def initialize(app:, cluster:nil, namespace:nil, managed_policies: [], inline_policies: [], role_name: nil, ksa: nil)
@app, @cluster, @managed_policies, @inline_policies = app, cluster, managed_policies, inline_policies

# conventional names
@ksa = ksa || @app # convention: app
@namespace = namespace || "#{@app}-#{Kubes.env}" # convention: app-env
@role_name = role_name || "#{@app}-#{Kubes.env}" # convention: app-env
@cluster ||= infer_cluster
end

def call
Expand All @@ -27,6 +28,24 @@ def call
add_inline_policies
end

# Attempts to infer the EKS cluster name using kubectl
def infer_cluster
command = "kubectl config view --minify --output 'jsonpath={..contexts..context.cluster}'"
out = `#{command}`
success = $?.success?
name = out.split('/').last
if !success or name.blank?
logger.error <<~EOL.color(:red)
ERROR: unable to determine EKS cluster name. Please specify it in:
KubesAws::IamRole.new
EOL
exit 1
end
name
end

def add_inline_policies
@inline_policies.each do |policy|
params = normalize_inline_policy(policy)
Expand Down

0 comments on commit a906b66

Please sign in to comment.