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 format of trait (
Browse files Browse the repository at this point in the history
…#242)

* add mutating for appConfig to support name/properties fomart of trait

Signed-off-by: 天元 <jianbo.sjb@alibaba-inc.com>

* add example for webhook

Signed-off-by: 天元 <jianbo.sjb@alibaba-inc.com>
  • Loading branch information
wonderflow authored Oct 12, 2020
1 parent c6573e6 commit d1bbf9f
Show file tree
Hide file tree
Showing 15 changed files with 775 additions and 30 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
23 changes: 23 additions & 0 deletions examples/webhook-enabled/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: core.oam.dev/v1alpha2
kind: Component
metadata:
name: foo
spec:
workload:
type: fooworkload
spec:
key: workload

---
apiVersion: core.oam.dev/v1alpha2
kind: ApplicationConfiguration
metadata:
name: example-appconfig
spec:
components:
- componentName: foo
traits:
- trait:
name: footrait
properties:
key: trait
57 changes: 57 additions & 0 deletions examples/webhook-enabled/definition.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: foo.example.com
spec:
group: example.com
names:
kind: Foo
listKind: FooList
plural: foo
singular: foo
scope: Namespaced
version: v1
preserveUnknownFields: true
---
apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
metadata:
name: fooworkload
spec:
definitionRef:
name: foo.example.com
---
apiVersion: core.oam.dev/v1alpha2
kind: TraitDefinition
metadata:
name: footrait
spec:
definitionRef:
name: foo.example.com

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: foo:system:aggregate-to-controller
labels:
rbac.oam.dev/aggregate-to-controller: "true"
rules:
- apiGroups:
- example.com
resources:
- "*"
verbs:
- "*"
- apiGroups:
- apps
resources:
- controllerrevisions
verbs:
- "*"
- apiGroups:
- apiextensions.k8s.io
resources:
- customresourcedefinitions
verbs:
- "*"
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 d1bbf9f

Please sign in to comment.