Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Q
qa-platform
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
qa-platform
Commits
664afe93
Commit
664afe93
authored
Mar 11, 2022
by
黎博
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
NamespaceCache初始化修改
parent
0ba84c2f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
10 deletions
+111
-10
NamespaceCache.java
src/main/java/cn/qg/qaplatform/config/NamespaceCache.java
+8
-6
K8sService.java
src/main/java/cn/qg/qaplatform/utils/K8sService.java
+103
-4
No files found.
src/main/java/cn/qg/qaplatform/config/NamespaceCache.java
View file @
664afe93
package
cn
.
qg
.
qaplatform
.
config
;
import
cn.qg.qaplatform.utils.HttpClientUtils
;
import
cn.qg.qaplatform.utils.K8sService
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
...
...
@@ -20,16 +22,16 @@ import java.util.Map;
@Component
public
class
NamespaceCache
{
@Autowired
K8sService
k8sService
;
public
static
Map
<
String
,
String
>
namespaceMap
=
new
HashMap
<>();
@PostConstruct
public
void
init
()
{
String
url
=
"http://holmes.liangkebang.com/k8s/namespace/list?env=all"
;
JSONObject
response
=
HttpClientUtils
.
doGetReturnJson
(
url
);
JSONArray
nsJsonArray
=
(
JSONArray
)
response
.
get
(
"data"
);
List
<
Map
>
namespaceList
=
JSONArray
.
parseArray
(
nsJsonArray
.
toJSONString
(),
Map
.
class
);
for
(
Map
map:
namespaceList
)
{
namespaceMap
.
put
(
map
.
get
(
"name"
).
toString
(),
map
.
get
(
"mysqlHost"
)
+
":"
+
map
.
get
(
"mysqlPort"
));
List
<
Map
<
String
,
String
>>
namespaceList
=
k8sService
.
getNamespaceList
(
"all"
);
for
(
Map
<
String
,
String
>
namespace:
namespaceList
)
{
namespaceMap
.
put
(
namespace
.
get
(
"name"
),
namespace
.
get
(
"mysqlHost"
)
+
":"
+
namespace
.
get
(
"mysqlPort"
));
}
log
.
info
(
"获取到Namespace列表:{}"
,
JSON
.
toJSONString
(
namespaceMap
));
}
...
...
src/main/java/cn/qg/qaplatform/utils/K8sService.java
View file @
664afe93
package
cn
.
qg
.
qaplatform
.
utils
;
import
com.alibaba.fastjson.JSON
;
import
io.fabric8.kubernetes.api.model.Namespace
;
import
io.fabric8.kubernetes.api.model.Pod
;
import
io.fabric8.kubernetes.api.model.Service
;
import
io.fabric8.kubernetes.api.model.ServicePort
;
...
...
@@ -14,10 +16,8 @@ import java.io.BufferedReader;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.TreeMap
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
@Slf4j
@Component
...
...
@@ -111,4 +111,103 @@ public class K8sService {
}
return
envMap
;
}
/**
* 获取namespace列表
* @return
*/
public
List
<
Map
<
String
,
String
>>
getNamespaceList
(
String
env
)
{
List
<
Map
<
String
,
String
>>
resultList
=
new
ArrayList
<>();
List
<
Namespace
>
namespaceList
=
kubernetesClient
.
namespaces
().
list
().
getItems
();
for
(
Namespace
namespace
:
namespaceList
)
{
if
(
namespace
.
getMetadata
().
getAnnotations
()
!=
null
&&
env
.
equals
(
"all"
))
{
String
name
=
namespace
.
getMetadata
().
getName
();
// 获取Service
Service
service
=
kubernetesClient
.
services
().
inNamespace
(
name
).
withName
(
"mysql"
).
get
();
// 获取Pod
Map
<
String
,
String
>
labels
=
new
HashMap
<>();
labels
.
put
(
"qcloud-app"
,
"mysql"
);
labels
.
put
(
"type"
,
"base"
);
List
<
Pod
>
podList
=
kubernetesClient
.
pods
().
inNamespace
(
name
).
withLabels
(
labels
).
list
().
getItems
();
Integer
port
=
null
;
String
host
=
null
;
if
(
service
!=
null
)
{
port
=
service
.
getSpec
().
getPorts
().
get
(
0
).
getNodePort
();
}
if
(
podList
.
size
()
==
1
)
{
host
=
podList
.
get
(
0
).
getStatus
().
getHostIP
();
}
if
(
podList
.
size
()
>=
2
)
{
for
(
Pod
pod:
podList
)
{
if
(
pod
.
getStatus
().
getHostIP
()
!=
null
)
{
host
=
pod
.
getStatus
().
getHostIP
();
break
;
}
}
}
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
);
df
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
));
SimpleDateFormat
df2
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
Map
<
String
,
String
>
map
=
new
HashMap
<>();
map
.
put
(
"name"
,
name
);
map
.
put
(
"description"
,
namespace
.
getMetadata
().
getAnnotations
().
get
(
"description"
));
map
.
put
(
"owner"
,
namespace
.
getMetadata
().
getAnnotations
().
get
(
"owner"
));
map
.
put
(
"status"
,
namespace
.
getStatus
().
getPhase
());
map
.
put
(
"mysqlHost"
,
host
);
map
.
put
(
"mysqlPort"
,
String
.
valueOf
(
port
));
try
{
map
.
put
(
"createdAt"
,
df2
.
format
(
df
.
parse
(
namespace
.
getMetadata
().
getCreationTimestamp
())));
}
catch
(
Exception
e
)
{
log
.
info
(
"时间解析异常!"
);
e
.
printStackTrace
();
map
.
put
(
"createdAt"
,
namespace
.
getMetadata
().
getCreationTimestamp
());
}
resultList
.
add
(
map
);
}
else
if
(
namespace
.
getMetadata
().
getAnnotations
()
!=
null
&&
namespace
.
getMetadata
().
getAnnotations
().
get
(
"description"
).
equals
(
env
))
{
String
name
=
namespace
.
getMetadata
().
getName
();
// 获取Service
Service
service
=
kubernetesClient
.
services
().
inNamespace
(
name
).
withName
(
"mysql"
).
get
();
// 获取Pod
Map
<
String
,
String
>
labels
=
new
HashMap
<>();
labels
.
put
(
"qcloud-app"
,
"mysql"
);
labels
.
put
(
"type"
,
"base"
);
List
<
Pod
>
podList
=
kubernetesClient
.
pods
().
inNamespace
(
name
).
withLabels
(
labels
).
list
().
getItems
();
Integer
port
=
null
;
String
host
=
null
;
if
(
service
!=
null
)
{
port
=
service
.
getSpec
().
getPorts
().
get
(
0
).
getNodePort
();
}
if
(
podList
.
size
()
==
1
)
{
host
=
podList
.
get
(
0
).
getStatus
().
getHostIP
();
}
if
(
podList
.
size
()
>=
2
)
{
for
(
Pod
pod:
podList
)
{
if
(
pod
.
getStatus
().
getHostIP
()
!=
null
)
{
host
=
pod
.
getStatus
().
getHostIP
();
break
;
}
}
}
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"yyyy-MM-dd'T'HH:mm:ss'Z'"
);
df
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
));
SimpleDateFormat
df2
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
Map
<
String
,
String
>
map
=
new
HashMap
<>();
map
.
put
(
"name"
,
name
);
map
.
put
(
"description"
,
namespace
.
getMetadata
().
getAnnotations
().
get
(
"description"
));
map
.
put
(
"owner"
,
namespace
.
getMetadata
().
getAnnotations
().
get
(
"owner"
));
map
.
put
(
"status"
,
namespace
.
getStatus
().
getPhase
());
map
.
put
(
"mysqlHost"
,
host
);
map
.
put
(
"mysqlPort"
,
String
.
valueOf
(
port
));
try
{
map
.
put
(
"createdAt"
,
df2
.
format
(
df
.
parse
(
namespace
.
getMetadata
().
getCreationTimestamp
())));
}
catch
(
Exception
e
)
{
log
.
info
(
"时间解析异常!"
);
e
.
printStackTrace
();
map
.
put
(
"createdAt"
,
namespace
.
getMetadata
().
getCreationTimestamp
());
}
resultList
.
add
(
map
);
}
}
return
resultList
;
}
}
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