Class: Gitlab::Kubernetes::Kubeconfig::Template
- Inherits:
-
Object
- Object
- Gitlab::Kubernetes::Kubeconfig::Template
- Defined in:
- lib/gitlab/kubernetes/kubeconfig/template.rb
Constant Summary collapse
- ENTRIES =
{ cluster: Gitlab::Kubernetes::Kubeconfig::Entry::Cluster, user: Gitlab::Kubernetes::Kubeconfig::Entry::User, context: Gitlab::Kubernetes::Kubeconfig::Entry::Context }.freeze
Instance Method Summary collapse
- #add_cluster(**args) ⇒ Object
- #add_context(**args) ⇒ Object
- #add_user(**args) ⇒ Object
-
#initialize ⇒ Template
constructor
A new instance of Template.
- #merge_yaml(kubeconfig_yaml) ⇒ Object
- #to_h ⇒ Object
- #to_yaml ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ Template
Returns a new instance of Template.
13 14 15 16 17 18 |
# File 'lib/gitlab/kubernetes/kubeconfig/template.rb', line 13 def initialize @clusters = [] @users = [] @contexts = [] @current_context = nil end |
Instance Method Details
#add_cluster(**args) ⇒ Object
24 25 26 |
# File 'lib/gitlab/kubernetes/kubeconfig/template.rb', line 24 def add_cluster(**args) clusters << new_entry(:cluster, **args) end |
#add_context(**args) ⇒ Object
32 33 34 |
# File 'lib/gitlab/kubernetes/kubeconfig/template.rb', line 32 def add_context(**args) contexts << new_entry(:context, **args) end |
#add_user(**args) ⇒ Object
28 29 30 |
# File 'lib/gitlab/kubernetes/kubeconfig/template.rb', line 28 def add_user(**args) users << new_entry(:user, **args) end |
#merge_yaml(kubeconfig_yaml) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gitlab/kubernetes/kubeconfig/template.rb', line 36 def merge_yaml(kubeconfig_yaml) return unless kubeconfig_yaml kubeconfig_yaml = YAML.safe_load(kubeconfig_yaml, symbolize_names: true) kubeconfig_yaml[:users].each do |user| add_user( name: user[:name], token: user.dig(:user, :token) ) end kubeconfig_yaml[:clusters].each do |cluster| ca_pem = cluster.dig(:cluster, :'certificate-authority-data')&.yield_self do |data| Base64.strict_decode64(data) end add_cluster( name: cluster[:name], url: cluster.dig(:cluster, :server), ca_pem: ca_pem ) end kubeconfig_yaml[:contexts].each do |context| add_context( name: context[:name], **context[:context]&.slice(:cluster, :user, :namespace) ) end @current_context = kubeconfig_yaml[:'current-context'] end |
#to_h ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/gitlab/kubernetes/kubeconfig/template.rb', line 66 def to_h { apiVersion: 'v1', kind: 'Config', clusters: clusters.map(&:to_h), users: users.map(&:to_h), contexts: contexts.map(&:to_h), 'current-context': current_context }.compact end |
#to_yaml ⇒ Object
77 78 79 |
# File 'lib/gitlab/kubernetes/kubeconfig/template.rb', line 77 def to_yaml YAML.dump(to_h.deep_stringify_keys) end |
#valid? ⇒ Boolean
20 21 22 |
# File 'lib/gitlab/kubernetes/kubeconfig/template.rb', line 20 def valid? contexts.present? end |