@@ -61,53 +61,9 @@ Building on the example introduced in [Controller Example](../basics/simple_cont
61
61
62
62
{% sample lang="go" %}
63
63
``` 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...
78
65
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
111
67
found := &appsv1.Deployment {}
112
68
err = r.Get (context.TODO (), types.NamespacedName {Name: deploy.Name , Namespace : deploy.Namespace }, found)
113
69
if err != nil && errors.IsNotFound (err) {
@@ -116,25 +72,28 @@ func (r *ReconcileContainerSet) Reconcile(request reconcile.Request) (reconcile.
116
72
if err != nil {
117
73
return reconcile.Result {}, err
118
74
}
75
+
119
76
// Write an event to the ContainerSet instance with the namespace and name of the
120
77
// created deployment
121
78
r.recorder .Event (instance, " Normal" , " Created" , fmt.Sprintf (" Created deployment %s /%s " , deploy.Namespace , deploy.Name ))
79
+
122
80
} else if err != nil {
123
81
return reconcile.Result {}, err
124
82
}
125
83
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
128
85
if !reflect.DeepEqual (deploy.Spec , found.Spec ) {
129
86
found.Spec = deploy.Spec
130
87
log.Printf (" Updating Deployment %s /%s \n " , deploy.Namespace , deploy.Name )
131
88
err = r.Update (context.TODO (), found)
132
89
if err != nil {
133
90
return reconcile.Result {}, err
134
91
}
92
+
135
93
// Write an event to the ContainerSet instance with the namespace and name of the
136
94
// updated deployment
137
95
r.recorder .Event (instance, " Normal" , " Updated" , fmt.Sprintf (" Updated deployment %s /%s " , deploy.Namespace , deploy.Name ))
96
+
138
97
}
139
98
return reconcile.Result {}, nil
140
99
}
0 commit comments