Skip to content

Commit

Permalink
Kfdef converter (kubeflow#4298)
Browse files Browse the repository at this point in the history
* kfdef-converter init

* add root cmd

* change naming

* add flag --out

* load kfdef into kfconfig

* use configconverters to convert KfDef

* comment

* simple README

* add example cmd
  • Loading branch information
gabrielwen authored and k8s-ci-robot committed Oct 15, 2019
1 parent 27da9c8 commit fedf5d9
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bootstrap/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ build-bootstrap-dev-gcb:

#***************************************************************************************************

generate-kfdef-converter: generate
@${GO} generate ./cmd/kfdefConverter/...

build-kfdef-converter: generate-kfdef-converter fmt vet
${GO} build -gcflags '-N -l' -ldflags "-X main.VERSION=$(TAG)" -o bin/kfdef-converter cmd/kfdefConverter/main.go

#***************************************************************************************************

clean:
rm -rf test && mkdir test

Expand Down
30 changes: 30 additions & 0 deletions bootstrap/cmd/kfdefConverter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# KfDef version converter

## Overview

This is a simple helper CLI that converts KfDef between versions.

## Usage

```
A simple CLI to convert KfDef from v1alpha1 to v1beta1
Usage:
kfdef-converter [command]
Available Commands:
help Help about any command
tov1beta1 Convert a KfDef config in v1alpha1 into v1beta1 format.
Flags:
-h, --help help for kfdef-converter
Use "kfdef-converter [command] --help" for more information about a command.
```

### Example conversion

Converting to `v1beta1`:
```bash
kfdef-converter tov1beta1 foo/kfdef.yaml -o path/to/output.yaml
```
34 changes: 34 additions & 0 deletions bootstrap/cmd/kfdefConverter/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"os"
)

var rootCmd = &cobra.Command{
Use: "kfdef-converter",
Short: "A simple CLI to convert KfDef from v1alpha1 to v1beta1.",
Long: "A simple CLI to convert KfDef from v1alpha1 to v1beta1",
}

var (
// VERSION is set during build.
VERSION string
)

func Execute(version string) {
VERSION = version

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
cobra.OnInitialize(initConfig)
}

func initConfig() {
}
67 changes: 67 additions & 0 deletions bootstrap/cmd/kfdefConverter/cmd/tov1beta1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cmd

import (
"fmt"
"strings"

"github.com/kubeflow/kubeflow/bootstrap/v3/pkg/apis/apps/configconverters"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var tov1beta1Cfg = viper.New()

var (
OutPathFlag = "out"
)

var tov1beta1Cmd = &cobra.Command{
Use: "tov1beta1 <kfdef-v1alpha1-filename>",
Short: "Convert a KfDef config in v1alpha1 into v1beta1 format.",
Long: "Convert a KfDef config in v1alpha1 into v1beta1 format.",

RunE: func(cmd *cobra.Command, args []string) error {
log.SetLevel(log.InfoLevel)

if len(args) > 1 {
return fmt.Errorf("Unknown args: %v", args[1:])
} else if len(args) < 1 {
return fmt.Errorf("Filename to converting KfDef is required.")
}

config, err := configconverters.LoadConfigFromURI(args[0])
if err != nil {
return fmt.Errorf("Error when loading KfDef: %v", err)
}

log.Infof("KfDef loaded for %v, converting.", args[0])
outPath := tov1beta1Cfg.GetString(OutPathFlag)
if outPath == "" {
outPath = args[0]
}

// A hack to force converter to output KfDef in v1beta1.
apiVersion := strings.Split(config.APIVersion, "/")
if len(apiVersion) != 2 {
return fmt.Errorf("Unknown format of API version: %v", config.APIVersion)
}
apiVersion[1] = "v1beta1"
config.APIVersion = strings.Join(apiVersion, "/")
return configconverters.WriteConfigToFile(*config, outPath)
},
}

func init() {
rootCmd.AddCommand(tov1beta1Cmd)

tov1beta1Cfg.SetConfigName("conversion")
tov1beta1Cfg.SetConfigType("yaml")

tov1beta1Cmd.Flags().StringP(OutPathFlag, "o", "",
"Path to write converted KfDef. If not set, the original file will be overriden.")
if err := tov1beta1Cfg.BindPFlag(OutPathFlag, tov1beta1Cmd.Flags().Lookup(OutPathFlag)); err != nil {
log.Errorf("couldn't set flag --%v: %v", OutPathFlag, err)
return
}
}
22 changes: 22 additions & 0 deletions bootstrap/cmd/kfdefConverter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/kubeflow/kubeflow/bootstrap/v3/cmd/kfdefConverter/cmd"
"github.com/onrik/logrus/filename"
log "github.com/sirupsen/logrus"
)

var (
VERSION = "0.0.1"
)

func init() {
// Add filename as one of the fields of the structured log message.
filenameHook := filename.NewHook()
filenameHook.Field = "filename"
log.AddHook(filenameHook)
}

func main() {
cmd.Execute(VERSION)
}

0 comments on commit fedf5d9

Please sign in to comment.