kubeshop / vscode-monokle

An extension for Visual Studio Code to validate your Kubernetes configuration
https://marketplace.visualstudio.com/items?itemName=kubeshop.monokle
MIT License
6 stars 0 forks source link

Integrate remote policies (part 1) #15

Closed f1ames closed 1 year ago

f1ames commented 1 year ago

This PR introduces handling of remote policies to Monokle extension.

At this stage, it includes everything needed but authentication flow (which will be covered in a separate PR and will use device flow).

It also fixes #5, fixes #13.

Changes

Policy puller

PolicyPuller is responsible for remote policy fetching. It will query Cloud GraphQL API, first fetching user data (list of all projects) based on auth token. Then if a local folder is a git repo with any remote (so we can extract owner and name) it will be matched with a project.

Matching is done as follow:

After matching with a project, Cloud GraphQL API will be queried for a policy. If there is one, it will be saved locally (in /path/to/extension/.monokle/ folder). On every validation, the policy is read from locally saved file.

The process above is the same for initial policy fetch (on extension activation) and on periodical fetches (every 30 seconds now).

Policies priorities

We have 4 types of policies configurations:

The priorities are (from highest) - remote, config, file, default. Meaning that I can have local monokle.validation.yaml file, but when monokle.remotePolicyUrl is set, remote config will be used.

Handling special (kind of) cases

Assuming that monokle.remotePolicyUrl is configured correctly, the "happy case" is that a folder:

In such case we can fetch remote policy.

Now, any of those may not be true, meaning that extension needs to react in each case. See below what will happen in each case:

  1. Folder is not a git repo - show warning and fallback to local configs.
  2. Folder is a git repo, but doesn't have any remotes - show error, don't validate.
  3. Folder is a git repo, have remote, but does not belong to any project in the cloud - show error, don't validate.
  4. Folder is a git repo, have remote, belongs to a project in the cloud, but project doesn't have a policy defined - show error, don't validate.

I think the key here is to provide clear messages, and if possible, with links to docs, or deep links to cloud (e.g. policy wizard) so users can act on those errors.

Fixes

Testing

See CONTRIBUTING.md how to test it.

There is a MONOKLE_VSC_API_TOKEN env variable used which can be set to auth token (obtain from staging or prod after logging in) to allow communication with API.

Then monokle.remotePolicyUrl should be set to enable remote policy fetching.

When there is something missing (regarding remote configuration), you should see related error notifications:

image

Also hovering status bar item reveals a bit more information.

Checklist

f1ames commented 1 year ago

@WitoDelnat @olensmar maybe you can take a quick look on the above description/assumptions (especially policy priorities and handling special cases) to make sure it also aligns with your vision?