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

new init prompt function and helper functions #5141

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 30 additions & 0 deletions pkg/skaffold/initializer/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ import (
"strings"

"github.com/AlecAivazis/survey/v2"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

// For testing
var (
BuildConfigFunc = buildConfig
PortForwardResourceFunc = portForwardResource
askOne = survey.AskOne
)

func buildConfig(image string, choices []string) (string, error) {
Expand Down Expand Up @@ -100,3 +104,29 @@ func portForwardResource(out io.Writer, imageName string) (int, error) {
responseInt, _ := strconv.Atoi(response)
return responseInt, nil
}

// ConfirmInitOptions prompts the user to confirm that they are okay with what skaffold will do if they
// run with the current config
func ConfirmInitOptions(out io.Writer, config *latest.SkaffoldConfig) (bool, error) {
builders := strings.Join(util.ListBuilders(&config.Build), ",")
deployers := strings.Join(util.ListDeployers(&config.Deploy), ",")

fmt.Fprintf(out, `If you choose to continue, skaffold will do the following:
- Create a skaffold config file for you
- Build your application using %s
- Deploy your application to your current kubernetes context using %s

`, builders, deployers)

var response bool
prompt := &survey.Confirm{
Message: "Would you like to continue?",
}
err := askOne(prompt, &response, nil)
if err != nil {
return true, err
}

// invert response because "no" == done and "yes" == !done
return !response, nil
}
76 changes: 76 additions & 0 deletions pkg/skaffold/initializer/prompt/prompt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2020 The Skaffold 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 prompt

import (
"errors"
"io/ioutil"
"testing"

"github.com/AlecAivazis/survey/v2"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestConfirmInitOptions(t *testing.T) {
tests := []struct {
description string
config *latest.SkaffoldConfig
promptResponse bool
expectedDone bool
shouldErr bool
}{
{
description: "yes response",
config: &latest.SkaffoldConfig{},
promptResponse: true,
expectedDone: false,
shouldErr: false,
},
{
description: "no response",
config: &latest.SkaffoldConfig{},
promptResponse: false,
expectedDone: true,
shouldErr: false,
},
{
description: "error",
config: &latest.SkaffoldConfig{},
promptResponse: false,
expectedDone: true,
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&askOne, func(_ survey.Prompt, response interface{}, _ ...survey.AskOpt) error {
r := response.(*bool)
*r = test.promptResponse

if test.shouldErr {
return errors.New("error")
}
return nil
})

done, err := ConfirmInitOptions(ioutil.Discard, test.config)
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expectedDone, done)
})
}
}
60 changes: 60 additions & 0 deletions pkg/skaffold/initializer/util/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
Copyright 2020 The Skaffold 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 util

import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

// ListBuilders returns a list of builder names being used in the given build config.
func ListBuilders(build *latest.BuildConfig) []string {
if build == nil {
return []string{}
}

results := util.NewStringSet()
for _, artifact := range build.Artifacts {
results.Insert(misc.ArtifactType(artifact))
}

return results.ToList()
}

// ListDeployers returns a list of deployer names being used in the given deploy config.
func ListDeployers(deploy *latest.DeployConfig) []string {
Copy link
Contributor

@IsaacPD IsaacPD Dec 12, 2020

Choose a reason for hiding this comment

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

Looks good just thought this function might be useful since I used it for the same use case so I think it could help here too by using it as the underlying function. It just uses the yaml that deploy *latest.DeployConfig unmarshals to, to get the list of deployers.

meter.Deployers = yamltags.GetYamlTags(config.Deploy.DeployType)

// GetYamlTags returns the tags of the non-nested fields of the given non-nil value
// If value interface{} is
// latest.DeployType{HelmDeploy: &HelmDeploy{...}, KustomizeDeploy: &KustomizeDeploy{...}}
// then this parses that interface as it's raw yaml:
// kubectl:
// manifests:
// - k8s/*.yaml
// kustomize:
// paths:
// - k8s/
// and returns ["kubectl", "kustomize"]
// empty structs (e.g. latest.DeployType{}) when parsed look like "{}"" and so this function returns []
func GetYamlTags(value interface{}) []string {

Copy link
Contributor

@IsaacPD IsaacPD Dec 12, 2020

Choose a reason for hiding this comment

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

Also now realizing there's a tab right before kubectl in the comment that shouldn't be there 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for sharing this, I think your function is probably what I should use here because it should be fine in the case of a new deployer being added to skaffold

if deploy == nil {
return []string{}
}

results := util.NewStringSet()
if deploy.HelmDeploy != nil {
results.Insert("helm")
}
if deploy.KptDeploy != nil {
results.Insert("kpt")
}
if deploy.KubectlDeploy != nil {
results.Insert("kubectl")
}
if deploy.KustomizeDeploy != nil {
results.Insert("kustomize")
}

return results.ToList()
}
103 changes: 103 additions & 0 deletions pkg/skaffold/initializer/util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
Copyright 2020 The Skaffold 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 util

import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestListBuilders(t *testing.T) {
tests := []struct {
description string
build *latest.BuildConfig
expected []string
}{
{
description: "nil config",
build: nil,
expected: []string{},
},
{
description: "multiple same builder config",
build: &latest.BuildConfig{
Artifacts: []*latest.Artifact{
{ImageName: "img1", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
{ImageName: "img2", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
},
},
expected: []string{"docker"},
},
{
description: "different builders config",
build: &latest.BuildConfig{
Artifacts: []*latest.Artifact{
{ImageName: "img1", ArtifactType: latest.ArtifactType{DockerArtifact: &latest.DockerArtifact{}}},
{ImageName: "img2", ArtifactType: latest.ArtifactType{JibArtifact: &latest.JibArtifact{}}},
},
},
expected: []string{"docker", "jib"},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
got := ListBuilders(test.build)
t.CheckDeepEqual(test.expected, got)
})
}
}

func TestListDeployers(t *testing.T) {
tests := []struct {
description string
deploy *latest.DeployConfig
expected []string
}{
{
description: "nil config",
deploy: nil,
expected: []string{},
},
{
description: "single deployer config",
deploy: &latest.DeployConfig{
DeployType: latest.DeployType{
KubectlDeploy: &latest.KubectlDeploy{},
},
},
expected: []string{"kubectl"},
},
{
description: "multiple deployers config",
deploy: &latest.DeployConfig{
DeployType: latest.DeployType{
HelmDeploy: &latest.HelmDeploy{},
KubectlDeploy: &latest.KubectlDeploy{},
},
},
expected: []string{"helm", "kubectl"},
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
got := ListDeployers(test.deploy)
t.CheckDeepEqual(test.expected, got)
})
}
}