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

Add Dynamic Admission Control for ComponentDefinition #1648

Merged
merged 3 commits into from May 15, 2021

Conversation

yangsoon
Copy link
Collaborator

@yangsoon yangsoon commented May 12, 2021

fix #1473, align with #1563

Add dynamic admission control for ComponentDefinition.

MutatingWebhook

MutatingWebhook will generate WorkloadDefinition based on the Spec.Workload.Definition field(if workloaddefinition does not exist), and fill in the Spec.Workload.Type field to point to the created workloaddefinition.

apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
  name: worker
  namespace: default
spec:
  workload:
    definition:
      apiVersion: apps/v1
      kind: Deployment
  schematic:
    cue:
      template: |
....

After being processed by MutatingWebhook

apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
  name: worker
  namespace: default
spec:
  workload:
    definition:
      apiVersion: apps/v1
      kind: Deployment
    type: deployments.app # MutatingWebhook filld the type field
  schematic:
    cue:
      template: |
...

Generate a WorkloadDefinition named deployments.app(If it doesn't exist).

kind: WorkloadDefinition
metadata:
  name: deployments.app
spec:
  definitionRef:
    name: deployments.app
    version: v1

ValidatingWebhook check Type and Definition fields in Spec.Workload:

  1. If the fields are all empty, ValidatingWebhook will reject;
# Rejected!
apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
  name: worker
  namespace: default
spec:
  workload:
    definition:
    type:
  schematic:
    cue:
      template: |
....
  1. if Type and Definitiondon‘t point to the same workloaddefinition, ValidatingWebhook will also reject.
# Rejected!
apiVersion: core.oam.dev/v1beta1
kind: ComponentDefinition
metadata:
  name: worker
  namespace: default
spec:
  workload:
    definition:
      apiVersion: apps/v1
      kind: Deployment
    type: containerizedworkloads.core.oam.dev  # definition and type point to different workload type
  schematic:
    cue:
      template: |
...

for details, please see:

https://github.com/oam-dev/spec/blob/master/3.component_model.md
https://github.com/oam-dev/spec/blob/master/4.workload_types.md

  • add webhook
  • add test

@codecov
Copy link

codecov bot commented May 12, 2021

Codecov Report

Merging #1648 (c7cb8f8) into master (80b2c37) will increase coverage by 0.12%.
The diff coverage is 74.48%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1648      +/-   ##
==========================================
+ Coverage   62.46%   62.59%   +0.12%     
==========================================
  Files         120      122       +2     
  Lines       11952    12027      +75     
==========================================
+ Hits         7466     7528      +62     
- Misses       3722     3732      +10     
- Partials      764      767       +3     
Flag Coverage Δ
e2etests 43.84% <62.24%> (+0.21%) ⬆️
unittests 58.33% <29.16%> (-0.27%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pkg/oam/util/helper.go 80.28% <ø> (-0.12%) ⬇️
pkg/controller/utils/utils.go 39.47% <42.10%> (-1.79%) ⬇️
...v/v1alpha2/componentdefinition/mutating_handler.go 70.73% <70.73%> (ø)
...v1alpha2/componentdefinition/validating_handler.go 92.85% <92.85%> (ø)
...ponentdefinition/componentdefinition_controller.go 84.55% <100.00%> (+1.35%) ⬆️
...ha2/core/components/componentdefinition/handler.go 78.78% <100.00%> (ø)
...aits/traitdefinition/traitdefinition_controller.go 75.00% <100.00%> (-5.00%) ⬇️
pkg/webhook/core.oam.dev/register.go 100.00% <100.00%> (ø)
...g/controller/core.oam.dev/v1alpha2/core/revison.go 68.46% <0.00%> (-2.71%) ⬇️
... and 9 more

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 80b2c37...c7cb8f8. Read the comment docs.

@yangsoon
Copy link
Collaborator Author

I fix the webhook. please \cc @captainroy-hy

@yangsoon yangsoon force-pushed the add-webhook branch 3 times, most recently from 47dc3e1 to 255c9e8 Compare May 13, 2021 07:18
@yangsoon yangsoon changed the title [WIP] Add Dynamic Admission Control for ComponentDefinition Add Dynamic Admission Control for ComponentDefinition May 13, 2021
@yangsoon yangsoon force-pushed the add-webhook branch 5 times, most recently from 8efc219 to f7d1049 Compare May 13, 2021 10:46
@captainroy-hy
Copy link
Collaborator

LGTM.

@wonderflow wonderflow merged commit a11ae2f into kubevela:master May 15, 2021
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.

[Feature] Add webhook to check the WorkloadTypeDescriptor field of ComponentDefinition
3 participants