Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commited36c68

Browse files
committed
kubeconfig module
1 parentf38f392 commited36c68

File tree

4 files changed

+156
-26
lines changed

4 files changed

+156
-26
lines changed

‎cpanfile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
requires'Carp';
22
requires'JSON';
3+
requires'YAML::XS';
34
requires'DateTime';
45
requires'Log::Any';
56
requires'Module::Runtime';
@@ -8,6 +9,7 @@ requires 'Module::Find';
89
requires'LWP::UserAgent';
910
requires'LWP::Protocol::https';
1011
requires'Class::Accessor';
12+
requires'MIME::Base64';
1113

1214
on'test'=>sub {
1315
requires'Test::Simple';

‎examples/load_kubeconfig.pl‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use strict;
2+
use warningsFATAL=>'all';
3+
4+
use Kubernetes::Util::KubeConfig;
5+
6+
use Log::Any::Adapter ('Stdout');
7+
use Log::Any::Adapter ('Stderr');
8+
9+
use Net::SSLeay;
10+
$Net::SSLeay::trace = 2;
11+
12+
my$kubeconfig = Kubernetes::Util::KubeConfig->load_yaml_file();
13+
14+
my$api_factory =$kubeconfig->new_api_factory();
15+
16+
my$corev1_api =$api_factory->get_api('CoreV1');
17+
18+
my$namespaceList =$corev1_api->list_namespace();
19+
20+
printf"found%d namespaces:\n",scalar @{$namespaceList->items};
21+
printf"=====================\n";
22+
foreachmy$namespace (@{$namespaceList->items}) {
23+
printf"%s\n",$namespace->metadata->name;
24+
}

‎examples/ssl_client.pl‎

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎lib/Kubernetes/Util/KubeConfig.pm‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
packageKubernetes::Util::KubeConfig;
2+
3+
use strict;
4+
use warningsFATAL=>'all';
5+
use Carp;
6+
7+
use YAML::XSqw(Load LoadFile);
8+
use MIME::Base64;
9+
use Kubernetes::ApiClient;
10+
use Kubernetes::ApiFactory;
11+
use IO::Socket::SSL;
12+
use Log::Anyqw($log);
13+
use IO::Socket::SSL::Utils;
14+
15+
use base'Exporter';
16+
17+
our@EXPORT =qw(
18+
load_yaml_file
19+
load_yaml
20+
new_api_factory
21+
);
22+
23+
subload_yaml_file {
24+
my ($self,$yaml_file_path) =@_;
25+
26+
if (notdefined$yaml_file_path) {
27+
$log->debugf('Defaulting Kube-Config file path to $HOME/.kube/config');
28+
$yaml_file_path =$ENV{HOME} .'/.kube/config';
29+
}
30+
31+
die"$yaml_file_path doesn't exist!"ifnot-e$yaml_file_path;
32+
33+
my$kubeconfig = LoadFile($yaml_file_path);
34+
bless$kubeconfig=>$self;
35+
36+
$kubeconfig->validate;
37+
38+
return$kubeconfig;
39+
}
40+
41+
subload_yaml {
42+
my ($self,$yaml_str) =@_;
43+
44+
if (notdefined$yaml_str) {
45+
returnundef;
46+
}
47+
48+
my$kubeconfig = Load($yaml_str);
49+
bless$kubeconfig=>$self;
50+
51+
$kubeconfig->validate;
52+
53+
return$kubeconfig;
54+
}
55+
56+
subvalidate {
57+
my ($self) =@_;
58+
if (notdefined$self->{apiVersion} ||$self->{apiVersion}ne'v1') {
59+
die"Invalid Kube-Config content: ('apiVersion' is not 'v1')";
60+
}
61+
if (notdefined$self->{kind} ||$self->{kind}ne'Config') {
62+
die"Invalid Kube-Config content: ('kind' is not 'v1')";
63+
}
64+
if (notdefined$self->{contexts}) {
65+
die"Invalid Kube-Config content: missing 'contexts' section";
66+
}
67+
if (notdefined$self->{clusters}) {
68+
die"Invalid Kube-Config content: missing 'clusters' section";
69+
}
70+
if (notdefined$self->{users}) {
71+
die"Invalid Kube-Config content: missing 'users' section";
72+
}
73+
if (notdefined$self->{'current-context'}) {
74+
die"Invalid Kube-Config content: missing 'current-context' section";
75+
}
76+
}
77+
78+
subnew_api_factory {
79+
my ($self) =@_;
80+
81+
my$context_name =$self->{'current-context'};
82+
my$cluster_name;
83+
my$user_name;
84+
85+
foreachmy$ctx (@{$self->{contexts}}) {
86+
if ($context_nameeq$ctx->{name}) {
87+
$cluster_name =$ctx->{context}->{cluster};
88+
$user_name =$ctx->{context}->{user};
89+
}
90+
}
91+
92+
93+
my$current_cluster;
94+
foreachmy$cluster (@{$self->{clusters}}) {
95+
if ($cluster_nameeq$cluster->{name}) {
96+
$current_cluster =$cluster;
97+
}
98+
}
99+
100+
101+
my$current_user;
102+
foreachmy$user (@{$self->{users}}) {
103+
if ($user_nameeq$user->{name}) {
104+
$current_user =$user;
105+
}
106+
}
107+
108+
my%ssl_opts;
109+
110+
my$skip_tls_verify =$current_cluster->{cluster}->{'insecure-skip-tls-verify'};
111+
112+
if (defined$skip_tls_verify &&$skip_tls_verify == 1) {
113+
$ssl_opts{verify_hostname} = 0;
114+
$ssl_opts{SSL_verify_mode} = IO::Socket::SSL::SSL_VERIFY_NONE;
115+
}else {
116+
# print Dumper($current_cluster);
117+
$ssl_opts{SSL_ca} = [PEM_string2cert(decode_base64($current_cluster->{cluster}->{'certificate-authority-data'}))];
118+
$ssl_opts{SSL_use_cert} = 1;
119+
}
120+
121+
$ssl_opts{SSL_cert} = PEM_string2cert(decode_base64($current_user->{user}->{'client-certificate-data'}));
122+
$ssl_opts{SSL_key} = PEM_string2key(decode_base64($current_user->{user}->{'client-key-data'}));
123+
124+
return Kubernetes::ApiFactory->new(
125+
base_url=>$current_cluster->{cluster}->{server},
126+
ssl_opts=> \%ssl_opts,
127+
);
128+
}
129+
130+
1;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp