Skip to content

Commit

Permalink
Fix could not find WorkloadDefinition issue
Browse files Browse the repository at this point in the history
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 <wonderflow.sun@gmail.com>
Signed-off-by: zzxwill <zzxwill@gmail.com>
  • Loading branch information
zzxwill and wonderflow committed Sep 18, 2020
1 parent a760d93 commit b536e68
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,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}'`
Expand Down
9 changes: 9 additions & 0 deletions pkg/oam/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 <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 {
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 != "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{}})
Expand Down
4 changes: 3 additions & 1 deletion pkg/webhook/v1alpha2/component/mutating_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b536e68

Please sign in to comment.