Skip to content

Commit 1debe35

Browse files
committed
re-add tests, rm src foldeR
1 parent fbc24ba commit 1debe35

15 files changed

+170
-180
lines changed
File renamed without changes.

crd/instanced-deploy.yaml

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
---
2+
apiVersion: v1
3+
kind: Namespace
4+
metadata:
5+
name: instanced
6+
---
7+
apiVersion: v1
8+
kind: ServiceAccount
9+
metadata:
10+
labels:
11+
app.kubernetes.io/component: instanced
12+
app.kubernetes.io/name: instanced
13+
app.kubernetes.io/part-of: maplectf-instancing
14+
name: instanced
15+
namespace: instanced
16+
---
17+
apiVersion: rbac.authorization.k8s.io/v1
18+
kind: ClusterRole
19+
metadata:
20+
name: challenge-instancer
21+
labels:
22+
app.kubernetes.io/component: instanced
23+
app.kubernetes.io/name: instanced
24+
app.kubernetes.io/part-of: maplectf-instancing
25+
rules:
26+
- apiGroups: [""]
27+
resources: ["pods"]
28+
verbs: ["get", "watch", "list"]
29+
- apiGroups: ["apps"]
30+
resources: ["deployments"]
31+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
32+
- apiGroups: [""]
33+
resources: ["services"]
34+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
35+
- apiGroups: ["networking.k8s.io"]
36+
resources: ["ingresses"]
37+
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
38+
- apiGroups: ["k8s.maplebacon.org"]
39+
resources:
40+
- '*'
41+
verbs:
42+
- '*'
43+
---
44+
apiVersion: rbac.authorization.k8s.io/v1
45+
kind: RoleBinding
46+
metadata:
47+
name: manage-challenges
48+
namespace: challenges
49+
labels:
50+
app.kubernetes.io/component: instanced
51+
app.kubernetes.io/name: instanced
52+
app.kubernetes.io/part-of: maplectf-instancing
53+
subjects:
54+
- kind: ServiceAccount
55+
name: instanced
56+
namespace: instanced
57+
roleRef:
58+
apiGroup: rbac.authorization.k8s.io
59+
kind: ClusterRole
60+
name: challenge-instancer
61+
---
62+
apiVersion: v1
63+
kind: ConfigMap
64+
metadata:
65+
name: instanced-config
66+
namespace: instanced
67+
data:
68+
config.yaml: |
69+
listenAddr: ":8080"
70+
---
71+
apiVersion: v1
72+
kind: Service
73+
metadata:
74+
labels:
75+
app.kubernetes.io/name: instanced
76+
name: instanced
77+
namespace: instanced
78+
spec:
79+
ports:
80+
- name: http
81+
port: 80
82+
protocol: TCP
83+
targetPort: 8080
84+
selector:
85+
app.kubernetes.io/name: instanced
86+
---
87+
apiVersion: apps/v1
88+
kind: StatefulSet
89+
metadata:
90+
name: instanced
91+
namespace: instanced
92+
spec:
93+
selector:
94+
matchLabels:
95+
app.kubernetes.io/name: instanced
96+
serviceName: instanced
97+
replicas: 1
98+
template:
99+
metadata:
100+
labels:
101+
app.kubernetes.io/name: instanced
102+
spec:
103+
serviceAccountName: instanced
104+
automountServiceAccountToken: true
105+
containers:
106+
- image: us.gcr.io/maplectf/instanced:latest
107+
name: instanced
108+
ports:
109+
- name: http-port
110+
containerPort: 8080
111+
hostPort: 8080
112+
livenessProbe:
113+
httpGet:
114+
path: /healthz
115+
port: http-port
116+
initialDelaySeconds: 20
117+
periodSeconds: 10
118+
volumeMounts:
119+
- name: data
120+
mountPath: /data
121+
- name: config
122+
mountPath: "/config"
123+
readOnly: true
124+
resources:
125+
requests:
126+
cpu: "10m"
127+
memory: "20Mi"
128+
- image: alpine/curl:latest
129+
name: client
130+
command: ["sleep"]
131+
args: ["infinity"]
132+
resources:
133+
requests:
134+
cpu: "1m"
135+
memory: "2Mi"
136+
volumes:
137+
- name: config
138+
configMap:
139+
name: instanced-config
140+
items:
141+
- key: "config.yaml"
142+
path: "config.yaml"
143+
volumeClaimTemplates:
144+
- metadata:
145+
name: data
146+
spec:
147+
accessModes: ["ReadWriteOnce"]
148+
resources:
149+
requests:
150+
storage: 200Mi
151+
storageClassName: standard-rwo

src/db/db.go db/db.go

File renamed without changes.

src/db/schema.go db/schema.go

File renamed without changes.

src/instancer/api.go instancer/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/labstack/echo/v4"
1111
"github.com/labstack/echo/v4/middleware"
1212
"github.com/rs/zerolog"
13-
"github.com/ubcctf/instanced/src/adapters"
13+
"github.com/ubcctf/instanced/adapters"
1414
)
1515

1616
type InstancesResponse struct {
File renamed without changes.
File renamed without changes.

src/instancer/instancer.go instancer/instancer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
"github.com/labstack/echo/v4"
1212
"github.com/rs/zerolog"
13-
"github.com/ubcctf/instanced/src/db"
14-
"github.com/ubcctf/instanced/src/k8s"
13+
"github.com/ubcctf/instanced/db"
14+
"github.com/ubcctf/instanced/k8s"
1515
)
1616

1717
type Instancer struct {

src/instancer/procedures.go instancer/procedures.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"time"
99

1010
"github.com/google/uuid"
11-
"github.com/ubcctf/instanced/src/db"
12-
"github.com/ubcctf/instanced/src/k8s"
11+
"github.com/ubcctf/instanced/db"
12+
"github.com/ubcctf/instanced/k8s"
1313
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1414
)
1515

src/k8s/client.go k8s/client.go

File renamed without changes.

src/k8s/crd.go k8s/crd.go

File renamed without changes.
+13-81
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
package main
1+
package k8s_test
22

33
import (
4+
"fmt"
45
"log"
5-
"time"
66

7-
"github.com/google/uuid"
7+
"github.com/ubcctf/instanced/k8s"
88
)
99

1010
func ExampleUnmarshalSingleManifest() {
11-
log.Println("Running: ExampleUnmarshalSingleManifest")
1211
manifest := `apiVersion: apps/v1
1312
kind: Deployment
1413
metadata:
@@ -30,16 +29,15 @@ spec:
3029
ports:
3130
- containerPort: 80
3231
`
33-
obj, err := UnmarshalSingleManifest(manifest)
32+
obj, err := k8s.UnmarshalSingleManifest(manifest)
3433
if err != nil {
3534
log.Fatal(err.Error())
3635
}
37-
log.Println(obj)
38-
// Output:
36+
fmt.Println(obj)
37+
// Output: &{map[apiVersion:apps/v1 kind:Deployment metadata:map[name:nginx-instance-test-deployment namespace:challenges] spec:map[matchLabels:map[app:nginx-instance-test] replicas:1 selector:<nil> template:map[app:nginx-instance-test metadata:map[labels:<nil>] spec:map[containers:[map[image:nginx:1.14.2 name:nginx ports:[map[containerPort:80]]]]]]]]}
3938
}
4039

4140
func ExampleUnmarshalManifestFile() {
42-
log.Println("Running: ExampleUnmarshalManifestFile")
4341
manifest := `---
4442
apiVersion: apps/v1
4543
kind: Deployment
@@ -77,20 +75,20 @@ spec:
7775
targetPort: 80
7876
---
7977
`
80-
objs, err := UnmarshalManifestFile(manifest)
78+
objs, err := k8s.UnmarshalManifestFile(manifest)
8179
if err != nil {
8280
log.Fatal(err.Error())
8381
}
8482
for _, v := range objs {
8583
if v.GetName() != "" {
86-
log.Println(v.GetKind(), v.GetName())
84+
fmt.Println(v.GetKind(), v.GetName())
8785
}
8886
}
89-
// Output:
87+
// Output: Deployment nginx-instance-test-deployment
88+
// Service nginx-instance-test
9089
}
9190

9291
func ExampleUnmarshalChallenges() {
93-
log.Println("Running: ExampleUnmarshalChallenges")
9492
chals := make(map[string]string, 2)
9593
chals["nginx"] = `---
9694
apiVersion: apps/v1
@@ -129,80 +127,14 @@ spec:
129127
targetPort: 80
130128
---
131129
`
132-
chalMap, err := UnmarshalChallenges(chals)
130+
chalMap, err := k8s.UnmarshalChallenges(chals)
133131
if err != nil {
134132
log.Fatal(err)
135133
}
136134
obj, ok := chalMap["nginx"]
137135
if !ok {
138136
log.Fatal("key does not exist")
139137
}
140-
log.Println(obj)
141-
// Output:
142-
}
143-
144-
func ExampleGetChalObjsFromTemplate() {
145-
log.Println("Running: ExampleGetChalObjsFromTemplate")
146-
in := Instancer{}
147-
in.conf = &Config{}
148-
in.conf.Challenges = make(map[string]string, 2)
149-
in.conf.Challenges["nginx"] = `---
150-
apiVersion: apps/v1
151-
kind: Deployment
152-
metadata:
153-
name: nginx-instance-{{.ID}}-deployment
154-
namespace: challenges
155-
spec:
156-
selector:
157-
matchLabels:
158-
app: nginx-instance-{{.ID}}
159-
replicas: 1
160-
template:
161-
metadata:
162-
labels:
163-
app: nginx-instance-{{.ID}}
164-
spec:
165-
containers:
166-
- name: nginx
167-
image: nginx:1.14.2
168-
ports:
169-
- containerPort: 80
170-
---
171-
apiVersion: v1
172-
kind: Service
173-
metadata:
174-
name: nginx-instance-{{.ID}}
175-
namespace: challenges
176-
spec:
177-
selector:
178-
app: nginx-instance-{{.ID}}
179-
ports:
180-
- name: http
181-
protocol: TCP
182-
port: 8080
183-
targetPort: 80
184-
---
185-
`
186-
in.ParseTemplates()
187-
chal, err := in.GetChalObjsFromTemplate("nginx", uuid.NewString()[0:8])
188-
if err != nil {
189-
log.Fatal(err)
190-
}
191-
log.Println(len(chal))
192-
log.Println(chal)
193-
// Output:
194-
}
195-
196-
func ExampleInsertInstanceRecord() {
197-
in := Instancer{}
198-
in.InitDB("./tmp/test.db")
199-
in.InsertInstanceRecord(time.Hour, "1", "test_challenge", "abcdefg")
200-
recs, err := in.ReadInstanceRecords()
201-
if err != nil {
202-
log.Fatal(err)
203-
}
204-
for _, v := range recs {
205-
log.Println(v)
206-
}
207-
// Output:
138+
fmt.Println(obj)
139+
// Output: [{map[apiVersion:apps/v1 kind:Deployment metadata:map[name:nginx-instance-test-deployment namespace:challenges] spec:map[replicas:1 selector:map[matchLabels:map[app:nginx-instance-test]] template:map[metadata:map[labels:map[app:nginx-instance-test]] spec:map[containers:[map[image:nginx:1.14.2 name:nginx ports:[map[containerPort:80]]]]]]]]} {map[apiVersion:v1 kind:Service metadata:map[name:nginx-instance-test namespace:challenges] spec:map[ports:[map[name:http port:8080 protocol:TCP targetPort:80]] selector:map[app:nginx-instance-test]]]}]
208140
}

src/main.go main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"github.com/ubcctf/instanced/src/instancer"
4+
"github.com/ubcctf/instanced/instancer"
55
)
66

77
func main() {

0 commit comments

Comments
 (0)