Skip to content

Commit b55276b

Browse files
lobziikStranger6667
authored andcommitted
Add kcp (https://github.com/kcp-dev/kcp) as target
Tested only with schemathesis at this point
1 parent 7f80e47 commit b55276b

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2022 The KCP Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Build the binary
16+
FROM golang:1.17 AS builder
17+
18+
WORKDIR /workspace
19+
20+
21+
# Replace copy from upstream dockerfile to just an in-place clone
22+
RUN git clone https://github.com/kcp-dev/kcp.git .
23+
# checkaut latest commit on 3.6.2022
24+
RUN git checkout 340946a62487f1c29bef5e443abf98a49658f54d
25+
26+
RUN go mod download
27+
28+
RUN apt-get update && apt-get install -y jq && mkdir -p bin
29+
RUN CGO_ENABLED=0 make
30+
31+
# Use distroless as minimal base image to package the manager binary
32+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
33+
# FROM gcr.io/distroless/static:nonroot
34+
FROM alpine:3.15
35+
36+
ENV UID $UID
37+
ENV GID $GID
38+
39+
WORKDIR /
40+
COPY --from=builder workspace/bin/kcp-front-proxy workspace/bin/kcp workspace/bin/virtual-workspaces /
41+
RUN mkdir -p /data && \
42+
chown $UID:$GID /data
43+
USER $UID:$UID
44+
WORKDIR /data
45+
VOLUME /data
46+
ENTRYPOINT ["/kcp"]
47+
CMD ["start"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
from functools import cached_property
3+
from pathlib import Path
4+
from typing import Dict
5+
6+
import attr
7+
import yaml
8+
9+
from wafp.targets import (
10+
BaseTarget,
11+
Language,
12+
Metadata,
13+
Package,
14+
SchemaSource,
15+
SchemaSourceType,
16+
Specification,
17+
SpecificationType,
18+
)
19+
20+
KUBECONFIG_FILENAME = "admin.kubeconfig"
21+
KCP_DATA_PATH = Path(__file__).parent.absolute() / "kcp_data" / ".kcp"
22+
23+
24+
@attr.s
25+
class Default(BaseTarget):
26+
# TODO:
27+
# - proper cleanup of kcp_data folder
28+
# and / or
29+
# - move etcd dump to targets artifacts folder after run ends
30+
31+
# port: int = attr.field(factory=lambda: 6443)
32+
fuzzer_skip_ssl_verify: bool = attr.ib(default=True)
33+
34+
def get_base_url(self) -> str:
35+
# self.port does not work despite it was overrided in this child class, random came anyway
36+
# hardcode it for now
37+
return "https://0.0.0.0:6443"
38+
39+
def get_schema_location(self) -> str:
40+
return f"{self.get_base_url()}/openapi/v2"
41+
42+
def get_environment_variables(self):
43+
env = super().get_environment_variables()
44+
env.update({
45+
"UID": f"{os.getuid()}",
46+
"GID": f"{os.getgid()}"
47+
})
48+
print(env)
49+
return env
50+
51+
def is_ready(self, line: bytes) -> bool:
52+
return b"Reconciling namespace root|default" in line
53+
54+
def get_metadata(self) -> Metadata:
55+
return Metadata(
56+
language=Language.GO,
57+
framework=Package(name="kubernetes", version="1.23.5"),
58+
schema_source=SchemaSource(
59+
type=SchemaSourceType.GENERATED, library=Package(name="kubebuilder", version="unknown")
60+
),
61+
validation_from_schema=True,
62+
specification=Specification(name=SpecificationType.OPENAPI, version="2.0"),
63+
)
64+
65+
def after_start(self, stdout: bytes, headers: Dict[str, str]) -> None:
66+
headers["Authorization"] = f"Bearer {self.auth_token}"
67+
68+
@cached_property
69+
def auth_token(self) -> str:
70+
with open(KCP_DATA_PATH / KUBECONFIG_FILENAME, "r") as fd:
71+
kubeconfig = fd.read()
72+
kubeconfig = yaml.safe_load(kubeconfig)
73+
# TODO look up for kubeconfig type
74+
token: str = kubeconfig["users"][0]["user"]["token"] # type: ignore
75+
return token
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: '3'
2+
services:
3+
kcp:
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
environment:
8+
- UID=${UID-1000}
9+
- GID=${GID-1000}
10+
user: "${UID-1000}:${GID-1000}"
11+
volumes:
12+
- ./kcp_data:/data:Z
13+
ports:
14+
- '6443:6443' # kube apiserver default port

src/wafp/targets/catalog/kubernetes_kcp/kcp_data/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)