Class: LanguageOperator::CLI::Helpers::ClusterContext

Inherits:
Object
  • Object
show all
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(options[:cluster])
cluster_config = ClusterValidator.get_cluster_config(cluster)
k8s = ClusterValidator.kubernetes_client(options[:cluster])

Use:

ctx = ClusterContext.from_options(options)
# Access: ctx.name, ctx.config, ctx.client, ctx.namespace

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, config, client) ⇒ ClusterContext

Initialize with cluster details

Parameters:



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

#clientObject (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

#configObject (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

#nameObject (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

#namespaceObject (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

Parameters:

  • options (Hash)

    Thor command options (expects :cluster key)

Returns:



26
27
28
29
30
31
# File 'lib/language_operator/cli/helpers/cluster_context.rb', line 26

def self.from_options(options)
  name = ClusterValidator.get_cluster(options[:cluster])
  config = ClusterValidator.get_cluster_config(name)
  client = ClusterValidator.kubernetes_client(options[:cluster])
  new(name, config, client)
end

Instance Method Details

#kubectl_argsHash

Build kubectl command args for this cluster context All user-controlled inputs are properly escaped to prevent command injection

Returns:

  • (Hash)

    kubectl arguments



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_prefixString

Build kubectl command prefix string

Returns:

  • (String)

    kubectl command prefix



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