Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
DEMO
spring-boot
Commits
ddb5cc3f
Commit
ddb5cc3f
authored
Feb 07, 2020
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Make Kubernetes detection slightly more efficient"
See gh-19002
parent
aee22bfb
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
45 deletions
+49
-45
CloudPlatform.java
...in/java/org/springframework/boot/cloud/CloudPlatform.java
+13
-20
CloudPlatformTests.java
...va/org/springframework/boot/cloud/CloudPlatformTests.java
+36
-25
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/cloud/CloudPlatform.java
View file @
ddb5cc3f
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -16,8 +16,6 @@
package
org
.
springframework
.
boot
.
cloud
;
import
java.io.File
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.EnumerablePropertySource
;
import
org.springframework.core.env.Environment
;
...
...
@@ -75,22 +73,16 @@ public enum CloudPlatform {
*/
KUBERNETES
{
private
static
final
String
SERVICE_HOST_SUFFIX
=
"_SERVICE_HOST"
;
private
static
final
String
SERVICE_PORT_SUFFIX
=
"_SERVICE_PORT"
;
private
static
final
String
SECRET_LOCATION
=
"/var/run/secrets/kubernetes.io"
;
private
static
final
String
KUBERNETES_SERVICE_HOST
=
"KUBERNETES_SERVICE_HOST"
;
private
static
final
String
KUBERNETES_SERVICE_PORT
=
"KUBERNETES_SERVICE_PORT"
;
private
static
final
String
SERVICE_HOST_SUFFIX
=
"_SERVICE_HOST"
;
private
static
final
String
SERVICE_PORT_SUFFIX
=
"_SERVICE_PORT"
;
@Override
public
boolean
isActive
(
Environment
environment
)
{
if
(
environment
.
containsProperty
(
KUBERNETES_SERVICE_HOST
)
||
environment
.
containsProperty
(
KUBERNETES_SERVICE_PORT
)
||
isSecretLocationExists
())
{
return
true
;
}
if
(
environment
instanceof
ConfigurableEnvironment
)
{
return
isActive
((
ConfigurableEnvironment
)
environment
);
}
...
...
@@ -100,9 +92,15 @@ public enum CloudPlatform {
private
boolean
isActive
(
ConfigurableEnvironment
environment
)
{
PropertySource
<?>
environmentPropertySource
=
environment
.
getPropertySources
()
.
get
(
StandardEnvironment
.
SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
);
if
(
environmentPropertySource
!=
null
)
{
if
(
environmentPropertySource
.
containsProperty
(
KUBERNETES_SERVICE_HOST
)
&&
environmentPropertySource
.
containsProperty
(
KUBERNETES_SERVICE_PORT
))
{
return
true
;
}
if
(
environmentPropertySource
instanceof
EnumerablePropertySource
)
{
return
isActive
((
EnumerablePropertySource
<?>)
environmentPropertySource
);
}
}
return
false
;
}
...
...
@@ -119,11 +117,6 @@ public enum CloudPlatform {
return
false
;
}
private
boolean
isSecretLocationExists
()
{
File
file
=
new
File
(
SECRET_LOCATION
);
return
file
.
exists
()
&&
file
.
isDirectory
();
}
};
/**
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/cloud/CloudPlatformTests.java
View file @
ddb5cc3f
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -16,7 +16,6 @@
package
org
.
springframework
.
boot
.
cloud
;
import
java.io.File
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -85,28 +84,38 @@ class CloudPlatformTests {
}
@Test
void
getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
source
.
put
(
"EXAMPLE_SERVICE_HOST"
,
"---"
);
source
.
put
(
"EXAMPLE_SERVICE_PORT"
,
"8080"
);
PropertySource
<?>
propertySource
=
new
SystemEnvironmentPropertySource
(
StandardEnvironment
.
SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
,
source
);
environment
.
getPropertySources
().
addFirst
(
propertySource
);
void
getActiveWhenHasKubernetesServiceHostAndPortShouldReturnKubernetes
()
{
Map
<
String
,
Object
>
envVars
=
new
HashMap
<>();
envVars
.
put
(
"KUBERNETES_SERVICE_HOST"
,
"---"
);
envVars
.
put
(
"KUBERNETES_SERVICE_PORT"
,
"8080"
);
Environment
environment
=
getEnvironmentWithEnvVariables
(
envVars
);
CloudPlatform
platform
=
CloudPlatform
.
getActive
(
environment
);
assertThat
(
platform
).
isEqualTo
(
CloudPlatform
.
KUBERNETES
);
assertThat
(
platform
.
isActive
(
environment
)).
isTrue
();
}
@Test
void
getActiveWhenHasKubernetesHostAndPortShouldReturnKubernetes
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
source
.
put
(
"KUBERNETES_SERVICE_HOST"
,
"---"
);
source
.
put
(
"KUBERNETES_SERVICE_PORT"
,
"8080"
);
PropertySource
<?>
propertySource
=
new
SystemEnvironmentPropertySource
(
StandardEnvironment
.
SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
,
source
);
environment
.
getPropertySources
().
addFirst
(
propertySource
);
void
getActiveWhenHasKubernetesServiceHostAndNoKubernetesServicePortShouldNotReturnKubernetes
()
{
Environment
environment
=
getEnvironmentWithEnvVariables
(
Collections
.
singletonMap
(
"KUBERNETES_SERVICE_HOST"
,
"---"
));
CloudPlatform
platform
=
CloudPlatform
.
getActive
(
environment
);
assertThat
(
platform
).
isNull
();
}
@Test
void
getActiveWhenHasKubernetesServicePortAndNoKubernetesServiceHostShouldNotReturnKubernetes
()
{
Environment
environment
=
getEnvironmentWithEnvVariables
(
Collections
.
singletonMap
(
"KUBERNETES_SERVICE_PORT"
,
"8080"
));
CloudPlatform
platform
=
CloudPlatform
.
getActive
(
environment
);
assertThat
(
platform
).
isNull
();
}
@Test
void
getActiveWhenHasServiceHostAndServicePortShouldReturnKubernetes
()
{
Map
<
String
,
Object
>
envVars
=
new
HashMap
<>();
envVars
.
put
(
"EXAMPLE_SERVICE_HOST"
,
"---"
);
envVars
.
put
(
"EXAMPLE_SERVICE_PORT"
,
"8080"
);
Environment
environment
=
getEnvironmentWithEnvVariables
(
envVars
);
CloudPlatform
platform
=
CloudPlatform
.
getActive
(
environment
);
assertThat
(
platform
).
isEqualTo
(
CloudPlatform
.
KUBERNETES
);
assertThat
(
platform
.
isActive
(
environment
)).
isTrue
();
...
...
@@ -114,16 +123,18 @@ class CloudPlatformTests {
@Test
void
getActiveWhenHasServiceHostAndNoServicePortShouldNotReturnKubernetes
()
{
MockEnvironment
environment
=
new
MockEnvironment
();
PropertySource
<?>
propertySource
=
new
SystemEnvironmentPropertySource
(
StandardEnvironment
.
SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
,
Environment
environment
=
getEnvironmentWithEnvVariables
(
Collections
.
singletonMap
(
"EXAMPLE_SERVICE_HOST"
,
"---"
));
environment
.
getPropertySources
().
addFirst
(
propertySource
);
CloudPlatform
platform
=
CloudPlatform
.
getActive
(
environment
);
File
path
=
new
File
(
"/var/run/secrets/kubernetes.io"
);
if
(!
path
.
exists
()
&&
!
path
.
isDirectory
())
{
assertThat
(
platform
).
isNull
();
}
private
Environment
getEnvironmentWithEnvVariables
(
Map
<
String
,
Object
>
environmentVariables
)
{
MockEnvironment
environment
=
new
MockEnvironment
();
PropertySource
<?>
propertySource
=
new
SystemEnvironmentPropertySource
(
StandardEnvironment
.
SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME
,
environmentVariables
);
environment
.
getPropertySources
().
addFirst
(
propertySource
);
return
environment
;
}
}
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