Class: Kubes::Compiler::Decorator::Hashable::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/kubes/compiler/decorator/hashable/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ Field

Returns a new instance of Field.



9
10
11
# File 'lib/kubes/compiler/decorator/hashable/field.rb', line 9

def initialize(item)
  @item = item
end

Instance Attribute Details

#itemObject (readonly)

item is full structure

secretRef:           <--- wrapper_key
  name: demo-secret  <--- target_key is 'name' from wrapper_map[wrapper_key]


8
9
10
# File 'lib/kubes/compiler/decorator/hashable/field.rb', line 8

def item
  @item
end

Instance Method Details

#hashable?Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/kubes/compiler/decorator/hashable/field.rb', line 13

def hashable?
  x = @item.keys & wrapper_map.keys
  !x.empty?
end

#kindObject



18
19
20
# File 'lib/kubes/compiler/decorator/hashable/field.rb', line 18

def kind
  wrapper_key =~ /configMap/ ? "ConfigMap" : "Secret"
end

#target_keyObject

The target key of the hashable value is that key used for find value to add hash

envFrom:
- secretRef:             <--- wrapper_key
    name: demo-secret    <--- target_key is 'name' from wrapper_map[wrapper_key]


28
29
30
# File 'lib/kubes/compiler/decorator/hashable/field.rb', line 28

def target_key
  wrapper_map[wrapper_key]
end

#wrapper_keyObject

The wrapper field is nested right above the item with the hashable value. Simple example:

envFrom:
- secretRef:           <--- wrapper_key
    name: demo-secret  <--- target_key is 'name' from wrapper_map[wrapper_key]

More complex example where there’s a configMap.name key also on the same level.

volumes:
- name: config-map-volume
  configMap:
    name: demo-config-map

Note: Wont work for case when there’s are 2 matching wrapper_map keys, but don’t think Kubernetes allows 2 matching wrapper_map keys. Example: This is invalid Kubernetes YAML.

volumes:
- name: config-map-volume
  configMap:
    name: demo-config-map
  secretRef:
    name: demo-seret


56
57
58
# File 'lib/kubes/compiler/decorator/hashable/field.rb', line 56

def wrapper_key
  @item.keys.find { |k| wrapper_map.keys.include?(k) } # this key used for map[wrapper_key]
end

#wrapper_mapObject

wrapper element to key that stores the hashable value



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kubes/compiler/decorator/hashable/field.rb', line 61

def wrapper_map
  {
    'configMapRef' => 'name',    # containers.env.envFrom.configMapRef.name
    'configMapKeyRef' => 'name', # containers.env.valueFrom.configMapKeyRef.name
    'configMap' => 'name',       # containers.env.envFrom.configMapRef.name
    'secretRef' => 'name',       # containers.env.envFrom.secretRef.name
    'secretKeyRef' => 'name',    # containers.env.valueFrom.secretKeyRef.name
    'secret' => 'secretName',    # volumes.secret.secretName
    'tls' => 'secretName',       # tls.secretName
  }
end