Module: LanguageOperator::CLI::Helpers::LabelUtils
- Defined in:
- lib/language_operator/cli/helpers/label_utils.rb
Overview
Utilities for working with Kubernetes labels
Class Method Summary collapse
-
.agent_pod_selector(agent_name) ⇒ String
Build a label selector for finding agent pods.
-
.debug_pod_search(ctx, agent_name) ⇒ Hash
Get debugging information about label selector matching.
-
.find_pods_by_deployment_labels(ctx, deployment_name, labels) ⇒ Array
Convert deployment labels to pod selector and find matching pods.
-
.normalize_agent_name(agent_name) ⇒ String
Normalize an agent name for use as a Kubernetes label value.
-
.valid_label_value?(agent_name) ⇒ Boolean
Validate that an agent name will work as a label value.
Class Method Details
.agent_pod_selector(agent_name) ⇒ String
Build a label selector for finding agent pods
29 30 31 32 |
# File 'lib/language_operator/cli/helpers/label_utils.rb', line 29 def self.agent_pod_selector(agent_name) normalized_name = normalize_agent_name(agent_name) Constants::KubernetesLabels.agent_selector(normalized_name) end |
.debug_pod_search(ctx, agent_name) ⇒ Hash
Get debugging information about label selector matching
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/language_operator/cli/helpers/label_utils.rb', line 59 def self.debug_pod_search(ctx, agent_name) selector = agent_pod_selector(agent_name) { agent_name: agent_name, normalized_name: normalize_agent_name(agent_name), label_selector: selector, namespace: ctx.namespace, valid_label_value: valid_label_value?(agent_name) } end |
.find_pods_by_deployment_labels(ctx, deployment_name, labels) ⇒ Array
Convert deployment labels to pod selector and find matching pods
This method handles the common pattern of extracting selector labels from a deployment, converting them to a label selector string, and finding matching pods.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/language_operator/cli/helpers/label_utils.rb', line 81 def self.find_pods_by_deployment_labels(ctx, deployment_name, labels) raise "Deployment '#{deployment_name}' has no selector labels" if labels.nil? # Convert to hash if needed (K8s API may return K8s::Resource objects) labels_hash = labels.respond_to?(:to_h) ? labels.to_h : labels raise "Deployment '#{deployment_name}' has empty selector labels" if labels_hash.empty? # Convert labels to Kubernetes label selector format label_selector = labels_hash.map { |k, v| "#{k}=#{v}" }.join(',') # Find matching pods ctx.client.list_resources('Pod', namespace: ctx.namespace, label_selector: label_selector) end |
.normalize_agent_name(agent_name) ⇒ String
Normalize an agent name for use as a Kubernetes label value
Kubernetes label values must follow DNS-1123 subdomain format:
- contain only lowercase alphanumeric characters, '-' or '.'
- start and end with an alphanumeric character
- be at most 63 characters
19 20 21 22 23 |
# File 'lib/language_operator/cli/helpers/label_utils.rb', line 19 def self.normalize_agent_name(agent_name) # Agent names should already be valid Kubernetes resource names, # which are compatible with label values. Just ensure lowercase. agent_name.to_s.downcase end |
.valid_label_value?(agent_name) ⇒ Boolean
Validate that an agent name will work as a label value
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/language_operator/cli/helpers/label_utils.rb', line 38 def self.valid_label_value?(agent_name) return false if agent_name.nil? || agent_name.empty? # Check original string first (before normalization) agent_str = agent_name.to_s # Check DNS-1123 subdomain requirements: # - 63 characters or less # - lowercase letters, numbers, hyphens, and dots only # - start and end with alphanumeric character return false if agent_str.length > 63 return false unless agent_str.match?(/\A[a-z0-9]([a-z0-9\-.]*[a-z0-9])?\z/) true end |