2019-04-07 17:07:55 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 The Kubernetes Authors.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package create
|
|
|
|
|
|
|
|
import (
|
2020-03-26 21:07:15 +00:00
|
|
|
"context"
|
2019-04-07 17:07:55 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
batchv1 "k8s.io/api/batch/v1"
|
|
|
|
batchv1beta1 "k8s.io/api/batch/v1beta1"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
|
|
|
"k8s.io/cli-runtime/pkg/resource"
|
2021-03-18 22:40:29 +00:00
|
|
|
batchv1client "k8s.io/client-go/kubernetes/typed/batch/v1"
|
2019-04-07 17:07:55 +00:00
|
|
|
batchv1beta1client "k8s.io/client-go/kubernetes/typed/batch/v1beta1"
|
2019-09-27 21:51:53 +00:00
|
|
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
|
|
|
"k8s.io/kubectl/pkg/scheme"
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/kubectl/pkg/util"
|
2020-12-01 01:06:26 +00:00
|
|
|
"k8s.io/kubectl/pkg/util/i18n"
|
2019-09-27 21:51:53 +00:00
|
|
|
"k8s.io/kubectl/pkg/util/templates"
|
2019-04-07 17:07:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-01 01:06:26 +00:00
|
|
|
cronjobLong = templates.LongDesc(i18n.T(`
|
|
|
|
Create a cronjob with the specified name.`))
|
2019-04-07 17:07:55 +00:00
|
|
|
|
|
|
|
cronjobExample = templates.Examples(`
|
|
|
|
# Create a cronjob
|
2020-08-10 17:43:49 +00:00
|
|
|
kubectl create cronjob my-job --image=busybox --schedule="*/1 * * * *"
|
2019-04-07 17:07:55 +00:00
|
|
|
|
|
|
|
# Create a cronjob with command
|
2020-08-10 17:43:49 +00:00
|
|
|
kubectl create cronjob my-job --image=busybox --schedule="*/1 * * * *" -- date`)
|
2019-04-07 17:07:55 +00:00
|
|
|
)
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// CreateCronJobOptions is returned by NewCreateCronJobOptions
|
2019-04-07 17:07:55 +00:00
|
|
|
type CreateCronJobOptions struct {
|
|
|
|
PrintFlags *genericclioptions.PrintFlags
|
|
|
|
|
|
|
|
PrintObj func(obj runtime.Object) error
|
|
|
|
|
|
|
|
Name string
|
|
|
|
Image string
|
|
|
|
Schedule string
|
|
|
|
Command []string
|
|
|
|
Restart string
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
Namespace string
|
|
|
|
EnforceNamespace bool
|
2021-03-18 22:40:29 +00:00
|
|
|
ClientBeta batchv1beta1client.BatchV1beta1Interface
|
|
|
|
Client batchv1client.BatchV1Interface
|
2020-08-10 17:43:49 +00:00
|
|
|
DryRunStrategy cmdutil.DryRunStrategy
|
|
|
|
DryRunVerifier *resource.DryRunVerifier
|
|
|
|
Builder *resource.Builder
|
|
|
|
FieldManager string
|
|
|
|
CreateAnnotation bool
|
2019-04-07 17:07:55 +00:00
|
|
|
|
|
|
|
genericclioptions.IOStreams
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// NewCreateCronJobOptions returns an initialized CreateCronJobOptions instance
|
2019-04-07 17:07:55 +00:00
|
|
|
func NewCreateCronJobOptions(ioStreams genericclioptions.IOStreams) *CreateCronJobOptions {
|
|
|
|
return &CreateCronJobOptions{
|
|
|
|
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
|
|
|
|
IOStreams: ioStreams,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 01:27:03 +00:00
|
|
|
// NewCmdCreateCronJob is a command to create CronJobs.
|
2019-04-07 17:07:55 +00:00
|
|
|
func NewCmdCreateCronJob(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
|
|
|
o := NewCreateCronJobOptions(ioStreams)
|
|
|
|
cmd := &cobra.Command{
|
2020-08-10 17:43:49 +00:00
|
|
|
Use: "cronjob NAME --image=image --schedule='0/5 * * * ?' -- [COMMAND] [args...]",
|
|
|
|
DisableFlagsInUseLine: false,
|
|
|
|
Aliases: []string{"cj"},
|
|
|
|
Short: cronjobLong,
|
|
|
|
Long: cronjobLong,
|
|
|
|
Example: cronjobExample,
|
2019-04-07 17:07:55 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
|
|
|
cmdutil.CheckErr(o.Run())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
o.PrintFlags.AddFlags(cmd)
|
|
|
|
|
|
|
|
cmdutil.AddApplyAnnotationFlags(cmd)
|
|
|
|
cmdutil.AddValidateFlags(cmd)
|
|
|
|
cmdutil.AddDryRunFlag(cmd)
|
|
|
|
cmd.Flags().StringVar(&o.Image, "image", o.Image, "Image name to run.")
|
2020-08-10 17:43:49 +00:00
|
|
|
cmd.MarkFlagRequired("image")
|
2019-04-07 17:07:55 +00:00
|
|
|
cmd.Flags().StringVar(&o.Schedule, "schedule", o.Schedule, "A schedule in the Cron format the job should be run with.")
|
2020-08-10 17:43:49 +00:00
|
|
|
cmd.MarkFlagRequired("schedule")
|
2019-04-07 17:07:55 +00:00
|
|
|
cmd.Flags().StringVar(&o.Restart, "restart", o.Restart, "job's restart policy. supported values: OnFailure, Never")
|
2020-08-10 17:43:49 +00:00
|
|
|
cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-create")
|
2019-04-07 17:07:55 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// Complete completes all the required options
|
2019-04-07 17:07:55 +00:00
|
|
|
func (o *CreateCronJobOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []string) error {
|
|
|
|
name, err := NameFromCommandArgs(cmd, args)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Name = name
|
|
|
|
if len(args) > 1 {
|
|
|
|
o.Command = args[1:]
|
|
|
|
}
|
|
|
|
if len(o.Restart) == 0 {
|
|
|
|
o.Restart = "OnFailure"
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConfig, err := f.ToRESTConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
|
|
|
|
// TODO: drop this condition when beta disappears in 1.25
|
|
|
|
clientset, err := f.KubernetesClientSet()
|
2019-04-07 17:07:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
resources, err := clientset.Discovery().ServerResourcesForGroupVersion(batchv1.SchemeGroupVersion.String())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to discover supported resources: %v", err)
|
|
|
|
}
|
|
|
|
found := false
|
|
|
|
for _, serverResource := range resources.APIResources {
|
|
|
|
if serverResource.Name == "cronjobs" {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
|
|
|
o.Client, err = batchv1client.NewForConfig(clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
o.ClientBeta, err = batchv1beta1client.NewForConfig(clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 17:07:55 +00:00
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
|
2019-04-07 17:07:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Builder = f.NewBuilder()
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
o.CreateAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)
|
2019-04-07 17:07:55 +00:00
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
o.DryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dynamicClient, err := f.DynamicClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
o.DryRunVerifier = resource.NewDryRunVerifier(dynamicClient, f.OpenAPIGetter())
|
2020-03-26 21:07:15 +00:00
|
|
|
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
|
2019-04-07 17:07:55 +00:00
|
|
|
printer, err := o.PrintFlags.ToPrinter()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.PrintObj = func(obj runtime.Object) error {
|
|
|
|
return printer.PrintObj(obj, o.Out)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
// Run performs the execution of 'create cronjob' sub command
|
2019-04-07 17:07:55 +00:00
|
|
|
func (o *CreateCronJobOptions) Run() error {
|
2021-03-18 22:40:29 +00:00
|
|
|
if o.Client != nil {
|
|
|
|
cronJob := o.createCronJob()
|
|
|
|
if err := util.CreateOrUpdateAnnotation(o.CreateAnnotation, cronJob, scheme.DefaultJSONEncoder()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
if o.DryRunStrategy != cmdutil.DryRunClient {
|
|
|
|
createOptions := metav1.CreateOptions{}
|
|
|
|
if o.FieldManager != "" {
|
|
|
|
createOptions.FieldManager = o.FieldManager
|
|
|
|
}
|
|
|
|
if o.DryRunStrategy == cmdutil.DryRunServer {
|
|
|
|
if err := o.DryRunVerifier.HasSupport(cronJob.GroupVersionKind()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
createOptions.DryRun = []string{metav1.DryRunAll}
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
cronJob, err = o.Client.CronJobs(o.Namespace).Create(context.TODO(), cronJob, createOptions)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create cronjob: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.PrintObj(cronJob)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: drop this condition when beta disappears in 1.25
|
|
|
|
cronJobBeta := o.createCronJobBeta()
|
|
|
|
if err := util.CreateOrUpdateAnnotation(o.CreateAnnotation, cronJobBeta, scheme.DefaultJSONEncoder()); err != nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-07 17:07:55 +00:00
|
|
|
|
2020-03-26 21:07:15 +00:00
|
|
|
if o.DryRunStrategy != cmdutil.DryRunClient {
|
|
|
|
createOptions := metav1.CreateOptions{}
|
2020-08-10 17:43:49 +00:00
|
|
|
if o.FieldManager != "" {
|
|
|
|
createOptions.FieldManager = o.FieldManager
|
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
if o.DryRunStrategy == cmdutil.DryRunServer {
|
2021-03-18 22:40:29 +00:00
|
|
|
if err := o.DryRunVerifier.HasSupport(cronJobBeta.GroupVersionKind()); err != nil {
|
2020-03-26 21:07:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
createOptions.DryRun = []string{metav1.DryRunAll}
|
|
|
|
}
|
2019-04-07 17:07:55 +00:00
|
|
|
var err error
|
2021-03-18 22:40:29 +00:00
|
|
|
cronJobBeta, err = o.ClientBeta.CronJobs(o.Namespace).Create(context.TODO(), cronJobBeta, createOptions)
|
2019-04-07 17:07:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create cronjob: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
return o.PrintObj(cronJobBeta)
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 22:40:29 +00:00
|
|
|
func (o *CreateCronJobOptions) createCronJobBeta() *batchv1beta1.CronJob {
|
2020-08-10 17:43:49 +00:00
|
|
|
cronjob := &batchv1beta1.CronJob{
|
2019-04-07 17:07:55 +00:00
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: batchv1beta1.SchemeGroupVersion.String(), Kind: "CronJob"},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: o.Name,
|
|
|
|
},
|
|
|
|
Spec: batchv1beta1.CronJobSpec{
|
|
|
|
Schedule: o.Schedule,
|
|
|
|
JobTemplate: batchv1beta1.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),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
if o.EnforceNamespace {
|
|
|
|
cronjob.Namespace = o.Namespace
|
|
|
|
}
|
|
|
|
return cronjob
|
2019-04-07 17:07:55 +00:00
|
|
|
}
|
2021-03-18 22:40:29 +00:00
|
|
|
|
|
|
|
func (o *CreateCronJobOptions) createCronJob() *batchv1.CronJob {
|
|
|
|
cronjob := &batchv1.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),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if o.EnforceNamespace {
|
|
|
|
cronjob.Namespace = o.Namespace
|
|
|
|
}
|
|
|
|
return cronjob
|
|
|
|
}
|