From 5d1ee24831cbe0067d76b609fa2a85e11588cdd4 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Fri, 18 Sep 2020 11:11:18 +0800 Subject: [PATCH 1/5] Fix `could not find WorkloadDefinition` issue When there is spec.type in Workload, an annotation will set for the workload which can help find the WorkloadDefinition. Fix issue #207, related to feature #195. Co-authored-by: Sun Jianbo Signed-off-by: zzxwill --- README.md | 2 +- pkg/oam/util/helper.go | 9 +++++++++ .../applicationconfiguration/applicationconfiguration.go | 2 +- pkg/webhook/v1alpha2/component/mutating_handler.go | 4 +++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 59efdf28..c4df4ab6 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ Or you can install with webhook enabled by following steps: kubectl -n oam-system create secret generic webhook-server-cert --from-file=tls.key=./oam-kubernetes-runtime-webhook.key --from-file=tls.crt=./oam-kubernetes-runtime-webhook.pem ``` - - Step 3: Get CA Bundle info and install with it's value + - Step 3: Get CA Bundle info and install with its value ```shell script caValue=`kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}'` diff --git a/pkg/oam/util/helper.go b/pkg/oam/util/helper.go index 3238a853..5c19f8df 100644 --- a/pkg/oam/util/helper.go +++ b/pkg/oam/util/helper.go @@ -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" ) const ( @@ -226,7 +229,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 . +// 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 annotations := u.GetAnnotations(); annotations != nil { + if crdName, ok := annotations[DefinitionAnnotation]; ok { + return crdName + } + } group, _ := APIVersion2GroupVersion(u.GetAPIVersion()) resources := []string{Kind2Resource(u.GetKind())} if group != "" { diff --git a/pkg/webhook/v1alpha2/applicationconfiguration/applicationconfiguration.go b/pkg/webhook/v1alpha2/applicationconfiguration/applicationconfiguration.go index 8dbcdd73..c2e7129d 100644 --- a/pkg/webhook/v1alpha2/applicationconfiguration/applicationconfiguration.go +++ b/pkg/webhook/v1alpha2/applicationconfiguration/applicationconfiguration.go @@ -136,7 +136,7 @@ func (h *ValidatingHandler) InjectDecoder(d *admission.Decoder) error { return nil } -// Register will regsiter application configuration validation to webhook +// Register will register application configuration validation to webhook func Register(mgr manager.Manager) { server := mgr.GetWebhookServer() server.Register("/validating-core-oam-dev-v1alpha2-applicationconfigurations", &webhook.Admission{Handler: &ValidatingHandler{}}) diff --git a/pkg/webhook/v1alpha2/component/mutating_handler.go b/pkg/webhook/v1alpha2/component/mutating_handler.go index 8068dd1c..c6b24e59 100644 --- a/pkg/webhook/v1alpha2/component/mutating_handler.go +++ b/pkg/webhook/v1alpha2/component/mutating_handler.go @@ -125,7 +125,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{util.DefinitionAnnotation: workloadType})) + mutatelog.Info("Set annotation definition.oam.dev/name for workload", "annotation value", workloadType) // copy back the object rawBye, err := json.Marshal(workload.Object) if err != nil { From b92d0d16bd6fe2879486445bfb5fdc308bc5a542 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Fri, 18 Sep 2020 14:40:12 +0800 Subject: [PATCH 2/5] Address comments and add unit-test Signed-off-by: zzxwill --- .gitignore | 8 +++++++- pkg/oam/types.go | 4 ++++ pkg/oam/util/helper.go | 5 +---- pkg/oam/util/helper_test.go | 13 +++++++++++++ pkg/webhook/v1alpha2/component/component_test.go | 4 +++- pkg/webhook/v1alpha2/component/mutating_handler.go | 4 +++- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index bf3a4c48..8300ca68 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,10 @@ bin/ tmp/ # Vscode files -.vscode \ No newline at end of file +.vscode + +# Webhook certificates +csr.conf +oam-kubernetes-runtime-webhook.csr +oam-kubernetes-runtime-webhook.key +oam-kubernetes-runtime-webhook.pem \ No newline at end of file diff --git a/pkg/oam/types.go b/pkg/oam/types.go index 456c96c2..72b725d5 100644 --- a/pkg/oam/types.go +++ b/pkg/oam/types.go @@ -27,6 +27,10 @@ import ( runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1" ) +// DefinitionAnnotation is one automatically generated annotation of workload and trait to indicate which +// workloadDefinition/traitDefinition it is generated by +const DefinitionAnnotation = "definition.oam.dev/name" + // ScopeKind contains the type metadata for a kind of an OAM scope resource. type ScopeKind schema.GroupVersionKind diff --git a/pkg/oam/util/helper.go b/pkg/oam/util/helper.go index 5c19f8df..3bc21dfa 100644 --- a/pkg/oam/util/helper.go +++ b/pkg/oam/util/helper.go @@ -39,9 +39,6 @@ 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" ) const ( @@ -232,7 +229,7 @@ func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject // 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 annotations := u.GetAnnotations(); annotations != nil { - if crdName, ok := annotations[DefinitionAnnotation]; ok { + if crdName, ok := annotations[oam.DefinitionAnnotation]; ok { return crdName } } diff --git a/pkg/oam/util/helper_test.go b/pkg/oam/util/helper_test.go index 5987f9ec..7f49afaa 100644 --- a/pkg/oam/util/helper_test.go +++ b/pkg/oam/util/helper_test.go @@ -715,6 +715,19 @@ var _ = Describe("Test unstructured related helper utils", func() { }}, exp: "simplerollouttraits.extend.oam.dev", }, + "extended resource with annotation": { + u: &unstructured.Unstructured{Object: map[string]interface{}{ + "apiVersion": "extend.oam.dev/v1alpha2", + "kind": "ContainerizedWorkload", + "metadata": map[string]interface{}{ + "annotations": map[string]interface{}{ + "definition.oam.dev/name": "web-service", + }, + }, + }, + }, + exp: "web-service", + }, } for name, ti := range tests { got := util.GetCRDName(ti.u) diff --git a/pkg/webhook/v1alpha2/component/component_test.go b/pkg/webhook/v1alpha2/component/component_test.go index 2f80435f..3594545a 100644 --- a/pkg/webhook/v1alpha2/component/component_test.go +++ b/pkg/webhook/v1alpha2/component/component_test.go @@ -3,6 +3,7 @@ package component_test import ( "context" "fmt" + "github.com/crossplane/oam-kubernetes-runtime/pkg/oam" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -121,6 +122,7 @@ var _ = Describe("Component Admission controller Test", func() { mutatedWorkload := baseWorkload.DeepCopy() mutatedWorkload.SetNamespace(component.GetNamespace()) mutatedWorkload.SetLabels(util.MergeMap(label, map[string]string{WorkloadTypeLabel: workloadTypeName})) + mutatedWorkload.SetAnnotations(map[string]string{oam.DefinitionAnnotation: ""}) tests := map[string]struct { client client.Client workload interface{} @@ -152,7 +154,7 @@ var _ = Describe("Component Admission controller Test", func() { workload: workloadWithType.DeepCopyObject(), errMsg: "does not exist", }, - "update gvk and label case": { + "update gvk, label and annotation case": { client: &test.MockClient{ MockGet: func(ctx context.Context, key types.NamespacedName, obj runtime.Object) error { switch o := obj.(type) { diff --git a/pkg/webhook/v1alpha2/component/mutating_handler.go b/pkg/webhook/v1alpha2/component/mutating_handler.go index c6b24e59..540f2961 100644 --- a/pkg/webhook/v1alpha2/component/mutating_handler.go +++ b/pkg/webhook/v1alpha2/component/mutating_handler.go @@ -22,6 +22,8 @@ import ( "fmt" "net/http" + "github.com/crossplane/oam-kubernetes-runtime/pkg/oam" + crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -126,7 +128,7 @@ func (h *MutatingHandler) Mutate(obj *v1alpha2.Component) error { workload.SetNamespace(obj.GetNamespace()) workload.SetLabels(util.MergeMap(obj.GetLabels(), map[string]string{WorkloadTypeLabel: workloadType})) // Add another annotation DefinitionAnnotation which can mark the name of WorkloadDefinition - workload.SetAnnotations(util.MergeMap(obj.GetAnnotations(), map[string]string{util.DefinitionAnnotation: workloadType})) + 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 back the object rawBye, err := json.Marshal(workload.Object) From a6c3e811e45d81fa20656faa7b8c301b4d762fe5 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Fri, 18 Sep 2020 14:45:10 +0800 Subject: [PATCH 3/5] Fix goimports issue Signed-off-by: zzxwill --- pkg/webhook/v1alpha2/component/component_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/webhook/v1alpha2/component/component_test.go b/pkg/webhook/v1alpha2/component/component_test.go index 3594545a..b7425459 100644 --- a/pkg/webhook/v1alpha2/component/component_test.go +++ b/pkg/webhook/v1alpha2/component/component_test.go @@ -3,6 +3,7 @@ package component_test import ( "context" "fmt" + "github.com/crossplane/oam-kubernetes-runtime/pkg/oam" . "github.com/onsi/ginkgo" From 8786b7e442610b040cbcf7cd856aa0f8807bde10 Mon Sep 17 00:00:00 2001 From: zzxwill Date: Mon, 21 Sep 2020 14:37:11 +0800 Subject: [PATCH 4/5] Change function name and add unit-test Signed-off-by: zzxwill --- .../applicationconfiguration/render_test.go | 4 ++-- .../containerizedworkload/translate_test.go | 20 +++++++++++++++++++ pkg/oam/util/helper.go | 14 ++++++------- pkg/oam/util/helper_test.go | 2 +- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/pkg/controller/v1alpha2/applicationconfiguration/render_test.go b/pkg/controller/v1alpha2/applicationconfiguration/render_test.go index 8fbe5569..e4aa9ab3 100644 --- a/pkg/controller/v1alpha2/applicationconfiguration/render_test.go +++ b/pkg/controller/v1alpha2/applicationconfiguration/render_test.go @@ -724,7 +724,7 @@ func TestRenderTraitWithoutMetadataName(t *testing.T) { } } -func TestGetCRDName(t *testing.T) { +func TestGetDefinitionName(t *testing.T) { tests := map[string]struct { u *unstructured.Unstructured exp string @@ -749,7 +749,7 @@ func TestGetCRDName(t *testing.T) { } for name, ti := range tests { t.Run(name, func(t *testing.T) { - got := util.GetCRDName(ti.u) + got := util.GetDefinitionName(ti.u) if got != ti.exp { t.Errorf("%s getCRDName want %s got %s ", ti.reason, ti.exp, got) } diff --git a/pkg/controller/v1alpha2/core/workloads/containerizedworkload/translate_test.go b/pkg/controller/v1alpha2/core/workloads/containerizedworkload/translate_test.go index 6d207cd5..25756409 100644 --- a/pkg/controller/v1alpha2/core/workloads/containerizedworkload/translate_test.go +++ b/pkg/controller/v1alpha2/core/workloads/containerizedworkload/translate_test.go @@ -388,6 +388,11 @@ func TestServiceInjector(t *testing.T) { err error } + invalideDeployment := &appsv1.StatefulSet{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1alpha1", + }} + cases := map[string]struct { reason string args args @@ -400,6 +405,21 @@ func TestServiceInjector(t *testing.T) { }, want: want{}, }, + "InvalidObject": { + reason: "invalid object should immediately return nil.", + args: args{ + w: &mock.Workload{}, + o: []oam.Object{ + invalideDeployment, + }, + }, + want: want{ + result: []oam.Object{ + invalideDeployment, + }, + err: nil, + }, + }, "SuccessfulInjectService_1D_1C_1P": { reason: "A Deployment with a port(s) should have a Service injected for first defined port.", args: args{ diff --git a/pkg/oam/util/helper.go b/pkg/oam/util/helper.go index 3bc21dfa..d33d2bcf 100644 --- a/pkg/oam/util/helper.go +++ b/pkg/oam/util/helper.go @@ -115,7 +115,7 @@ func FetchWorkload(ctx context.Context, c client.Client, mLog logr.Logger, oamTr func FetchScopeDefinition(ctx context.Context, r client.Reader, scope *unstructured.Unstructured) (*v1alpha2.ScopeDefinition, error) { // The name of the scopeDefinition CR is the CRD name of the scope - spName := GetCRDName(scope) + spName := GetDefinitionName(scope) // the scopeDefinition crd is cluster scoped nn := types.NamespacedName{Name: spName} // Fetch the corresponding scopeDefinition CR @@ -130,7 +130,7 @@ func FetchScopeDefinition(ctx context.Context, r client.Reader, func FetchTraitDefinition(ctx context.Context, r client.Reader, trait *unstructured.Unstructured) (*v1alpha2.TraitDefinition, error) { // The name of the traitDefinition CR is the CRD name of the trait - trName := GetCRDName(trait) + trName := GetDefinitionName(trait) // the traitDefinition crd is cluster scoped nn := types.NamespacedName{Name: trName} // Fetch the corresponding traitDefinition CR @@ -145,7 +145,7 @@ func FetchTraitDefinition(ctx context.Context, r client.Reader, func FetchWorkloadDefinition(ctx context.Context, r client.Reader, workload *unstructured.Unstructured) (*v1alpha2.WorkloadDefinition, error) { // The name of the workloadDefinition CR is the CRD name of the component - wldName := GetCRDName(workload) + wldName := GetDefinitionName(workload) // the workloadDefinition crd is cluster scoped nn := types.NamespacedName{Name: wldName} // Fetch the corresponding workloadDefinition CR @@ -224,10 +224,10 @@ func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject childObj.SetAnnotations(MergeMap(parentObj.GetAnnotations(), childObj.GetAnnotations())) } -// GetCRDName return the CRD name of any resources -// the format of the CRD of a resource is . -// 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 { +// GetDefinitionName return the Definition name of any resources +// the format of the definition of a resource is . +// 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 { return crdName diff --git a/pkg/oam/util/helper_test.go b/pkg/oam/util/helper_test.go index 7f49afaa..9d6cb1bd 100644 --- a/pkg/oam/util/helper_test.go +++ b/pkg/oam/util/helper_test.go @@ -730,7 +730,7 @@ var _ = Describe("Test unstructured related helper utils", func() { }, } for name, ti := range tests { - got := util.GetCRDName(ti.u) + got := util.GetDefinitionName(ti.u) By(fmt.Sprint("Running test: ", name)) Expect(ti.exp).Should(Equal(got)) } From 94b0a8e17c13f9356f61a9093624abecbd6008bb Mon Sep 17 00:00:00 2001 From: zzxwill Date: Mon, 21 Sep 2020 21:28:51 +0800 Subject: [PATCH 5/5] Removed maker annotation `definition.oam.dev/name` Signed-off-by: zzxwill zzxwill@gmail.com Signed-off-by: zzxwill --- .../containerizedworkload/translate_test.go | 6 +++--- pkg/oam/types.go | 5 ++--- pkg/oam/util/helper.go | 8 +++++--- pkg/oam/util/helper_test.go | 13 ------------- pkg/webhook/v1alpha2/component/component_test.go | 8 +++----- pkg/webhook/v1alpha2/component/mutating_handler.go | 12 +++--------- 6 files changed, 16 insertions(+), 36 deletions(-) diff --git a/pkg/controller/v1alpha2/core/workloads/containerizedworkload/translate_test.go b/pkg/controller/v1alpha2/core/workloads/containerizedworkload/translate_test.go index 25756409..d5ee0324 100644 --- a/pkg/controller/v1alpha2/core/workloads/containerizedworkload/translate_test.go +++ b/pkg/controller/v1alpha2/core/workloads/containerizedworkload/translate_test.go @@ -388,7 +388,7 @@ func TestServiceInjector(t *testing.T) { err error } - invalideDeployment := &appsv1.StatefulSet{ + invalidDeployment := &appsv1.StatefulSet{ TypeMeta: metav1.TypeMeta{ APIVersion: "v1alpha1", }} @@ -410,12 +410,12 @@ func TestServiceInjector(t *testing.T) { args: args{ w: &mock.Workload{}, o: []oam.Object{ - invalideDeployment, + invalidDeployment, }, }, want: want{ result: []oam.Object{ - invalideDeployment, + invalidDeployment, }, err: nil, }, diff --git a/pkg/oam/types.go b/pkg/oam/types.go index 72b725d5..5cc3be19 100644 --- a/pkg/oam/types.go +++ b/pkg/oam/types.go @@ -27,9 +27,8 @@ import ( runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1" ) -// DefinitionAnnotation is one automatically generated annotation of workload and trait to indicate which -// workloadDefinition/traitDefinition it is generated by -const DefinitionAnnotation = "definition.oam.dev/name" +// WorkloadTypeLabel indicates the type of the workloadDefinition +const WorkloadTypeLabel = "workload.oam.dev/type" // ScopeKind contains the type metadata for a kind of an OAM scope resource. type ScopeKind schema.GroupVersionKind diff --git a/pkg/oam/util/helper.go b/pkg/oam/util/helper.go index d33d2bcf..4fa18fae 100644 --- a/pkg/oam/util/helper.go +++ b/pkg/oam/util/helper.go @@ -228,9 +228,11 @@ func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject // the format of the definition of a resource is . // 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 { - return crdName + if labels := u.GetLabels(); labels != nil { + if resourceType, ok := labels[oam.LabelOAMResourceType]; ok && resourceType == "WORKLOAD" { + if definitionName, ok := labels[oam.WorkloadTypeLabel]; ok { + return definitionName + } } } group, _ := APIVersion2GroupVersion(u.GetAPIVersion()) diff --git a/pkg/oam/util/helper_test.go b/pkg/oam/util/helper_test.go index 9d6cb1bd..2703e20f 100644 --- a/pkg/oam/util/helper_test.go +++ b/pkg/oam/util/helper_test.go @@ -715,19 +715,6 @@ var _ = Describe("Test unstructured related helper utils", func() { }}, exp: "simplerollouttraits.extend.oam.dev", }, - "extended resource with annotation": { - u: &unstructured.Unstructured{Object: map[string]interface{}{ - "apiVersion": "extend.oam.dev/v1alpha2", - "kind": "ContainerizedWorkload", - "metadata": map[string]interface{}{ - "annotations": map[string]interface{}{ - "definition.oam.dev/name": "web-service", - }, - }, - }, - }, - exp: "web-service", - }, } for name, ti := range tests { got := util.GetDefinitionName(ti.u) diff --git a/pkg/webhook/v1alpha2/component/component_test.go b/pkg/webhook/v1alpha2/component/component_test.go index b7425459..461dad1a 100644 --- a/pkg/webhook/v1alpha2/component/component_test.go +++ b/pkg/webhook/v1alpha2/component/component_test.go @@ -4,8 +4,6 @@ import ( "context" "fmt" - "github.com/crossplane/oam-kubernetes-runtime/pkg/oam" - . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" @@ -22,6 +20,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2" + "github.com/crossplane/oam-kubernetes-runtime/pkg/oam" "github.com/crossplane/oam-kubernetes-runtime/pkg/oam/util" . "github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component" ) @@ -122,8 +121,7 @@ var _ = Describe("Component Admission controller Test", func() { // set up the result mutatedWorkload := baseWorkload.DeepCopy() mutatedWorkload.SetNamespace(component.GetNamespace()) - mutatedWorkload.SetLabels(util.MergeMap(label, map[string]string{WorkloadTypeLabel: workloadTypeName})) - mutatedWorkload.SetAnnotations(map[string]string{oam.DefinitionAnnotation: ""}) + mutatedWorkload.SetLabels(util.MergeMap(label, map[string]string{oam.WorkloadTypeLabel: workloadTypeName})) tests := map[string]struct { client client.Client workload interface{} @@ -155,7 +153,7 @@ var _ = Describe("Component Admission controller Test", func() { workload: workloadWithType.DeepCopyObject(), errMsg: "does not exist", }, - "update gvk, label and annotation case": { + "update gvk and label case": { client: &test.MockClient{ MockGet: func(ctx context.Context, key types.NamespacedName, obj runtime.Object) error { switch o := obj.(type) { diff --git a/pkg/webhook/v1alpha2/component/mutating_handler.go b/pkg/webhook/v1alpha2/component/mutating_handler.go index 540f2961..6b853552 100644 --- a/pkg/webhook/v1alpha2/component/mutating_handler.go +++ b/pkg/webhook/v1alpha2/component/mutating_handler.go @@ -22,8 +22,6 @@ import ( "fmt" "net/http" - "github.com/crossplane/oam-kubernetes-runtime/pkg/oam" - crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -36,15 +34,13 @@ import ( "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2" + "github.com/crossplane/oam-kubernetes-runtime/pkg/oam" "github.com/crossplane/oam-kubernetes-runtime/pkg/oam/util" ) const ( // TypeField is the special field indicate the type of the workloadDefinition TypeField = "type" - - // WorkloadTypeLabel indicates the type of the workloadDefinition - WorkloadTypeLabel = "workload.oam.dev/type" ) // MutatingHandler handles Component @@ -126,10 +122,8 @@ func (h *MutatingHandler) Mutate(obj *v1alpha2.Component) error { mutatelog.Info("Set the component workload GVK", "workload api version", workload.GetAPIVersion(), "workload Kind", workload.GetKind()) // 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})) - // 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) + workload.SetLabels(util.MergeMap(obj.GetLabels(), map[string]string{oam.WorkloadTypeLabel: workloadType})) + workload.SetAnnotations(obj.GetAnnotations()) // copy back the object rawBye, err := json.Marshal(workload.Object) if err != nil {