Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
339 additions
and
9 deletions.
There are no files selected for viewing
45 changes: 44 additions & 1 deletion
45
simple-microservices/department-service/k8s/deployment.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
apiVersion: networking.istio.io/v1beta1 | ||
kind: DestinationRule | ||
metadata: | ||
name: department-service-destination | ||
spec: | ||
host: department-service.default.svc.cluster.local | ||
subsets: | ||
- name: v1 | ||
labels: | ||
version: v1 | ||
- name: v2 | ||
labels: | ||
version: v2 | ||
--- | ||
apiVersion: networking.istio.io/v1beta1 | ||
kind: VirtualService | ||
metadata: | ||
name: department-service-route | ||
spec: | ||
hosts: | ||
- department-service.default.svc.cluster.local | ||
http: | ||
- match: | ||
- headers: | ||
X-Version: | ||
exact: v1 | ||
route: | ||
- destination: | ||
host: department-service.default.svc.cluster.local | ||
subset: v1 | ||
- match: | ||
- headers: | ||
X-Version: | ||
exact: v2 | ||
route: | ||
- destination: | ||
host: department-service.default.svc.cluster.local | ||
subset: v2 | ||
- route: | ||
- destination: | ||
host: department-service.default.svc.cluster.local | ||
subset: v1 |
6 changes: 6 additions & 0 deletions
6
...partment-service/src/main/kotlin/pl/piomin/samples/kubernetes/department/DepartmentApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...ain/kotlin/pl/piomin/samples/kubernetes/department/interceptor/RestTemplateInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pl.piomin.samples.kubernetes.department.interceptor | ||
|
||
import org.springframework.http.HttpRequest | ||
import org.springframework.http.client.ClientHttpRequestExecution | ||
import org.springframework.http.client.ClientHttpRequestInterceptor | ||
import org.springframework.http.client.ClientHttpResponse | ||
import org.springframework.stereotype.Component | ||
import pl.piomin.samples.kubernetes.department.service.AppVersion | ||
|
||
@Component | ||
class RestTemplateInterceptor(val appVersion: AppVersion): ClientHttpRequestInterceptor { | ||
|
||
override fun intercept(req: HttpRequest, | ||
body: ByteArray, | ||
execution: ClientHttpRequestExecution): ClientHttpResponse { | ||
req.headers.add("X-Version", appVersion.getVersion()) | ||
return execution.execute(req, body) | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...ent-service/src/main/kotlin/pl/piomin/samples/kubernetes/department/service/AppVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package pl.piomin.samples.kubernetes.department.service | ||
|
||
import org.springframework.stereotype.Service | ||
import java.io.File | ||
|
||
@Service | ||
class AppVersion { | ||
|
||
fun getVersion(): String? { | ||
val file = File("/etc/podinfo/labels") | ||
return if(file.exists()) { | ||
file.readLines().first{ it.startsWith("version=")} | ||
.split("=")[1] | ||
} else null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
apiVersion: networking.istio.io/v1beta1 | ||
kind: DestinationRule | ||
metadata: | ||
name: employee-service-destination | ||
spec: | ||
host: employee-service.default.svc.cluster.local | ||
subsets: | ||
- name: v1 | ||
labels: | ||
version: v1 | ||
- name: v2 | ||
labels: | ||
version: v2 | ||
--- | ||
apiVersion: networking.istio.io/v1beta1 | ||
kind: VirtualService | ||
metadata: | ||
name: employee-service-route | ||
spec: | ||
hosts: | ||
- employee-service.default.svc.cluster.local | ||
http: | ||
- match: | ||
- headers: | ||
X-Version: | ||
exact: v1 | ||
route: | ||
- destination: | ||
host: employee-service.default.svc.cluster.local | ||
subset: v1 | ||
- match: | ||
- headers: | ||
X-Version: | ||
exact: v2 | ||
route: | ||
- destination: | ||
host: employee-service.default.svc.cluster.local | ||
subset: v2 | ||
- route: | ||
- destination: | ||
host: employee-service.default.svc.cluster.local | ||
subset: v1 |
35 changes: 28 additions & 7 deletions
35
...ce/src/main/kotlin/pl/piomin/samples/kubernetes/employee/controller/EmployeeController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,49 @@ | ||
package pl.piomin.samples.kubernetes.employee.controller | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.web.bind.annotation.* | ||
import pl.piomin.samples.kubernetes.employee.domain.Employee | ||
import pl.piomin.samples.kubernetes.employee.domain.dto.AbstractEmployeeDTO | ||
import pl.piomin.samples.kubernetes.employee.domain.dto.EmployeeV1DTO | ||
import pl.piomin.samples.kubernetes.employee.domain.dto.EmployeeV2DTO | ||
import pl.piomin.samples.kubernetes.employee.repository.EmployeeRepository | ||
import pl.piomin.samples.kubernetes.employee.service.AppVersion | ||
import java.util.* | ||
|
||
@RestController | ||
@RequestMapping("/employees") | ||
class EmployeeController(val repository: EmployeeRepository) { | ||
class EmployeeController(val repository: EmployeeRepository, | ||
val appVersion: AppVersion) { | ||
|
||
val logger: Logger = LoggerFactory.getLogger(EmployeeController::class.java) | ||
|
||
@PostMapping | ||
fun add(@RequestBody employee: Employee): Employee = repository.save(employee) | ||
|
||
@GetMapping("/{id}") | ||
fun findById(@PathVariable id: Int): Optional<Employee> = repository.findById(id) | ||
fun findById(@PathVariable id: Int): Optional<AbstractEmployeeDTO> = repository.findById(id) | ||
.map { mapEmployee(it) } | ||
|
||
@GetMapping | ||
fun findAll(): Iterable<Employee> = repository.findAll() | ||
fun findAll(): Iterable<AbstractEmployeeDTO> = repository.findAll().map { mapEmployee(it) } | ||
|
||
@GetMapping("/organization/{organizationId}") | ||
fun findByOrganizationId(@PathVariable organizationId: Int): Set<Employee> = | ||
repository.findByOrganizationId(organizationId) | ||
fun findByOrganizationId(@PathVariable organizationId: Int): List<AbstractEmployeeDTO> = | ||
repository.findByOrganizationId(organizationId).map { mapEmployee(it) } | ||
|
||
@GetMapping("/department/{departmentId}") | ||
fun findByDepartmentId(@PathVariable departmentId: Int): Set<Employee> = | ||
repository.findByDepartmentId(departmentId) | ||
fun findByDepartmentId(@PathVariable departmentId: Int): List<AbstractEmployeeDTO> = | ||
repository.findByDepartmentId(departmentId).map { mapEmployee(it) } | ||
|
||
fun mapEmployee(employee: Employee): AbstractEmployeeDTO { | ||
logger.info("Version: {}", appVersion.getVersion()) | ||
return if (appVersion.getVersion()!!.contains("v1")) | ||
EmployeeV1DTO(employee.id, employee.firstName, employee.lastName, employee.position, | ||
employee.organizationId, employee.departmentId) | ||
else | ||
EmployeeV2DTO(employee.id, "${employee.firstName} ${employee.lastName}", employee.position, | ||
employee.organizationId, employee.departmentId) | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
...e/src/main/kotlin/pl/piomin/samples/kubernetes/employee/domain/dto/AbstractEmployeeDTO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package pl.piomin.samples.kubernetes.employee.domain.dto | ||
|
||
abstract class AbstractEmployeeDTO(var id: Int? = null, | ||
val position: String = "", | ||
val organizationId: Int = 0, | ||
val departmentId: Int = 0) |
9 changes: 9 additions & 0 deletions
9
...service/src/main/kotlin/pl/piomin/samples/kubernetes/employee/domain/dto/EmployeeV1DTO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package pl.piomin.samples.kubernetes.employee.domain.dto | ||
|
||
class EmployeeV1DTO(id: Int? = null, | ||
val firstName: String = "", | ||
val lastName: String = "", | ||
position: String = "", | ||
organizationId: Int = 0, | ||
departmentId: Int = 0): | ||
AbstractEmployeeDTO(id, position, organizationId, departmentId) |
8 changes: 8 additions & 0 deletions
8
...service/src/main/kotlin/pl/piomin/samples/kubernetes/employee/domain/dto/EmployeeV2DTO.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package pl.piomin.samples.kubernetes.employee.domain.dto | ||
|
||
class EmployeeV2DTO(id: Int? = null, | ||
val name: String = "", | ||
position: String = "", | ||
organizationId: Int = 0, | ||
departmentId: Int = 0): | ||
AbstractEmployeeDTO(id, position, organizationId, departmentId) |
16 changes: 16 additions & 0 deletions
16
...loyee-service/src/main/kotlin/pl/piomin/samples/kubernetes/employee/service/AppVersion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package pl.piomin.samples.kubernetes.employee.service | ||
|
||
import org.springframework.stereotype.Service | ||
import java.io.File | ||
|
||
@Service | ||
class AppVersion { | ||
|
||
fun getVersion(): String? { | ||
val file = File("/etc/podinfo/labels") | ||
return if(file.exists()) { | ||
file.readLines().first{ it.startsWith("version=")} | ||
.split("=")[1] | ||
} else null | ||
} | ||
} |
Oops, something went wrong.