Skip to content

Comments

Config Structural Validation Schema Part 1: Commons, HNC, CAPI, Harbor, Storage#2063

Merged
Zash merged 1 commit intomainfrom
ka/jsonschema-core
May 13, 2024
Merged

Config Structural Validation Schema Part 1: Commons, HNC, CAPI, Harbor, Storage#2063
Zash merged 1 commit intomainfrom
ka/jsonschema-core

Conversation

@Zash
Copy link
Contributor

@Zash Zash commented Mar 27, 2024

Warning

This is a public repository, ensure not to disclose:

  • personal data beyond what is necessary for interacting with this pull request, nor
  • business confidential information, such as customer names.

What kind of PR is this?

Required: Mark one of the following that is applicable:

  • kind/feature
  • kind/improvement
  • kind/deprecation
  • kind/documentation
  • kind/clean-up
  • kind/bug
  • kind/other

Optional: Mark one or more of the following that are applicable:

Important

Breaking changes should be marked kind/admin-change or kind/dev-change depending on type
Critical security fixes should be marked with kind/security

  • kind/admin-change
  • kind/dev-change
  • kind/security
  • kind/adr

What does this PR do / why do we need this PR?

This adds structural validation for the following top-level config sections:

  • user
  • hnc
  • clusterApi
  • harbor
  • clusterApi
  • storageClasses
  • objectStorage
  • rookCeph

For more context including the required scripts and see #1862
Part of issue #1427

Information to reviewers

ck8s validate should now perform validation of the above sections.

While the CI is failing:
Please help determine whether it is the schema that is lacking or whether it detected a config mistake.

Suggestions and improvements very welcome!

Also see the README for brief information about the schema format

Checklist

  • Proper commit message prefix on all commits
  • Change checks:
    • The change is transparent
    • The change is disruptive
    • The change requires no migration steps
    • The change requires migration steps
    • The change upgrades CRDs
  • Metrics checks:
    • The metrics are still exposed and present in Grafana after the change
    • The metrics names didn't change (Grafana dashboards and Prometheus alerts are not affected)
    • The metrics names did change (Grafana dashboards and Prometheus alerts were fixed)
  • Logs checks:
    • The logs do not show any errors after the change
  • Pod Security Policy checks:
    • Any changed pod is covered by Pod Security Admission
    • Any changed pod is covered by Gatekeeper Pod Security Policies
    • The change does not cause any pods to be blocked by Pod Security Admission or Policies
  • Network Policy checks:
    • Any changed pod is covered by Network Policies
    • The change does not cause any dropped packets in the NetworkPolicy Dashboard
  • Audit checks:
    • The change does not cause any unnecessary Kubernetes audit events
    • The change requires changes to Kubernetes audit policy
  • Falco checks:
    • The change does not cause any alerts to be generated by Falco
  • Bug checks:
    • The bug fix is covered by regression tests
  • Config checks:
    • The schema was updated

@Zash Zash force-pushed the ka/jsonschema branch 2 times, most recently from 089dffc to 777f75f Compare March 27, 2024 12:56
@Zash Zash force-pushed the ka/jsonschema-core branch from c04f818 to f5ba5cd Compare March 27, 2024 13:30
@Zash Zash changed the title Config Schema Part 1: Commons, HNC, CAPI, Harbor, Storage Config Structural Validation Schema Part 1: Commons, HNC, CAPI, Harbor, Storage Mar 27, 2024
Base automatically changed from ka/jsonschema to main March 27, 2024 13:44
@Zash Zash force-pushed the ka/jsonschema-core branch from f5ba5cd to 67a2a80 Compare March 27, 2024 13:48
@Zash Zash marked this pull request as ready for review March 27, 2024 13:50
@Zash Zash marked this pull request as draft March 27, 2024 14:15
@Zash Zash force-pushed the ka/jsonschema-core branch from 608c589 to e523d22 Compare March 28, 2024 13:45
@Zash Zash force-pushed the ka/jsonschema-core branch from 9c79c33 to 45169fa Compare April 2, 2024 12:28
@Zash Zash marked this pull request as ready for review April 2, 2024 12:43
@Zash Zash force-pushed the ka/jsonschema-core branch from 45169fa to 070b325 Compare April 2, 2024 13:32
Copy link
Contributor

@Xartos Xartos left a comment

Choose a reason for hiding this comment

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

I'm not familiar with this schema format. Do you have any good link to where I can read about it? Or maybe just the name of it so I can search for it

@Zash
Copy link
Contributor Author

Zash commented Apr 3, 2024

See https://github.com/elastisys/compliantkubernetes-apps/blob/main/config/schemas/README.md and remind me tomorrow to add a link to that in the reviewer info.

It's also closely related to the OpenAPI format used in CRDs.

@Zash Zash force-pushed the ka/jsonschema-core branch 2 times, most recently from e30f226 to f73acdf Compare April 15, 2024 12:20
Copy link
Contributor

@Xartos Xartos left a comment

Choose a reason for hiding this comment

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

Some minor things but overall it looks good 👍

@Zash Zash force-pushed the ka/jsonschema-core branch from ad37254 to c8c9100 Compare April 29, 2024 13:26
Comment on lines 10 to 24
kubernetesAffinity:
$comment: Real k8s resources might have existing schemas that could be reused as-is
properties:
nodeAffinity:
type: object
podAffinity:
type: object
podAntiAffinity:
type: object
additionalProperties: false
examples:
- nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/control-plane
operator: Exists
title: Kubernetes Affinity
type: object
Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to source from the upstream OpenAPI spec, though it don't know how well it matches.
The bulk of common definitions are in api_openapi.json and basically everything are refs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

diff --git a/config/schemas/config.yaml b/config/schemas/config.yaml
index b10d2a9f..9c3fe3f9 100644
--- a/config/schemas/config.yaml
+++ b/config/schemas/config.yaml
@@ -8,7 +8,6 @@ description: |
   file will contain different settings.
 $defs:
   kubernetesAffinity:
-    $comment: Real k8s resources might have existing schemas that could be reused as-is
     properties:
       nodeAffinity:
         type: object
@@ -19,11 +18,7 @@ $defs:
     additionalProperties: false
     examples:
       - nodeAffinity:
-          requiredDuringSchedulingIgnoredDuringExecution:
-            nodeSelectorTerms:
-              - matchExpressions:
-                  - key: node-role.kubernetes.io/control-plane
-                    operator: Exists
+          $ref: https://github.com/kubernetes/kubernetes/raw/master/api/openapi-spec/v3/api__v1_openapi.json#/components/schemas/io.k8s.api.core.v1.NodeAffinity
     title: Kubernetes Affinity
     type: object
   component:

🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Heh, accidentally put the $ref under examples. Now fixed.

I'm a bit uneasy about referencing remote files tho, and not sure if licensing allows copying subsets.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should be fine to copy the subset with proper attribution given that the upstream spec is Apache-2.0, so if you add a comment in the spec where you include it and a notice in the readme then it should be OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Those affinity settings contain further $references which point to things with even more references, turning this into a recursive chase that may need further though or some scripting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hooray for loops!

     [ck8s] Failed schema validation:
     wc-config.yaml: hnc.manager.nodeSelector: nodeSelectorTerms is required
     wc-config.yaml: velero.nodeSelector: nodeSelectorTerms is required

What to do with these? This could be due to importing definitions from latest k8s git. I haven't investigated yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh no, NodeSelector and nodeSelector are different things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made it so that .$defs.kubernetesThing are kubernetes properties where we wrote the schema, while those imported from the kubernetes API spec files have names like .$defs["io.k8s.api.core.v1.Affinity"].

Copy link
Contributor

@robinAwallace robinAwallace left a comment

Choose a reason for hiding this comment

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

I think it LGTM 🙂 Found one question mark.

@Zash Zash requested a review from aarnq May 10, 2024 10:44
@Zash
Copy link
Contributor Author

Zash commented May 10, 2024

Incoming rebase onto latest main...

@Zash Zash force-pushed the ka/jsonschema-core branch 4 times, most recently from 2eb9e72 to 3f7fcba Compare May 10, 2024 12:08
@Zash
Copy link
Contributor Author

Zash commented May 10, 2024

I've prepared a squashed commit, aiming to push and merge on Monday.

This adds:
- common reusable things under $defs
- clusterApi
- harbor
- hnc
- objectStorage
- rookCeph
- storageClasses
- user
- velero

Common objects defined by the Kubernetes project are either prefixed
with 'kubernetes' when authored by us, using an 'io.k8s' prefix when
imported directly from the Kubernetes API definitions.

They can be found in the file kubernetes/spec/api/openapi-spec/v3/api__v1_openapi.json

The following script was used to find all referenced objects that were needed.

```bash
set -euxo pipefail

yq4 -i -P 'del(.$defs.kubernetesAffinity)' config/schemas/config.yaml
c="io.k8s.api.core.v1.Affinity" yq4 -i -P '.$defs[env(c)] = load(".../kubernetes/api/openapi-spec/v3/api__v1_openapi.json").components.schemas[env(c)]' config/schemas/config.yaml
c="io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelectorRequirement" yq4 -i -P '.$defs[env(c)] = load(".../kubernetes/api/openapi-spec/v3/api__v1_openapi.json").components.schemas[env(c)]' config/schemas/config.yaml

while grep -qF '#/components/schemas/' config/schemas/config.yaml; do
	for component in $(grep -oP "#/components/schemas/[^'\"]*" config/schemas/config.yaml | cut -d/ -f4); do
    c="${component}" yq4 -i -P '.$defs[env(c)] = load(".../kubernetes/api/openapi-spec/v3/api__v1_openapi.json").components.schemas[env(c)]' config/schemas/config.yaml
    sed -i "s|#/components/schemas/${component}|#/\$defs/${component}|" config/schemas/config.yaml
  done
done
```

Co-authored-by: André Arnqvist <58822152+aarnq@users.noreply.github.com>
Co-authored-by: Fredrik Liv <fredrik.liv@elastisys.com>
@Zash Zash force-pushed the ka/jsonschema-core branch from 3f7fcba to 1619aee Compare May 13, 2024 08:42
@Zash Zash merged commit 1b70c09 into main May 13, 2024
@Zash Zash deleted the ka/jsonschema-core branch May 13, 2024 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants