Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize resource name to support watching other resources in events plugin #62

Merged
merged 3 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deploy/kubernetes/fluentd-sumologic.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ data:
events.conf: |-
<source>
@type events
namespace $NAMESPACE
</source>
<match kubernetes.**>
@type sumologic
Expand Down
32 changes: 16 additions & 16 deletions fluent-plugin-events/lib/fluent/plugin/in_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class EventsInput < Fluent::Plugin::Input

# parameters for connecting to k8s api server
config_param :kubernetes_url, :string, default: nil
config_param :apiVersion, :string, default: 'v1'
config_param :api_version, :string, default: 'v1'
config_param :client_cert, :string, default: nil
config_param :client_key, :string, default: nil
config_param :ca_file, :string, default: nil
Expand All @@ -28,6 +28,7 @@ class EventsInput < Fluent::Plugin::Input
config_param :ssl_partial_chain, :bool, default: false

# parameters for events collection
config_param :resource_name, :string, default: "events"
config_param :tag, :string, default: 'kubernetes.*'
config_param :namespace, :string, default: nil
config_param :label_selector, :string, default: nil
Expand Down Expand Up @@ -55,7 +56,7 @@ def start

def stop
log.debug "Clean up before stopping completely"
update_config_map
patch_config_map
@watch_stream.finish if @watch_stream
super
end
Expand Down Expand Up @@ -96,7 +97,7 @@ def start_monitor
log.debug "last_recreated updated to #{last_recreated}"
end

update_config_map
patch_config_map
sleep(@configmap_update_interval_seconds)
end
end
Expand All @@ -111,11 +112,11 @@ def start_watcher_thread
params[:namespace] = @namespace
params[:timeout_seconds] = @watch_interval_seconds + 60

@watcher = @client.public_send("watch_events", params).tap do |watcher|
thread_create(:"watch_events") do
@watcher = @client.public_send("watch_#{resource_name}", params).tap do |watcher|
thread_create(:"watch_#{resource_name}") do
@watch_stream = watcher
@watcher_id = Thread.current.object_id
log.debug "New thread to watch events #{@watcher_id} from resource version #{params[:resource_version]}"
log.debug "New thread to watch #{resource_name} #{@watcher_id} from resource version #{params[:resource_version]}"

watcher.each do |entity|
begin
Expand All @@ -138,33 +139,32 @@ def start_watcher_thread
end

def create_config_map
@resource_version = 0
@configmap.data = { "resource-version": "#{@resource_version}" }
@configmap.data = { "resource-version-#{resource_name}": "#{@resource_version}" }
@client.public_send("create_config_map", @configmap).tap do |map|
log.debug "Created config map: #{map}"
end
end

def update_config_map
def patch_config_map
pull_resource_version
@configmap.data = { "resource-version": "#{@resource_version}"}
@client.public_send("update_config_map", @configmap).tap do |map|
log.debug "Updated config map: #{map}"
@client.public_send("patch_config_map", "fluentd-config-resource-version", {data: { "resource-version-#{resource_name}": "#{@resource_version}"}}, @namespace).tap do |map|
log.debug "Patched config map: #{map}"
end
end

def initialize_resource_version
@resource_version = 0
@configmap = ::Kubeclient::Resource.new
@configmap.metadata = {
name: "fluentd-config-resource-version",
namespace: "sumologic"
namespace: @namespace
maimaisie marked this conversation as resolved.
Show resolved Hide resolved
}

# get or create the config map
begin
@client.public_send("get_config_map", "fluentd-config-resource-version", "sumologic").tap do |resource|
@client.public_send("get_config_map", "fluentd-config-resource-version", @namespace).tap do |resource|
log.debug "Got config map: #{resource}"
version = resource.data['resource-version']
version = resource.data["resource-version-#{resource_name}"]
@resource_version = version.to_i if version
end
rescue Kubeclient::ResourceNotFoundError
Expand All @@ -175,7 +175,7 @@ def initialize_resource_version
def pull_resource_version
params = Hash.new
params[:as] = :raw
response = @client.public_send "get_events", params
response = @client.public_send "get_#{resource_name}", params
result = JSON.parse(response)

resource_version = result.fetch('resourceVersion') do
Expand Down
2 changes: 1 addition & 1 deletion fluent-plugin-events/lib/sumologic/kubernetes/connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Connector

def connect_kubernetes
@client = Kubeclient::Client.new(
@kubernetes_url, @apiVersion,
@kubernetes_url, @api_version,
ssl_options: ssl_options,
auth_options: auth_options
)
Expand Down