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

Adds webhook for Repository Validation #708

Merged
merged 9 commits into from Jun 1, 2022

Conversation

sm43
Copy link
Contributor

@sm43 sm43 commented May 30, 2022

This adds a webhook (custom admission controller) to validate repository url and reject create request
if user is trying to create a repository with url which already exist in some other repository.

Code changes:
Adds a new deployment for webhook
The webhook has 2 controllers:

  • certificates: this creates certs for webhook, we create a empty secret and this controller manages certs for webhook
  • validation controller, this has 2 parts
    • reconciler which watches the certs secret and ValidatingWebhookConfiguration, when ever the secret is updated, reconciler will update ValidatingWebhookConfiguration with certs and rules
    • Admit: this is the endpoint which will be executed when api server gets a request for creating/updating repository (validation logic)

Submitter Checklist

  • β™½ Run make test lint before submitting a PR (ie: with pre-commit, no need to waste CPU cycle on CI
  • πŸ“– If you are adding a user facing feature or make a change of the behavior, please verify that you have documented it
  • πŸ§ͺ 100% coverage is not a target but most of the time we would rather have a unit test if you make a code change.
  • 🎁 If that's something that is possible to do please ensure to check if we can add a e2e test.
  • πŸ”Ž If there is a flakiness in the CI tests then don't necessary ignore it, better get the flakyness fixed before merging or if that's not possible there is a good reason to bypass it. (token rate limitation may be a good reason to skip).

Shivam Mukhade added 3 commits May 30, 2022 16:46
this add validation webhook for repository so that
we dont allow user to create multiple respositories with
same git url.

Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
@sm43
Copy link
Contributor Author

sm43 commented May 30, 2022

it seems in e2e we don't cleanup repo crs πŸ˜„

Shivam Mukhade added 3 commits May 31, 2022 09:38
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
@@ -58,6 +59,22 @@ jobs:
tags: ${{ steps.meta-watcher.outputs.tags }}
labels: ${{ steps.meta-watcher.outputs.labels }}

- name: Extract metadata (tags, labels) for Docker (Webhook)
Copy link
Member

Choose a reason for hiding this comment

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

i think there is.a way to do "matrix" in google actions for not have to copy and paste large block everytime, i guess we can do that if we have to add another image again πŸ™ƒ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can use matrix but there will 4 jobs created currently we execute all in one

}

func checkIfRepoExist(ctx context.Context, pac versioned.Interface, repo *v1alpha1.Repository, ns string) (bool, error) {
repositories, err := pac.PipelinesascodeV1alpha1().Repositories(ns).List(
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it's an expensive operation and if we should cache,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

gonna use a lister here instead of client

runcnx.Clients.Log.Infof("Deleting NS %s", targetNS)
err := runcnx.Clients.Kube.CoreV1().Namespaces().Delete(ctx, targetNS, metav1.DeleteOptions{})
Copy link
Member

Choose a reason for hiding this comment

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

is this code never ran? i think go should have bugged us out on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is ran.. but we are deleting repo before deleting ns
test were failing as repo were not getting deleted from etcd
when ns was deleted

Copy link
Member

@chmouel chmouel May 31, 2022

Choose a reason for hiding this comment

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

i mean the (old) code would get "no new variables on left sides" since we were redefining twice err

(i had to double check since it's early morning here :D

image

)

@chmouel
Copy link
Member

chmouel commented May 31, 2022

I think it should be relatively easy to add a E2E test for this feature (try to create the same repo crd and check for failure)

Shivam Mukhade added 3 commits May 31, 2022 11:28
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
as can't create multiple repo with same url as webhook will
deny

Signed-off-by: Shivam Mukhade <smukhade@redhat.com>
@sm43 sm43 requested a review from chmouel May 31, 2022 07:12
@codecov-commenter
Copy link

Codecov Report

Merging #708 (4ea5b59) into main (4904db5) will decrease coverage by 0.98%.
The diff coverage is 18.07%.

@@            Coverage Diff             @@
##             main     #708      +/-   ##
==========================================
- Coverage   69.08%   68.10%   -0.99%     
==========================================
  Files          65       68       +3     
  Lines        3921     4016      +95     
==========================================
+ Hits         2709     2735      +26     
- Misses        944     1009      +65     
- Partials      268      272       +4     
Impacted Files Coverage Ξ”
pkg/webhook/controller.go 0.00% <0.00%> (ΓΈ)
pkg/webhook/reconciler.go 0.00% <0.00%> (ΓΈ)
pkg/webhook/validation.go 65.21% <65.21%> (ΓΈ)
pkg/git/git.go 74.35% <0.00%> (+7.69%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Ξ” = absolute <relative> (impact), ΓΈ = not affected, ? = missing data
Powered by Codecov. Last update 4904db5...4ea5b59. Read the comment docs.

@@ -12,23 +12,20 @@ import (
)

func TestGithubPullRequest(t *testing.T) {
t.Parallel()
Copy link
Member

Choose a reason for hiding this comment

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

t.Parallel actually makes it parallel isnt it ?

Copy link
Contributor Author

@sm43 sm43 May 31, 2022

Choose a reason for hiding this comment

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

yeah but we use same git repo right
so one test creats repo
other will also try to create and fail in a different ns

Copy link
Member

Choose a reason for hiding this comment

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

πŸ‘πŸ»

@@ -12,23 +12,20 @@ import (
)

func TestGithubPullRequest(t *testing.T) {
t.Parallel()
Copy link
Member

Choose a reason for hiding this comment

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

πŸ‘πŸ»

@chmouel chmouel merged commit 1d316fd into openshift-pipelines:main Jun 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants