Skip to content

Commit

Permalink
Version validation and readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
sjakati98 committed Jul 11, 2019
1 parent ef04b67 commit 8d7c354
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*.so
*.dylib


nginx-operator/
# Test binary, build with `go test -c`
*.test

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ go run main.go convert <OperatorName> --helm-chart=/path/to/chart --kind=Kind --
To create an operator from an *external* helm chart:
```
go run main.go convert <OperatorName> --helm-chart-repo=https://charts.example.io/ --helm-chart=example-chart --kind=Kind --api-version=apps.example.com/v1alpha1
```
```

## Common Problems
If you are experiencing build errors: `go: error loading module requirements`, execute the following command `export GO111MODULES=off` within the operator folder.
5 changes: 5 additions & 0 deletions cmd/helm2go-operator-sdk/convert/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func convertFunc(cmd *cobra.Command, args []string) error {
return err
}

if err := verifyOperatorSDKVersion(); err != nil {
log.Error(err)
return err
}

log.Infof("🤠 Converting Existing Helm Chart %s to Go Operator %s!", helmChartRef, outputDir)

// load the spcecified helm chart
Expand Down
84 changes: 84 additions & 0 deletions cmd/helm2go-operator-sdk/convert/cmd_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package convert

import (
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
)

Expand Down Expand Up @@ -50,3 +53,84 @@ func checkKindString(kind string) error {
}
return nil
}

func verifyOperatorSDKVersion() error {
var (
cmdOut []byte
err error
)
cmdName := "operator-sdk"
cmdArgs := []string{"version"}

// if operator-sdk is not installed, or not updated this will throw an error
if cmdOut, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil {
return fmt.Errorf("unexpected error: %v when verifying operator-sdk version; please install operator-sdk or update to latest version", err)
}
// make sure operator-sdk is atleast version 0.8.0 or higher
if err = matchVersion(&cmdOut); err != nil {
return fmt.Errorf("unexpected error: %v when verifying operator-sdk version; please update to latest version", err)
}

return nil
}

func matchVersion(cmdOut *[]byte) error {
pattern := regexp.MustCompile(`.*version\: +v(\d.\d.\d).*commit\: +(.*)`)
matches := pattern.FindStringSubmatch(string(*cmdOut))

fmt.Println(matches)

if l := len(matches); l != 2+1 {
return fmt.Errorf("expected three matches, received %d instead", l)
}

version := matches[1]
if len(version) == 0 {
return fmt.Errorf("expected operator-sdk version number, got: %v", version)
}

outdated, err := outdatedVersion(version)
if err != nil {
return err
}
if outdated {
return fmt.Errorf("operator-sdk version is outdated")
}

return nil

}

func outdatedVersion(version string) (bool, error) {

pattern := regexp.MustCompile(`^(\d)\.(\d)\.(\d)$`)
matches := pattern.FindStringSubmatch(version)

if l := len(matches); l != 3+1 {
return true, fmt.Errorf("expected four matches, received %d instead", l)
}

first := matches[1]
second := matches[2]

if len(first) == 0 || len(second) == 0 {
return true, fmt.Errorf("error parsing version number: %v", version)
}

firstInt, err := strconv.Atoi(first)
if err != nil {
return true, err
}
secondInt, err := strconv.Atoi(second)
if err != nil {
return true, err
}

if firstInt == 0 {
if secondInt < 8 {
return true, nil
}
}

return false, nil
}
1 change: 1 addition & 0 deletions pkg/render/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

// takes a map of injected files and writes them to a temp directory
func writeToTemp(files map[string]string, outParentDir string) (string, error) {
os.RemoveAll(filepath.Join(outParentDir, "temp"))
err := os.Mkdir(filepath.Join(outParentDir, "temp"), 0700)
if err != nil {
return "", err
Expand Down

0 comments on commit 8d7c354

Please sign in to comment.