-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[usecase, gctl]: showcase install/un-install of CRD (#28)
This commit showcases how GenericController can be used to install and uninstall a CRD when a particular namespace gets created and deleted correspondingly. This commit can be understood in two ways: 1/ Files in test/integration/generic/ shows how golang based integration code can be written to verify this use-case. 2/ Files in examples/gctl/install-uninstall-crd/ shows how metac controller & jsonnet files are used to verify this use-case. NOTES: 1/ There are some changes to vendoring as well. Latest k8s.io dependencies are set. 2/ Integration test case has been set to run only if test is not run in short mode. Signed-off-by: AmitKumarDas <[email protected]>
- Loading branch information
Amit Kumar Das
authored
Oct 9, 2019
1 parent
5091d52
commit 41435e8
Showing
18 changed files
with
783 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
## Install-Uninstall-CRD | ||
|
||
This is an example of GenericController that adds a CRD for a given Namespace. | ||
This also removes the CRD when the given Namespace is deleted. | ||
|
||
### Prerequisites | ||
|
||
* Kubernetes 1.8+ is recommended for its improved CRD support, especially garbage collection. | ||
* Install Metac using yamls from manifests folder | ||
|
||
```sh | ||
# cd to metac's root folder | ||
|
||
kubectl apply -f ./manifests/metacontroller-namespace.yaml | ||
kubectl apply -f ./manifests/metacontroller-rbac.yaml | ||
kubectl apply -f ./manifests/metacontroller.yaml | ||
|
||
# verify if metac was installed properly | ||
kubectl get crd | ||
kubectl get sts -n metac | ||
``` | ||
|
||
### Deploy the Controllers | ||
|
||
```sh | ||
# cd to examples/gctl/install-uninstall-crd/ | ||
|
||
# any change in any of the hooks should be accompanied | ||
# by deleting 1/ configmap & 2/ operator & 3/ re-applying both | ||
kubectl create configmap install-uninstall-crd -n metac --from-file=hooks | ||
kubectl apply -f operator.yaml | ||
|
||
# verify the deploy, svc & configmap of this example | ||
kubectl get gctl | ||
kubectl get cm -n metac | ||
kubectl get deploy -n metac | ||
kubectl get svc -n metac | ||
``` | ||
|
||
### Create the Namespace | ||
|
||
```sh | ||
kubectl apply -f my-namespace.yaml | ||
``` | ||
|
||
Watch for the CRD to get created: | ||
|
||
```sh | ||
kubectl get crds --watch | ||
|
||
kubectl get crd storages.dao.amitd.io | ||
``` | ||
|
||
Check that the CRD get cleaned up when this Namespace is deleted: | ||
|
||
```sh | ||
kubectl delete -f my-namespace.yaml | ||
kubectl get crds --watch | ||
``` |
7 changes: 7 additions & 0 deletions
7
examples/gctl/install-uninstall-crd/hooks/finalize-crd.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function(request) { | ||
// Delete the CRD since namespace is observed | ||
// to being deleted | ||
attachments: [], | ||
// Mark as finalized once we observe this CRD are gone. | ||
finalized: std.length(request.attachments['CustomResourceDefinition.apiextensions.k8s.io/v1beta1']) == 0 | ||
} |
54 changes: 54 additions & 0 deletions
54
examples/gctl/install-uninstall-crd/hooks/sync-crd.jsonnet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
function(request) { | ||
// Create a CRD for the namespace specified in GenericController | ||
attachments: [ | ||
{ | ||
apiVersion: "apiextensions.k8s.io/v1beta1", | ||
kind: "CustomResourceDefinition", | ||
metadata: { | ||
// name must match the spec fields below, and be in the form: | ||
// <plural>.<group> | ||
name: "storages.dao.amitd.io", | ||
}, | ||
spec: { | ||
// group name to use for REST API: /apis/<group>/<version> | ||
group: "dao.amitd.io", | ||
// version name to use for REST API: /apis/<group>/<version> | ||
version: "v1alpha1", | ||
// either Namespaced or Cluster | ||
scope: "Namespaced", | ||
names:{ | ||
// plural name to be used in the URL: | ||
// i.e. /apis/<group>/<version>/<plural> | ||
plural: "storages", | ||
// # singular name to be used as an alias on the CLI and for display | ||
singular: "storage", | ||
// kind is normally the CamelCased singular type. | ||
// Your resource manifests use this. | ||
kind: "Storage", | ||
# shortNames allow shorter string to match your resource on the CLI | ||
shortNames: ["stor"], | ||
}, | ||
additionalPrinterColumns: [ | ||
{ | ||
JSONPath: ".spec.capacity", | ||
name: "Capacity", | ||
description: "Capacity of the storage", | ||
type: "string", | ||
}, | ||
{ | ||
JSONPath: ".spec.nodeName", | ||
name: "NodeName", | ||
description: "Node where the storage gets attached", | ||
type: "string", | ||
}, | ||
{ | ||
JSONPath: ".status.phase", | ||
name: "Status", | ||
description: "Identifies the current status of the storage", | ||
type: "string", | ||
}, | ||
], | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: amitd | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
--- | ||
apiVersion: metac.openebs.io/v1alpha1 | ||
kind: GenericController | ||
metadata: | ||
name: install-un-crd | ||
spec: | ||
watch: | ||
apiVersion: v1 | ||
resource: namespaces | ||
nameSelector: | ||
# we are interested in amitd namespace only | ||
- amitd | ||
attachments: | ||
- apiVersion: apiextensions.k8s.io/v1beta1 | ||
resource: customresourcedefinitions | ||
nameSelector: | ||
# we are interested in storages CRD only | ||
- storages.dao.amitd.io | ||
hooks: | ||
sync: | ||
webhook: | ||
url: http://jsonnetd.metac/sync-crd | ||
finalize: | ||
webhook: | ||
url: http://jsonnetd.metac/finalize-crd | ||
--- | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: jsonnetd | ||
namespace: metac | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: jsonnetd | ||
template: | ||
metadata: | ||
labels: | ||
app: jsonnetd | ||
spec: | ||
containers: | ||
- name: hooks | ||
# this deployment is all about exposing jsonnet as a webhook | ||
# that understands metac request & response payload | ||
image: metacontroller/jsonnetd:0.1 | ||
imagePullPolicy: Always | ||
workingDir: /hooks | ||
volumeMounts: | ||
- name: hooks | ||
mountPath: /hooks | ||
volumes: | ||
- name: hooks | ||
configMap: | ||
# this configmap provides the jsonnet files that | ||
# get executed as webhooks | ||
name: install-uninstall-crd | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
# this name is used to build the webhook url | ||
name: jsonnetd | ||
# this namespace is used to build the webhook url | ||
namespace: metac | ||
spec: | ||
selector: | ||
app: jsonnetd | ||
ports: | ||
- port: 80 | ||
targetPort: 8080 |
Oops, something went wrong.