Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
holmes
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
QA
holmes
Commits
a9d5369d
Commit
a9d5369d
authored
Aug 25, 2021
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
获取namespace列表新增缓存
parent
cba3c864
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
88 additions
and
72 deletions
+88
-72
K8sService.java
src/main/java/cn/qg/holmes/service/k8s/K8sService.java
+88
-72
No files found.
src/main/java/cn/qg/holmes/service/k8s/K8sService.java
View file @
a9d5369d
...
@@ -2,6 +2,8 @@ package cn.qg.holmes.service.k8s;
...
@@ -2,6 +2,8 @@ package cn.qg.holmes.service.k8s;
import
cn.qg.holmes.entity.k8s.DockerProject
;
import
cn.qg.holmes.entity.k8s.DockerProject
;
import
cn.qg.holmes.entity.k8s.ServiceCreateVo
;
import
cn.qg.holmes.entity.k8s.ServiceCreateVo
;
import
cn.qg.holmes.utils.RedisUtils
;
import
com.alibaba.fastjson.JSON
;
import
io.fabric8.kubernetes.api.model.*
;
import
io.fabric8.kubernetes.api.model.*
;
import
io.fabric8.kubernetes.api.model.apps.Deployment
;
import
io.fabric8.kubernetes.api.model.apps.Deployment
;
import
io.fabric8.kubernetes.api.model.apps.DeploymentSpec
;
import
io.fabric8.kubernetes.api.model.apps.DeploymentSpec
;
...
@@ -12,6 +14,7 @@ import io.fabric8.kubernetes.client.Config;
...
@@ -12,6 +14,7 @@ import io.fabric8.kubernetes.client.Config;
import
io.fabric8.kubernetes.client.DefaultKubernetesClient
;
import
io.fabric8.kubernetes.client.DefaultKubernetesClient
;
import
io.fabric8.kubernetes.client.KubernetesClient
;
import
io.fabric8.kubernetes.client.KubernetesClient
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -27,11 +30,16 @@ import java.util.*;
...
@@ -27,11 +30,16 @@ import java.util.*;
@Component
@Component
public
class
K8sService
{
public
class
K8sService
{
@Autowired
RedisUtils
redisUtils
;
@Value
(
"${no.healthcheck.service}"
)
@Value
(
"${no.healthcheck.service}"
)
private
String
noHealthCheckService
;
private
String
noHealthCheckService
;
private
KubernetesClient
kubernetesClient
;
private
KubernetesClient
kubernetesClient
;
private
final
String
NS_PREFIX
=
"namespace:"
;
public
K8sService
()
{
public
K8sService
()
{
try
{
try
{
String
configYAML
=
String
.
join
(
"\n"
,
readConfigFile
(
"kube-config.yml"
));
String
configYAML
=
String
.
join
(
"\n"
,
readConfigFile
(
"kube-config.yml"
));
...
@@ -110,8 +118,13 @@ public class K8sService {
...
@@ -110,8 +118,13 @@ public class K8sService {
* 获取namespace列表
* 获取namespace列表
* @return
* @return
*/
*/
public
List
<
Map
<
String
,
Object
>>
getNamespaceList
(
String
env
)
{
public
List
<
Map
>
getNamespaceList
(
String
env
)
{
List
<
Map
<
String
,
Object
>>
resultList
=
new
ArrayList
<>();
// 首先从缓存获取
Object
redisValue
=
redisUtils
.
get
(
NS_PREFIX
+
env
);
if
(
redisValue
!=
null
)
{
return
JSON
.
parseArray
(
redisValue
.
toString
(),
Map
.
class
);
}
else
{
List
<
Map
>
resultList
=
new
ArrayList
<>();
List
<
Namespace
>
namespaceList
=
kubernetesClient
.
namespaces
().
list
().
getItems
();
List
<
Namespace
>
namespaceList
=
kubernetesClient
.
namespaces
().
list
().
getItems
();
for
(
Namespace
namespace
:
namespaceList
)
{
for
(
Namespace
namespace
:
namespaceList
)
{
if
(
namespace
.
getMetadata
().
getAnnotations
()
!=
null
&&
env
.
equals
(
"all"
))
{
if
(
namespace
.
getMetadata
().
getAnnotations
()
!=
null
&&
env
.
equals
(
"all"
))
{
...
@@ -133,7 +146,7 @@ public class K8sService {
...
@@ -133,7 +146,7 @@ public class K8sService {
}
}
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
);
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
);
df
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
));
df
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
));
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
Map
map
=
new
HashMap
<>();
map
.
put
(
"name"
,
name
);
map
.
put
(
"name"
,
name
);
map
.
put
(
"description"
,
namespace
.
getMetadata
().
getAnnotations
().
get
(
"description"
));
map
.
put
(
"description"
,
namespace
.
getMetadata
().
getAnnotations
().
get
(
"description"
));
map
.
put
(
"owner"
,
namespace
.
getMetadata
().
getAnnotations
().
get
(
"owner"
));
map
.
put
(
"owner"
,
namespace
.
getMetadata
().
getAnnotations
().
get
(
"owner"
));
...
@@ -184,8 +197,11 @@ public class K8sService {
...
@@ -184,8 +197,11 @@ public class K8sService {
resultList
.
add
(
map
);
resultList
.
add
(
map
);
}
}
}
}
// 设置缓存
redisUtils
.
set
(
NS_PREFIX
+
env
,
JSON
.
toJSONString
(
resultList
),
1800
);
return
resultList
;
return
resultList
;
}
}
}
/**
/**
* 获取运行中的pod列表
* 获取运行中的pod列表
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment