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

[go] import command implemented #543

Merged
merged 5 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 32 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ var (
inPlaceConvert bool
// noColor contains the boolean flag to disable color output
noColor bool
// importNamespace is the namespace that contains the release to be imported
importNamespace string
// importRelease is the name of the release to be imported
importRelease string
// importRepository is the helm repository for the imported release
importRepository string
)

func init() {
Expand All @@ -65,6 +71,10 @@ func init() {

convertCmd.Flags().BoolVarP(&inPlaceConvert, "in-place", "i", false, "If specified, will update the file in place, otherwise outputs to stdout.")

importCmd.Flags().StringVar(&importNamespace, "namespace", "", "Namespace that contains the release to be imported.")
importCmd.Flags().StringVar(&importRelease, "release_name", "", "The name of the release to import.")
importCmd.Flags().StringVar(&importRepository, "repository", "", "The helm repository for the imported release.")

// add commands here
rootCmd.AddCommand(plotCmd)
rootCmd.AddCommand(convertCmd)
Expand All @@ -73,6 +83,7 @@ func init() {
rootCmd.AddCommand(lintCmd)
rootCmd.AddCommand(getManifestsCmd)
rootCmd.AddCommand(updateCmd)
rootCmd.AddCommand(importCmd)

klog.InitFlags(nil)
pflag.CommandLine.AddGoFlag(flag.CommandLine.Lookup("v"))
Expand Down Expand Up @@ -262,6 +273,27 @@ var updateCmd = &cobra.Command{
},
}

var importCmd = &cobra.Command{
Use: "import",
Short: "import",
Long: "Outputs a release block that can be used to import the specified release.",
PreRunE: func(cmd *cobra.Command, args []string) error {
if importNamespace == "" || importRelease == "" {
return fmt.Errorf("import requires both a --namespace and --release")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
color.Yellow("Import is experimental and may be unreliable. Double check all output. Output assumes course schema v2.")
out, err := reckoner.ImportOutput(importRelease, importNamespace, importRepository)
if err != nil {
color.Red(err.Error())
os.Exit(1)
}
fmt.Println(out)
},
}

// getCourseFilePath returns the path to a course YAML file. This does not guarantee the file exists.
// The order of precedence is as follows (higher overrides lower)
// - command-line argument
Expand Down
2 changes: 1 addition & 1 deletion end_to_end_testing/tests/25_import.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ testcases:
- 'result.systemout ShouldContainSubstring "namespace: {{.namespace}}"'
- 'result.systemout ShouldContainSubstring "version: 0.4.3"'
- 'result.systemout ShouldContainSubstring "chart: basic-demo"'
- 'result.systemout ShouldContainSubstring "{{.release}}:"'
- 'result.systemout ShouldContainSubstring "name: {{.release}}"'
- 'result.systemout ShouldContainSubstring "expect-value: 1.05"'
- 'result.systemout ShouldContainSubstring "expect-bool-value: false"'
- name: 25 - cleanup
Expand Down
35 changes: 16 additions & 19 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/fatih/color v1.13.0
github.com/go-git/go-git/v5 v5.4.2
github.com/sergi/go-diff v1.1.0
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
Expand All @@ -15,26 +16,10 @@ require (
k8s.io/api v0.23.4
k8s.io/apimachinery v0.23.4
k8s.io/client-go v0.23.4
k8s.io/klog/v2 v2.30.0
sigs.k8s.io/controller-runtime v0.11.1
)

require (
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
)

require (
cloud.google.com/go v0.99.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
Expand All @@ -43,39 +28,51 @@ require (
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/go-git/go-git/v5 v5.4.2
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-logr/logr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/klog/v2 v2.30.0
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
14 changes: 14 additions & 0 deletions pkg/course/shellSecrets.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2020 Fairwinds
//
// 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 course

import (
Expand Down
44 changes: 42 additions & 2 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,53 @@ func (h Client) UpdateDependencies(path string) error {
return nil
}

// GetManifestString will run 'helm get manifest' on a given namespace and release and return string output.
func (h Client) GetManifestString(namespace, release string) (string, error) {
out, err := h.get("manifest", namespace, release)
if err != nil {
return "", err
}
return out, err
}

// GetUserSuppliedValues will run 'helm get values' on a given namespace and release and return []byte output suitable for yaml Marshaling.
func (h Client) GetUserSuppliedValuesYAML(namespace, release string) ([]byte, error) {
out, err := h.get("values", namespace, release, "--output", "yaml")
if err != nil {
return nil, err
}
return []byte(out), err
}

// ListNamespaceReleasesYAML will run 'helm list' on a given namespace and return []byte output suitable for yaml Marshaling.
func (h Client) ListNamespaceReleasesYAML(namespace string) ([]byte, error) {
out, err := h.list(namespace, "--output", "yaml")
if err != nil {
return nil, err
}
return []byte(out), nil
}

// get can run any 'helm get' command
func (h Client) get(kind, namespace, release string) (string, error) {
func (h Client) get(kind, namespace, release string, extraArgs ...string) (string, error) {
validKinds := []string{"all", "hooks", "manifest", "notes", "values"}
if !funk.Contains(validKinds, kind) {
return "", errors.New("invalid kind passed to helm: " + kind)
}
stdOut, stdErr, err := h.Exec("get", kind, "--namespace", namespace, release)
args := []string{"get", kind, "--namespace", namespace, release}
args = append(args, extraArgs...)
stdOut, stdErr, err := h.Exec(args...)
if err != nil && stdErr != "" {
return "", errors.New(stdErr)
}
return stdOut, nil
}

// list can run any 'helm list' command
func (h Client) list(namespace string, extraArgs ...string) (string, error) {
args := []string{"list", "--namespace", namespace}
args = append(args, extraArgs...)
stdOut, stdErr, err := h.Exec(args...)
if err != nil && stdErr != "" {
return "", errors.New(stdErr)
}
Expand Down
24 changes: 0 additions & 24 deletions pkg/helm/manifests.go

This file was deleted.

116 changes: 116 additions & 0 deletions pkg/reckoner/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2020 Fairwinds
//
// 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 reckoner

import (
"bytes"
"fmt"
"strings"

"github.com/Masterminds/semver/v3"
"github.com/fairwindsops/reckoner/pkg/course"
"github.com/fairwindsops/reckoner/pkg/helm"
"gopkg.in/yaml.v3"
)

type ImportInfo struct {
Chart string
Version string
Name string
Namespace string
}

type ImportValues map[string]interface{}

func ImportOutput(release, namespace, repository string) (string, error) {
helmClient, err := helm.NewClient()
if err != nil {
return "", err
}
releaseInfo, err := gatherReleaseInfo(helmClient, namespace, release)
if err != nil {
return "", err
}
if err := releaseInfo.parseChartVersion(); err != nil {
return "", err
}
userValues, err := gatherUserSuppliedValues(helmClient, namespace, release)
if err != nil {
return "", err
}

// set up and populate a release struct that will be used to output in yaml format
var releaseOut = new(course.Release)
releaseOut.Name = releaseInfo.Name
releaseOut.Namespace = releaseInfo.Namespace
releaseOut.Chart = releaseInfo.Chart
releaseOut.Version = releaseInfo.Version
releaseOut.Values = userValues
releaseOut.Repository = repository

// in order to set indent to 2 spaces we need to use a yaml encoder instead of yaml.Marshal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was fixed in Yaml v3 in the later versions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still seems to default to 4 spaces in my testing and I didn't see a way to change it in the library without opting for the encoder route. I can go look again.

ret := bytes.NewBuffer([]byte{})
encoder := yaml.NewEncoder(ret)
defer encoder.Close()
encoder.SetIndent(2)
if err := encoder.Encode(releaseOut); err != nil {
return "", err
}

return ret.String(), err
}

func gatherReleaseInfo(helmClient *helm.Client, namespace, release string) (*ImportInfo, error) {
var allReleases = new([]*ImportInfo)
releaseInfo, err := helmClient.ListNamespaceReleasesYAML(namespace)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(releaseInfo, allReleases); err != nil {
return nil, err
}
for _, r := range *allReleases {
if r.Name == release {
return r, nil
}
}
return nil, fmt.Errorf("could not find release %s in namespace %s", release, namespace)
}

func gatherUserSuppliedValues(helmClient *helm.Client, namespace, release string) (ImportValues, error) {
var values = make(ImportValues)
userValues, err := helmClient.GetUserSuppliedValuesYAML(namespace, release)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(userValues, values); err != nil {
return nil, err
}
return values, nil
}

func (i *ImportInfo) parseChartVersion() error {
splitChartVersion := strings.Split(i.Chart, "-")
if len(splitChartVersion) < 2 {
return fmt.Errorf("could not parse chart version from %s - expected at least one hyphen between chart name and version", i.Chart)
}
splitPoint := len(splitChartVersion) - 1
Comment on lines +106 to +109
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They made this annoying to get, didn't they?

i.Chart = strings.Join(splitChartVersion[:splitPoint], "-")
i.Version = splitChartVersion[splitPoint]
if _, err := semver.NewVersion(i.Version); err != nil {
return fmt.Errorf("go %s as version string which is not valid semver", i.Version)
}
return nil
}
Loading