..

列出 kubernetes 上 deployment apps 的镜像

今天盼哥问我说怎么看每个服务的版本,我说可以通过 kubectl describe pod 看,盼哥表示太麻烦了,于是我用 perl 写 了一个脚本,如下:

#!/bin/perl
use strict;
use warnings;
use Getopt::Long qw( GetOptions :config no_ignore_case);


sub usage ($);
GetOptions("n|namnespace=s",    \(my $namespace),
           "h|help",            \(my $help))
   or usage(1);

if($help){
	usage(0);
}
if(not $namespace) {
	$namespace = "default";
}
sub get_deployment(@){
	my $namespace = $_[0];
	my @deployments = `kubectl get deployments -n $namespace | grep -v NAME | awk '{print \$1}'`;
	return @deployments;
}


sub print_version(@){
	my $namespace = $_[0];
	my $deployment = $_[1];
	$deployment =~ s/^\s+|\s+$//g;
	my @image = `kubectl describe deploy $deployment -n $namespace | grep Image | awk '{print \$2}'`;
	my $image_str = join ", ", @image;
	$image_str =~ s/^\s+|\n+?//g;
	$image_str = "[" . $image_str . "]";
	printf "%-25s => %s\n", $deployment,$image_str;
}
my @deployments = get_deployment $namespace;

foreach my $deployment (@deployments) {
	chomp $deployment;
	print_version "test", $deployment;
	
}


sub usage ($) {
    my $rc = shift;
    my $msg = <<'_EOC_';
check [options] list deployment image version
Options:
    -n NAMESPACE
    --namespae               kubernetes namespace.

    -h
    --help                Print this help.
Copyright (C) QAX (DAYU) .
_EOC_
    if ($rc == 0) {
        print $msg;
        exit(0);
    }

    warn $msg;
    exit($rc);
}

命名为 check, 执行 check -h 可以看到帮助信息

check [options] list kubernetes deployment image version.

Options:

    -n NAMESPACE

    --namespae               kubernetes namespace.



    -h

    --help                Print this help.

Copyright (C) QAX (DAYU) .