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

Commit

Permalink
add mutating for appConfig to support name/properties fomart of trait
Browse files Browse the repository at this point in the history
Signed-off-by: 天元 <jianbo.sjb@alibaba-inc.com>
  • Loading branch information
wonderflow committed Oct 10, 2020
1 parent 763a9ef commit 7fa5a34
Show file tree
Hide file tree
Showing 14 changed files with 711 additions and 41 deletions.
16 changes: 16 additions & 0 deletions charts/oam-kubernetes-runtime/templates/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ metadata:
labels:
{{- include "oam-kubernetes-runtime.selectorLabels" . | nindent 4 }}
webhooks:
- name: "mutate.applicationconfigurations.core.oam.dev"
clientConfig:
service:
name: {{ template "oam-kubernetes-runtime.name" . }}-webhook
namespace: {{.Release.Namespace}}
path: /mutating-core-oam-dev-v1alpha2-applicationconfigurations
caBundle: "{{.Values.certificate.caBundle}}"
rules:
- apiGroups: ["core.oam.dev"]
apiVersions: ["v1alpha2"]
operations: ["CREATE", "UPDATE"]
resources: ["applicationconfigurations"]
scope: "Namespaced"
admissionReviewVersions: ["v1beta1"]
failurePolicy: Fail
timeoutSeconds: 5
- name: "mutate.component.core.oam.dev"
clientConfig:
service:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ func TestGetDefinitionName(t *testing.T) {
}
for name, ti := range tests {
t.Run(name, func(t *testing.T) {
got := util.GetDefinitionName(ti.u)
got := util.GetDefinitionName(ti.u, "")
if got != ti.exp {
t.Errorf("%s getCRDName want %s got %s ", ti.reason, ti.exp, got)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/oam/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const (
LabelAppComponentRevision = "app.oam.dev/revision"
// LabelOAMResourceType whether a CR is workload or trait
LabelOAMResourceType = "app.oam.dev/resourceType"

// WorkloadTypeLabel indicates the type of the workloadDefinition
WorkloadTypeLabel = "workload.oam.dev/type"
// TraitTypeLabel indicates the type of the traitDefinition
TraitTypeLabel = "trait.oam.dev/type"
)

const (
Expand Down
3 changes: 0 additions & 3 deletions pkg/oam/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ import (
runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
)

// 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

Expand Down
16 changes: 9 additions & 7 deletions pkg/oam/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ 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 := GetDefinitionName(scope)
// TODO(wonderflow): we haven't support scope definition label type yet.
spName := GetDefinitionName(scope, "")
// the scopeDefinition crd is cluster scoped
nn := types.NamespacedName{Name: spName}
// Fetch the corresponding scopeDefinition CR
Expand All @@ -130,7 +131,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 := GetDefinitionName(trait)
trName := GetDefinitionName(trait, oam.TraitTypeLabel)
// the traitDefinition crd is cluster scoped
nn := types.NamespacedName{Name: trName}
// Fetch the corresponding traitDefinition CR
Expand All @@ -145,7 +146,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 := GetDefinitionName(workload)
wldName := GetDefinitionName(workload, oam.WorkloadTypeLabel)
// the workloadDefinition crd is cluster scoped
nn := types.NamespacedName{Name: wldName}
// Fetch the corresponding workloadDefinition CR
Expand Down Expand Up @@ -227,10 +228,11 @@ func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject
// GetDefinitionName return the Definition name of any resources
// 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.GetLabels(); labels != nil {
if resourceType, ok := labels[oam.LabelOAMResourceType]; ok && resourceType == "WORKLOAD" {
if definitionName, ok := labels[oam.WorkloadTypeLabel]; ok {
// typeLabel specified which Definition it is, if specified, will directly get definition from label.
func GetDefinitionName(u *unstructured.Unstructured, typeLabel string) string {
if typeLabel != "" {
if labels := u.GetLabels(); labels != nil {
if definitionName, ok := labels[typeLabel]; ok {
return definitionName
}
}
Expand Down
35 changes: 31 additions & 4 deletions pkg/oam/util/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,9 @@ var _ = Describe("Test workload related helper utils", func() {
var _ = Describe("Test unstructured related helper utils", func() {
It("Test get CRD name from an unstructured object", func() {
tests := map[string]struct {
u *unstructured.Unstructured
exp string
u *unstructured.Unstructured
typeLabel string
exp string
}{
"native resource": {
u: &unstructured.Unstructured{Object: map[string]interface{}{
Expand All @@ -715,11 +716,37 @@ var _ = Describe("Test unstructured related helper utils", func() {
}},
exp: "simplerollouttraits.extend.oam.dev",
},
"trait": {
u: &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": "extend.oam.dev/v1alpha2",
"kind": "SimpleRolloutTrait",
"metadata": map[string]interface{}{
"labels": map[string]interface{}{
oam.TraitTypeLabel: "rollout",
},
},
}},
typeLabel: oam.TraitTypeLabel,
exp: "rollout",
},
"workload": {
u: &unstructured.Unstructured{Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": map[string]interface{}{
"labels": map[string]interface{}{
oam.WorkloadTypeLabel: "deploy",
},
},
}},
typeLabel: oam.WorkloadTypeLabel,
exp: "deploy",
},
}
for name, ti := range tests {
got := util.GetDefinitionName(ti.u)
got := util.GetDefinitionName(ti.u, ti.typeLabel)
By(fmt.Sprint("Running test: ", name))
Expect(ti.exp).Should(Equal(got))
Expect(got).Should(Equal(ti.exp))
}
})
})
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/v1alpha2/admit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

// Add will be called in main and register all validation handlers
func Add(mgr manager.Manager) {
applicationconfiguration.Register(mgr)
applicationconfiguration.RegisterValidatingHandler(mgr)
applicationconfiguration.RegisterMutatingHandler(mgr)
component.RegisterMutatingHandler(mgr)
component.RegisterValidatingHandler(mgr)
}
Loading

0 comments on commit 7fa5a34

Please sign in to comment.