Skip to content

Commit 3e1d702

Browse files
committed
Fix controller example in gitbook
Controller example in gitbook does not work due to wrong Reconcile function. This patch updates the function. Fixes #473
1 parent 002c18b commit 3e1d702

File tree

1 file changed

+52
-59
lines changed

1 file changed

+52
-59
lines changed

docs/book/basics/simple_controller.md

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -147,80 +147,73 @@ on the Deployment once the ContainerSet is deleted.
147147
```go
148148
var _ reconcile.Reconciler = &ContainerSetController{}
149149

150-
func (r *ContainerSetController) Reconcile(request reconcile.Request) (
151-
reconcile.Result, error) {
152-
// Read the ContainerSet
153-
cs := &workloadv1beta1.ContainerSet{}
154-
err := r.client.Get(context.TODO(), request.NamespacedName, cs)
155-
156-
// Handle deleted or error case
150+
func (r *ReconcileContainerSet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
151+
instance := &workloadsv1beta1.ContainerSet{}
152+
err := r.Get(context.TODO(), request.NamespacedName, instance)
157153
if err != nil {
158154
if errors.IsNotFound(err) {
159-
// Not found. Don't worry about cleaning up Deployments,
160-
// GC will handle it.
155+
// Object not found, return. Created objects are automatically garbage collected.
156+
// For additional cleanup logic use finalizers.
161157
return reconcile.Result{}, nil
162158
}
163159
// Error reading the object - requeue the request.
164160
return reconcile.Result{}, err
165161
}
166162

167-
// Calculate the expected Deployment Spec
168-
spec := getDeploymentSpec(request)
169-
170-
// Read the Deployment
171-
dep := &appsv1.Deployment{}
172-
err := r.client.Get(context.TODO(), request.NamespacedName, dep)
163+
// TODO(user): Change this to be the object type created by your controller
164+
// Define the desired Deployment object
165+
deploy := &appsv1.Deployment{
166+
ObjectMeta: metav1.ObjectMeta{
167+
Name: instance.Name + "-deployment",
168+
Namespace: instance.Namespace,
169+
},
170+
Spec: appsv1.DeploymentSpec{
171+
Selector: &metav1.LabelSelector{
172+
MatchLabels: map[string]string{"deployment": instance.Name + "-deployment"},
173+
},
174+
Replicas: &instance.Spec.Replicas,
175+
Template: corev1.PodTemplateSpec{
176+
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"deployment": instance.Name + "-deployment"}},
177+
Spec: corev1.PodSpec{
178+
Containers: []corev1.Container{
179+
{
180+
Name: instance.Name,
181+
Image: instance.Spec.Image,
182+
},
183+
},
184+
},
185+
},
186+
},
187+
}
188+
if err := controllerutil.SetControllerReference(instance, deploy, r.scheme); err != nil {
189+
return reconcile.Result{}, err
190+
}
173191

174-
// If not found, create it
175-
if errors.IsNotFound(err) {
176-
dep = &appsv1.Deployment{Spec: spec}
177-
dep.Name = request.Name
178-
dep.Namespace = request.Namespace
179-
if err := controllerutil.SetControllerReference(cs, deploy, r.scheme); err != nil {
180-
return reconcile.Result{}, err
181-
}
182-
if err := r.Create(context.TODO(), dep); err != nil {
192+
// TODO(user): Change this for the object type created by your controller
193+
// Check if the Deployment already exists
194+
found := &appsv1.Deployment{}
195+
err = r.Get(context.TODO(), types.NamespacedName{Name: deploy.Name, Namespace: deploy.Namespace}, found)
196+
if err != nil && errors.IsNotFound(err) {
197+
log.Printf("Creating Deployment %s/%s\n", deploy.Namespace, deploy.Name)
198+
err = r.Create(context.TODO(), deploy)
199+
if err != nil {
183200
return reconcile.Result{}, err
184201
}
185-
return reconcile.Result{}, nil
202+
} else if err != nil {
203+
return reconcile.Result{}, err
186204
}
187205

188-
// If found, update it
189-
image := dep.Spec.Template.Spec.Containers[0].Image
190-
replicas := *dep.Spec.Replicas
191-
if replicas == cs.Spec.Replicas && image == cs.Spec.Image {
192-
return reconcile.Result{}, nil
193-
}
194-
dep.Spec.Replicas = &cs.Spec.Replicas
195-
dep.Spec.Template.Spec.Containers[0].Image = cs.Spec.Image
196-
if err := r.Update(context.TODO(), dep); err != nil {
206+
// TODO(user): Change this for the object type created by your controller
207+
// Update the found object and write the result back if there are any changes
208+
if !reflect.DeepEqual(deploy.Spec, found.Spec) {
209+
found.Spec = deploy.Spec
210+
log.Printf("Updating Deployment %s/%s\n", deploy.Namespace, deploy.Name)
211+
err = r.Update(context.TODO(), found)
212+
if err != nil {
197213
return reconcile.Result{}, err
198214
}
199-
200-
return reconcile.Result{}, nil
201-
}
202-
203-
func getDeploymentSpec(request reconcile.Request) *appsv1.DeploymentSpec {
204-
return &appsv1.DeploymentSpec{
205-
Selector: &metav1.LabelSelector{
206-
MatchLabels: map[string]string{
207-
"container-set": request.Name},
208-
},
209-
Replicas: &cs.Spec.Replicas,
210-
Template: corev1.PodTemplateSpec{
211-
ObjectMeta: metav1.ObjectMeta{
212-
Labels: map[string]string{
213-
"container-set": request.Name,
214-
},
215-
},
216-
Spec: corev1.PodSpec{
217-
Containers: []corev1.Container{
218-
{Name: request.Name,
219-
Image: cs.Spec.Image},
220-
},
221-
},
222-
},
223-
}
215+
}
216+
return reconcile.Result{}, nil
224217
}
225218
```
226219
{% endmethod %}

0 commit comments

Comments
 (0)