|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/golang/glog" |
| 7 | + "github.com/pkg/errors" |
| 8 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 9 | + "openebs.io/metac/controller/generic" |
| 10 | + "openebs.io/metac/start" |
| 11 | +) |
| 12 | + |
| 13 | +// sync executes the reconciliation logic for the watched resource. |
| 14 | +// In this case resource under watch is a custom resource with kind |
| 15 | +// Helloworld. |
| 16 | +// |
| 17 | +// NOTE: |
| 18 | +// SyncHookRequest is the payload received as part of reconcile |
| 19 | +// request. Similarly, SyncHookResponse is the payload sent as a |
| 20 | +// response as part of reconcile request. |
| 21 | +// |
| 22 | +// NOTE: |
| 23 | +// SyncHookRequest has the resource that is under watch (here |
| 24 | +// Helloworld custom resource) |
| 25 | +// |
| 26 | +// NOTE: |
| 27 | +// Both SyncHookRequest & SyncHookResponse have the resources that |
| 28 | +// form the desired state w.r.t the watched resource. These desired |
| 29 | +// resources are termed as attachments by metac. SyncHookRequest has |
| 30 | +// these attachments filled up by metac based on what is observed |
| 31 | +// in the k8s cluster. SyncHookResponse's attachments needs to be |
| 32 | +// filled up with what is desired by this controller logic. |
| 33 | +func sync( |
| 34 | + request *generic.SyncHookRequest, |
| 35 | + response *generic.SyncHookResponse, |
| 36 | +) error { |
| 37 | + glog.Infof("Starting hello world sync") |
| 38 | + defer glog.Infof("Completed hello world sync") |
| 39 | + |
| 40 | + // extract spec.who from Helloworld |
| 41 | + who, found, err := unstructured.NestedString( |
| 42 | + request.Watch.UnstructuredContent(), |
| 43 | + "spec", |
| 44 | + "who", |
| 45 | + ) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + if !found { |
| 50 | + return errors.Errorf("Can't sync: spec.who is not set") |
| 51 | + } |
| 52 | + // build the desired Pod |
| 53 | + desired := &unstructured.Unstructured{ |
| 54 | + Object: map[string]interface{}{ |
| 55 | + "kind": "Pod", |
| 56 | + "apiVersion": "v1", |
| 57 | + "metadata": map[string]interface{}{ |
| 58 | + "name": request.Watch.GetName(), |
| 59 | + "namespace": request.Watch.GetNamespace(), |
| 60 | + "annotations": map[string]interface{}{ |
| 61 | + // this annotation helps in associating the watch |
| 62 | + // with its corresponding attachment |
| 63 | + "helloworld/uid": string(request.Watch.GetUID()), |
| 64 | + }, |
| 65 | + }, |
| 66 | + "spec": map[string]interface{}{ |
| 67 | + "restartPolicy": "OnFailure", |
| 68 | + "containers": []interface{}{ |
| 69 | + map[string]interface{}{ |
| 70 | + "name": "hello", |
| 71 | + "image": "busybox", |
| 72 | + "command": []interface{}{ |
| 73 | + "echo", |
| 74 | + fmt.Sprintf("Hello, %s", who), |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + // add this desired Pod instance to response |
| 82 | + // to let metac create this in the k8s cluster |
| 83 | + response.Attachments = append( |
| 84 | + response.Attachments, |
| 85 | + desired, |
| 86 | + ) |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// finalize will delete the attachment(s) created during |
| 91 | +// reconciliation of the watch |
| 92 | +// |
| 93 | +// NOTE: |
| 94 | +// Presence of finalize in the config automatically adds |
| 95 | +// finalizers against the watch |
| 96 | +// |
| 97 | +// NOTE: |
| 98 | +// Once attachments are deleted from the cluster, |
| 99 | +// 'response.Finalized' is set to true which in turn removes |
| 100 | +// this finalizers from the watch |
| 101 | +func finalize( |
| 102 | + request *generic.SyncHookRequest, |
| 103 | + response *generic.SyncHookResponse, |
| 104 | +) error { |
| 105 | + glog.Infof("Starting hello world finalize") |
| 106 | + defer glog.Infof("Completed hello world finalize") |
| 107 | + |
| 108 | + if request.Attachments.IsEmpty() { |
| 109 | + response.Finalized = true |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func main() { |
| 115 | + generic.AddToInlineRegistry("sync/helloworld", sync) |
| 116 | + generic.AddToInlineRegistry("finalize/helloworld", finalize) |
| 117 | + start.Start() |
| 118 | +} |
0 commit comments