Task: Add dockerfile and kube

This commit is contained in:
Sambo Chea 2021-12-21 11:55:35 +07:00
parent c72ee8968f
commit 2779aef185
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
15 changed files with 198 additions and 34 deletions

10
.dockerignore Normal file
View File

@ -0,0 +1,10 @@
.gradle/
.github/
.idea/
.m2/
.settings
.settings.xml
.settings.gradle
apps/
.k8s/
README.md

View File

@ -1,3 +1,13 @@
FROM cubetiq/openjdk:11u-ubuntu as builder
LABEL maintainer="sombochea@cubetiqs.com"
WORKDIR /app
RUN apt-get update && apt-get install -y git
COPY . .
RUN sh gradlew clean bootJar
FROM cubetiq/calpine-openjdk11:latest
LABEL maintainer="sombochea@cubetiqs.com"
@ -8,7 +18,7 @@ WORKDIR /opt/cubetiq
VOLUME ["/opt/cubetiq/data"]
# Copy build source to container
COPY api/build/libs/*.jar ./api.jar
COPY --from=builder /app/api/build/libs/*.jar ./api.jar
ENV APP_DATA_DIR "/opt/cubetiq/data"

View File

@ -32,6 +32,8 @@ springBoot {
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
// Migrating from SpringFox
implementation("org.springdoc:springdoc-openapi-ui:1.5.13")

View File

@ -1,8 +1,10 @@
package com.cubetiqs.web.config
import com.cubetiqs.web.property.AppProperties
import io.swagger.v3.oas.annotations.OpenAPIDefinition
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType
import io.swagger.v3.oas.annotations.security.SecurityScheme
import io.swagger.v3.oas.annotations.servers.Server
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.info.Info
import io.swagger.v3.oas.models.info.License
@ -18,11 +20,15 @@ import org.springframework.context.annotation.Configuration
scheme = "bearer",
bearerFormat = "JWT",
)
@OpenAPIDefinition(
servers = [
Server(url = "/")
],
)
class OpenApiDocConfig @Autowired constructor(
val appProperties: AppProperties,
) {
companion object {
private val ADMIN_API_PATH get() = "/admin/**"
private val DEFAULT_API_PATH get() = "/**"
}
@ -31,19 +37,10 @@ class OpenApiDocConfig @Autowired constructor(
return GroupedOpenApi.builder()
.group("default")
.pathsToMatch(DEFAULT_API_PATH)
.pathsToExclude("/error", ADMIN_API_PATH)
.packagesToScan("com.cubetiqs.web")
.build()
}
@Bean
fun adminApi(): GroupedOpenApi {
return GroupedOpenApi.builder()
.group("admin")
.pathsToMatch(ADMIN_API_PATH)
.build()
}
@Bean
fun cubetiqOpenAPI(): OpenAPI {
return OpenAPI()

View File

@ -0,0 +1,29 @@
package com.cubetiqs.web.controller
import com.cubetiqs.web.infrastructure.data.Customer
import com.cubetiqs.web.infrastructure.repository.CustomerRepository
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
@Tag(name = "Customer")
@RestController
@RequestMapping("/customers")
class CustomerController @Autowired constructor(
private val customerRepository: CustomerRepository,
) {
@GetMapping
fun getAll(): List<Customer> = customerRepository.findAll()
@GetMapping("/{id}")
fun getById(@PathVariable id: String): Customer = customerRepository.findById(id).orElse(null)
@PostMapping
fun create(@RequestBody customer: Customer) = customerRepository.save(customer)
@DeleteMapping("/{id}")
fun delete(@PathVariable id: String) = customerRepository.deleteById(id)
@PutMapping("/{id}")
fun update(@PathVariable id: String, @RequestBody customer: Customer) = customerRepository.save(customer)
}

View File

@ -12,6 +12,7 @@ import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.net.InetAddress
@Tag(name = "Miscellaneous")
@RestController
@ -26,14 +27,16 @@ class IndexController @Autowired constructor(
ApiInfoAuthorResponse(name = "Sambo Chea", email = "sombochea@cubetiqs.com"),
ApiInfoAuthorResponse(name = "CUBETIQ OSS", email = "oss@cubetiqs.com"),
)
val hostname = InetAddress.getLocalHost().hostName
val response = ApiInfoResponse(
info = "API Operation is running normally on ${environment.activeProfiles.joinToString(separator = ",")}",
name = buildProperties.name,
name = environment.getProperty("spring.application.name") ?: buildProperties.name,
service = buildProperties.artifact,
version = buildProperties.version,
date = buildProperties.time.toString(),
commit = buildProperties["commitId"],
authors = authors,
hostname = hostname,
)
return response(response)
}

View File

@ -1,20 +0,0 @@
package com.cubetiqs.web.controller.admin
import com.cubetiqs.web.annotation.ApiBearerAuth
import com.cubetiqs.web.controller.BaseController
import com.cubetiqs.web.util.RouteConstants
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
@Tag(name = "Admin")
@RestController
@RequestMapping(RouteConstants.ADMIN)
class AdminController : BaseController {
@ApiBearerAuth
@GetMapping
fun getAdmin(): String {
return "Admin"
}
}

View File

@ -0,0 +1,14 @@
package com.cubetiqs.web.infrastructure.data
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
@Document(collection = "customer")
data class Customer(
@Id
var id: String? = null,
var name: String? = null,
var email: String? = null,
var phone: String? = null,
var address: String? = null,
)

View File

@ -0,0 +1,8 @@
package com.cubetiqs.web.infrastructure.repository
import com.cubetiqs.web.infrastructure.data.Customer
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository
@Repository
interface CustomerRepository : MongoRepository<Customer, String>

View File

@ -1,7 +1,9 @@
package com.cubetiqs.web.model.response
import com.fasterxml.jackson.annotation.JsonInclude
import io.swagger.v3.oas.annotations.media.Schema
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(name = "ApiInfoResponse", description = "ApiInfoResponse")
data class ApiInfoResponse(
val name: String,
@ -10,6 +12,7 @@ data class ApiInfoResponse(
val version: String,
val date: String,
val commit: String,
val hostname: String? = null,
val authors: Collection<ApiInfoAuthorResponse> = listOf(),
) : BaseRequestModel

View File

@ -2,8 +2,10 @@ spring:
profiles:
active: ${APP_PROFILE:dev}
application:
name: cubetiq-api-service
name: ${APP_NAME:cubetiq-api-service}
data:
mongodb:
uri: ${MONGODB_URI:mongodb://192.168.0.202:27017/spring-web-api-db}
cubetiq:
app:
data-dir: ${APP_DATA_DIR:${user.home}/${spring.application.name}}

6
k8s/1-namespace.yaml Normal file
View File

@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
labels:
app: spring-dev
name: spring-dev

9
k8s/2-secret.yaml Normal file
View File

@ -0,0 +1,9 @@
apiVersion: v1
kind: Secret
metadata:
namespace: spring-dev
name: registry-secret
stringData:
.dockerconfigjson: |
{"auths":{"registry.kh.cubetiqs.com":{"auth":"c29tYm9jaGVhOm1z"}}}
type: kubernetes.io/dockerconfigjson

50
k8s/3-mongo.yaml Normal file
View File

@ -0,0 +1,50 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
namespace: spring-dev
name: mongo-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 256Mi
---
apiVersion: v1
kind: Service
metadata:
namespace: spring-dev
name: mongo
spec:
selector:
app: mongo
ports:
- port: 27017
targetPort: 27017
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: spring-dev
name: mongo
spec:
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- name: mongo
image: mongo
ports:
- containerPort: 27017
volumeMounts:
- name: storage
mountPath: /data/db
volumes:
- name: storage
persistentVolumeClaim:
claimName: mongo-pvc

41
k8s/app.yaml Normal file
View File

@ -0,0 +1,41 @@
apiVersion: v1
kind: Service
metadata:
namespace: spring-dev
name: spring-web-service
spec:
selector:
app: spring-web-api
ports:
- port: 8080
targetPort: 8080
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: spring-dev
name: spring-web-api
spec:
replicas: 1
selector:
matchLabels:
app: spring-web-api
template:
metadata:
labels:
app: spring-web-api
spec:
imagePullSecrets:
- name: registry-secret
containers:
- name: spring-web-app
image: registry.kh.cubetiqs.com/spring-web-api:latest
ports:
- containerPort: 8080
env:
- name: MONGODB_URI
value: mongodb://mongo:27017/spring-web-api
- name: APP_NAME
value: spring-web-api-hello
imagePullPolicy: Always