-
Notifications
You must be signed in to change notification settings - Fork 80
Fix could not find WorkloadDefinition
issue
#208
Conversation
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. |
pkg/oam/util/helper.go
Outdated
@@ -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` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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` |
pkg/oam/util/helper.go
Outdated
func GetCRDName(u *unstructured.Unstructured) string { | ||
if crdName, ok := u.GetAnnotations()[DefinitionAnnotation]; ok { |
There was a problem hiding this comment.
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
pkg/oam/util/helper.go
Outdated
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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 |
could not find WorkloadDefinition
could not find WorkloadDefinition
issue
pkg/oam/util/helper.go
Outdated
@@ -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" |
There was a problem hiding this comment.
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
5f60904
to
b536e68
Compare
pkg/oam/util/helper.go
Outdated
@@ -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 { |
There was a problem hiding this comment.
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
There was a problem hiding this 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.
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]>
Signed-off-by: zzxwill <[email protected]>
e172e6d
to
a6c3e81
Compare
Signed-off-by: zzxwill <[email protected]>
@@ -22,6 +22,8 @@ import ( | |||
"fmt" | |||
"net/http" | |||
|
|||
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam" |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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})) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
pkg/oam/util/helper.go
Outdated
// 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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
pkg/oam/util/helper.go
Outdated
// 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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Signed-off-by: zzxwill [email protected] Signed-off-by: zzxwill <[email protected]>
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]