Provider to run mailgun on cluster resources.
Cluster API is used to implement Cluster Provider.
Although Machines should also be implemented, only Clusters are used in this study in order to learn how to implement them.
This Provider sends mail to the destination specified in the Configmap, depending on the Requester specified in the .spec field of the MailgunCluster resource.
- It is implemented by the following versions of the tool.
- Kubebuilder("v3.10.0")
- Golang("v1.19.9")
- Kubernetes("v1.26.3")
- make("v4.3")
- gcc("v11.3.0")
- When using the latest version v1.4.2 of the Cluster API, the following versions of apimachinery, clinet-go, and controller-runtime must be used.
- k8s.io/apimachinery v0.26.5
- k8s.io/client-go v0.26.5
- sigs.k8s.io/cluster-api v1.4.2
- sigs.k8s.io/controller-runtime v0.14.6
- You must have already registered an account with Mailgun.
You do not need to create a domain name. In this case, we will use the Sandbox domain.
- You can use the pre-compiled manifest from the Cluster API release page, run clusterctl init, or clone cluster-api and use kustomize to apply that manifest.
cd cluster-api
make envsubst
kustomize build config/default | envsubst | kubectl apply -f -
- Setting Environment Variables
The configmap and secret resources are set using this environment variable.
export MAILGUN_DOMAIN="<Your Mailgun Sandbox Domain>"
export MAILGUN_API_KEY="<Your Mailgun Private API key>"
export MAIL_RECIPIENT="<Mail recipient address>"
- Build and push your image to the location specified by
IMG
:
make docker-build docker-push IMG=<Your Registry>/cluster-api-provider-mailgun:tag
- Deploy the controller to the cluster with the image specified by
IMG
:
make deploy IMG=<some-registry>/cluster-api-provider-mailgun:tag
- Edit Custom Resources Replace the .spec.requester in MailgunCluster with the Sandbox domain.
cat << EOT > config/samples/infrastructure_v1beta1_mailguncluster.yaml
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: hello-mailgun
namespace: cluster-api-provider-mailgun-system
spec:
clusterNetwork:
pods:
cidrBlocks: ["192.168.0.0/16"]
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: MailgunCluster
name: hello-mailgun
---
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
kind: MailgunCluster
metadata:
name: hello-mailgun
namespace: cluster-api-provider-mailgun-system
labels:
app.kubernetes.io/name: mailguncluster
app.kubernetes.io/instance: mailguncluster-sample
app.kubernetes.io/part-of: cluster-api-provider-mailgun
app.kubernetes.io/managed-by: kustomize
app.kubernetes.io/created-by: cluster-api-provider-mailgun
spec:
priority: "ExtremelyUrgent"
request: "Please make me a cluster, with sugar on top?"
requester: "<Your Domain>"
EOT
- Install Instances of Custom Resources:
kubectl apply -f config/samples/
- Check to see if the mail has been delivered to the address specified in the MAIL_RECIPIENT environment variable.
It is also possible to check from Mailgun's Logs as follows.
To delete the CRDs from the cluster:
make uninstall
UnDeploy the controller from the cluster:
make undeploy
This project aims to follow the Kubernetes Operator pattern.
It uses Controllers, which provide a reconcile function responsible for synchronizing resources until the desired state is reached on the cluster.
- Install the CRDs into the cluster:
make install
- Run your controller (this will run in the foreground, so switch to a new terminal if you want to leave it running):
export MAILGUN_DOMAIN="<Your Mailgun Sandbox Domain>"
export MAILGUN_API_KEY="<Your Mailgun Private API key>"
export MAIL_RECIPIENT="<Mail recipient address>"
make run
NOTE: You can also run this in one step by running: make install run
If you are editing the API definitions, generate the manifests such as CRs or CRDs using:
make manifests
NOTE: Run make --help
for more information on all potential make
targets
More information can be found via the Kubebuilder Documentation
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.