Skip to content

Commit

Permalink
remove global blank/present monkey-patch (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Oct 8, 2020
1 parent 672a6bc commit f65617d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 38 deletions.
23 changes: 13 additions & 10 deletions lib/fluent/plugin/filter_kubernetes_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def log.trace?
require 'lru_redux'
@stats = KubernetesMetadata::Stats.new

if @de_dot && (@de_dot_separator =~ /\./).present?
if @de_dot && @de_dot_separator.include?(".")
raise Fluent::ConfigError, "Invalid de_dot_separator: cannot be or contain '.'"
end

Expand All @@ -173,7 +173,7 @@ def log.trace?

env_host = ENV['KUBERNETES_SERVICE_HOST']
env_port = ENV['KUBERNETES_SERVICE_PORT']
if env_host.present? && env_port.present?
if present?(env_host) && present?(env_port)
if env_host =~ Resolv::IPv6::Regex
# Brackets are needed around IPv6 addresses
env_host = "[#{env_host}]"
Expand All @@ -191,22 +191,21 @@ def log.trace?
ca_cert = File.join(@secret_dir, K8_POD_CA_CERT)
pod_token = File.join(@secret_dir, K8_POD_TOKEN)

if !@ca_file.present? and File.exist?(ca_cert)
if !present?(@ca_file) and File.exist?(ca_cert)
log.debug "Found CA certificate: #{ca_cert}"
@ca_file = ca_cert
end

if !@bearer_token_file.present? and File.exist?(pod_token)
if !present?(@bearer_token_file) and File.exist?(pod_token)
log.debug "Found pod token: #{pod_token}"
@bearer_token_file = pod_token
end
end

if @kubernetes_url.present?

if present?(@kubernetes_url)
ssl_options = {
client_cert: @client_cert.present? ? OpenSSL::X509::Certificate.new(File.read(@client_cert)) : nil,
client_key: @client_key.present? ? OpenSSL::PKey::RSA.new(File.read(@client_key)) : nil,
client_cert: present?(@client_cert) ? OpenSSL::X509::Certificate.new(File.read(@client_cert)) : nil,
client_key: present?(@client_key) ? OpenSSL::PKey::RSA.new(File.read(@client_key)) : nil,
ca_file: @ca_file,
verify_ssl: @verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE
}
Expand All @@ -228,7 +227,7 @@ def log.trace?

auth_options = {}

if @bearer_token_file.present?
if present?(@bearer_token_file)
bearer_token = File.read(@bearer_token_file)
auth_options[:bearer_token] = bearer_token
end
Expand Down Expand Up @@ -281,7 +280,7 @@ def get_metadata_for_record(namespace_name, pod_name, container_name, container_
'pod_name' => pod_name
}
}
if @kubernetes_url.present?
if present?(@kubernetes_url)
pod_metadata = get_pod_metadata(container_id, namespace_name, pod_name, create_time, batch_miss_cache)

if (pod_metadata.include? 'containers') && (pod_metadata['containers'].include? container_id) && !@skip_container_metadata
Expand Down Expand Up @@ -372,5 +371,9 @@ def de_dot!(h)
end
end

# copied from activesupport
def present?(object)
object.respond_to?(:empty?) ? !object.empty? : !!object
end
end
end
26 changes: 0 additions & 26 deletions lib/fluent/plugin/kubernetes_metadata_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,3 @@ def syms_to_strs(hsh)

end
end

# copied from activesupport
class Object
# An object is blank if it's false, empty, or a whitespace string.
# For example, +nil+, '', ' ', [], {}, and +false+ are all blank.
#
# This simplifies
#
# !address || address.empty?
#
# to
#
# address.blank?
#
# @return [true, false]
def blank?
respond_to?(:empty?) ? !!empty? : !self
end

# An object is present if it's not blank.
#
# @return [true, false]
def present?
!blank?
end
end
4 changes: 2 additions & 2 deletions test/plugin/test_filter_kubernetes_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def create_driver(conf = '')
secret_dir #{dir}
")
assert_equal(d.instance.kubernetes_url, "https://localhost:8443/api")
assert_false(d.instance.ca_file.present?)
assert_false(d.instance.bearer_token_file.present?)
assert_nil(d.instance.ca_file, nil)
assert_nil(d.instance.bearer_token_file)
}
ensure
ENV['KUBERNETES_SERVICE_HOST'] = nil
Expand Down

0 comments on commit f65617d

Please sign in to comment.