Skip to content

Commit

Permalink
Merge pull request #15 from kcl-lang/extra-resource-example
Browse files Browse the repository at this point in the history
feat: add extra resource examples
  • Loading branch information
Peefy authored Jul 16, 2024
2 parents 6a31a7c + b294fea commit c43bd76
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/appops/extra-resources-example/base/base.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import konfig.models.kube.frontend
import konfig.models.kube.frontend.service
import konfig.models.kube.frontend.container

# Application Configuration
appConfiguration: frontend.Server {
# Main Container Configuration
mainContainer = container.Main {
ports = [
{containerPort = 80}
]
env.MY_ENV: {
value = "MY_VALUE"
}
}
image = "nginx:1.7.8"
services = [
service.Service {
name = "nginx"
type = "NodePort"
ports = [
{
nodePort = 30201
port = 80
targetPort = 80
}
]
}
]
}
10 changes: 10 additions & 0 deletions examples/appops/extra-resources-example/dev/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "nginx-example-dev"
version = "0.0.1"

[dependencies]
konfig = { path = "../../../../../konfig" }
k8s = "1.28"
[profile]
entries = ["../base/base.k", "main.k", "${konfig:KCL_MOD}/models/kube/render/render.k"]

13 changes: 13 additions & 0 deletions examples/appops/extra-resources-example/dev/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[dependencies]
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.28"
version = "1.28"
sum = "aTxPUVZyr9MdiB3YdiY/8pCh9sC55yURnZdGlJsKG6Q="
reg = "ghcr.io"
repo = "kcl-lang/k8s"
oci_tag = "1.28"
[dependencies.konfig]
name = "konfig"
full_name = "konfig_0.4.0"
version = "0.4.0"
25 changes: 25 additions & 0 deletions examples/appops/extra-resources-example/dev/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import konfig.models.kube.frontend
import konfig.models.kube.templates.resource as res_tpl

mw = {
apiVersion = "traefik.containo.us/v1alpha1"
kind = "Middleware"
metadata = {
name = option("app")
namespace = option("app")
labels = {app = option("app")}
}
spec = {
basicAuth = {
secret = option("app")
removeHeader = True
}
}
}

# The application configuration in stack will overwrite
# the configuration with the same attribute in base.
appConfiguration: frontend.Server {
schedulingStrategy.resource = res_tpl.tiny
extraResources.mw = mw
}
5 changes: 5 additions & 0 deletions models/kube/frontend/server.k
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ schema Server:
Ingresses is a list of Ingress which is collection of rules that allow inbound connections to reach the endpoints defined by a backend.
serviceAccount: sa.ServiceAccount, default is Undefined, optional.
ServiceAccount is used to run this pod.
extraResources: {str:{str:}}, default is Undefined, optional.
Extra resources used to output the final result.

Examples
--------
Expand Down Expand Up @@ -138,3 +140,6 @@ schema Server:
# app cloud resource
storage?: storage.ObjectStorage
database?: storage.DataBase

# extra resources
extraResources?: {str: {str:}}
3 changes: 3 additions & 0 deletions models/kube/render/render.k
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ remove_duplicated_iter = lambda data: [], func {

__renderServerFrontendInstances__ = [inst for inst in frontend.Server.instances() if typeof(inst) == "Server"]
__renderServerBackendInstances__ = [backend.ServerBackend(inst) for inst in __renderServerFrontendInstances__]
__serverExtraResources: [{str:}] = [v for s in __renderServerFrontendInstances__ for k, v in s.extraResources]

__renderJobFrontendInstances__ = [inst for inst in frontend.Job.instances() if typeof(inst) == "Job"]
__renderJobBackendInstances__ = [backend.JobBackend(inst) for inst in __renderJobFrontendInstances__]
Expand Down Expand Up @@ -83,5 +84,7 @@ getId = lambda _resource {
_output = {
_kind: [_resource for _resource in _res] \
for _kind, _res in __resource_map___
} | {
__extra: __serverExtraResources
}
manifestsResourceMap(_output)

0 comments on commit c43bd76

Please sign in to comment.