Class: Kubetailrb::Cmd::K8s
- Inherits:
-
Object
- Object
- Kubetailrb::Cmd::K8s
- Defined in:
- lib/kubetailrb/cmd/k8s.rb
Overview
Command to read k8s pod logs.
Constant Summary collapse
- DEFAULT_NB_LINES =
10- DEFAULT_NAMESPACE =
'default'- DEFAULT_CONTAINER_QUERY =
'.*'- NAMESPACE_FLAGS =
%w[-n --namespace].freeze
- TAIL_FLAG =
'--tail'- FOLLOW_FLAGS =
%w[-f --follow].freeze
- RAW_FLAGS =
%w[-r --raw].freeze
- DISPLAY_NAMES_FLAG =
'--display-names'- CONTAINER_FLAGS =
%w[-c --container].freeze
- EXCLUDES_FLAGS =
%w[-e --excludes].freeze
- MDCS_FLAGS =
%w[-m --mdcs].freeze
- FLAGS_WITH_VALUES =
( NAMESPACE_FLAGS + [TAIL_FLAG] + CONTAINER_FLAGS + EXCLUDES_FLAGS + MDCS_FLAGS ).freeze
Instance Attribute Summary collapse
-
#reader ⇒ Object
readonly
Returns the value of attribute reader.
Class Method Summary collapse
- .create(*args) ⇒ Object
- .parse_container_query(*args) ⇒ Object
- .parse_display_names(*args) ⇒ Object
-
.parse_excludes(*args) ⇒ Object
Parse log exclusion from arguments provided in the CLI, e.g.
- .parse_follow(*args) ⇒ Object
-
.parse_mdcs(*args) ⇒ Object
Parse MDCs to include from arguments provided in the CLI, e.g.
-
.parse_namespace(*args) ⇒ Object
Parse k8s namespace from arguments provided in the CLI, e.g.
-
.parse_nb_lines(*args) ⇒ Object
Parse nb lines from arguments provided in the CLI, e.g.
- .parse_pod_query(args) ⇒ Object
- .parse_raw(*args) ⇒ Object
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(pod_query:, container_query:, opts:) ⇒ K8s
constructor
A new instance of K8s.
Constructor Details
#initialize(pod_query:, container_query:, opts:) ⇒ K8s
Returns a new instance of K8s.
34 35 36 37 38 39 40 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 34 def initialize(pod_query:, container_query:, opts:) @reader = Kubetailrb::Reader::K8sPodsReader.new( pod_query: pod_query, container_query: container_query, opts: opts ) end |
Instance Attribute Details
#reader ⇒ Object (readonly)
Returns the value of attribute reader.
32 33 34 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 32 def reader @reader end |
Class Method Details
.create(*args) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 47 def create(*args) new( pod_query: parse_pod_query(args), container_query: parse_container_query(*args), opts: K8sOpts.new( namespace: parse_namespace(*args), last_nb_lines: parse_nb_lines(*args), follow: parse_follow(*args), raw: parse_raw(*args), display_names: parse_display_names(*args), excludes: parse_excludes(*args), mdcs: parse_mdcs(*args) ) ) end |
.parse_container_query(*args) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 137 def parse_container_query(*args) return DEFAULT_CONTAINER_QUERY unless args.any? { |arg| CONTAINER_FLAGS.include?(arg) } index = args.find_index { |arg| CONTAINER_FLAGS.include?(arg) }.to_i raise ArgumentError, "Missing #{CONTAINER_FLAGS} value." if args[index + 1].nil? args[index + 1] end |
.parse_display_names(*args) ⇒ Object
147 148 149 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 147 def parse_display_names(*args) args.include?(DISPLAY_NAMES_FLAG) end |
.parse_excludes(*args) ⇒ Object
Parse log exclusion from arguments provided in the CLI, e.g.
kubetailrb some-pod --exclude access-logs,dd-logs
will return [access-logs, dd-logs].
Will raise ArgumentError if the value is not provided:
kubetailrb some-pod --exclude
162 163 164 165 166 167 168 169 170 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 162 def parse_excludes(*args) return [] unless args.any? { |arg| EXCLUDES_FLAGS.include?(arg) } index = args.find_index { |arg| EXCLUDES_FLAGS.include?(arg) }.to_i raise ArgumentError, "Missing #{EXCLUDES_FLAGS} value." if args[index + 1].nil? args[index + 1].split(',') end |
.parse_follow(*args) ⇒ Object
129 130 131 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 129 def parse_follow(*args) args.any? { |arg| FOLLOW_FLAGS.include?(arg) } end |
.parse_mdcs(*args) ⇒ Object
Parse MDCs to include from arguments provided in the CLI, e.g.
kubetailrb some-pod --mdc account.id,process.thread.name
will return [account.id, thread.name].
Will raise ArgumentError if the value is not provided:
kubetailrb some-pod --mdc
183 184 185 186 187 188 189 190 191 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 183 def parse_mdcs(*args) return [] unless args.any? { |arg| MDCS_FLAGS.include?(arg) } index = args.find_index { |arg| MDCS_FLAGS.include?(arg) }.to_i raise ArgumentError, "Missing #{MDCS_FLAGS} value." if args[index + 1].nil? args[index + 1].split(',') end |
.parse_namespace(*args) ⇒ Object
Parse k8s namespace from arguments provided in the CLI, e.g.
kubetailrb some-pod -n sandbox
will return 'sandbox'.
Will raise ArgumentError if the value is not provided:
kubetailrb some-pod -n
89 90 91 92 93 94 95 96 97 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 89 def parse_namespace(*args) return DEFAULT_NAMESPACE unless args.any? { |arg| NAMESPACE_FLAGS.include?(arg) } index = args.find_index { |arg| NAMESPACE_FLAGS.include?(arg) }.to_i raise ArgumentError, "Missing #{NAMESPACE_FLAGS} value." if args[index + 1].nil? args[index + 1] end |
.parse_nb_lines(*args) ⇒ Object
Parse nb lines from arguments provided in the CLI, e.g.
kubetailrb some-pod --tail 3
will return 3.
Will raise ArgumentError if the value is not provided:
kubetailrb some-pod --tail
Will raise ArgumentError if the provided value is not a
number:
kubetailrb some-pod --tail some-string
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 115 def parse_nb_lines(*args) return DEFAULT_NB_LINES unless args.include?(TAIL_FLAG) index = args.find_index { |arg| arg == TAIL_FLAG }.to_i raise ArgumentError, "Missing #{TAIL_FLAG} value." if args[index + 1].nil? last_nb_lines = args[index + 1].to_i raise ArgumentError, "Invalid #{TAIL_FLAG} value: #{args[index + 1]}." if last_nb_lines.zero? last_nb_lines end |
.parse_pod_query(args) ⇒ Object
63 64 65 66 67 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 63 def parse_pod_query(args) args.each_with_index.find do |arg, idx| !arg.start_with?('-') && !consumed_by_flag?(args, idx) end&.first end |
.parse_raw(*args) ⇒ Object
133 134 135 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 133 def parse_raw(*args) args.any? { |arg| RAW_FLAGS.include?(arg) } end |
Instance Method Details
#execute ⇒ Object
42 43 44 |
# File 'lib/kubetailrb/cmd/k8s.rb', line 42 def execute @reader.read end |