From 62cb7ec5801996ced3b17d930683557777a1b7fc Mon Sep 17 00:00:00 2001 From: Siva Manivannan Date: Fri, 5 Jul 2024 14:59:50 -0500 Subject: [PATCH 1/8] add preferred channel slug --- cmd/kots/cli/install.go | 6 ++++++ pkg/kotsadm/objects/configmaps_objects.go | 1 + pkg/kotsadm/types/deployoptions.go | 1 + 3 files changed, 8 insertions(+) diff --git a/cmd/kots/cli/install.go b/cmd/kots/cli/install.go index 51488ecb0d..25f66a660a 100644 --- a/cmd/kots/cli/install.go +++ b/cmd/kots/cli/install.go @@ -163,6 +163,11 @@ func InstallCmd() *cobra.Command { upstream := pull.RewriteUpstream(args[0]) + preferredChannelSlug := "" + if len(args) > 1 { + preferredChannelSlug = args[1] + } + namespace := v.GetString("namespace") if namespace == "" { @@ -252,6 +257,7 @@ func InstallCmd() *cobra.Command { deployOptions := kotsadmtypes.DeployOptions{ Namespace: namespace, + PreferredChannelSlug: preferredChannelSlug, Context: v.GetString("context"), SharedPassword: sharedPassword, ApplicationMetadata: applicationMetadata.Manifest, diff --git a/pkg/kotsadm/objects/configmaps_objects.go b/pkg/kotsadm/objects/configmaps_objects.go index 70f28ccabd..0954cf2d02 100644 --- a/pkg/kotsadm/objects/configmaps_objects.go +++ b/pkg/kotsadm/objects/configmaps_objects.go @@ -12,6 +12,7 @@ import ( func KotsadmConfigMap(deployOptions types.DeployOptions) *corev1.ConfigMap { data := map[string]string{ + "preferred-channel-slug": fmt.Sprintf("%v", deployOptions.PreferredChannelSlug), "kots-install-id": fmt.Sprintf("%v", deployOptions.InstallID), "initial-app-images-pushed": fmt.Sprintf("%v", deployOptions.AppImagesPushed), "skip-preflights": fmt.Sprintf("%v", deployOptions.SkipPreflights), diff --git a/pkg/kotsadm/types/deployoptions.go b/pkg/kotsadm/types/deployoptions.go index 5db3215c06..4b6e1ce854 100644 --- a/pkg/kotsadm/types/deployoptions.go +++ b/pkg/kotsadm/types/deployoptions.go @@ -10,6 +10,7 @@ import ( type DeployOptions struct { Namespace string + PreferredChannelSlug string Context string SharedPassword string SharedPasswordBcrypt string From 6d0ba1c0cca745f8a5fb3aee389787f78b6697a3 Mon Sep 17 00:00:00 2001 From: Siva Manivannan Date: Sun, 7 Jul 2024 19:35:58 -0500 Subject: [PATCH 2/8] get preferred channel slug --- pkg/kotsadm/main.go | 14 ++++++++++++++ pkg/replicatedapp/api.go | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/pkg/kotsadm/main.go b/pkg/kotsadm/main.go index cf87267135..a4e09a358f 100644 --- a/pkg/kotsadm/main.go +++ b/pkg/kotsadm/main.go @@ -1170,3 +1170,17 @@ func GetRegistryConfigFromCluster(namespace string, clientset kubernetes.Interfa registryConfig.Password = creds.Password return registryConfig, nil } + +func GetPreferredChannelSlug(namespace string, clientset kubernetes.Interface) (string, error) { + configMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), types.KotsadmConfigMap, metav1.GetOptions{}) + if err != nil { + return "", errors.Wrap(err, "failed to get existing kotsadm config map") + } + + slug, ok := configMap.Data["preferred-channel-slug"] + if !ok { + return "", nil + } + + return slug, nil +} diff --git a/pkg/replicatedapp/api.go b/pkg/replicatedapp/api.go index 5d1863e3a8..79e713fda2 100644 --- a/pkg/replicatedapp/api.go +++ b/pkg/replicatedapp/api.go @@ -2,6 +2,7 @@ package replicatedapp import ( "bytes" + "context" "encoding/json" "fmt" "io" @@ -11,6 +12,8 @@ import ( "github.com/pkg/errors" apptypes "github.com/replicatedhq/kots/pkg/app/types" + "github.com/replicatedhq/kots/pkg/k8sutil" + "github.com/replicatedhq/kots/pkg/kotsadm/types" "github.com/replicatedhq/kots/pkg/logger" "github.com/replicatedhq/kots/pkg/persistence" "github.com/replicatedhq/kots/pkg/reporting" @@ -18,6 +21,7 @@ import ( "github.com/replicatedhq/kots/pkg/util" kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" "github.com/replicatedhq/kotskinds/client/kotsclientset/scheme" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type ApplicationMetadata struct { @@ -40,6 +44,25 @@ type LicenseData struct { License *kotsv1beta1.License } +func GetPreferredChannelSlug(namespace string) (string, error) { + clientset, err := k8sutil.GetClientset() + if err != nil { + return "", errors.Wrap(err, "failed to get clientset") + } + + configMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), types.KotsadmConfigMap, metav1.GetOptions{}) + if err != nil { + return "", errors.Wrap(err, "failed to get existing kotsadm config map") + } + + slug, ok := configMap.Data["preferred-channel-slug"] + if !ok { + return "", nil + } + + return slug, nil +} + func GetLatestLicense(license *kotsv1beta1.License) (*LicenseData, error) { url := fmt.Sprintf("%s/license/%s", license.Spec.Endpoint, license.Spec.AppSlug) From e411cb26c1441d9cf8760b4d90714c74ed6e4da2 Mon Sep 17 00:00:00 2001 From: Siva M Date: Mon, 8 Jul 2024 01:06:20 +0000 Subject: [PATCH 3/8] remove duplicate config map func --- pkg/kotsadm/main.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/pkg/kotsadm/main.go b/pkg/kotsadm/main.go index a4e09a358f..cf87267135 100644 --- a/pkg/kotsadm/main.go +++ b/pkg/kotsadm/main.go @@ -1170,17 +1170,3 @@ func GetRegistryConfigFromCluster(namespace string, clientset kubernetes.Interfa registryConfig.Password = creds.Password return registryConfig, nil } - -func GetPreferredChannelSlug(namespace string, clientset kubernetes.Interface) (string, error) { - configMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), types.KotsadmConfigMap, metav1.GetOptions{}) - if err != nil { - return "", errors.Wrap(err, "failed to get existing kotsadm config map") - } - - slug, ok := configMap.Data["preferred-channel-slug"] - if !ok { - return "", nil - } - - return slug, nil -} From 270f16ec27f89efc2f8bb47048a089ac63e3a672 Mon Sep 17 00:00:00 2001 From: Siva M Date: Mon, 8 Jul 2024 01:15:49 +0000 Subject: [PATCH 4/8] multi-channel uses /license/:appSlug/:channelSlug endpoint in replicated-app --- pkg/replicatedapp/api.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/replicatedapp/api.go b/pkg/replicatedapp/api.go index 79e713fda2..f03db14f63 100644 --- a/pkg/replicatedapp/api.go +++ b/pkg/replicatedapp/api.go @@ -44,12 +44,14 @@ type LicenseData struct { License *kotsv1beta1.License } -func GetPreferredChannelSlug(namespace string) (string, error) { +func GetPreferredChannelSlug() (string, error) { clientset, err := k8sutil.GetClientset() if err != nil { return "", errors.Wrap(err, "failed to get clientset") } + namespace := util.AppNamespace() + configMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), types.KotsadmConfigMap, metav1.GetOptions{}) if err != nil { return "", errors.Wrap(err, "failed to get existing kotsadm config map") @@ -64,7 +66,15 @@ func GetPreferredChannelSlug(namespace string) (string, error) { } func GetLatestLicense(license *kotsv1beta1.License) (*LicenseData, error) { + channelSlug, err := GetPreferredChannelSlug() + if err != nil { + return nil, errors.Wrap(err, "failed to get preferred channel slug") + } + url := fmt.Sprintf("%s/license/%s", license.Spec.Endpoint, license.Spec.AppSlug) + if channelSlug != "" { + url = fmt.Sprintf("%s/%s", url, channelSlug) + } licenseData, err := getLicenseFromAPI(url, license.Spec.LicenseID) if err != nil { From fc01a331008c9162d3527458a086ee3e35a34dda Mon Sep 17 00:00:00 2001 From: Florian Hines Date: Tue, 9 Jul 2024 02:18:17 +0000 Subject: [PATCH 5/8] pr feedback --- cmd/kots/cli/install.go | 6 ++-- cmd/kots/cli/util.go | 18 ++++++++++ cmd/kots/cli/util_test.go | 49 ++++++++++++++++++++++++++ pkg/airgap/airgap.go | 5 ++- pkg/automation/automation.go | 5 ++- pkg/handlers/garbage_collect_images.go | 3 +- pkg/handlers/license.go | 9 +++-- pkg/kotsadm/updates.go | 3 +- pkg/kotsutil/kots.go | 11 +++--- pkg/registry/images.go | 2 +- pkg/replicatedapp/api.go | 9 ++--- 11 files changed, 93 insertions(+), 27 deletions(-) diff --git a/cmd/kots/cli/install.go b/cmd/kots/cli/install.go index 25f66a660a..817f12a891 100644 --- a/cmd/kots/cli/install.go +++ b/cmd/kots/cli/install.go @@ -163,9 +163,9 @@ func InstallCmd() *cobra.Command { upstream := pull.RewriteUpstream(args[0]) - preferredChannelSlug := "" - if len(args) > 1 { - preferredChannelSlug = args[1] + preferredChannelSlug, err := extractPreferredChannelSlug(upstream) + if err != nil { + return errors.Wrap(err, "failed to extract preferred channel slug") } namespace := v.GetString("namespace") diff --git a/cmd/kots/cli/util.go b/cmd/kots/cli/util.go index 3eda9b2990..b8419793a5 100644 --- a/cmd/kots/cli/util.go +++ b/cmd/kots/cli/util.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/pkg/errors" + "github.com/replicatedhq/kots/pkg/replicatedapp" "github.com/replicatedhq/kots/pkg/util" ) @@ -40,3 +41,20 @@ func getHostFromEndpoint(endpoint string) (string, error) { return parsed.Host, nil } + +func extractPreferredChannelSlug(upstreamURI string) (string, error) { + u, err := url.ParseRequestURI(upstreamURI) + if err != nil { + return "", errors.Wrap(err, "failed to parse uri") + } + + replicatedUpstream, err := replicatedapp.ParseReplicatedURL(u) + if err != nil { + return "", errors.Wrap(err, "failed to parse replicated url") + } + + if replicatedUpstream.Channel != nil { + return *replicatedUpstream.Channel, nil + } + return "", nil +} diff --git a/cmd/kots/cli/util_test.go b/cmd/kots/cli/util_test.go index 821490a80a..ed1b35db4e 100644 --- a/cmd/kots/cli/util_test.go +++ b/cmd/kots/cli/util_test.go @@ -95,3 +95,52 @@ func Test_getHostFromEndpoint(t *testing.T) { }) } } + +func Test_extractPreferredChannelSlug(t *testing.T) { + type args struct { + upstreamURI string + } + tests := []struct { + name string + args args + want string + wantErr bool + }{ + { + "no channel", + args{ + upstreamURI: "replicated://app-slug@v1.2.0", + }, + "", + false, + }, + { + "with channel", + args{ + upstreamURI: "replicated://app-slug/channel", + }, + "channel", + false, + }, + { + "invalid uri", + args{ + upstreamURI: "junk", + }, + "", + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := extractPreferredChannelSlug(tt.args.upstreamURI) + if (err != nil) != tt.wantErr { + t.Errorf("extractPreferredChannelSlug() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("extractPreferredChannelSlug() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/airgap/airgap.go b/pkg/airgap/airgap.go index 13262947bc..2b8573e327 100644 --- a/pkg/airgap/airgap.go +++ b/pkg/airgap/airgap.go @@ -14,7 +14,6 @@ import ( "github.com/pkg/errors" "github.com/replicatedhq/kots/pkg/airgap/types" "github.com/replicatedhq/kots/pkg/archives" - kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types" kotsadmconfig "github.com/replicatedhq/kots/pkg/kotsadmconfig" identity "github.com/replicatedhq/kots/pkg/kotsadmidentity" "github.com/replicatedhq/kots/pkg/kotsutil" @@ -198,7 +197,7 @@ func CreateAppFromAirgap(opts CreateAirgapAppOpts) (finalError error) { } } - instParams, err := kotsutil.GetInstallationParams(kotsadmtypes.KotsadmConfigMap) + instParams, err := kotsutil.GetInstallationParams() if err != nil { return errors.Wrap(err, "failed to get existing kotsadm config map") } @@ -340,7 +339,7 @@ func CreateAppFromAirgap(opts CreateAirgapAppOpts) (finalError error) { } } - err = kotsutil.RemoveAppVersionLabelFromInstallationParams(kotsadmtypes.KotsadmConfigMap) + err = kotsutil.RemoveAppVersionLabelFromInstallationParams() if err != nil { logger.Error(errors.Wrapf(err, "failed to delete app version label from config")) } diff --git a/pkg/automation/automation.go b/pkg/automation/automation.go index 997a2481a0..ce98c4da5c 100644 --- a/pkg/automation/automation.go +++ b/pkg/automation/automation.go @@ -17,7 +17,6 @@ import ( airgaptypes "github.com/replicatedhq/kots/pkg/airgap/types" "github.com/replicatedhq/kots/pkg/k8sutil" "github.com/replicatedhq/kots/pkg/kotsadm" - kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types" kotsadmlicense "github.com/replicatedhq/kots/pkg/kotsadmlicense" "github.com/replicatedhq/kots/pkg/kotsutil" kotslicense "github.com/replicatedhq/kots/pkg/license" @@ -83,7 +82,7 @@ func AutomateInstall(opts AutomateInstallOptions) error { } cleanup := func(licenseSecret *corev1.Secret) { - err = kotsutil.RemoveAppVersionLabelFromInstallationParams(kotsadmtypes.KotsadmConfigMap) + err = kotsutil.RemoveAppVersionLabelFromInstallationParams() if err != nil { logger.Error(errors.Wrapf(err, "failed to delete app version label from config")) } @@ -235,7 +234,7 @@ func installLicenseSecret(clientset *kubernetes.Clientset, licenseSecret corev1. } } - instParams, err := kotsutil.GetInstallationParams(kotsadmtypes.KotsadmConfigMap) + instParams, err := kotsutil.GetInstallationParams() if err != nil { return errors.Wrap(err, "failed to get existing kotsadm config map") } diff --git a/pkg/handlers/garbage_collect_images.go b/pkg/handlers/garbage_collect_images.go index 0fba7385f5..3e8cbfa4bd 100644 --- a/pkg/handlers/garbage_collect_images.go +++ b/pkg/handlers/garbage_collect_images.go @@ -6,7 +6,6 @@ import ( "github.com/pkg/errors" "github.com/replicatedhq/kots/pkg/k8sutil" - kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types" "github.com/replicatedhq/kots/pkg/kotsutil" "github.com/replicatedhq/kots/pkg/kurl" "github.com/replicatedhq/kots/pkg/logger" @@ -33,7 +32,7 @@ func (h *Handler) GarbageCollectImages(w http.ResponseWriter, r *http.Request) { return } - installParams, err := kotsutil.GetInstallationParams(kotsadmtypes.KotsadmConfigMap) + installParams, err := kotsutil.GetInstallationParams() if err != nil { response.Error = "failed to get app registry info" logger.Error(errors.Wrap(err, response.Error)) diff --git a/pkg/handlers/license.go b/pkg/handlers/license.go index 6f9cf40723..7849e3430b 100644 --- a/pkg/handlers/license.go +++ b/pkg/handlers/license.go @@ -14,7 +14,6 @@ import ( apptypes "github.com/replicatedhq/kots/pkg/app/types" "github.com/replicatedhq/kots/pkg/k8sutil" "github.com/replicatedhq/kots/pkg/kotsadm" - kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types" license "github.com/replicatedhq/kots/pkg/kotsadmlicense" "github.com/replicatedhq/kots/pkg/kotsutil" kotslicense "github.com/replicatedhq/kots/pkg/license" @@ -322,7 +321,7 @@ func (h *Handler) UploadNewLicense(w http.ResponseWriter, r *http.Request) { } } - installationParams, err := kotsutil.GetInstallationParams(kotsadmtypes.KotsadmConfigMap) + installationParams, err := kotsutil.GetInstallationParams() if err != nil { logger.Error(err) uploadLicenseResponse.Error = err.Error() @@ -361,7 +360,7 @@ func (h *Handler) UploadNewLicense(w http.ResponseWriter, r *http.Request) { return } - err = kotsutil.RemoveAppVersionLabelFromInstallationParams(kotsadmtypes.KotsadmConfigMap) + err = kotsutil.RemoveAppVersionLabelFromInstallationParams() if err != nil { logger.Error(err) uploadLicenseResponse.Error = err.Error() @@ -417,7 +416,7 @@ func (h *Handler) ResumeInstallOnline(w http.ResponseWriter, r *http.Request) { return } - installationParams, err := kotsutil.GetInstallationParams(kotsadmtypes.KotsadmConfigMap) + installationParams, err := kotsutil.GetInstallationParams() if err != nil { logger.Error(err) resumeInstallOnlineResponse.Error = err.Error() @@ -464,7 +463,7 @@ func (h *Handler) ResumeInstallOnline(w http.ResponseWriter, r *http.Request) { return } - err = kotsutil.RemoveAppVersionLabelFromInstallationParams(kotsadmtypes.KotsadmConfigMap) + err = kotsutil.RemoveAppVersionLabelFromInstallationParams() if err != nil { logger.Error(err) resumeInstallOnlineResponse.Error = err.Error() diff --git a/pkg/kotsadm/updates.go b/pkg/kotsadm/updates.go index f61e82374e..907fee36e7 100644 --- a/pkg/kotsadm/updates.go +++ b/pkg/kotsadm/updates.go @@ -12,7 +12,6 @@ import ( "github.com/pkg/errors" "github.com/replicatedhq/kots/pkg/buildversion" "github.com/replicatedhq/kots/pkg/k8sutil" - kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types" "github.com/replicatedhq/kots/pkg/kotsutil" "github.com/replicatedhq/kots/pkg/logger" "github.com/replicatedhq/kots/pkg/rand" @@ -104,7 +103,7 @@ func UpdateToVersion(newVersion string) error { return errors.Wrap(err, "failed to get kots options from cluster") } - installationParams, err := kotsutil.GetInstallationParams(kotsadmtypes.KotsadmConfigMap) + installationParams, err := kotsutil.GetInstallationParams() if err != nil { return errors.Wrap(err, "failed to get installation params") } diff --git a/pkg/kotsutil/kots.go b/pkg/kotsutil/kots.go index 50ebe68cef..fe033f8d51 100644 --- a/pkg/kotsutil/kots.go +++ b/pkg/kotsutil/kots.go @@ -23,6 +23,7 @@ import ( "github.com/replicatedhq/kots/pkg/crypto" registrytypes "github.com/replicatedhq/kots/pkg/docker/registry/types" "github.com/replicatedhq/kots/pkg/k8sutil" + kotsadmtypes "github.com/replicatedhq/kots/pkg/kotsadm/types" "github.com/replicatedhq/kots/pkg/kurl" "github.com/replicatedhq/kots/pkg/logger" "github.com/replicatedhq/kots/pkg/util" @@ -1139,9 +1140,10 @@ type InstallationParams struct { WaitDuration time.Duration WithMinio bool AppVersionLabel string + PreferredChannelSlug string } -func GetInstallationParams(configMapName string) (InstallationParams, error) { +func GetInstallationParams() (InstallationParams, error) { autoConfig := InstallationParams{} clientset, err := k8sutil.GetClientset() @@ -1154,7 +1156,7 @@ func GetInstallationParams(configMapName string) (InstallationParams, error) { return autoConfig, errors.Wrap(err, "failed to check if cluster is kurl") } - kotsadmConfigMap, err := clientset.CoreV1().ConfigMaps(util.PodNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{}) + kotsadmConfigMap, err := clientset.CoreV1().ConfigMaps(util.PodNamespace).Get(context.TODO(), kotsadmtypes.KotsadmConfigMap, metav1.GetOptions{}) if err != nil { if kuberneteserrors.IsNotFound(err) { return autoConfig, nil @@ -1174,6 +1176,7 @@ func GetInstallationParams(configMapName string) (InstallationParams, error) { autoConfig.WaitDuration, _ = time.ParseDuration(kotsadmConfigMap.Data["wait-duration"]) autoConfig.WithMinio, _ = strconv.ParseBool(kotsadmConfigMap.Data["with-minio"]) autoConfig.AppVersionLabel = kotsadmConfigMap.Data["app-version-label"] + autoConfig.PreferredChannelSlug = kotsadmConfigMap.Data["preferred-channel-slug"] if enableImageDeletion, ok := kotsadmConfigMap.Data["enable-image-deletion"]; ok { autoConfig.EnableImageDeletion, _ = strconv.ParseBool(enableImageDeletion) @@ -1310,13 +1313,13 @@ func IsKotsAutoUpgradeSupported(app *kotsv1beta1.Application) bool { return false } -func RemoveAppVersionLabelFromInstallationParams(configMapName string) error { +func RemoveAppVersionLabelFromInstallationParams() error { clientset, err := k8sutil.GetClientset() if err != nil { return errors.Wrap(err, "failed to get k8s clientset") } - kotsadmConfigMap, err := clientset.CoreV1().ConfigMaps(util.PodNamespace).Get(context.TODO(), configMapName, metav1.GetOptions{}) + kotsadmConfigMap, err := clientset.CoreV1().ConfigMaps(util.PodNamespace).Get(context.TODO(), kotsadmtypes.KotsadmConfigMap, metav1.GetOptions{}) if err != nil { if kuberneteserrors.IsNotFound(err) { return nil diff --git a/pkg/registry/images.go b/pkg/registry/images.go index 3dac1a5f08..f5e70c9dda 100644 --- a/pkg/registry/images.go +++ b/pkg/registry/images.go @@ -71,7 +71,7 @@ func shouldGarbageCollectImages(isKurl bool, embeddedRegistryHost string, instal } func DeleteUnusedImages(appID string, ignoreRollback bool) error { - installParams, err := kotsutil.GetInstallationParams(kotsadmtypes.KotsadmConfigMap) + installParams, err := kotsutil.GetInstallationParams() if err != nil { return errors.Wrap(err, "failed to get app registry info") } diff --git a/pkg/replicatedapp/api.go b/pkg/replicatedapp/api.go index f03db14f63..2d71cb4a89 100644 --- a/pkg/replicatedapp/api.go +++ b/pkg/replicatedapp/api.go @@ -14,6 +14,7 @@ import ( apptypes "github.com/replicatedhq/kots/pkg/app/types" "github.com/replicatedhq/kots/pkg/k8sutil" "github.com/replicatedhq/kots/pkg/kotsadm/types" + "github.com/replicatedhq/kots/pkg/kotsutil" "github.com/replicatedhq/kots/pkg/logger" "github.com/replicatedhq/kots/pkg/persistence" "github.com/replicatedhq/kots/pkg/reporting" @@ -66,14 +67,14 @@ func GetPreferredChannelSlug() (string, error) { } func GetLatestLicense(license *kotsv1beta1.License) (*LicenseData, error) { - channelSlug, err := GetPreferredChannelSlug() + installParams, err := kotsutil.GetInstallationParams() if err != nil { - return nil, errors.Wrap(err, "failed to get preferred channel slug") + return nil, errors.Wrap(err, "failed to get installation params") } url := fmt.Sprintf("%s/license/%s", license.Spec.Endpoint, license.Spec.AppSlug) - if channelSlug != "" { - url = fmt.Sprintf("%s/%s", url, channelSlug) + if installParams.PreferredChannelSlug != "" { + url = fmt.Sprintf("%s/%s", url, installParams.PreferredChannelSlug) } licenseData, err := getLicenseFromAPI(url, license.Spec.LicenseID) From aa1cfb824e39b9e40d58c39381c9f6ef6746998e Mon Sep 17 00:00:00 2001 From: Florian Hines Date: Tue, 9 Jul 2024 02:20:53 +0000 Subject: [PATCH 6/8] Remove unused GetPreferredChannelSlug func --- pkg/replicatedapp/api.go | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/pkg/replicatedapp/api.go b/pkg/replicatedapp/api.go index 2d71cb4a89..f4837799e5 100644 --- a/pkg/replicatedapp/api.go +++ b/pkg/replicatedapp/api.go @@ -2,7 +2,6 @@ package replicatedapp import ( "bytes" - "context" "encoding/json" "fmt" "io" @@ -12,8 +11,6 @@ import ( "github.com/pkg/errors" apptypes "github.com/replicatedhq/kots/pkg/app/types" - "github.com/replicatedhq/kots/pkg/k8sutil" - "github.com/replicatedhq/kots/pkg/kotsadm/types" "github.com/replicatedhq/kots/pkg/kotsutil" "github.com/replicatedhq/kots/pkg/logger" "github.com/replicatedhq/kots/pkg/persistence" @@ -22,7 +19,6 @@ import ( "github.com/replicatedhq/kots/pkg/util" kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" "github.com/replicatedhq/kotskinds/client/kotsclientset/scheme" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) type ApplicationMetadata struct { @@ -45,27 +41,6 @@ type LicenseData struct { License *kotsv1beta1.License } -func GetPreferredChannelSlug() (string, error) { - clientset, err := k8sutil.GetClientset() - if err != nil { - return "", errors.Wrap(err, "failed to get clientset") - } - - namespace := util.AppNamespace() - - configMap, err := clientset.CoreV1().ConfigMaps(namespace).Get(context.TODO(), types.KotsadmConfigMap, metav1.GetOptions{}) - if err != nil { - return "", errors.Wrap(err, "failed to get existing kotsadm config map") - } - - slug, ok := configMap.Data["preferred-channel-slug"] - if !ok { - return "", nil - } - - return slug, nil -} - func GetLatestLicense(license *kotsv1beta1.License) (*LicenseData, error) { installParams, err := kotsutil.GetInstallationParams() if err != nil { From 74702932d2bc0c06cd6e6d725a7e92f030de1821 Mon Sep 17 00:00:00 2001 From: Florian Hines Date: Tue, 9 Jul 2024 02:25:15 +0000 Subject: [PATCH 7/8] Default to channel 'stable' when no channel is specified --- cmd/kots/cli/util.go | 2 +- cmd/kots/cli/util_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/kots/cli/util.go b/cmd/kots/cli/util.go index b8419793a5..350aac5334 100644 --- a/cmd/kots/cli/util.go +++ b/cmd/kots/cli/util.go @@ -56,5 +56,5 @@ func extractPreferredChannelSlug(upstreamURI string) (string, error) { if replicatedUpstream.Channel != nil { return *replicatedUpstream.Channel, nil } - return "", nil + return "stable", nil } diff --git a/cmd/kots/cli/util_test.go b/cmd/kots/cli/util_test.go index ed1b35db4e..a270dbaecf 100644 --- a/cmd/kots/cli/util_test.go +++ b/cmd/kots/cli/util_test.go @@ -109,9 +109,9 @@ func Test_extractPreferredChannelSlug(t *testing.T) { { "no channel", args{ - upstreamURI: "replicated://app-slug@v1.2.0", + upstreamURI: "replicated://app-slug", }, - "", + "stable", // default channel false, }, { From 729ddf06694da95e5403c6a5ead4cebe60a7da55 Mon Sep 17 00:00:00 2001 From: Florian Hines Date: Tue, 9 Jul 2024 18:51:00 +0000 Subject: [PATCH 8/8] GetLatestLicense warns but cont if GetInstallationParams fails --- pkg/replicatedapp/api.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/replicatedapp/api.go b/pkg/replicatedapp/api.go index f4837799e5..e3e9db95cf 100644 --- a/pkg/replicatedapp/api.go +++ b/pkg/replicatedapp/api.go @@ -42,13 +42,12 @@ type LicenseData struct { } func GetLatestLicense(license *kotsv1beta1.License) (*LicenseData, error) { + url := fmt.Sprintf("%s/license/%s", license.Spec.Endpoint, license.Spec.AppSlug) + installParams, err := kotsutil.GetInstallationParams() if err != nil { - return nil, errors.Wrap(err, "failed to get installation params") - } - - url := fmt.Sprintf("%s/license/%s", license.Spec.Endpoint, license.Spec.AppSlug) - if installParams.PreferredChannelSlug != "" { + logger.Warnf("failed to get preferred channel slug from installation params: %v", err) + } else if installParams.PreferredChannelSlug != "" { url = fmt.Sprintf("%s/%s", url, installParams.PreferredChannelSlug) }