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

Improved messaging when requested channel slug not allowed by license #4842

Merged
merged 9 commits into from
Sep 4, 2024
3 changes: 2 additions & 1 deletion cmd/kots/cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ func InstallCmd() *cobra.Command {
}()

upstream := pull.RewriteUpstream(args[0])
preferredChannelSlug, err := extractPreferredChannelSlug(upstream)
preferredChannelSlug, err := extractPreferredChannelSlug(log, upstream)
if err != nil {
return errors.Wrap(err, "failed to extract preferred channel slug")
}

license, err = kotslicense.VerifyAndUpdateLicense(log, license, preferredChannelSlug, isAirgap)
if err != nil {
return errors.Wrap(err, "failed to verify and update license")
Expand Down
9 changes: 5 additions & 4 deletions cmd/kots/cli/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ func PullCmd() *cobra.Command {
}

upstream := pull.RewriteUpstream(args[0])
preferredChannelSlug, err := extractPreferredChannelSlug(upstream)
if err != nil {
return errors.Wrap(err, "failed to extract preferred channel slug")
}

log := logger.NewCLILogger(cmd.OutOrStdout())
log.Initialize()

preferredChannelSlug, err := extractPreferredChannelSlug(log, upstream)
if err != nil {
return errors.Wrap(err, "failed to extract preferred channel slug")
}

// If we are passed a multi-channel license, verify that the requested channel is in the license
// so that we can warn the user immediately if it is not.
license, err = kotslicense.VerifyAndUpdateLicense(log, license, preferredChannelSlug, false)
Expand Down
7 changes: 6 additions & 1 deletion cmd/kots/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/replicatedapp"
"github.com/replicatedhq/kots/pkg/util"
)
Expand Down Expand Up @@ -53,7 +54,7 @@ func splitEndpointAndNamespace(endpoint string) (string, string) {
return registryEndpoint, registryNamespace
}

func extractPreferredChannelSlug(upstreamURI string) (string, error) {
func extractPreferredChannelSlug(log *logger.CLILogger, upstreamURI string) (string, error) {
u, err := url.ParseRequestURI(upstreamURI)
if err != nil {
return "", errors.Wrap(err, "failed to parse uri")
Expand All @@ -67,5 +68,9 @@ func extractPreferredChannelSlug(upstreamURI string) (string, error) {
if replicatedUpstream.Channel != nil {
return *replicatedUpstream.Channel, nil
}

if log != nil {
log.ActionWithoutSpinner("No channel specified in upstream URI, falling back to channel slug 'stable'.")
}
return "stable", nil
}
4 changes: 2 additions & 2 deletions cmd/kots/cli/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func Test_extractPreferredChannelSlug(t *testing.T) {
args{
upstreamURI: "replicated://app-slug",
},
"stable", // default channel
"stable",
false,
},
{
Expand All @@ -133,7 +133,7 @@ func Test_extractPreferredChannelSlug(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractPreferredChannelSlug(tt.args.upstreamURI)
got, err := extractPreferredChannelSlug(nil, tt.args.upstreamURI)
if (err != nil) != tt.wantErr {
t.Errorf("extractPreferredChannelSlug() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
25 changes: 23 additions & 2 deletions pkg/license/multichannel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package license

import (
"fmt"
"strings"

"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/replicatedapp"
Expand Down Expand Up @@ -40,11 +43,20 @@ func VerifyAndUpdateLicense(log *logger.CLILogger, license *kotsv1beta1.License,
return nil, nil
}
if isAirgap {
log.ActionWithSpinner("Verifying if channel slug %q is allowed by the license", preferredChannelSlug)
pandemicsyn marked this conversation as resolved.
Show resolved Hide resolved
if !canInstallFromChannel(preferredChannelSlug, license) {
return nil, errors.New("requested channel not found in supplied license")
log.FinishSpinnerWithError()
return license, errors.New(fmt.Sprintf("channel slug %q is not allowed by license", preferredChannelSlug))
}
log.FinishSpinner()
validChannels := []string{}
for _, channel := range license.Spec.Channels {
validChannels = append(validChannels, fmt.Sprintf("%s/%s", license.Spec.AppSlug, channel.ChannelSlug))
}
log.ChildActionWithoutSpinner(fmt.Sprintf("To install an allowed channel, use one of the following: %s", strings.Join(validChannels, ", ")))
pandemicsyn marked this conversation as resolved.
Show resolved Hide resolved
return license, nil
}

log.ActionWithSpinner("Checking for license update")
// we fetch the latest license to ensure that the license is up to date, before proceeding
updatedLicense, err := replicatedapp.GetLatestLicense(license, "")
Expand All @@ -53,8 +65,17 @@ func VerifyAndUpdateLicense(log *logger.CLILogger, license *kotsv1beta1.License,
return nil, errors.Wrap(err, "failed to get latest license")
}
log.FinishSpinner()

log.ActionWithSpinner("Verifying if channel slug %q is allowed by the license", preferredChannelSlug)
pandemicsyn marked this conversation as resolved.
Show resolved Hide resolved
if canInstallFromChannel(preferredChannelSlug, updatedLicense.License) {
log.FinishSpinner()
return updatedLicense.License, nil
}
return nil, errors.New("requested channel not found in latest license")
log.FinishSpinnerWithError()
validChannels := []string{}
for _, channel := range license.Spec.Channels {
validChannels = append(validChannels, fmt.Sprintf("%s/%s", license.Spec.AppSlug, channel.ChannelSlug))
}
log.ChildActionWithoutSpinner(fmt.Sprintf("To install an allowed channel, use one of the following: %s", strings.Join(validChannels, ", ")))
return updatedLicense.License, errors.New(fmt.Sprintf("channel slug %q is not allowed by latest license", preferredChannelSlug))
}
Loading