Skip to content

Commit 2880e72

Browse files
committed
feat: publish more modules
Signed-off-by: peefy <[email protected]>
1 parent e6c9d64 commit 2880e72

File tree

26 files changed

+327
-25
lines changed

26 files changed

+327
-25
lines changed
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
22
name = "argo-application-field-validation"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
description = "`argo-application-field-validation` is a kcl validation module, which can be used to perform some validation on Argo `Application` fields."
55

argo-application-field-validation/kcl.mod.lock

Whitespace-only changes.
+1-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
1-
import yaml
2-
31
# Define the validation function
42
validate = lambda item {
53
if item.kind == "Application" and item.apiVersion.startswith("argoproj.io"):
64
path = item?.spec?.source?.path
75
chart = item?.spec?.source?.chart
8-
server = item?.destination?.server
6+
server = item?.spec.destination?.server
97
name = item?.spec?.destination?.name
108
assert (path or chart) and not (path and chart), "`spec.source.path` OR `spec.source.chart` should be specified but never both."
119
assert (server or name) and not (server and name), "`spec.destination.server` OR `spec.destination.name` should be specified but never both."
1210
item
1311
}
1412
# Validate All resource
1513
items = [validate(i) for i in option("items") or []]
16-
17-
if option("__test__"):
18-
validate(yaml.decode("""\
19-
apiVersion: argoproj.io/v1alpha1
20-
kind: Application
21-
metadata:
22-
name: badapp01
23-
namespace: default
24-
spec:
25-
project: foo
26-
source:
27-
repoURL: https://github.com/argoproj/argocd-example-apps.git
28-
targetRevision: HEAD
29-
path: guestbook
30-
# chart: foo
31-
destination:
32-
server: https://kubernetes.default.svc
33-
namespace: guestbook
34-
"""))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import yaml
2+
3+
test_validate = lambda {
4+
validate(yaml.decode("""\
5+
apiVersion: argoproj.io/v1alpha1
6+
kind: Application
7+
metadata:
8+
name: badapp01
9+
namespace: default
10+
spec:
11+
project: foo
12+
source:
13+
repoURL: https://github.com/argoproj/argocd-example-apps.git
14+
targetRevision: HEAD
15+
path: guestbook
16+
# chart: foo
17+
destination:
18+
server: https://kubernetes.default.svc
19+
namespace: guestbook
20+
"""))
21+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[package]
22
name = "argo-application-prevent-default-project"
3-
edition = "0.0.1"
4-
version = "0.0.1"
3+
version = "0.0.2"
54

argo-application-prevent-default-project/kcl.mod.lock

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import yaml
2+
3+
test_validate = lambda {
4+
validate(yaml.decode("""\
5+
apiVersion: argoproj.io/v1alpha1
6+
kind: Application
7+
metadata:
8+
name: goodapp
9+
namespace: default
10+
spec:
11+
project: biz
12+
source:
13+
repoURL: https://github.com/argoproj/argocd-example-apps.git
14+
targetRevision: HEAD
15+
path: guestbook
16+
destination:
17+
server: https://kubernetes.default.svc
18+
namespace: guestbook
19+
"""))
20+
}

result/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Introduction
2+
3+
`result` is a KCL error handling library
4+
5+
## Resource
6+
7+
The Code source and documents are [here](https://github.com/kcl-lang/modules/tree/main/result)

result/kcl.mod

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "result"
3+
version = "0.0.1"
4+
description = "`result` is a KCL error handling library"

result/main.k

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
schema Result:
2+
result: bool
3+
error?: str
4+
5+
is_ok: () -> bool = lambda {
6+
result
7+
}
8+
9+
is_error: () -> bool = lambda {
10+
not result
11+
}
12+
13+
check:
14+
error, if not result, "error message should not be empty when the result is False"

scale-deployment-zero/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Introduction
2+
3+
`scale-deployment-zero` is a KCL mutation package
4+
5+
## Resource
6+
7+
The Code source and document are [here](https://github.com/kcl-lang/modules/tree/main/scale-deployment-zero)

scale-deployment-zero/kcl.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "scale-deployment-zero"
3+
edition = "*"
4+
version = "0.1.0"
5+
description = "`scale-deployment-zero` is a KCL mutation package"

scale-deployment-zero/main.k

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
annotations: {str:str} = option("params")?.annotations or {}
2+
3+
items = [item | {
4+
if item.kind == "Deployment":
5+
spec.replicas = 0
6+
metadata.annotations: annotations # Scale to zero and offer a useful annotation information
7+
} for item in option("items")]

spread-pods-across-topology/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## Introduction
2+
3+
`spread-pods-across-topology` is a KCL mutation package
4+
5+
## Resource
6+
7+
The Code source and document are [here](https://github.com/kcl-lang/modules/tree/main/spread-pods-across-topology)

spread-pods-across-topology/kcl.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "spread-pods-across-topology"
3+
edition = "*"
4+
version = "0.1.0"
5+
description = "`spread-pods-across-topology` is a KCL mutation package"

spread-pods-across-topology/main.k

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
annotations: {str:str} = option("params")?.annotations or {}
2+
items = [item | {
3+
if item.kind == "Deployment":
4+
spec.template.spec: {topologySpreadConstraints += [{
5+
maxSkew: 1
6+
topologyKey: "zone"
7+
whenUnsatisfiable: "DoNotSchedule"
8+
labelSelector.matchLabels.distributed: "required"
9+
}]}
10+
} for item in option("items")]

topology-spread-constraints/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Introduction
2+
3+
## Resource
4+
5+
The Code source and documents are [here](https://github.com/kcl-lang/modules/tree/main/topology-spread-constraints)

topology-spread-constraints/kcl.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "topology-spread-constraints"
3+
version = "0.1.0"
4+
description = "`topology-spread-constraints` is a kcl validation package"
5+

topology-spread-constraints/kcl.mod.lock

Whitespace-only changes.

topology-spread-constraints/main.k

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
KINDS = ["Deployment", "StatefulSet"]
2+
3+
to_str_set = lambda items: [str] {
4+
[item for item in {item = None for item in items}]
5+
}
6+
7+
# Define the validation function
8+
validate = lambda item: {str:} {
9+
if item.kind in KINDS and item.spec.replicas >= 3:
10+
contraints = item.spec.template.spec.topologySpreadConstraints
11+
valid_contraints = to_str_set([c.topologyKey for c in contraints if c?.topologyKey in ["kubernetes.io/hostname", "topology.kubernetes.io/zone"]])
12+
assert len(valid_contraints) == 2, "topologySpreadConstraint for kubernetes.io/hostname & topology.kubernetes.io/zone are required, got ${valid_contraints}"
13+
item
14+
}
15+
16+
# Validate All resource
17+
items = [validate(i) for i in option("items") or []]
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import yaml
2+
3+
test_validate = lambda {
4+
good_manifest = """\
5+
apiVersion: apps/v1
6+
kind: StatefulSet
7+
metadata:
8+
name: pass
9+
namespace: monitoring
10+
labels:
11+
app: thanos-memcached
12+
spec:
13+
selector:
14+
matchLabels:
15+
app: thanos-memcached
16+
serviceName: memcached
17+
replicas: 3
18+
updateStrategy:
19+
type: RollingUpdate
20+
template:
21+
metadata:
22+
labels:
23+
app: thanos-memcached
24+
spec:
25+
containers:
26+
- name: memcached
27+
image: memcached:1.6.17-alpine
28+
command:
29+
- memcached
30+
- -m 2048
31+
- -o
32+
- modern
33+
- -v
34+
ports:
35+
- name: tcp-memcached
36+
containerPort: 11211
37+
topologySpreadConstraints:
38+
- maxSkew: 1
39+
topologyKey: foo.bar/test
40+
whenUnsatisfiable: ScheduleAnyway
41+
labelSelector:
42+
matchLabels:
43+
app: thanos-memcached
44+
- maxSkew: 1
45+
topologyKey: kubernetes.io/hostname
46+
whenUnsatisfiable: ScheduleAnyway
47+
labelSelector:
48+
matchLabels:
49+
app: thanos-memcached
50+
- maxSkew: 1
51+
topologyKey: topology.kubernetes.io/zone
52+
whenUnsatisfiable: ScheduleAnyway
53+
labelSelector:
54+
matchLabels:
55+
app: thanos-memcached
56+
"""
57+
item = yaml.decode(good_manifest)
58+
validate(item)
59+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Introduction
2+
3+
## Resource
4+
5+
The Code source and documents are [here](https://github.com/kcl-lang/modules/tree/main/unique-ingress-host-and-path)

unique-ingress-host-and-path/kcl.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "unique-ingress-host-and-path"
3+
version = "0.1.0"
4+
description = "`unique-ingress-host-and-path` is a kcl validation package"
5+

unique-ingress-host-and-path/kcl.mod.lock

Whitespace-only changes.

unique-ingress-host-and-path/main.k

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
KINDS = ["Ingress"]
2+
3+
schema Result:
4+
result: bool
5+
error?: str
6+
7+
check:
8+
error if not result, "error message should not be empty when the result is False"
9+
10+
validate = lambda item: {str:} -> Result {
11+
result = Result {result = True}
12+
13+
if item.kind in KINDS:
14+
paths: [str] = [p.path for r in item.spec.rules for p in r.http.paths]
15+
if not isunique(paths):
16+
result = Result {
17+
result = False
18+
error = "Ingress paths must be unique, got ${paths}"
19+
}
20+
result
21+
22+
}
23+
# Define the validation function
24+
must_validate = lambda item {
25+
result = validate(item)
26+
assert result.result, result.error
27+
item
28+
}
29+
# Validate All resource
30+
items = [must_validate(i) for i in option("items") or []]
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import yaml
2+
3+
test_validate = lambda {
4+
assert validate(yaml.decode("""\
5+
apiVersion: networking.k8s.io/v1
6+
kind: Ingress
7+
metadata:
8+
name: goodingress01
9+
namespace: unique-ingress-paths-ns
10+
spec:
11+
rules:
12+
- host: endpoint01
13+
http:
14+
paths:
15+
- backend:
16+
service:
17+
name: demo-svc
18+
port:
19+
number: 8080
20+
path: /foo/abc/def
21+
pathType: Prefix
22+
- backend:
23+
service:
24+
name: demo-svc
25+
port:
26+
number: 8081
27+
path: /bar/def/bca
28+
pathType: Prefix
29+
- host: endpoint02
30+
http:
31+
paths:
32+
- backend:
33+
service:
34+
name: demo-svc
35+
port:
36+
number: 8080
37+
path: /bar/abc/bca
38+
pathType: Prefix
39+
- backend:
40+
service:
41+
name: demo-svc
42+
port:
43+
number: 8081
44+
path: /foo/fed/def
45+
pathType: Prefix
46+
""")).result == True
47+
assert validate(yaml.decode("""\
48+
apiVersion: networking.k8s.io/v1
49+
kind: Ingress
50+
metadata:
51+
name: goodingress01
52+
namespace: unique-ingress-paths-ns
53+
spec:
54+
rules:
55+
- host: endpoint01
56+
http:
57+
paths:
58+
- backend:
59+
service:
60+
name: demo-svc
61+
port:
62+
number: 8080
63+
path: /foo/abc/def
64+
pathType: Prefix
65+
- backend:
66+
service:
67+
name: demo-svc
68+
port:
69+
number: 8081
70+
path: /bar/def/bca
71+
pathType: Prefix
72+
- host: endpoint02
73+
http:
74+
paths:
75+
- backend:
76+
service:
77+
name: demo-svc
78+
port:
79+
number: 8080
80+
path: /bar/abc/bca
81+
pathType: Prefix
82+
- backend:
83+
service:
84+
name: demo-svc
85+
port:
86+
number: 8081
87+
path: /bar/abc/bca # Error: dup paths: /bar/abc/bca
88+
pathType: Prefix
89+
""")).result == False
90+
91+
}

0 commit comments

Comments
 (0)