Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: publish more modules #89

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion argo-application-field-validation/kcl.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "argo-application-field-validation"
version = "0.1.1"
version = "0.1.2"
description = "`argo-application-field-validation` is a kcl validation module, which can be used to perform some validation on Argo `Application` fields."

Empty file.
23 changes: 1 addition & 22 deletions argo-application-field-validation/main.k
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
import yaml

# Define the validation function
validate = lambda item {
if item.kind == "Application" and item.apiVersion.startswith("argoproj.io"):
path = item?.spec?.source?.path
chart = item?.spec?.source?.chart
server = item?.destination?.server
server = item?.spec.destination?.server
name = item?.spec?.destination?.name
assert (path or chart) and not (path and chart), "`spec.source.path` OR `spec.source.chart` should be specified but never both."
assert (server or name) and not (server and name), "`spec.destination.server` OR `spec.destination.name` should be specified but never both."
item
}
# Validate All resource
items = [validate(i) for i in option("items") or []]

if option("__test__"):
validate(yaml.decode("""\
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: badapp01
namespace: default
spec:
project: foo
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
# chart: foo
destination:
server: https://kubernetes.default.svc
namespace: guestbook
"""))
21 changes: 21 additions & 0 deletions argo-application-field-validation/main_test.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import yaml

test_validate = lambda {
validate(yaml.decode("""\
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: badapp01
namespace: default
spec:
project: foo
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
# chart: foo
destination:
server: https://kubernetes.default.svc
namespace: guestbook
"""))
}
3 changes: 1 addition & 2 deletions argo-application-prevent-default-project/kcl.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[package]
name = "argo-application-prevent-default-project"
edition = "0.0.1"
version = "0.0.1"
version = "0.0.2"

Empty file.
20 changes: 20 additions & 0 deletions argo-application-prevent-default-project/main_test.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import yaml

test_validate = lambda {
validate(yaml.decode("""\
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: goodapp
namespace: default
spec:
project: biz
source:
repoURL: https://github.com/argoproj/argocd-example-apps.git
targetRevision: HEAD
path: guestbook
destination:
server: https://kubernetes.default.svc
namespace: guestbook
"""))
}
7 changes: 7 additions & 0 deletions result/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Introduction

`result` is a KCL error handling library

## Resource

The Code source and documents are [here](https://github.com/kcl-lang/modules/tree/main/result)
4 changes: 4 additions & 0 deletions result/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "result"
version = "0.0.1"
description = "`result` is a KCL error handling library"
14 changes: 14 additions & 0 deletions result/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema Result:
result: bool
error?: str

is_ok: () -> bool = lambda {
result
}

is_error: () -> bool = lambda {
not result
}

check:
error, if not result, "error message should not be empty when the result is False"
7 changes: 7 additions & 0 deletions scale-deployment-zero/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Introduction

`scale-deployment-zero` is a KCL mutation package

## Resource

The Code source and document are [here](https://github.com/kcl-lang/modules/tree/main/scale-deployment-zero)
5 changes: 5 additions & 0 deletions scale-deployment-zero/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "scale-deployment-zero"
edition = "*"
version = "0.1.0"
description = "`scale-deployment-zero` is a KCL mutation package"
7 changes: 7 additions & 0 deletions scale-deployment-zero/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
annotations: {str:str} = option("params")?.annotations or {}

items = [item | {
if item.kind == "Deployment":
spec.replicas = 0
metadata.annotations: annotations # Scale to zero and offer a useful annotation information
} for item in option("items")]
7 changes: 7 additions & 0 deletions spread-pods-across-topology/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Introduction

`spread-pods-across-topology` is a KCL mutation package

## Resource

The Code source and document are [here](https://github.com/kcl-lang/modules/tree/main/spread-pods-across-topology)
5 changes: 5 additions & 0 deletions spread-pods-across-topology/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "spread-pods-across-topology"
edition = "*"
version = "0.1.0"
description = "`spread-pods-across-topology` is a KCL mutation package"
10 changes: 10 additions & 0 deletions spread-pods-across-topology/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
annotations: {str:str} = option("params")?.annotations or {}
items = [item | {
if item.kind == "Deployment":
spec.template.spec: {topologySpreadConstraints += [{
maxSkew: 1
topologyKey: "zone"
whenUnsatisfiable: "DoNotSchedule"
labelSelector.matchLabels.distributed: "required"
}]}
} for item in option("items")]
5 changes: 5 additions & 0 deletions topology-spread-constraints/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Introduction

## Resource

The Code source and documents are [here](https://github.com/kcl-lang/modules/tree/main/topology-spread-constraints)
5 changes: 5 additions & 0 deletions topology-spread-constraints/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "topology-spread-constraints"
version = "0.1.0"
description = "`topology-spread-constraints` is a kcl validation package"

Empty file.
17 changes: 17 additions & 0 deletions topology-spread-constraints/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
KINDS = ["Deployment", "StatefulSet"]

to_str_set = lambda items: [str] {
[item for item in {item = None for item in items}]
}

# Define the validation function
validate = lambda item: {str:} {
if item.kind in KINDS and item.spec.replicas >= 3:
contraints = item.spec.template.spec.topologySpreadConstraints
valid_contraints = to_str_set([c.topologyKey for c in contraints if c?.topologyKey in ["kubernetes.io/hostname", "topology.kubernetes.io/zone"]])
assert len(valid_contraints) == 2, "topologySpreadConstraint for kubernetes.io/hostname & topology.kubernetes.io/zone are required, got ${valid_contraints}"
item
}

# Validate All resource
items = [validate(i) for i in option("items") or []]
59 changes: 59 additions & 0 deletions topology-spread-constraints/main_test.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import yaml

test_validate = lambda {
good_manifest = """\
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: pass
namespace: monitoring
labels:
app: thanos-memcached
spec:
selector:
matchLabels:
app: thanos-memcached
serviceName: memcached
replicas: 3
updateStrategy:
type: RollingUpdate
template:
metadata:
labels:
app: thanos-memcached
spec:
containers:
- name: memcached
image: memcached:1.6.17-alpine
command:
- memcached
- -m 2048
- -o
- modern
- -v
ports:
- name: tcp-memcached
containerPort: 11211
topologySpreadConstraints:
- maxSkew: 1
topologyKey: foo.bar/test
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: thanos-memcached
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: thanos-memcached
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: thanos-memcached
"""
item = yaml.decode(good_manifest)
validate(item)
}
5 changes: 5 additions & 0 deletions unique-ingress-host-and-path/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Introduction

## Resource

The Code source and documents are [here](https://github.com/kcl-lang/modules/tree/main/unique-ingress-host-and-path)
5 changes: 5 additions & 0 deletions unique-ingress-host-and-path/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "unique-ingress-host-and-path"
version = "0.1.0"
description = "`unique-ingress-host-and-path` is a kcl validation package"

Empty file.
30 changes: 30 additions & 0 deletions unique-ingress-host-and-path/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
KINDS = ["Ingress"]

schema Result:
result: bool
error?: str

check:
error if not result, "error message should not be empty when the result is False"

validate = lambda item: {str:} -> Result {
result = Result {result = True}

if item.kind in KINDS:
paths: [str] = [p.path for r in item.spec.rules for p in r.http.paths]
if not isunique(paths):
result = Result {
result = False
error = "Ingress paths must be unique, got ${paths}"
}
result

}
# Define the validation function
must_validate = lambda item {
result = validate(item)
assert result.result, result.error
item
}
# Validate All resource
items = [must_validate(i) for i in option("items") or []]
91 changes: 91 additions & 0 deletions unique-ingress-host-and-path/main_test.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import yaml

test_validate = lambda {
assert validate(yaml.decode("""\
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: goodingress01
namespace: unique-ingress-paths-ns
spec:
rules:
- host: endpoint01
http:
paths:
- backend:
service:
name: demo-svc
port:
number: 8080
path: /foo/abc/def
pathType: Prefix
- backend:
service:
name: demo-svc
port:
number: 8081
path: /bar/def/bca
pathType: Prefix
- host: endpoint02
http:
paths:
- backend:
service:
name: demo-svc
port:
number: 8080
path: /bar/abc/bca
pathType: Prefix
- backend:
service:
name: demo-svc
port:
number: 8081
path: /foo/fed/def
pathType: Prefix
""")).result == True
assert validate(yaml.decode("""\
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: goodingress01
namespace: unique-ingress-paths-ns
spec:
rules:
- host: endpoint01
http:
paths:
- backend:
service:
name: demo-svc
port:
number: 8080
path: /foo/abc/def
pathType: Prefix
- backend:
service:
name: demo-svc
port:
number: 8081
path: /bar/def/bca
pathType: Prefix
- host: endpoint02
http:
paths:
- backend:
service:
name: demo-svc
port:
number: 8080
path: /bar/abc/bca
pathType: Prefix
- backend:
service:
name: demo-svc
port:
number: 8081
path: /bar/abc/bca # Error: dup paths: /bar/abc/bca
pathType: Prefix
""")).result == False

}