0
import (
    "context"
    "fmt"

    infinimeshv1beta1 "github.com/infinimesh/operator/pkg/apis/infinimesh/v1beta1"
    v1beta1 "k8s.io/api/batch/v1beta1"
    corev1 "k8s.io/api/core/v1"
    "k8s.io/apimachinery/pkg/api/errors"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/apimachinery/pkg/types"
    "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
    "sigs.k8s.io/controller-runtime/pkg/reconcile"
)

func (r *ReconcilePlatform) reconcileResetRootAccount(request reconcile.Request, instance *infinimeshv1beta1.Platform) error {
    log := logger.WithName("Reset Root Account Pwd")
    job := &v1beta1.CronJob{
        ObjectMeta: metav1.ObjectMeta{
            Name: "example",
        },
        Spec: v1beta1.CronJobSpec{
            Schedule:          "* * * * *",
            ConcurrencyPolicy: v1beta1.ForbidConcurrent,
            JobTemplate: v1beta1.JobTemplate{
                Spec: v1beta1.JobTemplateSpec{
                    Template: corev1.PodTemplateSpec{
                        Spec: corev1.PodSpec{
                            RestartPolicy: "Never",
                            Containers: []corev1.Container{
                                {
                                    Name:  "cli",
                                    Image: "busybox",
                                    Command: []string{
                                        "/bin/bash",
                                        "-c",
                                        "echo 1",
                                    },
                                    ImagePullPolicy: "Always",
                                },
                            },
                        },
                    },
                },
            },
        },
    }

I am getting error here

JobTemplate: v1beta1.JobTemplate{
                Spec: v1beta1.JobTemplateSpec{
                    Template: corev1.PodTemplateSpec{

as might be I am not defining it in the right way. Please guide me the right way to create a cronjob in Go. You can also write your own way of writing cronjobs in golang as I want to automate the cronjob in the kubernetes operator as when I restart the platform, cronjob will create automatically.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
ayesha kaleem
  • 77
  • 2
  • 7
  • A note on terminology: You talk about a "Go script", yet there is no such thing, as Go is compiled, and scripts, by definition, are not. – Jonathan Hall Dec 15 '20 at 13:10

1 Answers1

2

A cronjob wraps a job which wraps a pod.So you need to put the PodTemplateSpec inside a JobSpec inside the CronJobSpec.

    cronjob := &v1beta1.CronJob{
        ObjectMeta: metav1.ObjectMeta{
            Name:      "demo-cronjob",
            Namespace: "gitlab",
        },
        Spec: v1beta1.CronJobSpec{
            Schedule:          "* * * * *",
            ConcurrencyPolicy: v1beta1.ForbidConcurrent,
            JobTemplate: v1beta1.JobTemplateSpec{
                Spec: batchv1.JobSpec{
                    Template: v1.PodTemplateSpec{
                        Spec: v1.PodSpec{
                          ...
                        },
                    },
                },
            },
        },
    }
Arghya Sadhu
  • 41,002
  • 9
  • 78
  • 107