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

Fix could not find WorkloadDefinition issue #208

Merged
merged 5 commits into from
Sep 21, 2020

Conversation

zzxwill
Copy link
Member

@zzxwill zzxwill commented Sep 16, 2020

When WorkloadDefinition doesn't follow the pattern
. or there are different
workloadDefinitions pointing to the same workload Schema,
it will hit the issue of could not finding workloaddefinition.
Fix issue #207, feature #195.

Signed-off-by: zzxwill [email protected]

@zzxwill
Copy link
Member Author

zzxwill commented Sep 16, 2020

I will add unit-test and e2e-test tomorrow to make the PR ready for review.

#207 (comment) is an introduction and comparison with the solution of #195.

@@ -226,7 +228,11 @@ func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject

// GetCRDName return the CRD name of any resources
// the format of the CRD of a resource is <kind purals>.<group>
// Now the CRD name of a resource could also be defined as `metadata.annotations.definition.oam.dev/name`
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
// Now the CRD name of a resource could also be defined as `metadata.annotations.definition.oam.dev/name`
// Now the CRD name of a resource could also be defined as `definition.oam.dev/name` in `metadata.annotations`

func GetCRDName(u *unstructured.Unstructured) string {
if crdName, ok := u.GetAnnotations()[DefinitionAnnotation]; ok {
Copy link
Member

Choose a reason for hiding this comment

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

using index like this will easily cause panic if the map returned is nil

@@ -39,6 +39,8 @@ var (
const (
// TraitPrefixKey is prefix of trait name
TraitPrefixKey = "trait"
// DefinitionAnnotation is the annotation of filed `metadata.annotations` for WorkloadDefinition and Workload
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
// DefinitionAnnotation is the annotation of filed `metadata.annotations` for WorkloadDefinition and Workload
// DefinitionAnnotation is one automatically generated annotation of workload and trait to indicate which workloadDefinition/ traitDefinition it is generated

@zzxwill zzxwill closed this Sep 17, 2020
@zzxwill zzxwill changed the title Fix could not find WorkloadDefinition Fix could not find WorkloadDefinition issue Sep 18, 2020
@zzxwill zzxwill reopened this Sep 18, 2020
@zzxwill zzxwill marked this pull request as ready for review September 18, 2020 03:01
@@ -39,6 +39,9 @@ var (
const (
// TraitPrefixKey is prefix of trait name
TraitPrefixKey = "trait"
// DefinitionAnnotation is one automatically generated annotation of workload and trait to indicate which
//workloadDefinition/traitDefinition it is generated by
DefinitionAnnotation = "definition.oam.dev/name"
Copy link
Member

Choose a reason for hiding this comment

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

Maybe put this annotation const to types.go is better

@@ -226,7 +226,13 @@ func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject

// GetCRDName return the CRD name of any resources
// the format of the CRD of a resource is <kind purals>.<group>
// Now the CRD name of a resource could also be defined as `definition.oam.dev/name` in `metadata.annotations`
func GetCRDName(u *unstructured.Unstructured) string {
Copy link
Member

Choose a reason for hiding this comment

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

Please also refactor the function Name, it should not be GetCRDName , name it GetDefinitionName seems better

Copy link
Member

@wonderflow wonderflow left a comment

Choose a reason for hiding this comment

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

LGTM, In this PR, we add an annotation into workload to show which Definition it is generated from.

zzxwill and others added 3 commits September 21, 2020 14:32
When there is spec.type in Workload, an annotation will set for
the workload which can help find the WorkloadDefinition.

Fix issue crossplane#207, related to feature crossplane#195.

Co-authored-by: Sun Jianbo <[email protected]>
Signed-off-by: zzxwill <[email protected]>
Signed-off-by: zzxwill <[email protected]>
@zzxwill zzxwill force-pushed the definition branch 2 times, most recently from e172e6d to a6c3e81 Compare September 21, 2020 06:36
@@ -22,6 +22,8 @@ import (
"fmt"
"net/http"

"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"
Copy link
Collaborator

Choose a reason for hiding this comment

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

please move this with the local

workload.SetAnnotations(obj.GetAnnotations())
// Add another annotation DefinitionAnnotation which can mark the name of WorkloadDefinition
workload.SetAnnotations(util.MergeMap(obj.GetAnnotations(), map[string]string{oam.DefinitionAnnotation: workloadType}))
mutatelog.Info("Set annotation definition.oam.dev/name for workload", "annotation value", workloadType)
Copy link
Collaborator

Choose a reason for hiding this comment

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

this seems to be a debugging log for the testing purpose only

Copy link
Member Author

Choose a reason for hiding this comment

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

Webhook is a little bit for local debug, so I write the log to make sure it takes effect. As you recommended to use the existing label below, I will remove the annotation setting and the log. Thanks.

@@ -125,7 +127,9 @@ func (h *MutatingHandler) Mutate(obj *v1alpha2.Component) error {
// copy namespace/label/annotation to the workload and add workloadType label
workload.SetNamespace(obj.GetNamespace())
workload.SetLabels(util.MergeMap(obj.GetLabels(), map[string]string{WorkloadTypeLabel: workloadType}))
workload.SetAnnotations(obj.GetAnnotations())
// Add another annotation DefinitionAnnotation which can mark the name of WorkloadDefinition
workload.SetAnnotations(util.MergeMap(obj.GetAnnotations(), map[string]string{oam.DefinitionAnnotation: workloadType}))
Copy link
Collaborator

Choose a reason for hiding this comment

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

there is already a label to indicate the workloadType of the component. Where is this annotation with the same information used?

Copy link
Member

Choose a reason for hiding this comment

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

Oh,yes, using the existing WorkloadTypeLabel is better.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will use the label, instead.

@@ -4,6 +4,8 @@ import (
"context"
"fmt"

"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"
Copy link
Collaborator

Choose a reason for hiding this comment

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

move this to its group

// Now the definition name of a resource could also be defined as `definition.oam.dev/name` in `metadata.annotations`
func GetDefinitionName(u *unstructured.Unstructured) string {
if annotations := u.GetAnnotations(); annotations != nil {
if crdName, ok := annotations[oam.DefinitionAnnotation]; ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not use the existing label and add this extra annotation?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will use the label, thanks.

// the format of the definition of a resource is <kind plurals>.<group>
// Now the definition name of a resource could also be defined as `definition.oam.dev/name` in `metadata.annotations`
func GetDefinitionName(u *unstructured.Unstructured) string {
if labels := u.GetAnnotations(); labels != nil {
Copy link
Member

Choose a reason for hiding this comment

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

We need to get labels now, not annotations

Copy link
Member

Choose a reason for hiding this comment

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

If tests still passed with this obviously bug, you need to write more cases I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

If tests still passed with this obviously bug, you need to write more cases I think.

Sorry, the code was automatically reverted from Disk instead of Memory by Goland. My bad, fixed. Thanks.

@wonderflow wonderflow merged commit 1537fb2 into crossplane:master Sep 21, 2020
@zzxwill zzxwill deleted the definition branch September 21, 2020 15:59
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.

3 participants