Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Implement traits ConflictsWith #309

Closed
wants to merge 3 commits into from

Conversation

zzxwill
Copy link
Member

@zzxwill zzxwill commented Nov 20, 2020

When one trait conflicts with others, it should block the process
of traits rendering no matter they are trying to apply all in one
or one by one.

To fix #141

@zzxwill zzxwill force-pushed the feature-trait-conflicts branch 2 times, most recently from 0af56ec to 00b3cfc Compare November 21, 2020 13:04
When one trait conflicts with others, it should block the process
of traits rendering no matter they are trying to apply all in one
or one by one

Signed-off-by: zzxwill <[email protected]>
Signed-off-by: zzxwill <[email protected]>
@zzxwill zzxwill changed the title [WIP] Implement traits ConflictsWith Implement traits ConflictsWith Nov 23, 2020
@zzxwill
Copy link
Member Author

zzxwill commented Nov 23, 2020

Currently spec.conflictsWith only supports TraitDefinition name.

apiVersion: core.oam.dev/v1alpha2
kind: TraitDefinition
metadata:
  name: abc.extend.oam.dev
spec:
  revisionEnabled: true
  workloadRefPath: spec.workloadRef
  appliesToWorkloads:
    - core.oam.dev/v1alpha2.ContainerizedWorkload
    - deployments.apps
  definitionRef:
    name: abc.extend.oam.dev
  conflictsWith:
    - abc.extend.oam.dev
    - autoscale

API group or labelSelector are not yet supported.

  conflictsWith:
    - *.networking.k8s.io # API group
    - labelSelector:foo=bar

@@ -114,6 +114,12 @@ type TraitDefinitionSpec struct {
// +optional
AppliesToWorkloads []string `json:"appliesToWorkloads,omitempty"`

// ConflictsWith specifies the list of traits which could not apply to the
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// ConflictsWith specifies the list of traits which could not apply to the
// ConflictsWith specifies the list of traits(CRD name, Definition name, CRD group) which could not apply to the

@@ -325,7 +325,8 @@ func renderWorkload(data []byte, p ...Parameter) (*unstructured.Unstructured, er
return &unstructured.Unstructured{Object: w.UnstructuredContent()}, nil
}

func renderTrait(data []byte, _ ...Parameter) (*unstructured.Unstructured, error) {
// RenderTrait renders Trait to *unstructured.Unstructured format
func RenderTrait(data []byte, _ ...Parameter) (*unstructured.Unstructured, error) {
// TODO(negz): Is there a better decoder to use here?
Copy link
Member

Choose a reason for hiding this comment

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

we should create a common function for it instead of changing it public, and this common function could be in helper lib or anywhere else instead of the main logic lib.

@@ -110,6 +126,15 @@ func ValidateTraitObject(obj *v1alpha2.ApplicationConfiguration) field.ErrorList
allErrs = append(allErrs, field.Invalid(fldPath, content,
fmt.Sprintf("the trait data missing GVK, api = %s, kind = %s,", trait.GetAPIVersion(), trait.GetKind())))
}
compatibleTraits = append(compatibleTraits, preAppliedTraits...)
Copy link
Member

Choose a reason for hiding this comment

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

根本不需要那个 preAppliedTraits . 每次 compatibbleTraits append 一个当前检查通过的trait就行了。

if err != nil {
return nil, err
}
if conflict == compatibleTraitDef.Name {
Copy link
Member

@wonderflow wonderflow Nov 24, 2020

Choose a reason for hiding this comment

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

四个条件都要有,你这里只检查了第一条:

  1. CRD 名称定义冲突
    - services.k8s.io # API resource/crd name
  1. api group 定义冲突,这个是CRD 名称的增强版,前面用 *. 前缀
    - *.networking.k8s.io # API group
  1. label 定义冲突,前面固定前缀 labelSelector , label检查的是trait definition的label
    - labelSelector:foo=bar

  1. Definition 名称定义冲突,与第一条类似,只不过这里写的是definition的名字而不是CRD名字


for _, conflict := range traitDef.Spec.ConflictsWith {
for j := range compatibleTraits {
compatibleTraitDef, err := util.FetchTraitDefinition(ctx, client, dm, &compatibleTraits[j])
Copy link
Member

@wonderflow wonderflow Nov 24, 2020

Choose a reason for hiding this comment

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

这里每次重复去fetch调用RPC请求性能太差了,预先获取所有涉及到的trait definition

"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"
)

var _ = Describe("Trait conflict", func() {
Copy link
Member

Choose a reason for hiding this comment

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

这个改成单测吧,e2e现在太重了,不好检查,这个webhook的逻辑也没那么复杂,单测足够了。 参考: #310

@zzxwill zzxwill closed this Nov 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Add conflictsWith to TraitDefitnition
2 participants