Skip to content

Commit

Permalink
Add a more comprehensive example
Browse files Browse the repository at this point in the history
The new example includes apps.Deployment, batch.Job, Service, ConfigMap
and a custom CRD.
  • Loading branch information
nan-yu committed Feb 12, 2020
1 parent 06383a8 commit ee5bed6
Show file tree
Hide file tree
Showing 3 changed files with 279 additions and 0 deletions.
123 changes: 123 additions & 0 deletions docs/examples/test-app/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
apiVersion: app.k8s.io/v1beta1
kind: Application
metadata:
name: "test-application-01"
labels:
app.kubernetes.io/name: "test-01"
spec:
type: "test"
selector:
matchLabels:
app.kubernetes.io/name: "test-01"
componentKinds:
- group: apps
kind: Deployment
- group: batch
kind: Job
- group: v1
kind: Service
- group: v1
kind: ConfigMap
- group: test.crd.com
kind: TestCRD
addOwnerRef: true
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-hello
labels:
app.kubernetes.io/name: "test-01"
app.kubernetes.io/version: "3"
app.kubernetes.io/component: "hello-world"
app.kubernetes.io/tier: "backend"
spec:
selector:
matchLabels:
app.kubernetes.io/name: "test-01"
app.kubernetes.io/component: "hello-world"
app.kubernetes.io/tier: "backend"
replicas: 1
template:
metadata:
labels:
app.kubernetes.io/name: "test-01"
app.kubernetes.io/component: "hello-world"
app.kubernetes.io/tier: "backend"
spec:
containers:
- image: gcr.io/google-samples/node-hello:1.0
name: hello-world
ports:
- containerPort: 8080
protocol: TCP
---
apiVersion: batch/v1
kind: Job
metadata:
name: pi
labels:
app.kubernetes.io/name: "test-01"
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
---
apiVersion: v1
kind: Service
metadata:
name: test-webserver-svc
labels:
app.kubernetes.io/name: "test-01"
app.kubernetes.io/version: "3"
app.kubernetes.io/component: "test-svc"
app.kubernetes.io/tier: "frontend"
spec:
ports:
- port: 80
selector:
app.kubernetes.io/name: "test-01"
app.kubernetes.io/component: "test-webserver"
type: LoadBalancer
---
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
labels:
app.kubernetes.io/name: "test-01"
data:
data-1: value-1
data-2: value-2
---
apiVersion: test.crd.com/v1
kind: TestCRD
metadata:
name: testcrd-sample
labels:
app.kubernetes.io/name: "test-01"
spec:
foo: bar
---
apiVersion: test.crd.com/v1
kind: TestCRD
metadata:
name: testcrd-sample-2
labels:
app.kubernetes.io/name: "test-01"
spec:
foo: bar
---
apiVersion: test.crd.com/v1
kind: TestCRD
metadata:
name: testcrd-sample-3
labels:
app.kubernetes.io/name: "test-01"
spec:
foo: bar
100 changes: 100 additions & 0 deletions docs/examples/test-app/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env bash
# Copyright 2020 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0

set -o errexit
set -o nounset
set -o pipefail

source ../../../hack/tools/common.sh

# Cleanup on exit
function cleanup() {
header_text "Cleanup"
# kubectl delete -f application.yaml
# kubectl delete -f test_crd.yaml
# make undeploy
}

# Check if component's ownerReferences is not found
function ownerReferencesNotFound() {
if [[ "$1" == *"kind: Application"* ]] && [[ "$1" == *"name: test-application-01"* ]]; then
echo "$2 ownerReferences updated"
return 1
else
echo "$2 ownerReferences was not updated"
return 0
fi
}

header_text "Runn test on a Kubernetes cluster"

header_text "Deploy the application CRD with the controller"
#make deploy

header_text "Install the test CRD"
crd_output=$(kubectl apply -f test_crd.yaml)
if [ "$crd_output" != "customresourcedefinition.apiextensions.k8s.io/testcrds.test.crd.com created" ] &&
[ "$crd_output" != "customresourcedefinition.apiextensions.k8s.io/testcrds.test.crd.com configured" ]; then
echo "failed to install test CRD"
echo "$crd_output"
cleanup
exit 1
else
echo "$crd_output"
fi

header_text "Create the application object with all compoents"
app_output=$(kubectl apply -f application.yaml)
if [[ "$app_output" != *"application.app.k8s.io/test-application-01 created"* ]] &&
[[ "$app_output" != *"application.app.k8s.io/test-application-01 configured"* ]] &&
[[ "$app_output" != *"application.app.k8s.io/test-application-01 unchanged"* ]]; then
echo "failed to install application"
echo "$app_output"
cleanup
exit 1
else
echo "$app_output"
fi

header_text "Verify application's status/components shows all components"
# Timeout waiting after 150 seconds
total=150
counter=$total
updated=false
while [ $counter -gt 0 ]; do
app=$(kubectl get application test-application-01 -o yaml)
if [[ "$app" == *"/apis/apps/v1/namespaces/default/deployments/test-hello"* ]] &&
[[ "$app" == *"/apis/batch/v1/namespaces/default/jobs/pi"* ]] &&
[[ "$app" == *"/api/v1/namespaces/default/services/test-webserver-svc"* ]] &&
[[ "$app" == *"/api/v1/namespaces/default/configmaps/test-configmap"* ]] &&
[[ "$app" == *"/apis/test.crd.com/v1/namespaces/default/testcrds/testcrd-sample"* ]] &&
[[ "$app" == *"/apis/test.crd.com/v1/namespaces/default/testcrds/testcrd-sample-2"* ]]; then
echo "All components are appended to application status"
counter=0
updated=true
else
echo "Still waiting for controller to reconcile... ($counter/$total)"
sleep 1
((counter--))
fi
done

if [ "$updated" == "false" ]; then
echo "Timeout waiting for application status to be updated"
cleanup
exit 1
fi

header_text "Verify the application is added to all components' ownerReferences"
if ownerReferencesNotFound "$(kubectl get deployment test-hello -o yaml)" "deployment/test-hello" ||
ownerReferencesNotFound "$(kubectl get job pi -o yaml)" "job/pi" ||
ownerReferencesNotFound "$(kubectl get service test-webserver-svc -o yaml)" "service/test-webserver-svc" ||
ownerReferencesNotFound "$(kubectl get configmap test-configmap -o yaml)" "configmap/test-configmap" ||
ownerReferencesNotFound "$(kubectl get testcrd testcrd-sample -o yaml)" "testcrd/testcrd-sample" ||
ownerReferencesNotFound "$(kubectl get testcrd testcrd-sample-2 -o yaml)" "testcrd/testcrd-sample-2"; then
cleanup
exit 1
fi

trap cleanup EXIT
56 changes: 56 additions & 0 deletions docs/examples/test-app/test_crd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.2.4
creationTimestamp: null
name: testcrds.test.crd.com
spec:
group: test.crd.com
names:
kind: TestCRD
listKind: TestCRDList
plural: testcrds
singular: testcrd
scope: Namespaced
validation:
openAPIV3Schema:
description: TestCRD is the Schema for the testcrds API
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: TestCRDSpec defines the desired state of TestCRD
properties:
foo:
description: Foo is an example field of TestCRD. Edit TestCRD_types.go
to remove/update
type: string
type: object
status:
description: TestCRDStatus defines the observed state of TestCRD
type: object
type: object
version: v1
versions:
- name: v1
served: true
storage: true
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []

0 comments on commit ee5bed6

Please sign in to comment.