You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: Improvements to consistency and functionality of example code
- Consistently refer to cluster as mailgunCluster
- Add that you need to import the cluster-api package to your project before you can use the util function
- Use mailgunCluster.ObjectMeta instead of a pointer in GetOwnerCluster call
- Add explanation that we need to wait for the Cluster API controller to set the ownerref before we can use the cluster object, update code to do that
- Show entire Reconcile function again after adding many lines to it
Signed-off-by: cprivitere <[email protected]>
@@ -126,19 +126,57 @@ That may not always be what you want - what if the object's been deleted? So let
126
126
}
127
127
```
128
128
129
-
Now, if this were any old `kubebuilder` project you'd be done, but in our case you have one more object to retrieve.
130
-
Cluster API splits a cluster into two objects: the [`Cluster` defined by Cluster API itself][cluster].
131
-
We'll want to retrieve that as well.
132
-
Luckily, cluster API [provides a helper for us][getowner].
129
+
Now, if this were any old `kubebuilder` project you'd be done, but in our case you have one more object to retrieve. While we defined our own cluster object (`MailGunCluster`) that represents all the infrastructure provider specific details for our cluster, we also need to retrieve the upstream [`Cluster` object that is defined by Cluster API itself][cluster]. Luckily, cluster API [provides a helper for us][getowner].
130
+
131
+
First, you'll need to import the cluster-api package into our project if you haven't done so yet:
132
+
133
+
```bash
134
+
# In your Mailgun repository's root directory
135
+
go get sigs.k8s.io/cluster-api
136
+
go mod tidy
137
+
```
138
+
139
+
Now we can add in a call to the `GetOwnerCluster` function to retrieve the cluster object:
If our cluster was just created, the Cluster API controller may not have set the ownership reference on our object yet, so we'll have to return here and wait to do more with our cluster object until then. We can leave a log message noting that we're waiting for the main Cluster API controller to set the ownership reference. Here's what our `Reconcile()` function looks like now:
log.Info("Waiting for Cluster Controller to set OwnerRef on MailGunCluster")
176
+
return ctrl.Result{}, nil
177
+
}
178
+
```
179
+
142
180
### The fun part
143
181
144
182
_More Documentation: [The Kubebuilder Book][book] has some excellent documentation on many things, including [how to write good controllers!][implement]_
@@ -151,10 +189,10 @@ This is where your provider really comes into its own.
151
189
In our case, let's try sending some mail:
152
190
153
191
```go
154
-
subject:= fmt.Sprintf("[%s] New Cluster %s requested", mgCluster.Spec.Priority, cluster.Name)
155
-
body:= fmt.Sprint("Hello! One cluster please.\n\n%s\n", mgCluster.Spec.Request)
192
+
subject:= fmt.Sprintf("[%s] New Cluster %s requested", mailgunCluster.Spec.Priority, cluster.Name)
193
+
body:= fmt.Sprintf("Hello! One cluster please.\n\n%s\n", mailgunCluster.Spec.Request)
0 commit comments