Skip to content

Commit

Permalink
Merge pull request #419 from carlory/feature/shortnames
Browse files Browse the repository at this point in the history
add --short-name flag for resource creation commands
  • Loading branch information
k8s-ci-robot authored Sep 9, 2019
2 parents e281736 + b3a909d commit 733ee74
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion cmd/apiserver-boot/boot/create/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

var kindName string
var resourceName string
var shortName string
var nonNamespacedKind bool
var skipGenerateAdmissionController bool
var skipGenerateResource bool
Expand All @@ -54,6 +55,7 @@ apiserver-boot create group version resource --group insect --version v1beta1 --
func AddCreateResource(cmd *cobra.Command) {
RegisterResourceFlags(createResourceCmd)

createResourceCmd.Flags().StringVar(&shortName, "short-name", "", "if set, add a short name for the resource. It must be all lowercase.")
createResourceCmd.Flags().BoolVar(&nonNamespacedKind, "non-namespaced", false, "if set, the API kind will be non namespaced")

createResourceCmd.Flags().BoolVar(&skipGenerateResource, "skip-resource", false, "if set, the resources will not be generated")
Expand Down Expand Up @@ -112,6 +114,7 @@ func createResource(boilerplate string) {
versionName,
kindName,
resourceName,
shortName,
util.Repo,
inflect.NewDefaultRuleset().Pluralize(kindName),
nonNamespacedKind,
Expand Down Expand Up @@ -271,6 +274,7 @@ type resourceTemplateArgs struct {
Version string
Kind string
Resource string
ShortName string
Repo string
PluralizedKind string
NonNamespacedKind bool
Expand Down Expand Up @@ -323,7 +327,7 @@ import (
// {{.Kind}}
// +k8s:openapi-gen=true
// +resource:path={{.Resource}},strategy={{.Kind}}Strategy
// +resource:path={{.Resource}},strategy={{.Kind}}Strategy{{ if .ShortName }},shortname={{.ShortName}}{{ end }}
type {{.Kind}} struct {
metav1.TypeMeta ` + "`" + `json:",inline"` + "`" + `
metav1.ObjectMeta ` + "`" + `json:"metadata,omitempty"` + "`" + `
Expand Down
12 changes: 10 additions & 2 deletions cmd/apiserver-boot/boot/create/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ package create
import (
"bufio"
"fmt"
"k8s.io/klog"
"regexp"
"strings"

"github.com/markbates/inflect"
"github.com/spf13/cobra"

utilvalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/klog"

"sigs.k8s.io/apiserver-builder-alpha/cmd/apiserver-boot/boot/util"
)

Expand All @@ -43,6 +45,12 @@ func ValidateResourceFlags() {
if len(resourceName) == 0 {
resourceName = inflect.NewDefaultRuleset().Pluralize(strings.ToLower(kindName))
}
if len(shortName) > 0 {
if errs := utilvalidation.IsDNS1035Label(shortName); len(errs) > 0 {
detail := strings.Join(errs, ",")
klog.Fatalf("--short-name %q has bad format: %s", shortName, detail)
}
}

groupMatch := regexp.MustCompile("^[a-z]+$")
if !groupMatch.MatchString(groupName) {
Expand Down Expand Up @@ -93,4 +101,4 @@ func readstdin(reader *bufio.Reader) string {
klog.Fatalf("Error when reading input: %v", err)
}
return strings.TrimSpace(text)
}
}

0 comments on commit 733ee74

Please sign in to comment.