Skip to content

Commit

Permalink
Merge pull request #43 from zong-zhe/support-import-path
Browse files Browse the repository at this point in the history
feat: support import in krm-function
  • Loading branch information
Peefy authored Mar 19, 2024
2 parents 25f2c08 + 1926ca4 commit c0a26c9
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 1 deletion.
46 changes: 46 additions & 0 deletions examples/abstraction/web-service-with-import/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import schemas as s

# Convert the `App` model into Kubernetes Deployment and Service Manifests
kubernetesRender = lambda a: s.App {
# Construct the deployment manifest.
deployment = {
apiVersion = "apps/v1"
kind = "Deployment"
metadata.name = a.name
metadata.labels = a.labels
spec = {
replicas = a.replicas
selector.matchLabels = a.labels
template.metadata.labels = a.labels
template.spec.containers = [
{
name = name
image = c.image
command = c.command
args = c.args
env = c.env
volumeMounts = c.volumes
resources: c.resources
ports = c.ports
} for name, c in a.containers
]
}
}
# Construct the service manifest.
service = {
apiVersion = "v1"
kind = "Service"
metadata.name = a.name
metadata.labels = a.labels
spec = {
type = a.service?.$type
selector = a.labels
ports = a.service?.ports
}
}
# Returns Kubernetes manifests
[deployment, if a.service: service]
}

params: s.App = option("params")
items = kubernetesRender(params)
51 changes: 51 additions & 0 deletions examples/abstraction/web-service-with-import/schemas.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
schema App:
"""The application model."""
name: str
replicas: int = 1
labels?: {str:str} = {app = name}
service?: Service
containers?: {str: Container}

schema Service:
"""The service model."""
$type?: str
ports: [Port]

schema Port:
"""The port model."""
port: int
protocol: "TCP" | "UDP" | "SCTP" = "TCP"
targetPort?: int | str

schema Container:
"""The container model."""
image: str
command?: [str]
args?: [str]
env?: [Env]
volumes?: [Volume]
resources?: Resource
ports: [ContainerPort]

schema ContainerPort:
"""The container port model."""
name?: str
protocol: "TCP" | "UDP" | "SCTP" = "TCP"
containerPort: int

check:
1 <= containerPort <= 65535, "containerPort must be between 1 and 65535, inclusive"

schema Env:
name: str
value: str

schema Volume:
source: str
path: str
target: str
readOnly?: bool = False

schema Resource:
limits?: {str:}
requests?: {str:}
20 changes: 20 additions & 0 deletions examples/abstraction/web-service-with-import/suite/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: config.kubernetes.io/v1
kind: ResourceList
items:
functionConfig:
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLRun
spec:
params:
name: app
containers:
nginx:
image: nginx
ports:
- containerPort: 80
service:
ports:
- port: 80
labels:
name: app
source: oci://ghcr.io/kcl-lang/web-service
23 changes: 23 additions & 0 deletions examples/abstraction/web-service-with-import/suite/good.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLRun
metadata:
name: web-service
annotations:
krm.kcl.dev/version: 0.0.1
krm.kcl.dev/type: abstraction
documentation: >-
Web service application abstraction
spec:
params:
name: app
containers:
nginx:
image: nginx
ports:
- containerPort: 80
service:
ports:
- port: 80
labels:
name: app
source: ./examples/abstraction/web-service-with-import/main.k
4 changes: 3 additions & 1 deletion pkg/source/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ func LocaleSource(src string) (string, error) {
if IsOCI(src) {
// Read code from a OCI source.
return ReadFromOCISource(src)
} else if IsLocal(src) || IsRemoteUrl(src) || IsGit(src) || IsVCSDomain(src) {
} else if IsLocal(src) {
return ReadFromLocalSource(src)
} else if IsRemoteUrl(src) || IsGit(src) || IsVCSDomain(src) {
// Read code from local path or a remote url.
return ReadThroughGetter(src)
} else {
Expand Down
25 changes: 25 additions & 0 deletions pkg/source/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package source
import (
"path/filepath"
"strings"

"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/runner"
)

// IsLocal determines whether or not a source is to be treated as a local path.
Expand All @@ -12,3 +15,25 @@ func IsLocal(src string) bool {
}
return false
}

// ReadFromLocalSource reads source code from a local file system path source.
//
// Parameters:
// - src: a local file system path.
//
// Return:
// A string containing the source code, and an error if any.
func ReadFromLocalSource(src string) (string, error) {
// Find the mod root

modpath, kpmerr := runner.FindModRootFrom(src)
if kpmerr != nil {
if kpmerr.Type() != reporter.KclModNotFound {
return "", kpmerr
} else {
modpath = filepath.Dir(src)
}
}

return GetSourceFromDir(modpath)
}

0 comments on commit c0a26c9

Please sign in to comment.