Skip to content

Commit fa660c6

Browse files
Improved messaging when requested channel slug not allowed by license (#4842)
* Improved messaging when requested channel slug not allowed by license * make wording install/pull agnostic * pr feedback * Update cmd/kots/cli/util.go Co-authored-by: Salah Al Saleh <[email protected]> * Apply suggestions from code review Co-authored-by: Salah Al Saleh <[email protected]> * pr feedback --------- Co-authored-by: Salah Al Saleh <[email protected]>
1 parent 0cc081e commit fa660c6

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

cmd/kots/cli/install.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,11 @@ func InstallCmd() *cobra.Command {
163163
}()
164164

165165
upstream := pull.RewriteUpstream(args[0])
166-
preferredChannelSlug, err := extractPreferredChannelSlug(upstream)
166+
preferredChannelSlug, err := extractPreferredChannelSlug(log, upstream)
167167
if err != nil {
168168
return errors.Wrap(err, "failed to extract preferred channel slug")
169169
}
170+
170171
license, err = kotslicense.VerifyAndUpdateLicense(log, license, preferredChannelSlug, isAirgap)
171172
if err != nil {
172173
return errors.Wrap(err, "failed to verify and update license")

cmd/kots/cli/pull.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,15 @@ func PullCmd() *cobra.Command {
101101
}
102102

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

109105
log := logger.NewCLILogger(cmd.OutOrStdout())
110106
log.Initialize()
111107

108+
preferredChannelSlug, err := extractPreferredChannelSlug(log, upstream)
109+
if err != nil {
110+
return errors.Wrap(err, "failed to extract preferred channel slug")
111+
}
112+
112113
// If we are passed a multi-channel license, verify that the requested channel is in the license
113114
// so that we can warn the user immediately if it is not.
114115
license, err = kotslicense.VerifyAndUpdateLicense(log, license, preferredChannelSlug, false)

cmd/kots/cli/util.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/pkg/errors"
10+
"github.com/replicatedhq/kots/pkg/logger"
1011
"github.com/replicatedhq/kots/pkg/replicatedapp"
1112
"github.com/replicatedhq/kots/pkg/util"
1213
)
@@ -53,7 +54,7 @@ func splitEndpointAndNamespace(endpoint string) (string, string) {
5354
return registryEndpoint, registryNamespace
5455
}
5556

56-
func extractPreferredChannelSlug(upstreamURI string) (string, error) {
57+
func extractPreferredChannelSlug(log *logger.CLILogger, upstreamURI string) (string, error) {
5758
u, err := url.ParseRequestURI(upstreamURI)
5859
if err != nil {
5960
return "", errors.Wrap(err, "failed to parse uri")
@@ -67,5 +68,9 @@ func extractPreferredChannelSlug(upstreamURI string) (string, error) {
6768
if replicatedUpstream.Channel != nil {
6869
return *replicatedUpstream.Channel, nil
6970
}
71+
72+
if log != nil {
73+
log.ActionWithoutSpinner("No channel specified in upstream URI, falling back to channel slug 'stable'.")
74+
}
7075
return "stable", nil
7176
}

cmd/kots/cli/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func Test_extractPreferredChannelSlug(t *testing.T) {
111111
args{
112112
upstreamURI: "replicated://app-slug",
113113
},
114-
"stable", // default channel
114+
"stable",
115115
false,
116116
},
117117
{
@@ -133,7 +133,7 @@ func Test_extractPreferredChannelSlug(t *testing.T) {
133133
}
134134
for _, tt := range tests {
135135
t.Run(tt.name, func(t *testing.T) {
136-
got, err := extractPreferredChannelSlug(tt.args.upstreamURI)
136+
got, err := extractPreferredChannelSlug(nil, tt.args.upstreamURI)
137137
if (err != nil) != tt.wantErr {
138138
t.Errorf("extractPreferredChannelSlug() error = %v, wantErr %v", err, tt.wantErr)
139139
return

pkg/license/multichannel.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package license
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/pkg/errors"
58
"github.com/replicatedhq/kots/pkg/logger"
69
"github.com/replicatedhq/kots/pkg/replicatedapp"
@@ -40,11 +43,17 @@ func VerifyAndUpdateLicense(log *logger.CLILogger, license *kotsv1beta1.License,
4043
return nil, nil
4144
}
4245
if isAirgap {
43-
if !canInstallFromChannel(preferredChannelSlug, license) {
44-
return nil, errors.New("requested channel not found in supplied license")
46+
if canInstallFromChannel(preferredChannelSlug, license) {
47+
return license, nil
48+
}
49+
validChannels := []string{}
50+
for _, channel := range license.Spec.Channels {
51+
validChannels = append(validChannels, fmt.Sprintf("%s/%s", license.Spec.AppSlug, channel.ChannelSlug))
4552
}
46-
return license, nil
53+
log.Errorf("Channel slug %q is not allowed by license. Please use one of the following: %s", preferredChannelSlug, strings.Join(validChannels, ", "))
54+
return license, errors.New(fmt.Sprintf("channel slug %q is not allowed by license", preferredChannelSlug))
4755
}
56+
4857
log.ActionWithSpinner("Checking for license update")
4958
// we fetch the latest license to ensure that the license is up to date, before proceeding
5059
updatedLicense, err := replicatedapp.GetLatestLicense(license, "")
@@ -53,8 +62,14 @@ func VerifyAndUpdateLicense(log *logger.CLILogger, license *kotsv1beta1.License,
5362
return nil, errors.Wrap(err, "failed to get latest license")
5463
}
5564
log.FinishSpinner()
65+
5666
if canInstallFromChannel(preferredChannelSlug, updatedLicense.License) {
5767
return updatedLicense.License, nil
5868
}
59-
return nil, errors.New("requested channel not found in latest license")
69+
validChannels := []string{}
70+
for _, channel := range license.Spec.Channels {
71+
validChannels = append(validChannels, fmt.Sprintf("%s/%s", license.Spec.AppSlug, channel.ChannelSlug))
72+
}
73+
log.Errorf("Channel slug %q is not allowed by license. Please use one of the following: %s", preferredChannelSlug, strings.Join(validChannels, ", "))
74+
return updatedLicense.License, errors.New(fmt.Sprintf("channel slug %q is not allowed by latest license", preferredChannelSlug))
6075
}

0 commit comments

Comments
 (0)