Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First version of crd generator #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions cmd/codegen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2020 The Kubermatic 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 main

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/kubermatic/utils/pkg/codegen/scaffold"
)

func main() {
opts := &scaffold.ScaffoldOptions{
Resource: scaffold.Resource{
Version: "v1alpha1",
},
Spec: scaffold.Spec{
Metadata: true,
},
Boilerplate: "./hack/boilerplate/boilerplate.go.txt",
}
scaffoldCmd := &cobra.Command{
Use: "crdgen",
Short: "Quickly scaffold out basic bits of a Kubernetes type.",
Long: `Quickly scaffold out the structure of a type for a Kubernetes kind and associated types.
Produces:

- a root type with appropriate metadata fields
- Spec and Status types
- a list type

Also applies the appropriate comments to generate the code required to conform to runtime.Object.`,
Example: ` # Generate types for a Kind called Foo with a resource called foos
crdgen --kind Foo

# Generate types for a Kind called Bar with metadata in its Spec
crdgen --kind Bar --metadata`,
RunE: func(_ *cobra.Command, _ []string) error {
if err := opts.Validate(); err != nil {
return err
}
return opts.Scaffold()
},
}

scaffoldCmd.Flags().StringVar(&opts.OutputPath, "output", opts.OutputPath, "the Path of the generated file, for example foo_types.go")
scaffoldCmd.Flags().StringVar(&opts.Resource.Kind, "kind", opts.Resource.Kind, "The Kind of the typescaffold being scaffolded.")
scaffoldCmd.Flags().StringVar(&opts.Resource.Version, "version", opts.Resource.Version, "The Version of the typescaffold being scaffolded.")
scaffoldCmd.Flags().StringVar(&opts.Boilerplate, "boilerplate", opts.Boilerplate, "The Boilerplate header file path.")
scaffoldCmd.Flags().BoolVar(&opts.Spec.Metadata, "metadata", opts.Spec.Metadata, "Whether wants to generate metadata fields in the Spec of this Kind")

if err := cobra.MarkFlagRequired(scaffoldCmd.Flags(), "kind"); err != nil {
panic("unable to mark --kind as required")
}

if err := scaffoldCmd.Execute(); err != nil {
if _, err := fmt.Fprintln(os.Stderr, err); err != nil {
// this would be exceedingly bizarre if we ever got here
panic("unable to write to error details to standard error")
}
os.Exit(1)
}
}
37 changes: 37 additions & 0 deletions pkg/codegen/scaffold/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2020 The Kubermatic 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 scaffold

import (
"fmt"
)

// Resource contains the information required to scaffold files for a resource.
type Resource struct {
// Kind is the API Kind.
Kind string
// Version is the API Version.
Version string
}

// Validate checks the Resource values to make sure they are valid.
func (r *Resource) Validate() error {
if len(r.Kind) == 0 {
return fmt.Errorf("kind cannot be empty")
}
return nil
}
Loading