Class: LanguageOperator::CLI::Helpers::ClusterContext
- Inherits:
-
Object
- Object
- LanguageOperator::CLI::Helpers::ClusterContext
- Defined in:
- lib/language_operator/cli/helpers/cluster_context.rb
Overview
Encapsulates cluster context (name, config, client) to reduce boilerplate in command implementations.
Instead of repeating:
cluster = ClusterValidator.get_cluster([:cluster])
cluster_config = ClusterValidator.get_cluster_config(cluster)
k8s = ClusterValidator.kubernetes_client([:cluster])
Use:
ctx = ClusterContext.()
# Access: ctx.name, ctx.config, ctx.client, ctx.namespace
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Class Method Summary collapse
-
.from_options(options) ⇒ ClusterContext
Create ClusterContext from command options hash.
Instance Method Summary collapse
-
#initialize(name, config, client) ⇒ ClusterContext
constructor
Initialize with cluster details.
-
#kubectl_args ⇒ Hash
Build kubectl command args for this cluster context All user-controlled inputs are properly escaped to prevent command injection.
-
#kubectl_prefix ⇒ String
Build kubectl command prefix string.
Constructor Details
#initialize(name, config, client) ⇒ ClusterContext
Initialize with cluster details
37 38 39 40 41 42 |
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 37 def initialize(name, config, client) @name = name @config = config @client = client @namespace = config[:namespace] end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
21 22 23 |
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 21 def client @client end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
21 22 23 |
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 21 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 21 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
21 22 23 |
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 21 def namespace @namespace end |
Class Method Details
.from_options(options) ⇒ ClusterContext
Create ClusterContext from command options hash
26 27 28 29 30 31 |
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 26 def self.() name = ClusterValidator.get_cluster([:cluster]) config = ClusterValidator.get_cluster_config(name) client = ClusterValidator.kubernetes_client([:cluster]) new(name, config, client) end |
Instance Method Details
#kubectl_args ⇒ Hash
Build kubectl command args for this cluster context All user-controlled inputs are properly escaped to prevent command injection
47 48 49 50 51 52 53 |
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 47 def kubectl_args { kubeconfig: config[:kubeconfig] ? "--kubeconfig=#{Shellwords.escape(config[:kubeconfig])}" : '', context: config[:context] ? "--context=#{Shellwords.escape(config[:context])}" : '', namespace: "-n #{Shellwords.escape(namespace)}" } end |
#kubectl_prefix ⇒ String
Build kubectl command prefix string
57 58 59 60 |
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 57 def kubectl_prefix args = kubectl_args "kubectl #{args[:kubeconfig]} #{args[:context]} #{args[:namespace]}".strip.squeeze(' ') end |