Class: LanguageOperator::Utils::OrgContext

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/utils/org_context.rb

Overview

Utility for detecting organization context from cluster resources

Constant Summary collapse

ORG_ID_LABEL =
'langop.io/organization-id'

Class Method Summary collapse

Class Method Details

.clear_cache!Object

Clear the organization context cache



46
47
48
# File 'lib/language_operator/utils/org_context.rb', line 46

def clear_cache!
  @cache_mutex.synchronize { @cache.clear }
end

.current_org_id(k8s_client) ⇒ String?

Get the current organization ID from cluster resources

Parameters:

Returns:

  • (String, nil)

    Organization ID or nil if not found



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/language_operator/utils/org_context.rb', line 18

def current_org_id(k8s_client)
  return nil unless k8s_client

  cache_key = "#{k8s_client.context}:#{k8s_client.current_namespace}"

  @cache_mutex.synchronize do
    # Return cached result if available and not expired
    cached = @cache[cache_key]
    return cached[:org_id] if cached && Time.now - cached[:timestamp] < cache_ttl

    # Detect org ID from cluster resources
    org_id = detect_org_id(k8s_client)

    # Cache the result
    @cache[cache_key] = {
      org_id: org_id,
      timestamp: Time.now
    }

    org_id
  end
rescue StandardError => e
  # Log error but don't fail - return nil for legacy mode
  warn "Warning: Could not detect organization context: #{e.message}" if ENV['DEBUG']
  nil
end

.org_context?(resource) ⇒ Boolean

Check if a resource has organization context

Parameters:

  • resource (Hash)

    Kubernetes resource manifest

Returns:

  • (Boolean)

    True if resource has org ID label



62
63
64
# File 'lib/language_operator/utils/org_context.rb', line 62

def org_context?(resource)
  !org_id_from_resource(resource).nil?
end

.org_id_from_resource(resource) ⇒ String?

Get organization ID from a specific resource

Parameters:

  • resource (Hash)

    Kubernetes resource manifest

Returns:

  • (String, nil)

    Organization ID from labels



54
55
56
# File 'lib/language_operator/utils/org_context.rb', line 54

def org_id_from_resource(resource)
  resource.dig('metadata', 'labels', ORG_ID_LABEL)
end