|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Metatron |
| 4 | + module Templates |
| 5 | + # The LimitRange Kubernetes resource |
| 6 | + class LimitRange < Template |
| 7 | + # Limit is an internal class used to represent the limits in a LimitRange resource, |
| 8 | + # and to provide a consistent interface for rendering them. It is not intended to be |
| 9 | + # used outside of this class. Eventually, it may be used to provide validation for |
| 10 | + # the limits. |
| 11 | + class Limit |
| 12 | + attr_accessor :type, :default, :default_request, :max, :max_limit_request_ratio, :min |
| 13 | + |
| 14 | + alias defaultRequest default_request |
| 15 | + alias maxLimitRequestRatio max_limit_request_ratio |
| 16 | + |
| 17 | + class << self |
| 18 | + def to_limit(limit) |
| 19 | + limit.is_a?(Limit) ? limit : new(**limit) |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + def initialize(type:, default: nil, default_request: nil, max: nil, |
| 24 | + max_limit_request_ratio: nil, min: nil) |
| 25 | + @type = type |
| 26 | + @default = default |
| 27 | + @default_request = default_request |
| 28 | + @max = max |
| 29 | + @max_limit_request_ratio = max_limit_request_ratio |
| 30 | + @min = min |
| 31 | + end |
| 32 | + |
| 33 | + def render |
| 34 | + { |
| 35 | + type:, |
| 36 | + default:, |
| 37 | + defaultRequest:, |
| 38 | + max:, |
| 39 | + maxLimitRequestRatio:, |
| 40 | + min: |
| 41 | + }.compact |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + include Concerns::Annotated |
| 46 | + include Concerns::Namespaced |
| 47 | + |
| 48 | + attr_accessor :limits |
| 49 | + |
| 50 | + def initialize(name, limits = []) |
| 51 | + super(name) |
| 52 | + @limits = limits |
| 53 | + end |
| 54 | + |
| 55 | + def render |
| 56 | + { |
| 57 | + apiVersion:, |
| 58 | + kind:, |
| 59 | + metadata: { |
| 60 | + name:, |
| 61 | + labels: base_labels.merge(additional_labels) |
| 62 | + }.merge(formatted_annotations).merge(formatted_namespace), |
| 63 | + spec: formatted_limits |
| 64 | + }.compact |
| 65 | + end |
| 66 | + |
| 67 | + private |
| 68 | + |
| 69 | + def formatted_limits |
| 70 | + return {} if limits.empty? |
| 71 | + |
| 72 | + { limits: limits.map { Limit.to_limit(_1).render } } |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | +end |
0 commit comments