From 78b31ca6031da1e408729945b1398a62f3ceacdd Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Wed, 15 Nov 2017 11:28:49 -0500 Subject: [PATCH 01/11] k8s integrations support --- manifests/integrations/kubernetes.pp | 45 ++++++ templates/agent-conf.d/kubernetes.yaml.erb | 175 +++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 manifests/integrations/kubernetes.pp create mode 100644 templates/agent-conf.d/kubernetes.yaml.erb diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp new file mode 100644 index 00000000..a4c3188e --- /dev/null +++ b/manifests/integrations/kubernetes.pp @@ -0,0 +1,45 @@ +# Class: datadog_agent::integrations::kubernetes +# +# This class will install the necessary configuration for the kubernetes integration +# +# Parameters: +# $url: +# The URL for kubernetes API +# +# $tags: +# optional array of tags +# +# +# +# Sample Usage: +# +# class { 'datadog_agent::integrations::kubernetes' : +# api_server_url => 'https://kubernetes:443', +# } +# +class datadog_agent::integrations::kubernetes( + $api_server_url = 'Enter_Your_API_url', + $tags = [] +) inherits datadog_agent::params { + include datadog_agent + + if $::datadog_agent::agent6_enable { + $dst = "${datadog_agent::conf6_dir}/kubernetes.yaml" + } else { + $dst = "${datadog_agent::conf_dir}/kubernetes.yaml" + } + + package { 'dd-check-kubernetes': + ensure => present, + } + + file { $dst: + ensure => file, + owner => $datadog_agent::params::dd_user, + group => $datadog_agent::params::dd_group, + mode => '0644', + content => template('datadog_agent/agent-conf.d/kubernetes.yaml.erb'), + require => Package[$datadog_agent::params::package_name], + notify => Service[$datadog_agent::params::service_name] + } +} diff --git a/templates/agent-conf.d/kubernetes.yaml.erb b/templates/agent-conf.d/kubernetes.yaml.erb new file mode 100644 index 00000000..aecbe16c --- /dev/null +++ b/templates/agent-conf.d/kubernetes.yaml.erb @@ -0,0 +1,175 @@ +init_config: + # Initialization retries + # + # if the agent is expected to start before Kubelet, + # use these settings to configure the retry policy. + # + # init_retry_interval defines how long (in seconds) the kubelet client + # will wait before retrying initialization. + # Defaults to 0. + # + # init_retry_interval: 20 + # + # init_retries configures how many retries are made before failing permanently. + # Defaults to 0. + # + # init_retries: 5 + +instances: + # The kubernetes check retrieves metrics from cadvisor running under kubelet on each node. + # It also queries kubelet for its health and the list of local-running pods, and (optionally) + # the apiserver for services and events + # + # By default we assume we're running under docker and + # that the kubelet read-only port with no auth is enabled. + # + # In this case we will use the address of the default router to reach the kubelet and cadvisor APIs + # unless the environment variable KUBERNETES_KUBELET_HOST is found. You can set this variable to + # spec.nodeName (If your node name can be resolved by pods) or status.hostIP (for Kubernetes 1.7+) + # through the downward API. See + # https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/ + # + # To enable Kubernetes State Metrics, please refer to kubernetes_states integration. + # For more information, + # please consult http://docs.datadoghq.com/integrations/kubernetes/#kubernetes-state-metrics + # and https://github.com/DataDog/integrations-core/ on the kubernetes_state directory. + # + # If the read-only endpoint is disabled, the check will query kubelet over HTTPS + # + # To override this behavior, e.g. in the case of a standalone cadvisor instance, use the following: + # + # host: localhost + # port: 4194 + # method: http + + # cAdvisor port, set it to 0 if cAdvisor is unavailable + - port: 4194 + # + # cAdvisor host + # host: localhost + # + # kubelet port. It needs to be set if you are not using a default one (10250 or 10255) + # kubelet_port: 10255 + # + # apiserver url for cluster-level queries. If not configured here, the KUBERNETES_SERVICE_HOST + # and KUBERNETES_SERVICE_PORT environment variables will be used + # + # example = "api_server_url: https://kubernetes:443" + api_server_url: <%= @api_server_url %> + + # Client Authentication against the apiserver and kubelet + # + # By default the agent authenticates against the apiserver and kubelet with its service account + # bearer token. If you want to specify its path, set the following option. If X509 client certificates + # are set, either for the kubelet or apiserver, they will be used instead. The recommended way to expose + # these files to the agent is by using Kubernetes Secrets. + # + # bearer_token_path: /var/run/secrets/kubernetes.io/serviceaccount/token + # + # apiserver_client_crt: /path/to/client.crt + # apiserver_client_key: /path/to/client.key + # + # kubelet_client_crt: /path/to/client.crt + # kubelet_client_key: /path/to/client.key + + # Server Authentication for apiserver and kubelet + # + # Similarly we use the default CA cert of the agent's service account to verify the + # apiserver's identity, but custom ones can be specified here. + # + # apiserver_ca_cert: /path/to/cacert.crt + # kubelet_cert: /path/to/ca.pem + # + # The default for kubelet traffic is to try and use the read-only port that doesn't require TLS + # and to fall back to the HTTPS API with simple TLS validation. Providing a cert forces TLS validation on. + # Explicitly disabling tls_verify should be used with caution: + # if an attacker sniffs the agent requests they will see the agent's service account bearer token. + # + # kubelet_tls_verify: True + + # collect_events controls whether the agent should fetch events from + # the kubernetes API and ingest them in Datadog. + # To avoid duplicates, only one agent at a time across the entire + # cluster should have this feature enabled. + # If this is difficult to achieve in your deployment, see the following + # Leader election section. + # To enable the feature, set the parameter to `true`. + # + # collect_events: false + + # Leader election + # + # Agents can perform leader election among themselves. + # The leader agent will collect events from the apiserver + # even if collect_events is false. + # To perform the election it needs get, list, delete, create, and update + # rights on ConfigMaps (this can be configured with a Cluster Role). + # This makes sure we don't overload the apiserver with redundant + # queries coming from every agent. + # Default is false. + # + # leader_candidate: true + # + # lease duration is the duration for which a leader is elected. + # It should be at least twice the check run period (15s by default) + # When modifying this setting, keep in mind that the shorter + # the lease duration, the more often agents will query the apiserver + # Default is 5 minutes. + # + # leader_lease_duration: 600 + + # Matching the pods to Kubernetes services requires to retrieve events regularly. + # To reduce the traffic to the apiserver, we only query them every 5 minutes, adding a delay + # in pod -> service matching. You can configure it below (in seconds) or disable kube_service + # tagging completely + # + # collect_service_tags: true + # service_tag_update_freq: 300 + # + # The namespaces for which events should be collected. + # If not modified, the 'default' namespace will be used. + # + # namespaces: + # - default + + # The regexp used to select namespaces for which events should be collected. + # The matched namespaces will be added to the "namespaces" list. + # If empty, regexp selection will be ignored. + # + # namespace_name_regexp: + + # Node labels that should be collected and their name in host tags. Off by default. + # Some of these labels are redundant with metadata collected by + # cloud provider crawlers (AWS, GCE, Azure) + # + # node_labels_to_host_tags: + # kubernetes.io/hostname: nodename + # beta.kubernetes.io/os: os + + # use_histogram controls whether we send detailed metrics, i.e. one per container. + # When false, we send detailed metrics corresponding to individual containers, tagging by container id + # to keep them unique. + # When true, we aggregate data based on container image. + # + # use_histogram: false + # + # We can define a whitelist of patterns that permit publishing raw metrics. + # enabled_rates: + # - cpu.* + # - network.* + # + # enabled_gauges: + # - filesystem.* + # + # Prefix to use when converting pod labels to metric tags, can be made empty with "" + # label_to_tag_prefix: "kube_" + # + # Custom tags that should be applied to kubernetes metrics +<% if @tags and ! @tags.empty? -%> + tags: + <%- Array(@tags).each do |tag| -%> + <%- if tag != '' -%> + - <%= tag %> + <%- end -%> + <%- end -%> +<% end -%> From 186b756251807368ea029582c4da91fb99b73111 Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Wed, 15 Nov 2017 13:20:52 -0500 Subject: [PATCH 02/11] New k8s integration support puppet module. --- manifests/integrations/kubernetes.pp | 2 +- templates/agent-conf.d/kubernetes.yaml.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp index a4c3188e..b46210d6 100644 --- a/manifests/integrations/kubernetes.pp +++ b/manifests/integrations/kubernetes.pp @@ -1,4 +1,4 @@ -# Class: datadog_agent::integrations::kubernetes +# Class: datadog_agent::integrations::kubernetes # # This class will install the necessary configuration for the kubernetes integration # diff --git a/templates/agent-conf.d/kubernetes.yaml.erb b/templates/agent-conf.d/kubernetes.yaml.erb index aecbe16c..d09e0319 100644 --- a/templates/agent-conf.d/kubernetes.yaml.erb +++ b/templates/agent-conf.d/kubernetes.yaml.erb @@ -1,5 +1,5 @@ init_config: - # Initialization retries + # Initialization retries # # if the agent is expected to start before Kubelet, # use these settings to configure the retry policy. From 3162519751377dc8537f3b34bf2dd72329a12c20 Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Wed, 15 Nov 2017 13:24:45 -0500 Subject: [PATCH 03/11] Added kubernetes to the integrations list --- manifests/integrations/kubernetes.pp | 4 ++-- templates/agent-conf.d/kubernetes.yaml.erb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp index b46210d6..b61dfdf4 100644 --- a/manifests/integrations/kubernetes.pp +++ b/manifests/integrations/kubernetes.pp @@ -1,8 +1,8 @@ -# Class: datadog_agent::integrations::kubernetes +# Class: datadog_agent::integrations::kubernetes # # This class will install the necessary configuration for the kubernetes integration # -# Parameters: +# Parameters: # $url: # The URL for kubernetes API # diff --git a/templates/agent-conf.d/kubernetes.yaml.erb b/templates/agent-conf.d/kubernetes.yaml.erb index d09e0319..a4da39f4 100644 --- a/templates/agent-conf.d/kubernetes.yaml.erb +++ b/templates/agent-conf.d/kubernetes.yaml.erb @@ -1,6 +1,6 @@ init_config: - # Initialization retries - # + # Initialization retries + # # if the agent is expected to start before Kubelet, # use these settings to configure the retry policy. # From c2f46ace81f579374210a441f1fe8fd4ca8a63ed Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Wed, 15 Nov 2017 13:28:23 -0500 Subject: [PATCH 04/11] Added kubernetes to the integrations list --- manifests/integrations/kubernetes.pp | 2 +- templates/agent-conf.d/kubernetes.yaml.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp index b61dfdf4..a4c3188e 100644 --- a/manifests/integrations/kubernetes.pp +++ b/manifests/integrations/kubernetes.pp @@ -2,7 +2,7 @@ # # This class will install the necessary configuration for the kubernetes integration # -# Parameters: +# Parameters: # $url: # The URL for kubernetes API # diff --git a/templates/agent-conf.d/kubernetes.yaml.erb b/templates/agent-conf.d/kubernetes.yaml.erb index a4da39f4..aecbe16c 100644 --- a/templates/agent-conf.d/kubernetes.yaml.erb +++ b/templates/agent-conf.d/kubernetes.yaml.erb @@ -1,6 +1,6 @@ init_config: # Initialization retries - # + # # if the agent is expected to start before Kubelet, # use these settings to configure the retry policy. # From 3bab0db19ca9b3604e3448b8ae648fcef8c5cbc8 Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Thu, 16 Nov 2017 10:36:02 -0500 Subject: [PATCH 05/11] Removed the addition of the dd-check-kubernetes package. --- manifests/integrations/kubernetes.pp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp index a4c3188e..328925bd 100644 --- a/manifests/integrations/kubernetes.pp +++ b/manifests/integrations/kubernetes.pp @@ -29,10 +29,6 @@ $dst = "${datadog_agent::conf_dir}/kubernetes.yaml" } - package { 'dd-check-kubernetes': - ensure => present, - } - file { $dst: ensure => file, owner => $datadog_agent::params::dd_user, From f42b42b2b0021b37b7764a36673938e0eef87bdb Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Thu, 16 Nov 2017 12:54:52 -0500 Subject: [PATCH 06/11] Added k8 state yaml for state metrics. --- manifests/integrations/kubernetes.pp | 34 ++++++++++++++++--- templates/agent-conf.d/kubernetes.yaml.erb | 9 +++-- .../agent-conf.d/kubernetes_state.yaml.erb | 25 ++++++++++++++ 3 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 templates/agent-conf.d/kubernetes_state.yaml.erb diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp index 328925bd..b7a1400a 100644 --- a/manifests/integrations/kubernetes.pp +++ b/manifests/integrations/kubernetes.pp @@ -14,12 +14,25 @@ # Sample Usage: # # class { 'datadog_agent::integrations::kubernetes' : -# api_server_url => 'https://kubernetes:443', +# api_server_url => 'https://kubernetes:443', +# kube_state_url => 'http://kubernetes.com:8080/metrics', +# apiserver_client_crt => '/etc/ssl/certs/crt', +# apiserver_client_key => '/etc/ssl/private/key', +# kubelet_client_crt => '/etc/ssl/certs/crt', +# kubelet_client_key => '/etc/ssl/private/key', # } # class datadog_agent::integrations::kubernetes( $api_server_url = 'Enter_Your_API_url', - $tags = [] + $apiserver_client_crt = '/path/to/crt', + $apiserver_client_key = '/path/to/key', + $kubelet_client_crt = '/path/to/crt', + $kubelet_client_key = '/path/to/key', + $tags = [], + + $kube_state_url = 'Enter_State_URL', + $state_tags = [], + ) inherits datadog_agent::params { include datadog_agent @@ -29,13 +42,26 @@ $dst = "${datadog_agent::conf_dir}/kubernetes.yaml" } - file { $dst: + if $::datadog_agent::agent6_enable { + $dstate = "${datadog_agent::conf6_dir}/kubernetes_state.yaml" + } else { + $dstate = "${datadog_agent::conf_dir}/kubernetes_state.yaml" + } + + File { ensure => file, owner => $datadog_agent::params::dd_user, group => $datadog_agent::params::dd_group, mode => '0644', - content => template('datadog_agent/agent-conf.d/kubernetes.yaml.erb'), require => Package[$datadog_agent::params::package_name], notify => Service[$datadog_agent::params::service_name] } + + file { $dstate: + content => template('datadog_agent/agent-conf.d/kubernetes_state.yaml.erb'), + } + + file { $dst: + content => template('datadog_agent/agent-conf.d/kubernetes.yaml.erb'), + } } diff --git a/templates/agent-conf.d/kubernetes.yaml.erb b/templates/agent-conf.d/kubernetes.yaml.erb index aecbe16c..b68d791c 100644 --- a/templates/agent-conf.d/kubernetes.yaml.erb +++ b/templates/agent-conf.d/kubernetes.yaml.erb @@ -65,12 +65,11 @@ instances: # these files to the agent is by using Kubernetes Secrets. # # bearer_token_path: /var/run/secrets/kubernetes.io/serviceaccount/token + apiserver_client_crt: <%= @apiserver_client_crt %> + apiserver_client_key: <%= @apiserver_client_key %> # - # apiserver_client_crt: /path/to/client.crt - # apiserver_client_key: /path/to/client.key - # - # kubelet_client_crt: /path/to/client.crt - # kubelet_client_key: /path/to/client.key + kubelet_client_crt: <%= @kubelet_client_crt %> + kubelet_client_key: <%= @kubelet_client_key %> # Server Authentication for apiserver and kubelet # diff --git a/templates/agent-conf.d/kubernetes_state.yaml.erb b/templates/agent-conf.d/kubernetes_state.yaml.erb new file mode 100644 index 00000000..6896e9c2 --- /dev/null +++ b/templates/agent-conf.d/kubernetes_state.yaml.erb @@ -0,0 +1,25 @@ +init_config: + # Custom tags can be added to all metrics reported by this integration + # tags: + # - optional_tag1 + # - optional_tag2 + <% if @state_tags and ! @state_tags.empty? -%> + tags: + <%- Array(@state_tags).each do |tag| -%> + <%- if tag != '' -%> + - <%= tag %> + <%- end -%> + <%- end -%> + <% end -%> + +instances: + # To enable Kube State metrics you must specify the url exposing the API + # + # Note for RHEL and SUSE users: due to compatibility issues, the check does not make use of + # the CPP extension to process Protocol buffer messages coming from the api. Depending + # on the metrics volume, the check may run very slowly. + - kube_state_url: <%= @kube_state_url %> + # Tags are reported as set by kube-state-metrics. If you want to translate + # them to other tags, you can use the labels_mapper dictionary + # labels_mapper: + # namespace: kube_namespace From 2478f81c5a56c64a7d68f4465150897c039acba9 Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Thu, 16 Nov 2017 12:55:55 -0500 Subject: [PATCH 07/11] Removing tag comments from k8_state yaml file. --- templates/agent-conf.d/kubernetes_state.yaml.erb | 3 --- 1 file changed, 3 deletions(-) diff --git a/templates/agent-conf.d/kubernetes_state.yaml.erb b/templates/agent-conf.d/kubernetes_state.yaml.erb index 6896e9c2..b52f9668 100644 --- a/templates/agent-conf.d/kubernetes_state.yaml.erb +++ b/templates/agent-conf.d/kubernetes_state.yaml.erb @@ -1,8 +1,5 @@ init_config: # Custom tags can be added to all metrics reported by this integration - # tags: - # - optional_tag1 - # - optional_tag2 <% if @state_tags and ! @state_tags.empty? -%> tags: <%- Array(@state_tags).each do |tag| -%> From a78859b9bd40f523eb1aff4de7c42f9d6045e177 Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Thu, 16 Nov 2017 13:29:10 -0500 Subject: [PATCH 08/11] Added kubernetes_state as its own manifest --- manifests/integrations/kubernetes.pp | 19 +-------- manifests/integrations/kubernetes_state.pp | 41 +++++++++++++++++++ templates/agent-conf.d/kubernetes.yaml.erb | 4 +- .../agent-conf.d/kubernetes_state.yaml.erb | 6 +-- 4 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 manifests/integrations/kubernetes_state.pp diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp index b7a1400a..e8a9dbf7 100644 --- a/manifests/integrations/kubernetes.pp +++ b/manifests/integrations/kubernetes.pp @@ -30,9 +30,6 @@ $kubelet_client_key = '/path/to/key', $tags = [], - $kube_state_url = 'Enter_State_URL', - $state_tags = [], - ) inherits datadog_agent::params { include datadog_agent @@ -42,26 +39,12 @@ $dst = "${datadog_agent::conf_dir}/kubernetes.yaml" } - if $::datadog_agent::agent6_enable { - $dstate = "${datadog_agent::conf6_dir}/kubernetes_state.yaml" - } else { - $dstate = "${datadog_agent::conf_dir}/kubernetes_state.yaml" - } - - File { + file { $dst: ensure => file, owner => $datadog_agent::params::dd_user, group => $datadog_agent::params::dd_group, mode => '0644', require => Package[$datadog_agent::params::package_name], notify => Service[$datadog_agent::params::service_name] - } - - file { $dstate: - content => template('datadog_agent/agent-conf.d/kubernetes_state.yaml.erb'), - } - - file { $dst: content => template('datadog_agent/agent-conf.d/kubernetes.yaml.erb'), } -} diff --git a/manifests/integrations/kubernetes_state.pp b/manifests/integrations/kubernetes_state.pp new file mode 100644 index 00000000..61087fa9 --- /dev/null +++ b/manifests/integrations/kubernetes_state.pp @@ -0,0 +1,41 @@ +# Class: datadog_agent::integrations::kubernetes +# +# This class will install the necessary configuration for the kubernetes integration +# +# Parameters: +# $url: +# The URL for kubernetes metrics +# +# $tags: +# optional array of tags +# +# +# +# Sample Usage: +# +# class { 'datadog_agent::integrations::kubernetes_state' : +# url => 'http://kubernetes.com:8080/metrics', +# } +# +class datadog_agent::integrations::kubernetes_state( + $url = 'Enter_State_URL', + $tags = [], + +) inherits datadog_agent::params { + include datadog_agent + + if $::datadog_agent::agent6_enable { + $dst = "${datadog_agent::conf6_dir}/kubernetes_state.yaml" + } else { + $dst = "${datadog_agent::conf_dir}/kubernetes_state.yaml" + } + + file { $dst: + ensure => file, + owner => $datadog_agent::params::dd_user, + group => $datadog_agent::params::dd_group, + mode => '0644', + require => Package[$datadog_agent::params::package_name], + notify => Service[$datadog_agent::params::service_name] + content => template('datadog_agent/agent-conf.d/kubernetes_state.yaml.erb'), + } diff --git a/templates/agent-conf.d/kubernetes.yaml.erb b/templates/agent-conf.d/kubernetes.yaml.erb index b68d791c..be524268 100644 --- a/templates/agent-conf.d/kubernetes.yaml.erb +++ b/templates/agent-conf.d/kubernetes.yaml.erb @@ -15,7 +15,7 @@ init_config: # # init_retries: 5 -instances: +# instances: # The kubernetes check retrieves metrics from cadvisor running under kubelet on each node. # It also queries kubelet for its health and the list of local-running pods, and (optionally) # the apiserver for services and events @@ -43,7 +43,7 @@ instances: # method: http # cAdvisor port, set it to 0 if cAdvisor is unavailable - - port: 4194 + # - port: 4194 # # cAdvisor host # host: localhost diff --git a/templates/agent-conf.d/kubernetes_state.yaml.erb b/templates/agent-conf.d/kubernetes_state.yaml.erb index b52f9668..92ddc863 100644 --- a/templates/agent-conf.d/kubernetes_state.yaml.erb +++ b/templates/agent-conf.d/kubernetes_state.yaml.erb @@ -1,8 +1,8 @@ init_config: # Custom tags can be added to all metrics reported by this integration - <% if @state_tags and ! @state_tags.empty? -%> + <% if @tags and ! @tags.empty? -%> tags: - <%- Array(@state_tags).each do |tag| -%> + <%- Array(@tags).each do |tag| -%> <%- if tag != '' -%> - <%= tag %> <%- end -%> @@ -15,7 +15,7 @@ instances: # Note for RHEL and SUSE users: due to compatibility issues, the check does not make use of # the CPP extension to process Protocol buffer messages coming from the api. Depending # on the metrics volume, the check may run very slowly. - - kube_state_url: <%= @kube_state_url %> + - kube_state_url: <%= @url %> # Tags are reported as set by kube-state-metrics. If you want to translate # them to other tags, you can use the labels_mapper dictionary # labels_mapper: From 92e4867866e2300a4467c6d57ea695da739be24c Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Thu, 16 Nov 2017 14:21:36 -0500 Subject: [PATCH 09/11] Spec test classes created. --- ...adog_agent_integrations_kubernetes_spec.rb | 83 +++++++++++++++++++ ...gent_integrations_kubernetes_state_spec.rb | 83 +++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 spec/classes/datadog_agent_integrations_kubernetes_spec.rb create mode 100644 spec/classes/datadog_agent_integrations_kubernetes_state_spec.rb diff --git a/spec/classes/datadog_agent_integrations_kubernetes_spec.rb b/spec/classes/datadog_agent_integrations_kubernetes_spec.rb new file mode 100644 index 00000000..a37b1050 --- /dev/null +++ b/spec/classes/datadog_agent_integrations_kubernetes_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' + +describe 'datadog_agent::integrations::kubernetes' do + context 'supported agents - v5 and v6' do + agents = { '5' => false, '6' => true } + agents.each do |_, enabled| + let(:pre_condition) { "class {'::datadog_agent': agent6_enable => #{enabled}}" } + let(:facts) {{ + operatingsystem: 'Ubuntu', + }} + if !enabled + let(:conf_dir) { '/etc/dd-agent/conf.d' } + else + let(:conf_dir) { '/etc/datadog-agent/conf.d' } + end + let(:dd_user) { 'dd-agent' } + let(:dd_group) { 'root' } + let(:dd_package) { 'datadog-agent' } + let(:dd_service) { 'datadog-agent' } + if !enabled + let(:conf_file) { "#{conf_dir}/kubernetes.yaml" } + else + let(:conf_file) { "#{conf_dir}/kubernetes.yaml" } + end + + it { should compile.with_all_deps } + it { should contain_file(conf_file).with( + owner: dd_user, + group: dd_group, + mode: '0644', + )} + it { should contain_file(conf_file).that_requires("Package[#{dd_package}]") } + it { should contain_file(conf_file).that_notifies("Service[#{dd_service}]") } + + context 'with default parameters' do + it { should contain_file(conf_file).with_content(%r{url: Enter_Your_API_url}) } + end + + context 'with parameters set' do + let(:params) {{ + url: 'unix://foo/bar/baz.sock', + }} + it { should contain_file(conf_file).with_content(%r{url: unix://foo/bar/baz.sock}) } + end + + context 'with tags parameter array' do + let(:params) {{ + tags: %w{ foo bar baz }, + }} + it { should contain_file(conf_file).with_content(/tags:\s+- foo\s+- bar\s+- baz\s*?[^-]/m) } + end + + context 'with tags parameter with an empty tag' do + end + + context 'with tags parameter empty values' do + context 'mixed in with other tags' do + let(:params) {{ + tags: [ 'foo', '', 'baz' ] + }} + + it { should contain_file(conf_file).with_content(/tags:\s+- foo\s+- baz\s*?[^-]/m) } + end + + context 'single element array of an empty string' do + let(:params) {{ + tags: [''], + }} + + skip("undefined behavior") + end + + context 'single value empty string' do + let(:params) {{ + tags: '', + }} + + skip("doubly undefined behavior") + end + end + end + end +end diff --git a/spec/classes/datadog_agent_integrations_kubernetes_state_spec.rb b/spec/classes/datadog_agent_integrations_kubernetes_state_spec.rb new file mode 100644 index 00000000..fe2a351b --- /dev/null +++ b/spec/classes/datadog_agent_integrations_kubernetes_state_spec.rb @@ -0,0 +1,83 @@ +require 'spec_helper' + +describe 'datadog_agent::integrations::kubernetes_state' do + context 'supported agents - v5 and v6' do + agents = { '5' => false, '6' => true } + agents.each do |_, enabled| + let(:pre_condition) { "class {'::datadog_agent': agent6_enable => #{enabled}}" } + let(:facts) {{ + operatingsystem: 'Ubuntu', + }} + if !enabled + let(:conf_dir) { '/etc/dd-agent/conf.d' } + else + let(:conf_dir) { '/etc/datadog-agent/conf.d' } + end + let(:dd_user) { 'dd-agent' } + let(:dd_group) { 'root' } + let(:dd_package) { 'datadog-agent' } + let(:dd_service) { 'datadog-agent' } + if !enabled + let(:conf_file) { "#{conf_dir}/kubernetes_state.yaml" } + else + let(:conf_file) { "#{conf_dir}/kubernetes_state.yaml" } + end + + it { should compile.with_all_deps } + it { should contain_file(conf_file).with( + owner: dd_user, + group: dd_group, + mode: '0644', + )} + it { should contain_file(conf_file).that_requires("Package[#{dd_package}]") } + it { should contain_file(conf_file).that_notifies("Service[#{dd_service}]") } + + context 'with default parameters' do + it { should contain_file(conf_file).with_content(%r{url: Enter_State_URL}) } + end + + context 'with parameters set' do + let(:params) {{ + url: 'Enter_State_URL', + }} + it { should contain_file(conf_file).with_content(%r{url: Enter_State_URL}) } + end + + context 'with tags parameter array' do + let(:params) {{ + tags: %w{ foo bar baz }, + }} + it { should contain_file(conf_file).with_content(/tags:\s+- foo\s+- bar\s+- baz\s*?[^-]/m) } + end + + context 'with tags parameter with an empty tag' do + end + + context 'with tags parameter empty values' do + context 'mixed in with other tags' do + let(:params) {{ + tags: [ 'foo', '', 'baz' ] + }} + + it { should contain_file(conf_file).with_content(/tags:\s+- foo\s+- baz\s*?[^-]/m) } + end + + context 'single element array of an empty string' do + let(:params) {{ + tags: [''], + }} + + skip("undefined behavior") + end + + context 'single value empty string' do + let(:params) {{ + tags: '', + }} + + skip("doubly undefined behavior") + end + end + end + end +end From cbadfd5bc3bf6dd0e224aa1c8c88f9370fd6088f Mon Sep 17 00:00:00 2001 From: Edwin Carrasquillo Date: Fri, 17 Nov 2017 11:50:34 -0500 Subject: [PATCH 10/11] Removed comment from prior correction. --- manifests/integrations/kubernetes.pp | 1 - 1 file changed, 1 deletion(-) diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp index e8a9dbf7..fe64395d 100644 --- a/manifests/integrations/kubernetes.pp +++ b/manifests/integrations/kubernetes.pp @@ -15,7 +15,6 @@ # # class { 'datadog_agent::integrations::kubernetes' : # api_server_url => 'https://kubernetes:443', -# kube_state_url => 'http://kubernetes.com:8080/metrics', # apiserver_client_crt => '/etc/ssl/certs/crt', # apiserver_client_key => '/etc/ssl/private/key', # kubelet_client_crt => '/etc/ssl/certs/crt', From 89c4066ce8b52ac8da7de094b6800f4be6b9018e Mon Sep 17 00:00:00 2001 From: Jaime Fullaondo Date: Wed, 22 Nov 2017 19:00:18 +0100 Subject: [PATCH 11/11] [k8s] fixing spec tests --- manifests/integrations/kubernetes.pp | 3 ++- manifests/integrations/kubernetes_state.pp | 3 ++- .../datadog_agent_integrations_kubernetes_spec.rb | 10 +++++++--- ...datadog_agent_integrations_kubernetes_state_spec.rb | 6 +++--- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/manifests/integrations/kubernetes.pp b/manifests/integrations/kubernetes.pp index fe64395d..2edd1f6e 100644 --- a/manifests/integrations/kubernetes.pp +++ b/manifests/integrations/kubernetes.pp @@ -44,6 +44,7 @@ group => $datadog_agent::params::dd_group, mode => '0644', require => Package[$datadog_agent::params::package_name], - notify => Service[$datadog_agent::params::service_name] + notify => Service[$datadog_agent::params::service_name], content => template('datadog_agent/agent-conf.d/kubernetes.yaml.erb'), } +} diff --git a/manifests/integrations/kubernetes_state.pp b/manifests/integrations/kubernetes_state.pp index 61087fa9..cfe9d36b 100644 --- a/manifests/integrations/kubernetes_state.pp +++ b/manifests/integrations/kubernetes_state.pp @@ -36,6 +36,7 @@ group => $datadog_agent::params::dd_group, mode => '0644', require => Package[$datadog_agent::params::package_name], - notify => Service[$datadog_agent::params::service_name] + notify => Service[$datadog_agent::params::service_name], content => template('datadog_agent/agent-conf.d/kubernetes_state.yaml.erb'), } +} diff --git a/spec/classes/datadog_agent_integrations_kubernetes_spec.rb b/spec/classes/datadog_agent_integrations_kubernetes_spec.rb index a37b1050..53b77b00 100644 --- a/spec/classes/datadog_agent_integrations_kubernetes_spec.rb +++ b/spec/classes/datadog_agent_integrations_kubernetes_spec.rb @@ -33,14 +33,18 @@ it { should contain_file(conf_file).that_notifies("Service[#{dd_service}]") } context 'with default parameters' do - it { should contain_file(conf_file).with_content(%r{url: Enter_Your_API_url}) } + it { should contain_file(conf_file).with_content(%r{api_server_url: Enter_Your_API_url}) } + it { should contain_file(conf_file).with_content(%r{apiserver_client_crt: /path/to/crt}) } + it { should contain_file(conf_file).with_content(%r{apiserver_client_key: /path/to/key}) } + it { should contain_file(conf_file).with_content(%r{kubelet_client_crt: /path/to/crt}) } + it { should contain_file(conf_file).with_content(%r{kubelet_client_key: /path/to/key}) } end context 'with parameters set' do let(:params) {{ - url: 'unix://foo/bar/baz.sock', + api_server_url: 'unix://foo/bar/baz.sock', }} - it { should contain_file(conf_file).with_content(%r{url: unix://foo/bar/baz.sock}) } + it { should contain_file(conf_file).with_content(%r{api_server_url: unix://foo/bar/baz.sock}) } end context 'with tags parameter array' do diff --git a/spec/classes/datadog_agent_integrations_kubernetes_state_spec.rb b/spec/classes/datadog_agent_integrations_kubernetes_state_spec.rb index fe2a351b..641ec28e 100644 --- a/spec/classes/datadog_agent_integrations_kubernetes_state_spec.rb +++ b/spec/classes/datadog_agent_integrations_kubernetes_state_spec.rb @@ -33,14 +33,14 @@ it { should contain_file(conf_file).that_notifies("Service[#{dd_service}]") } context 'with default parameters' do - it { should contain_file(conf_file).with_content(%r{url: Enter_State_URL}) } + it { should contain_file(conf_file).with_content(%r{kube_state_url: Enter_State_URL}) } end context 'with parameters set' do let(:params) {{ - url: 'Enter_State_URL', + url: 'Some_Other_State_URL', }} - it { should contain_file(conf_file).with_content(%r{url: Enter_State_URL}) } + it { should contain_file(conf_file).with_content(%r{kube_state_url: Some_Other_State_URL}) } end context 'with tags parameter array' do