2017-10-31 09:16:25 +00: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 .
* /
2018-04-10 14:21:50 +00:00
package create
2017-10-31 09:16:25 +00:00
import (
"github.com/spf13/cobra"
2018-08-21 10:46:39 +00:00
"k8s.io/cli-runtime/pkg/genericclioptions"
2017-10-31 09:16:25 +00:00
"k8s.io/kubernetes/pkg/kubectl"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
2018-10-10 18:29:30 +00:00
"k8s.io/kubernetes/pkg/kubectl/util/templates"
2017-10-31 09:16:25 +00:00
)
var (
pcLong = templates . LongDesc ( i18n . T ( `
Create a priorityclass with the specified name , value , globalDefault and description ` ) )
pcExample = templates . Examples ( i18n . T ( `
# Create a priorityclass named high - priority
2018-07-02 06:55:49 +00:00
kubectl create priorityclass high - priority -- value = 1000 -- description = "high priority"
2017-10-31 09:16:25 +00:00
# Create a priorityclass named default - priority that considered as the global default priority
kubectl create priorityclass default - priority -- value = 1000 -- global - default = true -- description = "default priority" ` ) )
)
2018-04-05 22:39:17 +00:00
type PriorityClassOpts struct {
CreateSubcommandOptions * CreateSubcommandOptions
}
2017-10-31 09:16:25 +00:00
// NewCmdCreatePriorityClass is a macro command to create a new priorityClass.
2018-04-19 21:43:28 +00:00
func NewCmdCreatePriorityClass ( f cmdutil . Factory , ioStreams genericclioptions . IOStreams ) * cobra . Command {
2018-04-05 22:39:17 +00:00
options := & PriorityClassOpts {
2018-04-19 21:43:28 +00:00
CreateSubcommandOptions : NewCreateSubcommandOptions ( ioStreams ) ,
2018-04-05 22:39:17 +00:00
}
2017-10-31 09:16:25 +00:00
cmd := & cobra . Command {
2018-10-05 19:59:38 +00:00
Use : "priorityclass NAME --value=VALUE --global-default=BOOL [--dry-run]" ,
2017-10-11 06:26:02 +00:00
DisableFlagsInUseLine : true ,
Aliases : [ ] string { "pc" } ,
Short : i18n . T ( "Create a priorityclass with the specified name." ) ,
Long : pcLong ,
Example : pcExample ,
2017-10-31 09:16:25 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2018-05-07 21:47:29 +00:00
cmdutil . CheckErr ( options . Complete ( f , cmd , args ) )
cmdutil . CheckErr ( options . Run ( ) )
2017-10-31 09:16:25 +00:00
} ,
}
2018-04-05 22:39:17 +00:00
options . CreateSubcommandOptions . PrintFlags . AddFlags ( cmd )
2017-10-31 09:16:25 +00:00
cmdutil . AddApplyAnnotationFlags ( cmd )
cmdutil . AddValidateFlags ( cmd )
cmdutil . AddGeneratorFlags ( cmd , cmdutil . PriorityClassV1Alpha1GeneratorName )
cmd . Flags ( ) . Int32 ( "value" , 0 , i18n . T ( "the value of this priority class." ) )
cmd . Flags ( ) . Bool ( "global-default" , false , i18n . T ( "global-default specifies whether this PriorityClass should be considered as the default priority." ) )
cmd . Flags ( ) . String ( "description" , "" , i18n . T ( "description is an arbitrary string that usually provides guidelines on when this priority class should be used." ) )
return cmd
}
2018-05-07 21:47:29 +00:00
func ( o * PriorityClassOpts ) Complete ( f cmdutil . Factory , cmd * cobra . Command , args [ ] string ) error {
2017-10-31 09:16:25 +00:00
name , err := NameFromCommandArgs ( cmd , args )
if err != nil {
return err
}
2018-04-05 22:39:17 +00:00
2017-10-31 09:16:25 +00:00
var generator kubectl . StructuredGenerator
switch generatorName := cmdutil . GetFlagString ( cmd , "generator" ) ; generatorName {
case cmdutil . PriorityClassV1Alpha1GeneratorName :
generator = & kubectl . PriorityClassV1Generator {
Name : name ,
Value : cmdutil . GetFlagInt32 ( cmd , "value" ) ,
GlobalDefault : cmdutil . GetFlagBool ( cmd , "global-default" ) ,
Description : cmdutil . GetFlagString ( cmd , "description" ) ,
}
default :
return errUnsupportedGenerator ( cmd , generatorName )
}
2018-04-05 22:39:17 +00:00
2018-05-07 21:47:29 +00:00
return o . CreateSubcommandOptions . Complete ( f , cmd , args , generator )
2018-04-05 22:39:17 +00:00
}
// CreatePriorityClass implements the behavior to run the create priorityClass command.
2018-05-07 21:47:29 +00:00
func ( o * PriorityClassOpts ) Run ( ) error {
return o . CreateSubcommandOptions . Run ( )
2017-10-31 09:16:25 +00:00
}