From 8a3790af0ef1459fd5b076b72b0eeac88b5d1dd7 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 25 Jan 2023 10:39:09 +0000 Subject: [PATCH] (plugin) cloud::kubernetes - add --namespace option (#4179) --- src/cloud/kubernetes/custom/api.pm | 142 ++++++++++++------ src/cloud/kubernetes/custom/kubectl.pm | 31 ++-- src/cloud/kubernetes/mode/clusterevents.pm | 31 ++-- src/cloud/kubernetes/mode/cronjobstatus.pm | 3 +- .../mode/{discoverynodes.pm => discovery.pm} | 101 +++++++++---- src/cloud/kubernetes/mode/listdeployments.pm | 26 ++-- src/cloud/kubernetes/mode/listingresses.pm | 20 ++- src/cloud/kubernetes/mode/listnamespaces.pm | 22 ++- src/cloud/kubernetes/plugin.pm | 13 +- 9 files changed, 248 insertions(+), 141 deletions(-) rename src/cloud/kubernetes/mode/{discoverynodes.pm => discovery.pm} (53%) diff --git a/src/cloud/kubernetes/custom/api.pm b/src/cloud/kubernetes/custom/api.pm index 9e0843a0a..102639144 100644 --- a/src/cloud/kubernetes/custom/api.pm +++ b/src/cloud/kubernetes/custom/api.pm @@ -49,7 +49,8 @@ sub new { 'token:s' => { name => 'token' }, 'timeout:s' => { name => 'timeout' }, 'limit:s' => { name => 'limit' }, - 'config-file:s' => { name => 'config_file' } + 'config-file:s' => { name => 'config_file' }, + 'namespace:s' => { name => 'namespace' } }); } $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); @@ -71,18 +72,20 @@ sub set_defaults {} sub check_options { my ($self, %options) = @_; - $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : undef; + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443; $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https'; $self->{timeout} = (defined($self->{option_results}->{timeout})) && $self->{option_results}->{timeout} =~ /(\d+)/ ? $1 : 10; $self->{token} = (defined($self->{option_results}->{token})) ? $self->{option_results}->{token} : ''; $self->{limit} = (defined($self->{option_results}->{limit})) && $self->{option_results}->{limit} =~ /(\d+)/ ? $1 : 100; - - if (!defined($self->{hostname}) || $self->{hostname} eq '') { + $self->{namespace} = defined($self->{option_results}->{namespace}) && $self->{option_results}->{namespace} ne '' ? + $self->{option_results}->{namespace} : ''; + + if ($self->{hostname} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); $self->{output}->option_exit(); } - if (!defined($self->{token}) || $self->{token} eq '') { + if ($self->{token} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify --token option."); $self->{output}->option_exit(); } @@ -116,10 +119,13 @@ sub settings { sub request_api { my ($self, %options) = @_; - $self->settings; + $self->settings(); - $self->{output}->output_add(long_msg => "URL: '" . $self->{proto} . '://' . $self->{hostname} . - ':' . $self->{port} . $options{url_path} . "'", debug => 1); + $self->{output}->output_add( + long_msg => "URL: '" . $self->{proto} . '://' . $self->{hostname} . + ':' . $self->{port} . $options{url_path} . "'", + debug => 1 + ); my $response = $self->{http}->request(%options); @@ -153,7 +159,7 @@ sub request_api { $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $response"); $self->{output}->option_exit(); } - + return $decoded; } @@ -163,7 +169,7 @@ sub request_api_paginate { my @items; my @get_param = ( 'limit=' . $self->{limit} ); push @get_param, @{$options{get_param}} if (defined($options{get_param})); - + while (1) { my $response = $self->request_api( method => $options{method}, @@ -183,105 +189,141 @@ sub request_api_paginate { sub kubernetes_list_cronjobs { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/apis/batch/v1beta1/cronjobs'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/apis/batch/v1beta1/namespaces/' . $self->{namespace} . '/cronjobs' : '/apis/batch/v1beta1/cronjobs' + ); + return $response; } sub kubernetes_list_daemonsets { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/apis/apps/v1/daemonsets'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/apis/apps/v1/namespaces/' . $self->{namespace} . '/daemonsets' : '/apis/apps/v1/daemonsets' + ); + return $response; } sub kubernetes_list_deployments { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/apis/apps/v1/deployments'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/apis/apps/v1/namespaces/' . $self->{namespace} . '/deployments' : '/apis/apps/v1/deployments' + ); + return $response; } sub kubernetes_list_events { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/api/v1/events'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/api/v1/namespaces/' . $self->{namespace} . '/events' : '/api/v1/events' + ); + return $response; } sub kubernetes_list_ingresses { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/apis/extensions/v1beta1/ingresses'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/apis/extensions/v1beta1/namespaces/' . $self->{namespace} . '/ingresses' : '/apis/extensions/v1beta1/ingresses' + ); + return $response; } sub kubernetes_list_namespaces { my ($self, %options) = @_; - + my $response = $self->request_api_paginate(method => 'GET', url_path => '/api/v1/namespaces'); - + return $response; } sub kubernetes_list_nodes { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/api/v1/nodes'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => '/api/v1/nodes' + ); + return $response; } sub kubernetes_list_rcs { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/api/v1/replicationcontrollers'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/api/v1/namespaces/' . $self->{namespace} . '/replicationcontrollers' : '/api/v1/replicationcontrollers' + ); + return $response; } sub kubernetes_list_replicasets { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/apis/apps/v1/replicasets'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/apis/apps/v1/namespaces/' . $self->{namespace} . '/replicasets' : '/apis/apps/v1/replicasets' + ); + return $response; } sub kubernetes_list_services { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/apis/v1/services'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/api/v1/namespaces/' . $self->{namespace} . '/services' : '/api/v1/services' + ); + return $response; } sub kubernetes_list_statefulsets { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/apis/apps/v1/statefulsets'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/apis/apps/v1/namespaces/' . $self->{namespace} . '/statefulsets' : '/apis/apps/v1/statefulsets' + ); + return $response; } sub kubernetes_list_pods { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/api/v1/pods'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => $self->{namespace} ne '' ? '/api/v1/namespaces/' . $self->{namespace} . '/pods' : '/api/v1/pods' + ); + return $response; } sub kubernetes_list_pvs { my ($self, %options) = @_; - - my $response = $self->request_api_paginate(method => 'GET', url_path => '/api/v1/persistentvolumes'); - + + my $response = $self->request_api_paginate( + method => 'GET', + url_path => '/api/v1/persistentvolumes' + ); + return $response; } @@ -325,6 +367,10 @@ Number of responses to return for each list calls. See https://kubernetes.io/docs/reference/kubernetes-api/common-parameters/common-parameters/#limit +=item B<--namespace> + +Set namespace to get informations. + =back =head1 DESCRIPTION diff --git a/src/cloud/kubernetes/custom/kubectl.pm b/src/cloud/kubernetes/custom/kubectl.pm index 801b6ab60..ff2811a6d 100644 --- a/src/cloud/kubernetes/custom/kubectl.pm +++ b/src/cloud/kubernetes/custom/kubectl.pm @@ -48,6 +48,7 @@ sub new { 'timeout:s' => { name => 'timeout', default => 10 }, 'config-file:s' => { name => 'config_file', default => '~/.kube/config' }, 'context:s' => { name => 'context' }, + 'namespace:s' => { name => 'namespace' }, 'sudo' => { name => 'sudo' }, 'command:s' => { name => 'command', default => '' }, 'command-path:s' => { name => 'command_path' }, @@ -76,6 +77,8 @@ sub check_options { $self->{config_file} = (defined($self->{option_results}->{config_file})) ? $self->{option_results}->{config_file} : ''; $self->{context} = (defined($self->{option_results}->{context})) ? $self->{option_results}->{context} : ''; $self->{timeout} = (defined($self->{option_results}->{timeout})) && $self->{option_results}->{timeout} =~ /(\d+)/ ? $1 : 10; + $self->{namespace_option} = defined($self->{option_results}->{namespace}) && $self->{option_results}->{namespace} ne '' ? + "--namespace='$self->{option_results}->{namespace}'" : '--all-namespaces'; if (!defined($self->{config_file}) || $self->{config_file} eq '') { $self->{output}->add_option_msg(short_msg => "Need to specify --config-file option."); @@ -155,7 +158,7 @@ sub execute { sub kubernetes_list_cronjobs { my ($self, %options) = @_; - my $cmd = "get cronjobs --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get cronjobs $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -167,7 +170,7 @@ sub kubernetes_list_cronjobs { sub kubernetes_list_daemonsets { my ($self, %options) = @_; - my $cmd = "get daemonsets --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get daemonsets $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -179,7 +182,7 @@ sub kubernetes_list_daemonsets { sub kubernetes_list_deployments { my ($self, %options) = @_; - my $cmd = "get deployments --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get deployments $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -191,7 +194,7 @@ sub kubernetes_list_deployments { sub kubernetes_list_events { my ($self, %options) = @_; - my $cmd = "get events --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get events $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -203,7 +206,7 @@ sub kubernetes_list_events { sub kubernetes_list_ingresses { my ($self, %options) = @_; - my $cmd = "get ingresses --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get ingresses $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -227,7 +230,7 @@ sub kubernetes_list_namespaces { sub kubernetes_list_nodes { my ($self, %options) = @_; - my $cmd = "get nodes --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get nodes $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -239,7 +242,7 @@ sub kubernetes_list_nodes { sub kubernetes_list_rcs { my ($self, %options) = @_; - my $cmd = "get replicationcontroller --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get replicationcontroller $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -251,7 +254,7 @@ sub kubernetes_list_rcs { sub kubernetes_list_replicasets { my ($self, %options) = @_; - my $cmd = "get replicasets --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get replicasets $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -263,7 +266,7 @@ sub kubernetes_list_replicasets { sub kubernetes_list_services { my ($self, %options) = @_; - my $cmd = "get services --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get services $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -275,7 +278,7 @@ sub kubernetes_list_services { sub kubernetes_list_statefulsets { my ($self, %options) = @_; - my $cmd = "get statefulsets --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get statefulsets $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -287,7 +290,7 @@ sub kubernetes_list_statefulsets { sub kubernetes_list_pods { my ($self, %options) = @_; - my $cmd = "get pods --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get pods $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -299,7 +302,7 @@ sub kubernetes_list_pods { sub kubernetes_list_pvs { my ($self, %options) = @_; - my $cmd = "get pv --all-namespaces --output='json' --kubeconfig='" . $self->{config_file} . "'" + my $cmd = "get pv $self->{namespace_option} --output='json' --kubeconfig='" . $self->{config_file} . "'" . " --request-timeout='" . $self->{timeout} . "'"; $cmd .= " --context='" . $self->{context} . "'" if (defined($self->{context}) && $self->{context} ne ''); @@ -335,6 +338,10 @@ Kubernetes configuration file path (Default: '~/.kube/config'). Context to use in configuration file. +=item B<--namespace> + +Set namespace to get informations. + =item B<--timeout> Set timeout in seconds (Default: 10). diff --git a/src/cloud/kubernetes/mode/clusterevents.pm b/src/cloud/kubernetes/mode/clusterevents.pm index d97ae3e5f..c16080bd3 100644 --- a/src/cloud/kubernetes/mode/clusterevents.pm +++ b/src/cloud/kubernetes/mode/clusterevents.pm @@ -100,20 +100,20 @@ sub set_counters { $self->{maps_counters}->{global} = [ { label => 'warning', nlabel => 'events.type.warning.count', set => { key_values => [ { name => 'warning' } ], - output_template => 'Warning : %d', + output_template => 'warning: %d', perfdatas => [ - { label => 'warning_events', template => '%d', min => 0 } + { template => '%d', min => 0 } ] } }, { label => 'normal', nlabel => 'events.type.normal.count', set => { key_values => [ { name => 'normal' } ], - output_template => 'Normal : %d', + output_template => 'normal: %d', perfdatas => [ - { label => 'normal_events', template => '%d', min => 0 } + { template => '%d', min => 0 } ] } - }, + } ]; $self->{maps_counters}->{events} = [ @@ -140,31 +140,22 @@ sub new { my ($class, %options) = @_; my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); bless $self, $class; - + $options{options}->add_options(arguments => { - "filter-type:s" => { name => 'filter_type' }, - "filter-namespace:s" => { name => 'filter_namespace' }, + 'filter-type:s' => { name => 'filter_type' }, + 'filter-namespace:s' => { name => 'filter_namespace' } }); - + return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); - - $self->change_macros(macros => ['warning_status', 'critical_status']); -} - sub manage_selection { my ($self, %options) = @_; - $self->{events} = {}; - my $results = $options{custom}->kubernetes_list_events(); $self->{global} = { normal => 0, warning => 0 }; - + $self->{events} = {}; foreach my $event (@{$results}) { if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' && $event->{type} !~ /$self->{option_results}->{filter_type}/) { @@ -178,7 +169,7 @@ sub manage_selection { } $self->{global}->{lc($event->{type})}++; - + $self->{events}->{$event->{metadata}->{uid}} = { name => $event->{metadata}->{name}, namespace => $event->{metadata}->{namespace}, diff --git a/src/cloud/kubernetes/mode/cronjobstatus.pm b/src/cloud/kubernetes/mode/cronjobstatus.pm index 418be041b..18fe569db 100644 --- a/src/cloud/kubernetes/mode/cronjobstatus.pm +++ b/src/cloud/kubernetes/mode/cronjobstatus.pm @@ -90,7 +90,8 @@ sub set_counters { { label => 'status', type => 2, set => { key_values => [ { name => 'active' }, { name => 'last_schedule_time' }, { name => 'name' }, - { name => 'namespace' } ], + { name => 'namespace' } + ], closure_custom_calc => $self->can('custom_status_calc'), closure_custom_output => $self->can('custom_status_output'), closure_custom_perfdata => $self->can('custom_status_perfdata'), diff --git a/src/cloud/kubernetes/mode/discoverynodes.pm b/src/cloud/kubernetes/mode/discovery.pm similarity index 53% rename from src/cloud/kubernetes/mode/discoverynodes.pm rename to src/cloud/kubernetes/mode/discovery.pm index eaed4ae7d..9a287bd1a 100644 --- a/src/cloud/kubernetes/mode/discoverynodes.pm +++ b/src/cloud/kubernetes/mode/discovery.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package cloud::kubernetes::mode::discoverynodes; +package cloud::kubernetes::mode::discovery; use base qw(centreon::plugins::mode); @@ -31,7 +31,8 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - "prettify" => { name => 'prettify' }, + 'resource-type:s' => { name => 'resource_type' }, + 'prettify' => { name => 'prettify' } }); return $self; @@ -40,6 +41,60 @@ sub new { sub check_options { my ($self, %options) = @_; $self->SUPER::init(%options); + + if ($self->{option_results}->{resource_type} !~ /^node|namespace$/) { + $self->{output}->add_option_msg(short_msg => 'unknown resource type'); + $self->{output}->option_exit(); + } +} + +sub discovery_node { + my ($self, %options) = @_; + + my $nodes = $options{custom}->kubernetes_list_nodes(); + + my $disco_data = []; + foreach my $node (@$nodes) { + my %entry; + $entry{name} = $node->{metadata}->{name}; + $entry{uid} = $node->{metadata}->{uid}; + $entry{os_image} = $node->{status}->{nodeInfo}->{osImage}; + $entry{kubelet_version} = $node->{status}->{nodeInfo}->{kubeletVersion}; + if (defined($node->{metadata}->{labels}->{'node-role.kubernetes.io/control-plane'})) { + $entry{node_role} = "control-plane"; + } elsif (defined($node->{metadata}->{labels}->{'node-role.kubernetes.io/master'})) { + $entry{node_role} = "master"; + } else { + $entry{node_role} = "worker"; + } + + foreach my $address (@{$node->{status}->{addresses}}) { + $entry{internal_ip} = $address->{address} if ($address->{type} eq "InternalIP"); + $entry{external_ip} = $address->{address} if ($address->{type} eq "ExternalIP"); + $entry{hostname} = $address->{address} if ($address->{type} eq "Hostname"); + } + + push @$disco_data, \%entry; + } + + return $disco_data; +} + +sub discovery_namespace { + my ($self, %options) = @_; + + my $namespaces = $options{custom}->kubernetes_list_namespaces(); + + my $disco_data = []; + foreach my $namespace (@$namespaces) { + my %entry; + $entry{name} = $namespace->{metadata}->{name}; + $entry{uid} = $namespace->{metadata}->{uid}; + + push @$disco_data, \%entry; + } + + return $disco_data; } sub run { @@ -50,35 +105,21 @@ sub run { $disco_stats->{start_time} = time(); - my $nodes = $options{custom}->kubernetes_list_nodes(); - - foreach my $node (@{$nodes}) { - my %node; - $node{name} = $node->{metadata}->{name}; - $node{uid} = $node->{metadata}->{uid}; - $node{os_image} = $node->{status}->{nodeInfo}->{osImage}; - $node{kubelet_version} = $node->{status}->{nodeInfo}->{kubeletVersion}; - if (defined($node->{metadata}->{labels}->{'node-role.kubernetes.io/control-plane'})) { - $node{node_role} = "control-plane"; - } elsif (defined($node->{metadata}->{labels}->{'node-role.kubernetes.io/master'})) { - $node{node_role} = "master"; - } else { - $node{node_role} = "worker"; - } - - foreach my $address (@{$node->{status}->{addresses}}) { - $node{internal_ip} = $address->{address} if ($address->{type} eq "InternalIP"); - $node{external_ip} = $address->{address} if ($address->{type} eq "ExternalIP"); - $node{hostname} = $address->{address} if ($address->{type} eq "Hostname"); - } - - push @disco_data, \%node; + my $results = []; + if ($self->{option_results}->{resource_type} eq 'node') { + $results = $self->discovery_node( + custom => $options{custom} + ); + } elsif ($self->{option_results}->{resource_type} eq 'namespace') { + $results = $self->discovery_namespace( + custom => $options{custom} + ); } $disco_stats->{end_time} = time(); $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; - $disco_stats->{discovered_items} = @disco_data; - $disco_stats->{results} = \@disco_data; + $disco_stats->{discovered_items} = scalar(@$results); + $disco_stats->{results} = $results; my $encoded_data; eval { @@ -91,7 +132,7 @@ sub run { if ($@) { $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}'; } - + $self->{output}->output_add(short_msg => $encoded_data); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1); $self->{output}->exit(); @@ -107,6 +148,10 @@ Nodes discovery. =over 8 +=item B<--resource-type> + +Choose the type of resources to discover (Can be: 'node', 'namespace'). + =item B<--prettify> Prettify JSON output. diff --git a/src/cloud/kubernetes/mode/listdeployments.pm b/src/cloud/kubernetes/mode/listdeployments.pm index 6e23978f2..fc468c40e 100644 --- a/src/cloud/kubernetes/mode/listdeployments.pm +++ b/src/cloud/kubernetes/mode/listdeployments.pm @@ -31,8 +31,8 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - "filter-name:s" => { name => 'filter_name' }, - "filter-namespace:s" => { name => 'filter_namespace' }, + 'filter-name:s' => { name => 'filter_name' }, + 'filter-namespace:s' => { name => 'filter_namespace' } }); return $self; @@ -63,7 +63,7 @@ sub manage_selection { $self->{deployments}->{$deployment->{metadata}->{uid}} = { uid => $deployment->{metadata}->{uid}, name => $deployment->{metadata}->{name}, - namespace => $deployment->{metadata}->{namespace}, + namespace => $deployment->{metadata}->{namespace} } } } @@ -73,14 +73,20 @@ sub run { $self->manage_selection(%options); foreach my $deployment (sort keys %{$self->{deployments}}) { - $self->{output}->output_add(long_msg => sprintf("[uid = %s] [name = %s] [namespace = %s]", - $self->{deployments}->{$deployment}->{uid}, - $self->{deployments}->{$deployment}->{name}, - $self->{deployments}->{$deployment}->{namespace})); + $self->{output}->output_add( + long_msg => sprintf( + '[uid: %s] [name: %s] [namespace: %s]', + $self->{deployments}->{$deployment}->{uid}, + $self->{deployments}->{$deployment}->{name}, + $self->{deployments}->{$deployment}->{namespace} + ) + ); } - $self->{output}->output_add(severity => 'OK', - short_msg => 'List deployments:'); + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List deployments:' + ); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); $self->{output}->exit(); } @@ -99,7 +105,7 @@ sub disco_show { $self->{output}->add_disco_entry( uid => $self->{deployments}->{$deployment}->{uid}, name => $self->{deployments}->{$deployment}->{name}, - namespace => $self->{deployments}->{$deployment}->{namespace}, + namespace => $self->{deployments}->{$deployment}->{namespace} ); } } diff --git a/src/cloud/kubernetes/mode/listingresses.pm b/src/cloud/kubernetes/mode/listingresses.pm index 650449791..00f733e8f 100644 --- a/src/cloud/kubernetes/mode/listingresses.pm +++ b/src/cloud/kubernetes/mode/listingresses.pm @@ -73,14 +73,20 @@ sub run { $self->manage_selection(%options); foreach my $ingress (sort keys %{$self->{ingresses}}) { - $self->{output}->output_add(long_msg => sprintf("[uid = %s] [name = %s] [namespace = %s]", - $self->{ingresses}->{$ingress}->{uid}, - $self->{ingresses}->{$ingress}->{name}, - $self->{ingresses}->{$ingress}->{namespace})); + $self->{output}->output_add( + long_msg => sprintf( + '[uid: %s] [name: %s] [namespace: %s]', + $self->{ingresses}->{$ingress}->{uid}, + $self->{ingresses}->{$ingress}->{name}, + $self->{ingresses}->{$ingress}->{namespace} + ) + ); } - $self->{output}->output_add(severity => 'OK', - short_msg => 'List ingresses:'); + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List ingresses:' + ); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); $self->{output}->exit(); } @@ -99,7 +105,7 @@ sub disco_show { $self->{output}->add_disco_entry( uid => $self->{ingresses}->{$ingress}->{uid}, name => $self->{ingresses}->{$ingress}->{name}, - namespace => $self->{ingresses}->{$ingress}->{namespace}, + namespace => $self->{ingresses}->{$ingress}->{namespace} ); } } diff --git a/src/cloud/kubernetes/mode/listnamespaces.pm b/src/cloud/kubernetes/mode/listnamespaces.pm index 3f82a2a01..d67951c7d 100644 --- a/src/cloud/kubernetes/mode/listnamespaces.pm +++ b/src/cloud/kubernetes/mode/listnamespaces.pm @@ -54,9 +54,9 @@ sub manage_selection { next; } - $self->{namespaces}->{$namespace->{metadata}->{uid}} = { + $self->{namespaces}->{ $namespace->{metadata}->{uid} } = { uid => $namespace->{metadata}->{uid}, - name => $namespace->{metadata}->{name}, + name => $namespace->{metadata}->{name} } } } @@ -66,13 +66,19 @@ sub run { $self->manage_selection(%options); foreach my $namespace (sort keys %{$self->{namespaces}}) { - $self->{output}->output_add(long_msg => sprintf("[uid = %s] [name = %s]", - $self->{namespaces}->{$namespace}->{uid}, - $self->{namespaces}->{$namespace}->{name})); + $self->{output}->output_add( + long_msg => sprintf( + '[uid: %s] [name: %s]', + $self->{namespaces}->{$namespace}->{uid}, + $self->{namespaces}->{$namespace}->{name} + ) + ); } - $self->{output}->output_add(severity => 'OK', - short_msg => 'List namespaces:'); + $self->{output}->output_add( + severity => 'OK', + short_msg => 'List namespaces:' + ); $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); $self->{output}->exit(); } @@ -90,7 +96,7 @@ sub disco_show { foreach my $namespace (sort keys %{$self->{namespaces}}) { $self->{output}->add_disco_entry( uid => $self->{namespaces}->{$namespace}->{uid}, - name => $self->{namespaces}->{$namespace}->{name}, + name => $self->{namespaces}->{$namespace}->{name} ); } } diff --git a/src/cloud/kubernetes/plugin.pm b/src/cloud/kubernetes/plugin.pm index b5fe752f6..b2704855e 100644 --- a/src/cloud/kubernetes/plugin.pm +++ b/src/cloud/kubernetes/plugin.pm @@ -29,13 +29,12 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $self->{version} = '1.0'; - %{$self->{modes}} = ( + $self->{modes} = { 'cluster-events' => 'cloud::kubernetes::mode::clusterevents', 'cronjob-status' => 'cloud::kubernetes::mode::cronjobstatus', 'daemonset-status' => 'cloud::kubernetes::mode::daemonsetstatus', 'deployment-status' => 'cloud::kubernetes::mode::deploymentstatus', - 'discovery-nodes' => 'cloud::kubernetes::mode::discoverynodes', + 'discovery' => 'cloud::kubernetes::mode::discovery', 'list-cronjobs' => 'cloud::kubernetes::mode::listcronjobs', 'list-daemonsets' => 'cloud::kubernetes::mode::listdaemonsets', 'list-deployments' => 'cloud::kubernetes::mode::listdeployments', @@ -54,11 +53,11 @@ sub new { 'pod-status' => 'cloud::kubernetes::mode::podstatus', 'replicaset-status' => 'cloud::kubernetes::mode::replicasetstatus', 'replicationcontroller-status' => 'cloud::kubernetes::mode::replicationcontrollerstatus', - 'statefulset-status' => 'cloud::kubernetes::mode::statefulsetstatus', - ); + 'statefulset-status' => 'cloud::kubernetes::mode::statefulsetstatus' + }; - $self->{custom_modes}{api} = 'cloud::kubernetes::custom::api'; - $self->{custom_modes}{kubectl} = 'cloud::kubernetes::custom::kubectl'; + $self->{custom_modes}->{api} = 'cloud::kubernetes::custom::api'; + $self->{custom_modes}->{kubectl} = 'cloud::kubernetes::custom::kubectl'; return $self; }