2019-01-12 04:58:27 +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-01-12 04:58:27 +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"
|
2019-04-07 17:07:55 +00:00
|
|
|
"k8s.io/cli-runtime/pkg/resource"
|
2019-01-12 04:58:27 +00:00
|
|
|
batchv1client "k8s.io/client-go/kubernetes/typed/batch/v1"
|
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"
|
2019-09-27 21:51:53 +00:00
|
|
|
"k8s.io/kubectl/pkg/util/i18n"
|
|
|
|
"k8s.io/kubectl/pkg/util/templates"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
jobLong = templates.LongDesc(i18n.T(`
|
|
|
|
Create a job with the specified name.`))
|
|
|
|
|
|
|
|
jobExample = templates.Examples(i18n.T(`
|
|
|
|
# Create a job
|
|
|
|
kubectl create job my-job --image=busybox
|
|
|
|
|
|
|
|
# Create a job with command
|
|
|
|
kubectl create job my-job --image=busybox -- date
|
|
|
|
|
|
|
|
# Create a job from a CronJob named "a-cronjob"
|
|
|
|
kubectl create job test-job --from=cronjob/a-cronjob`))
|
|
|
|
)
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// CreateJobOptions is the command line options for 'create job'
|
2019-01-12 04:58:27 +00:00
|
|
|
type CreateJobOptions struct {
|
|
|
|
PrintFlags *genericclioptions.PrintFlags
|
|
|
|
|
|
|
|
PrintObj func(obj runtime.Object) error
|
|
|
|
|
|
|
|
Name string
|
|
|
|
Image string
|
|
|
|
From string
|
|
|
|
Command []string
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
Namespace string
|
|
|
|
EnforceNamespace bool
|
|
|
|
Client batchv1client.BatchV1Interface
|
|
|
|
DryRunStrategy cmdutil.DryRunStrategy
|
|
|
|
DryRunVerifier *resource.DryRunVerifier
|
|
|
|
Builder *resource.Builder
|
|
|
|
FieldManager string
|
|
|
|
CreateAnnotation bool
|
2019-01-12 04:58:27 +00:00
|
|
|
|
|
|
|
genericclioptions.IOStreams
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// NewCreateJobOptions initializes and returns new CreateJobOptions instance
|
2019-01-12 04:58:27 +00:00
|
|
|
func NewCreateJobOptions(ioStreams genericclioptions.IOStreams) *CreateJobOptions {
|
|
|
|
return &CreateJobOptions{
|
|
|
|
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
|
|
|
|
IOStreams: ioStreams,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCmdCreateJob is a command to ease creating Jobs from CronJobs.
|
|
|
|
func NewCmdCreateJob(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
|
|
|
|
o := NewCreateJobOptions(ioStreams)
|
|
|
|
cmd := &cobra.Command{
|
2020-08-10 17:43:49 +00:00
|
|
|
Use: "job NAME --image=image [--from=cronjob/name] -- [COMMAND] [args...]",
|
|
|
|
DisableFlagsInUseLine: true,
|
|
|
|
Short: jobLong,
|
|
|
|
Long: jobLong,
|
|
|
|
Example: jobExample,
|
2019-01-12 04:58:27 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
cmdutil.CheckErr(o.Complete(f, cmd, args))
|
|
|
|
cmdutil.CheckErr(o.Validate())
|
|
|
|
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.")
|
|
|
|
cmd.Flags().StringVar(&o.From, "from", o.From, "The name of the resource to create a Job from (only cronjob is supported).")
|
2020-08-10 17:43:49 +00:00
|
|
|
cmdutil.AddFieldManagerFlagVar(cmd, &o.FieldManager, "kubectl-create")
|
2019-01-12 04:58:27 +00:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// Complete completes all the required options
|
2019-01-12 04:58:27 +00:00
|
|
|
func (o *CreateJobOptions) 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:]
|
|
|
|
}
|
|
|
|
|
|
|
|
clientConfig, err := f.ToRESTConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Client, err = batchv1client.NewForConfig(clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
o.CreateAnnotation = cmdutil.GetFlagBool(cmd, cmdutil.ApplyAnnotationsFlag)
|
|
|
|
|
|
|
|
o.Namespace, o.EnforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.Builder = f.NewBuilder()
|
|
|
|
|
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-01-12 04:58:27 +00:00
|
|
|
}
|
2020-03-26 21:07:15 +00:00
|
|
|
discoveryClient, err := f.ToDiscoveryClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.DryRunVerifier = resource.NewDryRunVerifier(dynamicClient, discoveryClient)
|
|
|
|
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
|
2019-01-12 04:58:27 +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
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// Validate makes sure provided values and valid Job options
|
2019-01-12 04:58:27 +00:00
|
|
|
func (o *CreateJobOptions) Validate() error {
|
|
|
|
if (len(o.Image) == 0 && len(o.From) == 0) || (len(o.Image) != 0 && len(o.From) != 0) {
|
|
|
|
return fmt.Errorf("either --image or --from must be specified")
|
|
|
|
}
|
|
|
|
if o.Command != nil && len(o.Command) != 0 && len(o.From) != 0 {
|
|
|
|
return fmt.Errorf("cannot specify --from and command")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-07 17:07:55 +00:00
|
|
|
// Run performs the execution of 'create job' sub command
|
2019-01-12 04:58:27 +00:00
|
|
|
func (o *CreateJobOptions) Run() error {
|
|
|
|
var job *batchv1.Job
|
|
|
|
if len(o.Image) > 0 {
|
|
|
|
job = o.createJob()
|
|
|
|
} else {
|
|
|
|
infos, err := o.Builder.
|
|
|
|
Unstructured().
|
|
|
|
NamespaceParam(o.Namespace).DefaultNamespace().
|
|
|
|
ResourceTypeOrNameArgs(false, o.From).
|
|
|
|
Flatten().
|
|
|
|
Latest().
|
|
|
|
Do().
|
|
|
|
Infos()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(infos) != 1 {
|
|
|
|
return fmt.Errorf("from must be an existing cronjob")
|
|
|
|
}
|
|
|
|
|
|
|
|
uncastVersionedObj, err := scheme.Scheme.ConvertToVersion(infos[0].Object, batchv1beta1.SchemeGroupVersion)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("from must be an existing cronjob: %v", err)
|
|
|
|
}
|
|
|
|
cronJob, ok := uncastVersionedObj.(*batchv1beta1.CronJob)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("from must be an existing cronjob")
|
|
|
|
}
|
|
|
|
|
|
|
|
job = o.createJobFromCronJob(cronJob)
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
|
|
|
|
if err := util.CreateOrUpdateAnnotation(o.CreateAnnotation, job, scheme.DefaultJSONEncoder()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
if err := o.DryRunVerifier.HasSupport(job.GroupVersionKind()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
createOptions.DryRun = []string{metav1.DryRunAll}
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
var err error
|
2020-03-26 21:07:15 +00:00
|
|
|
job, err = o.Client.Jobs(o.Namespace).Create(context.TODO(), job, createOptions)
|
2019-01-12 04:58:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create job: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return o.PrintObj(job)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *CreateJobOptions) createJob() *batchv1.Job {
|
2020-08-10 17:43:49 +00:00
|
|
|
job := &batchv1.Job{
|
2019-01-12 04:58:27 +00:00
|
|
|
// this is ok because we know exactly how we want to be serialized
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
|
|
|
|
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.RestartPolicyNever,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
if o.EnforceNamespace {
|
|
|
|
job.Namespace = o.Namespace
|
|
|
|
}
|
|
|
|
return job
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *CreateJobOptions) createJobFromCronJob(cronJob *batchv1beta1.CronJob) *batchv1.Job {
|
|
|
|
annotations := make(map[string]string)
|
|
|
|
annotations["cronjob.kubernetes.io/instantiate"] = "manual"
|
|
|
|
for k, v := range cronJob.Spec.JobTemplate.Annotations {
|
|
|
|
annotations[k] = v
|
|
|
|
}
|
2019-08-30 18:33:25 +00:00
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
job := &batchv1.Job{
|
2019-01-12 04:58:27 +00:00
|
|
|
// this is ok because we know exactly how we want to be serialized
|
|
|
|
TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
|
|
|
|
ObjectMeta: metav1.ObjectMeta{
|
|
|
|
Name: o.Name,
|
|
|
|
Annotations: annotations,
|
|
|
|
Labels: cronJob.Spec.JobTemplate.Labels,
|
2019-08-30 18:33:25 +00:00
|
|
|
OwnerReferences: []metav1.OwnerReference{
|
2020-08-10 17:43:49 +00:00
|
|
|
{
|
|
|
|
APIVersion: batchv1beta1.SchemeGroupVersion.String(),
|
|
|
|
Kind: cronJob.Kind,
|
|
|
|
Name: cronJob.GetName(),
|
|
|
|
UID: cronJob.GetUID(),
|
|
|
|
},
|
2019-08-30 18:33:25 +00:00
|
|
|
},
|
2019-01-12 04:58:27 +00:00
|
|
|
},
|
|
|
|
Spec: cronJob.Spec.JobTemplate.Spec,
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
if o.EnforceNamespace {
|
|
|
|
job.Namespace = o.Namespace
|
|
|
|
}
|
|
|
|
return job
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|