Skip to content

Commit

Permalink
Merge pull request #17 from Peefy/feat-default-settings-file
Browse files Browse the repository at this point in the history
feat: support default settings file kcl.yaml loading
  • Loading branch information
Peefy authored Nov 27, 2023
2 parents 4975f0d + d0da38a commit e90ae43
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/konfig/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run:
cd app && kcl run && cd ..

test:
make run
7 changes: 7 additions & 0 deletions examples/konfig/app/kcl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kcl_cli_configs:
file:
- main.k
- ../pkg/render/render.k
kcl_options:
- key: env
value: prod
19 changes: 19 additions & 0 deletions examples/konfig/app/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pkg.app as app_pkg

app: app_pkg.App {
name: "app"
domainType = "Standard"
containerPort = 80
if option("env") == "prod":
volumes = [
{
mountPath = "/tmp"
}
]
services = [
{
clusterIP = "None"
$type = "ClusterIP"
}
]
}
3 changes: 3 additions & 0 deletions examples/konfig/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "konfig"

Empty file added examples/konfig/kcl.mod.lock
Empty file.
23 changes: 23 additions & 0 deletions examples/konfig/pkg/app/app_module.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
schema App:
name: str
domainType: "Standard" | "Customized" | "Global"
containerPort: int
volumes?: [Volume]
services: [Service]

check:
1 <= containerPort <= 65535

schema Service:
clusterIP: str
$type: str

check:
clusterIP == "None" if $type == "ClusterIP"

schema Volume:
container: str = "*" # The default value of `container` is "*"
mountPath: str

check:
mountPath not in ["/", "/boot", "/home", "dev", "/etc", "/root"]
26 changes: 26 additions & 0 deletions examples/konfig/pkg/render/render.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pkg.app as app_pkg
import manifests

app: app_pkg.App {
domainType = "Standard"
containerPort = 80
volumes = [
{
mountPath = "/tmp"
}
]
services = [
{
clusterIP = "None"
$type = "ClusterIP"
}
]
}
render = lambda app: app_pkg.App {
{
apiVersion = "v1"
metadata.name = app.name
}
}

manifests.yaml_stream([render(a) for a in app_pkg.App.instances()])
5 changes: 5 additions & 0 deletions examples/server/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run:
kcl server.k

test:
make run
7 changes: 7 additions & 0 deletions examples/server/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "server"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
k8s = "1.28"
9 changes: 9 additions & 0 deletions examples/server/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[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"
108 changes: 108 additions & 0 deletions examples/server/server.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import k8s.api.core.v1
import regex

protocol PVCProtocol:
pvc?: {str:}

mixin PersistenVolumeClaimMixin for PVCProtocol:
"""
PersistentVolumeClaim (PVC) sample:
Link: https://kubernetes.io/docs/concepts/storage/persistent-volumes/#persistentvolumeclaims
"""

# Mix in a new attribute `kubernetesPVC`
kubernetesPVC: v1.PersistentVolumeClaim

if pvc:
kubernetesPVC = v1.PersistentVolumeClaim {
metadata.name = pvc.name
metadata.labels = pvc.labels
spec = {
accessModes = pvc.accessModes
resources = pvc.resources
storageClassName = pvc.storageClassName
}
}

schema Server(Deployment):
mixin [PersistenVolumeClaimMixin]
pvc?: {str:}
""" pvc user interface data defined by PersistentVolumeClaimMixin """

schema Deployment[priority: int]:
name: str
cpu: int = _cpu
memory: int = _cpu * 2
volumes?: [Volume]
image: str
service?: Service
replica: int = 1
command: [str]
labels?: {str: str}

if priority == 1:
_cpu = 256
elif priority == 2:
_cpu = 512
elif priority == 3:
_cpu = 1024
else:
_cpu = 2048

check:
multiplyof(cpu, 256), "CPU must be a multiple of 256"
regex.match(image, "^[a-zA-Z]+:\d+\.\d+\.\d+$"), "image name should be like 'nginx:1.14.2'"
1 <= replica <= 100, "replica must be between 1 and 100"
len(labels) >= 2, "The length of labels should be greater than or equal to 2"
"env" in labels, "'env' must be in labels"
len(command) > 0, "command-list should be non-empty"

schema Port:
name: str
protocol: str
port: int
targetPort: int

check:
port in [80, 443], "port must be 80 or 443"
protocol in ["TCP", "HTTP"], "protocol must be TCP or HTTP"
1024 < targetPort, "targetPort must be greater than 1024"

schema Service:
name: "my-service" = "my-service"
ports: [Port]

check:
len(ports) > 0, "ports-list should be non-empty"

schema Volume:
name: str
mountPath: str
hostPath: str

server = Server {
name = "my-nginx"
image = "nginx:1.14.2"
volumes = [Volume {
name = "mydir"
mountPath = "/test-pd"
hostPath = "/data"
}]
command = ["nginx"]
labels = {
run = "my-nginx"
env = "pre-prod"
}
service.ports = [Port {
name = "http"
protocol = "TCP"
port = 80
targetPort = 9376
}]
pvc = {
name = "my_pvc"
acccessModes = ["ReadWriteOnce"]
resources.requests.storage = "8Gi"
storageClassName = "slow"
}
}
2 changes: 1 addition & 1 deletion examples/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pwd=$(
pwd
)

for path in "configuration" "validation" "abstraction" "definition" "mutation" "data-integration" "automation" "package-management" "kubernetes" "codelab"; do
for path in "configuration" "validation" "abstraction" "definition" "konfig" "mutation" "data-integration" "automation" "package-management" "kubernetes" "codelab" "server"; do
echo "\033[1mTesting $path ...\033[0m"
if (cd $pwd/$path && make test); then
echo "\033[32mTest SUCCESSED - $path\033[0m\n"
Expand Down
8 changes: 8 additions & 0 deletions pkg/fs/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ func IsDir(path string) bool {
}
return fileInfo.IsDir()
}

func FileExists(path string) bool {
fi, err := os.Lstat(path)
if err != nil || fi.IsDir() {
return false
}
return true
}
8 changes: 8 additions & 0 deletions pkg/options/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"kcl-lang.io/kpm/pkg/runner"
)

const (
DefaultSettingsFile = "kcl.yaml"
)

// RunOptions is a struct that holds the options for the run command.
type RunOptions struct {
// Entries is the list of the kcl code entry including filepath, folder, OCI package, etc.
Expand Down Expand Up @@ -214,6 +218,10 @@ func CompileOptionFromCli(o *RunOptions) *opt.CompileOptions {
opts.Merge(kcl.WithSettings(sPath))
}
opts.SetHasSettingsYaml(true)
} else if fs.FileExists(DefaultSettingsFile) {
// If exists default kcl.yaml, load it.
opts.Merge(kcl.WithSettings(DefaultSettingsFile))
opts.SetHasSettingsYaml(true)
}

// --argument, -D
Expand Down

0 comments on commit e90ae43

Please sign in to comment.