why cant specify container name when using imperative cmd to create cronjob in k8s?

99 Views Asked by At

I'm trying to run a cmd to create cronjob:

"k create cronjob mycron --image:httpd --schedule="*/8 * * * *" --name=mycontainer-name"

but it does not recognize, i tried to use flag "--container-name=mycontainer-name" but not working too.

is there way to do 1 liner and specify container name in it? or the only way is to use declarative like

"k create cronjob mycron --image:httpd --schedule="*/8 * * * *" --dry-run=client -oyaml > d.yaml"

and then change it in yaml file and run apply?

2

There are 2 best solutions below

1
On BEST ANSWER

I don't think CLI supports any Options to name container. You can check kubectl create cronjob --help for all the available options. kubectl apply seems to be the only possible way.

2
On

Just to add to @TarunVerma's answer. Here is current implementation of kubectl create cronjob:

        TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "CronJob"},
        ObjectMeta: metav1.ObjectMeta{
            Name: o.Name,
        },
        Spec: batchv1.CronJobSpec{
            Schedule: o.Schedule,
            JobTemplate: batchv1.JobTemplateSpec{
                ObjectMeta: metav1.ObjectMeta{
                    Name: o.Name,
                },
                Spec: batchv1.JobSpec{
                    Template: corev1.PodTemplateSpec{
                        Spec: corev1.PodSpec{
                            Containers: []corev1.Container{
                                {
                                    Name:    o.Name,
                                    Image:   o.Image,
                                    Command: o.Command,
                                },
                            },
                            RestartPolicy: corev1.RestartPolicy(o.Restart),
                        },
                    },
                },
            },
        },
    }

o.Name in code above is used for 1) name of cron job, 2) name in template and 3) name of container at same time. No way to specify it separately.