[go: nahoru, domu]

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

Commit

Permalink
add component webhook to support workload definition type
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Zhang <yangzhangrice@hotmail.com>
  • Loading branch information
ryanzhang-oss committed Sep 10, 2020
1 parent 0e6d12c commit 41eb0c6
Show file tree
Hide file tree
Showing 17 changed files with 777 additions and 12 deletions.
5 changes: 3 additions & 2 deletions apis/core/v1alpha2/core_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ type ComponentParameter struct {
// A ComponentSpec defines the desired state of a Component.
type ComponentSpec struct {
// A Workload that will be created for each ApplicationConfiguration that
// includes this Component. Workloads must be defined by a
// WorkloadDefinition.
// includes this Component. Workload is an instance of a workloadDefinition.
// We either use the GVK info or a special "type" field in the workload to associate
// the content of the workload with its workloadDefinition
// +kubebuilder:validation:EmbeddedResource
// +kubebuilder:pruning:PreserveUnknownFields
Workload runtime.RawExtension `json:"workload"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ spec:
type: array
workload:
description: A Workload that will be created for each ApplicationConfiguration
that includes this Component. Workloads must be defined by a WorkloadDefinition.
that includes this Component. Workload is an instance of a workloadDefinition.
We either use the GVK info or a special "type" field in the workload
to associate the content of the workload with its workloadDefinition
type: object
x-kubernetes-embedded-resource: true
x-kubernetes-preserve-unknown-fields: true
Expand Down
44 changes: 42 additions & 2 deletions charts/oam-kubernetes-runtime/templates/webhook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,58 @@ webhooks:
rules:
- apiGroups: ["core.oam.dev"]
apiVersions: ["v1alpha2"]
operations: ["CREATE", "UPDATE","DELETE"]
operations: ["CREATE", "UPDATE"]
resources: ["applicationconfigurations"]
scope: "Namespaced"
clientConfig:
service:
namespace: {{.Release.Namespace}}
name: {{ template "oam-kubernetes-runtime.name" . }}-webhook
path: /validating-applicationconfigurations
path: /validating-core-oam-dev-v1alpha2-applicationconfigurations
caBundle: "{{.Values.certificate.caBundle}}"
admissionReviewVersions: ["v1beta1"]
failurePolicy: Fail
timeoutSeconds: 5
- name: "validate.component.core.oam.dev"
clientConfig:
service:
name: {{ template "oam-kubernetes-runtime.name" . }}-webhook
namespace: {{.Release.Namespace}}
path: /validating-core-oam-dev-v1alpha2-components
caBundle: "{{.Values.certificate.caBundle}}"
rules:
- apiGroups: ["core.oam.dev"]
apiVersions: ["v1alpha2"]
operations: ["CREATE", "UPDATE"]
resources: ["components"]
scope: "Namespaced"
admissionReviewVersions: ["v1beta1"]
failurePolicy: Fail
timeoutSeconds: 5
---
apiVersion: admissionregistration.k8s.io/v1beta1
kind: MutatingWebhookConfiguration
metadata:
name: {{ include "oam-kubernetes-runtime.fullname" . }}
labels:
{{- include "oam-kubernetes-runtime.selectorLabels" . | nindent 4 }}
webhooks:
- name: "mutate.component.core.oam.dev"
clientConfig:
service:
name: {{ template "oam-kubernetes-runtime.name" . }}-webhook
namespace: {{.Release.Namespace}}
path: /mutating-core-oam-dev-v1alpha2-components
caBundle: "{{.Values.certificate.caBundle}}"
rules:
- apiGroups: ["core.oam.dev"]
apiVersions: ["v1alpha2"]
operations: ["CREATE", "UPDATE"]
resources: ["components"]
scope: "Namespaced"
admissionReviewVersions: ["v1beta1"]
failurePolicy: Fail
timeoutSeconds: 5
---
apiVersion: v1
kind: Service
Expand Down
2 changes: 1 addition & 1 deletion charts/oam-kubernetes-runtime/values.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Declare variables to be passed into your templates.

replicaCount: 1
useWebhook: false
useWebhook: true
image:
repository: crossplane/oam-kubernetes-runtime
tag: %%VERSION%%
Expand Down
2 changes: 2 additions & 0 deletions cmd/oam-kubernetes-runtime/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/crossplane/crossplane-runtime/pkg/logging"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -25,6 +26,7 @@ var scheme = runtime.NewScheme()
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = core.AddToScheme(scheme)
_ = crdv1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}

Expand Down
12 changes: 12 additions & 0 deletions examples/typed-component/definitions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: core.oam.dev/v1alpha2
kind: WorkloadDefinition
metadata:
name: web-service
spec:
definitionRef:
name: containerizedworkloads.core.oam.dev
childResourceKinds:
- apiVersion: apps/v1
kind: Deployment
- apiVersion: v1
kind: Service
30 changes: 30 additions & 0 deletions examples/typed-component/sample_application_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: core.oam.dev/v1alpha2
kind: HealthScope
metadata:
name: example-health-scope
spec:
workloadRefs: []
---
apiVersion: core.oam.dev/v1alpha2
kind: ApplicationConfiguration
metadata:
name: example-appconfig
spec:
components:
- componentName: web-service-component
parameterValues:
- name: image
value: wordpress:php7.2
traits:
- trait:
apiVersion: core.oam.dev/v1alpha2
kind: ManualScalerTrait
metadata:
name: example-appconfig-trait
spec:
replicaCount: 3
scopes:
- scopeRef:
apiVersion: core.oam.dev/v1alpha2
kind: HealthScope
name: example-health-scope
21 changes: 21 additions & 0 deletions examples/typed-component/sample_component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: core.oam.dev/v1alpha2
kind: Component
metadata:
name: web-service-component
spec:
workload:
type: web-service
spec:
containers:
- name: wordpress
image: wordpress:4.6.1-apache
ports:
- containerPort: 80
name: wordpress
env:
- name: TEST_ENV
value: test
parameters:
- name: image
fieldPaths:
- spec.containers[0].image
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ require (
k8s.io/klog v1.0.0
k8s.io/kube-openapi v0.0.0-20200410145947-61e04a5be9a6
k8s.io/kubectl v0.18.5
k8s.io/utils v0.0.0-20200324210504-a9aa75ae1b89
sigs.k8s.io/controller-runtime v0.6.0
sigs.k8s.io/controller-tools v0.2.4
)
9 changes: 5 additions & 4 deletions pkg/oam/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ type labelAnnotationObject interface {
// PassLabelAndAnnotation passes through labels and annotation objectMeta from the parent to the child object
func PassLabelAndAnnotation(parentObj oam.Object, childObj labelAnnotationObject) {
// pass app-config labels
childObj.SetLabels(mergeMap(parentObj.GetLabels(), childObj.GetLabels()))
childObj.SetLabels(MergeMap(parentObj.GetLabels(), childObj.GetLabels()))
// pass app-config annotation
childObj.SetAnnotations(mergeMap(parentObj.GetAnnotations(), childObj.GetAnnotations()))
childObj.SetAnnotations(MergeMap(parentObj.GetAnnotations(), childObj.GetAnnotations()))
}

// GetCRDName return the CRD name of any resources
Expand Down Expand Up @@ -344,10 +344,11 @@ func UnpackRevisionData(rev *appsv1.ControllerRevision) (*v1alpha2.Component, er

// AddLabels will merge labels with existing labels
func AddLabels(o *unstructured.Unstructured, labels map[string]string) {
o.SetLabels(mergeMap(o.GetLabels(), labels))
o.SetLabels(MergeMap(o.GetLabels(), labels))
}

func mergeMap(src, dst map[string]string) map[string]string {
//MergeMap merges two could be nil maps
func MergeMap(src, dst map[string]string) map[string]string {
if len(src) == 0 {
return dst
}
Expand Down
10 changes: 9 additions & 1 deletion pkg/oam/util/test_utils.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package util

import (
"encoding/json"

"github.com/onsi/gomega/format"
"github.com/onsi/gomega/types"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

// AlreadyExistMatcher matches the error to be already exist
// JSONMarshal returns the JSON encoding
func JSONMarshal(o interface{}) []byte {
j, _ := json.Marshal(o)
return j
}

//AlreadyExistMatcher matches the error to be already exist
type AlreadyExistMatcher struct {
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/webhook/v1alpha2/admit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package v1alpha2

import (
"github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/applicationconfiguration"
"github.com/crossplane/oam-kubernetes-runtime/pkg/webhook/v1alpha2/component"

"sigs.k8s.io/controller-runtime/pkg/manager"
)

// Add will be called in main and register all validation handlers
func Add(mgr manager.Manager) {
applicationconfiguration.Register(mgr)
component.RegisterMutatingHandler(mgr)
component.RegisterValidatingHandler(mgr)
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,5 @@ func (h *ValidatingHandler) InjectDecoder(d *admission.Decoder) error {
// Register will regsiter application configuration validation to webhook
func Register(mgr manager.Manager) {
server := mgr.GetWebhookServer()
server.Register("/validating-applicationconfigurations", &webhook.Admission{Handler: &ValidatingHandler{}})
server.Register("/validating-core-oam-dev-v1alpha2-applicationconfigurations", &webhook.Admission{Handler: &ValidatingHandler{}})
}
86 changes: 86 additions & 0 deletions pkg/webhook/v1alpha2/component/component_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package component_test

import (
"os"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/crossplane/oam-kubernetes-runtime/apis/core"
)

var scheme = runtime.NewScheme()
var crd crdv1.CustomResourceDefinition
var reqResource metav1.GroupVersionResource
var decoder *admission.Decoder

func TestComponentWebHandler(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Component Web handler")
}

var _ = BeforeSuite(func(done Done) {
By("Bootstrapping test environment")
ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
o.DestWritter = os.Stdout
}))
By("Setup scheme")
err := core.AddToScheme(scheme)
Expect(err).Should(BeNil())
err = clientgoscheme.AddToScheme(scheme)
Expect(err).Should(BeNil())
// the crd we will refer to
crd = crdv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{
Name: "foo.example.com",
Labels: map[string]string{"crd": "dependency"},
},
Spec: crdv1.CustomResourceDefinitionSpec{
Group: "example.com",
Names: crdv1.CustomResourceDefinitionNames{
Kind: "Foo",
ListKind: "FooList",
Plural: "foo",
Singular: "foo",
},
Versions: []crdv1.CustomResourceDefinitionVersion{
{
Name: "v1",
Served: true,
Storage: true,
Schema: &crdv1.CustomResourceValidation{
OpenAPIV3Schema: &crdv1.JSONSchemaProps{
Type: "object",
Properties: map[string]crdv1.JSONSchemaProps{
"status": {
Type: "object",
Properties: map[string]crdv1.JSONSchemaProps{
"key": {Type: "string"},
},
},
},
},
},
},
},
Scope: crdv1.NamespaceScoped,
},
}
By("Prepare for the admission resource")
reqResource = metav1.GroupVersionResource{Group: "core.oam.dev", Version: "v1alpha2", Resource: "components"}
By("Prepare for the admission decoder")
decoder, err = admission.NewDecoder(scheme)
Expect(err).Should(BeNil())
By("Finished test bootstrap")
close(done)
})
Loading

0 comments on commit 41eb0c6

Please sign in to comment.