Getting Started with Mayfly: Your Kubernetes Operator for Time-Based Resources

Aug 12, 2023 | Programming

Welcome to the world of Mayfly! If you’ve ever wished for a way to create or delete Kubernetes resources based on time, you’re in the right place. Mayfly is a Kubernetes operator designed to help you manage your cluster’s resources temporarily, organizing them efficiently and ensuring optimal utilization of your cloud environment.

What is Mayfly?

The Mayfly Operator allows you to create and manage resources in your Kubernetes cluster for a limited time. You can specify expiration dates or durations, meaning that resources can be automatically cleaned up once they are no longer needed. Think of it like a timer for your cloud resources – they can appear when needed and disappear once their time is up!

Why Use Mayfly?

  • Organized Cluster: Helps keep your cluster neat by automatically removing unnecessary resources.
  • Temporary Access: Ideal for temporary resources, like dev environments or testing services.
  • Efficiency: Reduces clutter and improves resource management.

How to Configure Mayfly

Setting up Mayfly is straightforward. You need to specify which resources should be monitored and set the RESOURCES environment variable to a list of the resources you want to target for deletion:

export RESOURCES=v1;Secret,test.com,v1alpha;MyCRD

This command tells Mayfly to monitor specific resources. Once you have this configured, Mayfly will start managing these resources based on the configurations you set.

Using Resource Expiration

Here’s where it gets interesting! You can annotate your resources with an expiration time using the following mechanisms:

  • Duration: For a duration value, Mayfly calculates when to delete the resource based on its creation time.
  • Exact Date & Time: Resources can be tagged for deletion on a specific date.

Here’s an analogy to help clarify the concept: imagine you’re using a rental service for furniture. You specify the rental period (duration) or a fixed return date (exact date). Once that time comes, the rental service picks up the furniture with no reminders needed!

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  annotations:
    mayfly.cloud.namecheap.com/expire: 30s
spec:
  containers:
    - name: alpine
      image: alpine
      command:
        - sleep
        - infinity
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  annotations:
    mayfly.cloud.namecheap.com/expire: 2024-12-31T00:00:00Z
spec:
  containers:
    - name: alpine
      image: alpine
      command:
        - sleep
        - infinity

Scheduled Resource Creation

With the ScheduledResource CRD, you can plan the creation of an object to occur at a future time:

apiVersion: cloud.namecheap.com/v1alpha1
kind: ScheduledResource
metadata:
  annotations:
    mayfly.cloud.namecheap.com/expire: 60m
  name: example
  namespace: default
spec:
  schedule: 10s
  content:
    apiVersion: v1
    kind: Secret
    metadata:
      name: example
      namespace: default
      annotations:
        mayfly.cloud.namecheap.com/expire: 30m
    data:
      .secret-file: dmFsdWUtMg0KDQo
status:
  condition: Scheduled

Deployment

The easiest way to deploy Mayfly is via Helm. Just add our Helm repository and install Mayfly:

helm repo add nccloud https://nccloud.github.io/charts
helm install mayfly nccloud/mayfly --set env[0].name=RESOURCES --set env[0].value=v1;Secret

Troubleshooting

If you encounter issues while using Mayfly, consider the following ideas:

  • Double-check the format of your expiration annotations to ensure they are correctly applied.
  • Make sure that the RESOURCES environment variable is set properly and matches the resources you intend to manage.
  • Refer to the issues page for common problems and fixes.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Mayfly is a powerful Kubernetes operator that can help streamline resource management within your cluster. With its ability to handle time-based resources effortlessly, it offers a practical approach to keeping your cloud environment clean and efficient.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox