2019-08-01 14:01:40 -04:00
/ *
Copyright 2017 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-09-18 11:00:29 -04:00
"context"
"fmt"
2019-08-01 14:01:40 -04:00
"github.com/spf13/cobra"
2020-09-18 11:00:29 -04:00
corev1 "k8s.io/api/core/v1"
schedulingv1 "k8s.io/api/scheduling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
2019-08-01 14:01:40 -04:00
"k8s.io/cli-runtime/pkg/genericclioptions"
2020-09-18 11:00:29 -04:00
"k8s.io/cli-runtime/pkg/resource"
schedulingv1client "k8s.io/client-go/kubernetes/typed/scheduling/v1"
2019-08-01 14:01:40 -04:00
cmdutil "k8s.io/kubectl/pkg/cmd/util"
2020-09-18 11:00:29 -04:00
"k8s.io/kubectl/pkg/scheme"
"k8s.io/kubectl/pkg/util"
2019-08-01 14:01:40 -04:00
"k8s.io/kubectl/pkg/util/i18n"
"k8s.io/kubectl/pkg/util/templates"
)
var (
pcLong = templates . LongDesc ( i18n . T ( `
2021-07-06 15:05:26 -04:00
Create a priority class with the specified name , value , globalDefault and description . ` ) )
2019-08-01 14:01:40 -04:00
pcExample = templates . Examples ( i18n . T ( `
2021-07-06 15:05:26 -04:00
# Create a priority class named high - priority
2019-08-01 14:01:40 -04:00
kubectl create priorityclass high - priority -- value = 1000 -- description = "high priority"
2021-07-06 15:05:26 -04:00
# Create a priority class named default - priority that is considered as the global default priority
2019-08-01 14:01:40 -04:00
kubectl create priorityclass default - priority -- value = 1000 -- global - default = true -- description = "default priority"
2020-02-13 16:28:22 -05:00
2021-07-06 15:05:26 -04:00
# Create a priority class named high - priority that cannot preempt pods with lower priority
2019-08-01 14:01:40 -04:00
kubectl create priorityclass high - priority -- value = 1000 -- description = "high priority" -- preemption - policy = "Never" ` ) )
)
2020-09-18 11:00:29 -04:00
// PriorityClassOptions holds the options for 'create priorityclass' sub command
type PriorityClassOptions struct {
PrintFlags * genericclioptions . PrintFlags
PrintObj func ( obj runtime . Object ) error
Name string
Value int32
GlobalDefault bool
Description string
PreemptionPolicy string
FieldManager string
CreateAnnotation bool
Client * schedulingv1client . SchedulingV1Client
DryRunStrategy cmdutil . DryRunStrategy
2022-03-09 09:51:50 -05:00
DryRunVerifier * resource . QueryParamVerifier
2020-09-18 11:00:29 -04:00
genericclioptions . IOStreams
}
2020-12-01 17:05:34 -05:00
// NewPriorityClassOptions returns an initialized PriorityClassOptions instance
2020-09-18 11:00:29 -04:00
func NewPriorityClassOptions ( ioStreams genericclioptions . IOStreams ) * PriorityClassOptions {
return & PriorityClassOptions {
Value : 0 ,
PreemptionPolicy : "PreemptLowerPriority" ,
PrintFlags : genericclioptions . NewPrintFlags ( "created" ) . WithTypeSetter ( scheme . Scheme ) ,
IOStreams : ioStreams ,
}
2019-08-01 14:01:40 -04:00
}
// NewCmdCreatePriorityClass is a macro command to create a new priorityClass.
func NewCmdCreatePriorityClass ( f cmdutil . Factory , ioStreams genericclioptions . IOStreams ) * cobra . Command {
2020-09-18 11:00:29 -04:00
o := NewPriorityClassOptions ( ioStreams )
2019-08-01 14:01:40 -04:00
cmd := & cobra . Command {
2020-02-13 16:28:22 -05:00
Use : "priorityclass NAME --value=VALUE --global-default=BOOL [--dry-run=server|client|none]" ,
2019-08-01 14:01:40 -04:00
DisableFlagsInUseLine : true ,
Aliases : [ ] string { "pc" } ,
2021-07-06 15:05:26 -04:00
Short : i18n . T ( "Create a priority class with the specified name" ) ,
2019-08-01 14:01:40 -04:00
Long : pcLong ,
Example : pcExample ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
2020-09-18 11:00:29 -04:00
cmdutil . CheckErr ( o . Complete ( f , cmd , args ) )
cmdutil . CheckErr ( o . Run ( ) )
2019-08-01 14:01:40 -04:00
} ,
}
2020-09-18 11:00:29 -04:00
o . PrintFlags . AddFlags ( cmd )
2019-08-01 14:01:40 -04:00
cmdutil . AddApplyAnnotationFlags ( cmd )
cmdutil . AddValidateFlags ( cmd )
2020-09-18 11:00:29 -04:00
cmdutil . AddDryRunFlag ( cmd )
cmd . Flags ( ) . Int32Var ( & o . Value , "value" , o . Value , i18n . T ( "the value of this priority class." ) )
cmd . Flags ( ) . BoolVar ( & o . GlobalDefault , "global-default" , o . GlobalDefault , i18n . T ( "global-default specifies whether this PriorityClass should be considered as the default priority." ) )
cmd . Flags ( ) . StringVar ( & o . Description , "description" , o . Description , i18n . T ( "description is an arbitrary string that usually provides guidelines on when this priority class should be used." ) )
cmd . Flags ( ) . StringVar ( & o . PreemptionPolicy , "preemption-policy" , o . PreemptionPolicy , i18n . T ( "preemption-policy is the policy for preempting pods with lower priority." ) )
cmdutil . AddFieldManagerFlagVar ( cmd , & o . FieldManager , "kubectl-create" )
2019-08-01 14:01:40 -04:00
return cmd
}
// Complete completes all the required options
2020-09-18 11:00:29 -04:00
func ( o * PriorityClassOptions ) Complete ( f cmdutil . Factory , cmd * cobra . Command , args [ ] string ) error {
var err error
o . Name , err = NameFromCommandArgs ( cmd , args )
if err != nil {
return err
}
restConfig , err := f . ToRESTConfig ( )
if err != nil {
return err
}
o . Client , err = schedulingv1client . NewForConfig ( restConfig )
if err != nil {
return err
}
o . CreateAnnotation = cmdutil . GetFlagBool ( cmd , cmdutil . ApplyAnnotationsFlag )
o . DryRunStrategy , err = cmdutil . GetDryRunStrategy ( cmd )
if err != nil {
return err
}
dynamicClient , err := f . DynamicClient ( )
if err != nil {
return err
}
2022-03-09 09:51:50 -05:00
o . DryRunVerifier = resource . NewQueryParamVerifier ( dynamicClient , f . OpenAPIGetter ( ) , resource . QueryParamDryRun )
2020-09-18 11:00:29 -04:00
cmdutil . PrintFlagsWithDryRunStrategy ( o . PrintFlags , o . DryRunStrategy )
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
}
// Run calls the CreateSubcommandOptions.Run in the PriorityClassOptions instance
func ( o * PriorityClassOptions ) Run ( ) error {
priorityClass , err := o . createPriorityClass ( )
2019-08-01 14:01:40 -04:00
if err != nil {
return err
}
2020-09-18 11:00:29 -04:00
if err := util . CreateOrUpdateAnnotation ( o . CreateAnnotation , priorityClass , scheme . DefaultJSONEncoder ( ) ) ; err != nil {
return err
}
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 ( priorityClass . GroupVersionKind ( ) ) ; err != nil {
return err
}
createOptions . DryRun = [ ] string { metav1 . DryRunAll }
}
var err error
priorityClass , err = o . Client . PriorityClasses ( ) . Create ( context . TODO ( ) , priorityClass , createOptions )
if err != nil {
2021-02-03 21:46:10 -05:00
return fmt . Errorf ( "failed to create priorityclass: %v" , err )
2019-08-01 14:01:40 -04:00
}
}
2020-09-18 11:00:29 -04:00
return o . PrintObj ( priorityClass )
2019-08-01 14:01:40 -04:00
}
2020-09-18 11:00:29 -04:00
func ( o * PriorityClassOptions ) createPriorityClass ( ) ( * schedulingv1 . PriorityClass , error ) {
preemptionPolicy := corev1 . PreemptionPolicy ( o . PreemptionPolicy )
return & schedulingv1 . PriorityClass {
// this is ok because we know exactly how we want to be serialized
TypeMeta : metav1 . TypeMeta { APIVersion : schedulingv1 . SchemeGroupVersion . String ( ) , Kind : "PriorityClass" } ,
ObjectMeta : metav1 . ObjectMeta {
Name : o . Name ,
} ,
Value : o . Value ,
GlobalDefault : o . GlobalDefault ,
Description : o . Description ,
PreemptionPolicy : & preemptionPolicy ,
} , nil
2019-08-01 14:01:40 -04:00
}