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

Make kubeval hermetic #233

Merged
merged 4 commits into from
Apr 23, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_modules
.DS_Store
.nyc_output
bin/
__pycache__

# We use sed -i.bak when doing in-line replace, because it works better cross-platform
*.bak
11 changes: 11 additions & 0 deletions examples/kubeval/simple/.expected/results.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
- message: Additional property templates is not allowed
severity: error
resourceRef:
apiVersion: v1
kind: ReplicationController
namespace: ''
name: bob
file:
path: resources.yaml
field:
path: templates
- message: 'Invalid type. Expected: [integer,null], given: string'
severity: error
resourceRef:
Expand Down
20 changes: 11 additions & 9 deletions examples/kubeval/simple/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# Kubeval
# kubeval: simple example

The `kubeval` KRM config function validates Kubernetes resources using kubeval.
Learn more on the [kubeval website].

This example invokes the kubeval function against Kubernetes v1.18.0.
This example invokes the kubeval function against the builtin Kubernetes
v1.19.8 schema.

## Function invocation

Get this example and try it out by running the following commands:

<!-- TODO: no --network. See: https://github.com/GoogleContainerTools/kpt/issues/1621 -->

```sh
kpt pkg get https://github.com/GoogleContainerTools/kpt-functions-catalog.git/examples/kubeval .
kpt fn run kubeval --network
kpt fn run kubeval
```

## Expected Results

This should give the following output:

```sh
[ERROR] Invalid type. Expected: [integer,null], given: string in object 'v1/ReplicationController//bob' in file example-config.yaml in field spec.replicas
[ERROR] Additional property templates is not allowed in object 'v1/ReplicationController//bob' in file resources.yaml in field templates
[ERROR] Invalid type. Expected: [integer,null], given: string in object 'v1/ReplicationController//bob' in file resources.yaml in field spec.replicas
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shell32-Natsu @mengqiy Related to the discussion about CLI output and structured results.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make sure these are updated once the output UI changes land.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment in kptdev/kpt#1548 (comment) to remind us.

error: exit status 1
```

In the `example-config.yaml` file, replace the value of `spec.replicas`
with an integer to pass validation and rerun the command. This will return
success (no output).
There are validation error in the `resources.yaml` file, to fix them:
- replace the value of `spec.replicas` with an integer
- change `templates` to `template`

Rerun the command, and it will return success (no output).

[kubeval website]: https://www.kubeval.com/
3 changes: 1 addition & 2 deletions examples/kubeval/simple/fn-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ metadata:
config.k8s.io/function: |
container:
image: gcr.io/kpt-fn/kubeval:unstable
network: true
config.kubernetes.io/local-config: 'true'
data:
schema_location: https://kubernetesjsonschema.dev/
strict: 'true'
11 changes: 8 additions & 3 deletions functions/ts/kubeval/build/kubeval.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:14.15-alpine3.12 as builder

ARG KUBEVAL_VERSION="0.15.0"
ARG KUBEVAL_VERSION="v0.16.1"
RUN apk add curl && \
curl -sSLf https://github.com/instrumenta/kubeval/releases/download/${KUBEVAL_VERSION}/kubeval-linux-amd64.tar.gz | \
tar xzf - -C /usr/local/bin
Expand All @@ -27,16 +27,21 @@ RUN npm run build && \

FROM node:14.15-alpine3.12

RUN apk add --update --no-cache python3 py3-pip && ln -sf python3 /usr/bin/python
RUN pip install pyyaml jsonref click
COPY third_party/github.com/instrumenta/openapi2jsonschema/openapi2jsonschema/*.py /openapi2jsonschema/
RUN chmod +x /openapi2jsonschema/command.py && ln -s /openapi2jsonschema/command.py /usr/bin/openapi2jsonschema

# Run as non-root user as a best-practices:
# https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md
USER node

WORKDIR /home/node/app

COPY --from=builder /home/node/app /home/node/app

COPY --from=builder /usr/local/bin /usr/local/bin
COPY openapi.json /home/node/

ENV PATH /usr/local/bin:$PATH
ENV LOG_TO_STDERR=true

ENTRYPOINT ["node", "/home/node/app/dist/kubeval_run.js"]
Loading