-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from SumoLogic/byi-plugin-impl
implement enhance-k8s-metadata plugin with label reading
- Loading branch information
Showing
14 changed files
with
1,069 additions
and
20 deletions.
There are no files selected for viewing
31 changes: 16 additions & 15 deletions
31
fluent-plugin-enhance-k8s-metadata/fluent-plugin-enhance-k8s-metadata.gemspec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,30 @@ | ||
lib = File.expand_path("../lib", __FILE__) | ||
lib = File.expand_path('../lib', __FILE__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
|
||
Gem::Specification.new do |spec| | ||
spec.name = "fluent-plugin-enhance-k8s-metadata" | ||
spec.version = "0.0.0" | ||
spec.authors = ["Sumo Logic"] | ||
spec.email = ["[email protected]"] | ||
spec.name = 'fluent-plugin-enhance-k8s-metadata' | ||
spec.version = '0.0.0' | ||
spec.authors = ['Sumo Logic'] | ||
spec.email = ['[email protected]'] | ||
|
||
spec.summary = "Fluentd plugin for appending extra metadata from Kubernetes." | ||
spec.homepage = "https://github.com/SumoLogic/sumologic-kubernetes-collection" | ||
spec.license = "Apache-2.0" | ||
spec.summary = 'Fluentd plugin for appending extra metadata from Kubernetes.' | ||
spec.homepage = 'https://github.com/SumoLogic/sumologic-kubernetes-collection' | ||
spec.license = 'Apache-2.0' | ||
|
||
test_files, files = `git ls-files -z`.split("\x0").partition do |f| | ||
f.match(%r{^(test|spec|features)/}) | ||
end | ||
spec.files = files | ||
spec.executables = files.grep(%r{^bin/}) { |f| File.basename(f) } | ||
spec.test_files = test_files | ||
spec.require_paths = ["lib"] | ||
spec.require_paths = ['lib'] | ||
|
||
spec.add_runtime_dependency "fluentd", [">= 0.14.10", "< 2"] | ||
spec.add_runtime_dependency "kubeclient", "~> 4.4.0" | ||
spec.add_runtime_dependency "lru_redux", "~> 1.1.0" | ||
spec.add_runtime_dependency 'fluentd', ['>= 0.14.10', '< 2'] | ||
spec.add_runtime_dependency 'kubeclient', '~> 4.4.0' | ||
spec.add_runtime_dependency 'lru_redux', '~> 1.1.0' | ||
|
||
spec.add_development_dependency "bundler", "~> 2.0" | ||
spec.add_development_dependency "rake", "~> 12.0" | ||
spec.add_development_dependency "test-unit", "~> 3.0" | ||
spec.add_development_dependency 'bundler', '~> 2.0' | ||
spec.add_development_dependency 'rake', '~> 12.0' | ||
spec.add_development_dependency 'test-unit', '~> 3.0' | ||
spec.add_development_dependency 'webmock', '~> 3.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
fluent-plugin-enhance-k8s-metadata/lib/sumologic/kubernetes/cache_strategy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module SumoLogic | ||
module Kubernetes | ||
# module for caching strategy | ||
module CacheStrategy | ||
require 'lru_redux' | ||
require_relative 'reader.rb' | ||
|
||
CACHE_TYPE_POD_LABELS = 'pod_labels'.freeze | ||
|
||
def init_cache | ||
@all_caches = { | ||
CACHE_TYPE_POD_LABELS => LruRedux::TTL::ThreadSafeCache.new(@cache_size, @cache_ttl), | ||
} | ||
end | ||
|
||
def get_pod_labels(namespace_name, pod_name) | ||
key = "#{namespace_name}::#{pod_name}" | ||
cache = @all_caches[CACHE_TYPE_POD_LABELS] | ||
labels = cache[key] | ||
if labels.nil? | ||
labels = fetch_pod_labels(namespace_name, pod_name) | ||
cache[key] = labels | ||
end | ||
labels | ||
end | ||
end | ||
end | ||
end |
60 changes: 60 additions & 0 deletions
60
fluent-plugin-enhance-k8s-metadata/lib/sumologic/kubernetes/connector.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module SumoLogic | ||
module Kubernetes | ||
# module for connecting to Kubernetes cluster | ||
module Connector | ||
require 'kubeclient' | ||
|
||
K8_POD_CA_CERT = 'ca.crt'.freeze | ||
K8_POD_TOKEN = 'token'.freeze | ||
|
||
def connect_kubernetes | ||
@client = Kubeclient::Client.new( | ||
@kubernetes_url, @apiVersion, | ||
ssl_options: ssl_options, | ||
auth_options: auth_options, | ||
as: :parsed | ||
) | ||
@client.api_valid? | ||
rescue Exception => e | ||
log.error e | ||
end | ||
|
||
def ssl_store | ||
require 'openssl' | ||
ssl_store = OpenSSL::X509::Store.new | ||
ssl_store.set_default_paths | ||
# if version of ruby does not define OpenSSL::X509::V_FLAG_PARTIAL_CHAIN | ||
flagval = 0x80000 | ||
flagval = OpenSSL::X509::V_FLAG_PARTIAL_CHAIN if defined? OpenSSL::X509::V_FLAG_PARTIAL_CHAIN | ||
ssl_store.flags = OpenSSL::X509::V_FLAG_CRL_CHECK_ALL | flagval | ||
ssl_store | ||
end | ||
|
||
def ssl_options | ||
ssl_options = {} | ||
ssl_options[:verify_ssl] = @verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE | ||
if !@ca_file.nil? && File.exist?(@ca_file) | ||
ssl_options[:ca_file] = @ca_file | ||
end | ||
if !@client_cert.nil? && File.exist?(@client_cert) | ||
ssl_options[:client_cert] = OpenSSL::X509::Certificate.new(File.read(@client_cert)) | ||
end | ||
if !@client_key.nil? && File.exist?(@client_key) | ||
ssl_options[:client_key] = OpenSSL::PKey::RSA.new(File.read(@client_key)) | ||
end | ||
ssl_options[:cert_store] = ssl_store if @ssl_partial_chain | ||
log.info "ssl_options: #{ssl_options}" | ||
ssl_options | ||
end | ||
|
||
def auth_options | ||
auth_options = {} | ||
if !@bearer_token_file.nil? && File.exist?(@bearer_token_file) | ||
auth_options[:bearer_token] = File.read(@bearer_token_file) | ||
end | ||
log.info "auth_options: #{ssl_options}" | ||
auth_options | ||
end | ||
end | ||
end | ||
end |
35 changes: 35 additions & 0 deletions
35
fluent-plugin-enhance-k8s-metadata/lib/sumologic/kubernetes/reader.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module SumoLogic | ||
module Kubernetes | ||
# module for reading from Kubernetes API server | ||
module Reader | ||
require_relative 'connector.rb' | ||
|
||
def fetch_pod(namespace_name, pod_name) | ||
log.info "fetching pod metadata: #{namespace_name}::#{pod_name}" | ||
pod = @client.get_pod(pod_name, namespace_name) | ||
log.debug "raw metadata for #{namespace_name}::#{pod_name}: #{pod}" | ||
pod | ||
end | ||
|
||
def extract_pod_labels(pod) | ||
if pod.nil? | ||
log.warn 'pod is nil' | ||
elsif pod['metadata'].nil? | ||
log.warn 'metadata is nil' | ||
elsif pod['metadata']['labels'].nil? | ||
log.warn 'labels is nil' | ||
else | ||
pod['metadata']['labels'] | ||
end | ||
end | ||
|
||
def fetch_pod_labels(namespace_name, pod_name) | ||
extract_pod_labels(fetch_pod(namespace_name, pod_name)) | ||
rescue Kubeclient::ResourceNotFoundError => e | ||
log.error e | ||
# TODO: we now cache empty if not found since some namespace/pod not matching | ||
{} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,39 @@ | ||
$LOAD_PATH.unshift(File.expand_path("../../", __FILE__)) | ||
require "test-unit" | ||
require "fluent/test" | ||
require "fluent/test/driver/filter" | ||
require "fluent/test/helpers" | ||
$LOAD_PATH.unshift(File.expand_path('../../', __FILE__)) | ||
require 'test-unit' | ||
require 'fluent/test' | ||
require 'fluent/test/driver/filter' | ||
require 'fluent/test/helpers' | ||
require 'webmock/test_unit' | ||
require 'kubeclient' | ||
|
||
Test::Unit::TestCase.include(Fluent::Test::Helpers) | ||
Test::Unit::TestCase.extend(Fluent::Test::Helpers) | ||
|
||
def test_resource(name) | ||
File.new("test/resources/#{name}") | ||
end | ||
|
||
def stub_apis | ||
init_globals | ||
stub_request(:get, %r{/api/v1$}) | ||
.to_return(body: test_resource('api_list.json'), status: 200) | ||
stub_request(:get, %r{/api/v1/namespaces/sumologic/pods}) | ||
.to_return(body: test_resource('pod_sumologic.json'), status: 200) | ||
stub_request(:get, %r{/api/v1/namespaces/kube-system/pods}) | ||
.to_return(body: test_resource('pod_kube-system.json'), status: 200) | ||
stub_request(:get, %r{/api/v1/namespaces/non-exist/pods}) | ||
.to_raise(Kubeclient::ResourceNotFoundError.new(404, nil, nil)) | ||
end | ||
|
||
def init_globals | ||
@kubernetes_url = 'http://localhost:8080/api/' | ||
@apiVersion = 'v1' | ||
@verify_ssl = false | ||
@ca_file = nil | ||
@client_cert = nil | ||
@client_key = nil | ||
@ssl_partial_chain = false | ||
@bearer_token_file = nil | ||
@cache_size = 1000 | ||
@cache_ttl = 60 * 60 | ||
end |
Oops, something went wrong.