Skip to content

Commit 3a08287

Browse files
author
Hagai Barel
committed
Simplified example
2 parents ae66113 + ceb589e commit 3a08287

File tree

1 file changed

+7
-48
lines changed

1 file changed

+7
-48
lines changed

docs/book/beyond_basics/creating_events.md

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -61,53 +61,9 @@ Building on the example introduced in [Controller Example](../basics/simple_cont
6161

6262
{% sample lang="go" %}
6363
```go
64-
var _ reconcile.Reconciler = &ContainerSetController{}
65-
66-
func (r *ReconcileContainerSet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
67-
instance := &workloadsv1beta1.ContainerSet{}
68-
err := r.Get(context.TODO(), request.NamespacedName, instance)
69-
if err != nil {
70-
if errors.IsNotFound(err) {
71-
// Object not found, return. Created objects are automatically garbage collected.
72-
// For additional cleanup logic use finalizers.
73-
return reconcile.Result{}, nil
74-
}
75-
// Error reading the object - requeue the request.
76-
return reconcile.Result{}, err
77-
}
64+
//Reconcile logic up here...
7865

79-
// TODO(user): Change this to be the object type created by your controller
80-
// Define the desired Deployment object
81-
deploy := &appsv1.Deployment{
82-
ObjectMeta: metav1.ObjectMeta{
83-
Name: instance.Name + "-deployment",
84-
Namespace: instance.Namespace,
85-
},
86-
Spec: appsv1.DeploymentSpec{
87-
Selector: &metav1.LabelSelector{
88-
MatchLabels: map[string]string{"deployment": instance.Name + "-deployment"},
89-
},
90-
Replicas: &instance.Spec.Replicas,
91-
Template: corev1.PodTemplateSpec{
92-
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"deployment": instance.Name + "-deployment"}},
93-
Spec: corev1.PodSpec{
94-
Containers: []corev1.Container{
95-
{
96-
Name: instance.Name,
97-
Image: instance.Spec.Image,
98-
},
99-
},
100-
},
101-
},
102-
},
103-
}
104-
105-
if err := controllerutil.SetControllerReference(instance, deploy, r.scheme); err != nil {
106-
return reconcile.Result{}, err
107-
}
108-
109-
// TODO(user): Change this for the object type created by your controller
110-
// Check if the Deployment already exists
66+
// Create the resource
11167
found := &appsv1.Deployment{}
11268
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
11369
if err != nil && errors.IsNotFound(err) {
@@ -116,25 +72,28 @@ func (r *ReconcileContainerSet) Reconcile(request reconcile.Request) (reconcile.
11672
if err != nil {
11773
return reconcile.Result{}, err
11874
}
75+
11976
// Write an event to the ContainerSet instance with the namespace and name of the
12077
// created deployment
12178
r.recorder.Event(instance, "Normal", "Created", fmt.Sprintf("Created deployment %s/%s", deploy.Namespace, deploy.Name))
79+
12280
} else if err != nil {
12381
return reconcile.Result{}, err
12482
}
12583

126-
// TODO(user): Change this for the object type created by your controller
127-
// Update the found object and write the result back if there are any changes
84+
// Preform update
12885
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
12986
found.Spec = deploy.Spec
13087
log.Printf("Updating Deployment %s/%s\n", deploy.Namespace, deploy.Name)
13188
err = r.Update(context.TODO(), found)
13289
if err != nil {
13390
return reconcile.Result{}, err
13491
}
92+
13593
// Write an event to the ContainerSet instance with the namespace and name of the
13694
// updated deployment
13795
r.recorder.Event(instance, "Normal", "Updated", fmt.Sprintf("Updated deployment %s/%s", deploy.Namespace, deploy.Name))
96+
13897
}
13998
return reconcile.Result{}, nil
14099
}

0 commit comments

Comments
 (0)