Skip to content
This repository was archived by the owner on Aug 9, 2024. It is now read-only.
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
16 changes: 3 additions & 13 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ var cfgFile string
// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "crd-validation",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Short: "Generator for kubeflow crd validation schema",
Long: `crd-validation is a CLI library to generate the needed files to validate custom objects
passed by user via OpenAPI v3 schema.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Expand All @@ -52,13 +48,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.

RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.crd-validation.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

Expand Down
13 changes: 9 additions & 4 deletions cmd/tfjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -48,11 +50,14 @@ var tfjobCmd = &cobra.Command{

func generateTFJob() {
original := config.NewCustomResourceDefinition("tfjob")
var outputDir string
var outputDir, jobVersion string
if viper.Get("global") != nil {
outputDir = viper.Get("global").(map[string]interface{})["output"].(string)
outputDir = viper.GetString("global.output")
jobVersion = viper.GetString("tfjob.spec.version")
}
generator := crd.NewTFJobGenerator(outputDir)
filename := fmt.Sprintf("tfjob-crd-%v.yaml", jobVersion)

generator := crd.NewTFJobGenerator(jobVersion)
final := generator.Generate(original)
generator.Export(final)
generator.Export(final, outputDir, filename)
}
15 changes: 5 additions & 10 deletions pkg/crd/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ import (

// Exporter exports the yaml config to file.
type Exporter struct {
outputDir string
filename string
writer *bufio.Writer
writer *bufio.Writer
}

func New(outputDir, filename string) *Exporter {
return &Exporter{
outputDir: outputDir,
filename: filename,
}
func New() *Exporter {
return &Exporter{}
}

// Export exports the yaml config to file.
func (e *Exporter) Export(final *apiextensions.CustomResourceDefinition) {
file, err := filepath.Abs(filepath.Join(e.outputDir, e.filename))
func (e *Exporter) Export(final *apiextensions.CustomResourceDefinition, outputDir string, filename string) {
file, err := filepath.Abs(filepath.Join(outputDir, filename))
if err != nil {
log.Fatalf("Failed to open the output file %s: %v", file, err)
}
Expand Down
36 changes: 22 additions & 14 deletions pkg/crd/tfjob.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,44 @@
package crd

import (
tfv1alpha2 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1alpha2"
log "github.com/sirupsen/logrus"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"fmt"

"github.com/kubeflow/crd-validation/pkg/crd/exporter"
"github.com/kubeflow/crd-validation/pkg/utils"
)

const (
// CRDName is the name for TFJob.
CRDName = "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1alpha2.TFJob"

generatedFile = "tfjob-crd-v1alpha2.yaml"
tfv1alpha2 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1alpha2"
tfv1beta1 "github.com/kubeflow/tf-operator/pkg/apis/tensorflow/v1beta1"
log "github.com/sirupsen/logrus"
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
)

// TFJobGenerator is the type for TFJob CRD generator.
type TFJobGenerator struct {
*exporter.Exporter
jobVersion string
}

// NewTFJobGenerator creates a new TFJob CRD generator.
func NewTFJobGenerator(outputDir string) *TFJobGenerator {
func NewTFJobGenerator(version string) *TFJobGenerator {
return &TFJobGenerator{
Exporter: exporter.New(outputDir, generatedFile),
Exporter: exporter.New(),
jobVersion: version,
}
}

// Generate generates the crd.
func (t TFJobGenerator) Generate(original *apiextensions.CustomResourceDefinition) *apiextensions.CustomResourceDefinition {
func (t *TFJobGenerator) Generate(original *apiextensions.CustomResourceDefinition) *apiextensions.CustomResourceDefinition {
log.Println("Generating validation for TFJob")
original.Spec.Validation = utils.GetCustomResourceValidation(CRDName, tfv1alpha2.GetOpenAPIDefinitions)

// CRDName is the name for TFJob.
CRDName := fmt.Sprintf("github.com/kubeflow/tf-operator/pkg/apis/tensorflow/%v.TFJob", t.jobVersion)

if t.jobVersion == "v1alpha2" {
original.Spec.Validation = utils.GetCustomResourceValidation(CRDName, tfv1alpha2.GetOpenAPIDefinitions)
} else if t.jobVersion == "v1beta1" {
original.Spec.Validation = utils.GetCustomResourceValidation(CRDName, tfv1beta1.GetOpenAPIDefinitions)
} else {
log.Errorf("Invalid version of TFJob %s", t.jobVersion)
}

return original
}