Skip to content

Commit

Permalink
Start supporting IsSemversion checks using the license channel entry
Browse files Browse the repository at this point in the history
downstream vers check uses IsSemverRequired from channel license
  • Loading branch information
Florian Hines committed Jul 19, 2024
1 parent b635366 commit 1355d88
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
21 changes: 20 additions & 1 deletion pkg/handlers/update_checker_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/store"
"github.com/replicatedhq/kots/pkg/updatechecker"
kotsv1beta1 "github.com/replicatedhq/kotskinds/apis/kots/v1beta1"
cron "github.com/robfig/cron/v3"
)

Expand Down Expand Up @@ -56,8 +57,26 @@ func (h *Handler) SetAutomaticUpdatesConfig(w http.ResponseWriter, r *http.Reque
return
}

var licenseChan *kotsv1beta1.Channel
if foundApp.ChannelID == "" {
// TODO: Backfill app.ChannelID in the database, this is an install from before multi-channel was introduced
if licenseChan, err = kotsutil.FindChannelInLicense(license.Spec.ChannelID, license); err != nil {
updateCheckerSpecResponse.Error = "failed to find channel in license"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, updateCheckerSpecResponse)
return
}
} else {
if licenseChan, err = kotsutil.FindChannelInLicense(foundApp.ChannelID, license); err != nil {
updateCheckerSpecResponse.Error = "failed to find channel in license"
logger.Error(errors.Wrap(err, updateCheckerSpecResponse.Error))
JSON(w, http.StatusInternalServerError, updateCheckerSpecResponse)
return
}
}

// Check if the deploy update configuration is valid based on app channel
if license.Spec.IsSemverRequired {
if licenseChan.IsSemverRequired {
if configureAutomaticUpdatesRequest.AutoDeploy == apptypes.AutoDeploySequence {
updateCheckerSpecResponse.Error = "automatic updates based on sequence type are not supported for semantic versioning apps"
JSON(w, http.StatusUnprocessableEntity, updateCheckerSpecResponse)
Expand Down
22 changes: 22 additions & 0 deletions pkg/store/kotsstore/app_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,28 @@ func (s *KOTSStore) SetAppChannelChanged(appID string, channelChanged bool) erro
return nil
}

func (s *KOTSStore) GetAppChannelID(appID string) (string, error) {
db := persistence.MustGetDBSession()
query := `select channel_id from app where id = ?`
rows, err := db.QueryOneParameterized(gorqlite.ParameterizedStatement{
Query: query,
Arguments: []interface{}{appID},
})
if err != nil {
return "", fmt.Errorf("failed to query: %v: %v", err, rows.Err)
}
if !rows.Next() {
return "", ErrNotFound
}

var channelID gorqlite.NullString
if err := rows.Scan(&channelID); err != nil {
return "", errors.Wrap(err, "failed to scan channel id")
}

return channelID.String, nil
}

func (s *KOTSStore) SetAppChannelID(appID string, channelID string) error {
db := persistence.MustGetDBSession()

Expand Down
33 changes: 30 additions & 3 deletions pkg/store/kotsstore/downstream_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,21 @@ func (s *KOTSStore) GetDownstreamVersions(appID string, clusterID string, downlo
if err != nil {
return nil, errors.Wrap(err, "failed to get app license")
}
downstreamtypes.SortDownstreamVersions(result.AllVersions, license.Spec.IsSemverRequired)

foundChannelID, err := s.GetAppChannelID(appID)
var licenseChan *kotsv1beta1.Channel
if foundChannelID == "" {
// TODO: Backfill app.ChannelID in the database, this is an install from before multi-channel was introduced
if licenseChan, err = kotsutil.FindChannelInLicense(license.Spec.ChannelID, license); err != nil {
return nil, errors.Wrap(err, "failed to find channel in license")
}
} else {
if licenseChan, err = kotsutil.FindChannelInLicense(foundChannelID, license); err != nil {
return nil, errors.Wrap(err, "failed to find channel in license")
}
}

downstreamtypes.SortDownstreamVersions(result.AllVersions, licenseChan.IsSemverRequired)

// retrieve additional details about the latest downloaded version,
// since it's used for detecting things like if a certain feature is enabled or not.
Expand All @@ -422,7 +436,7 @@ func (s *KOTSStore) GetDownstreamVersions(appID string, clusterID string, downlo
if err := s.AddDownstreamVersionDetails(appID, clusterID, v, false); err != nil {
return nil, errors.Wrap(err, "failed to add details to latest downloaded version")
}
v.IsDeployable, v.NonDeployableCause = isAppVersionDeployable(v, result, license.Spec.IsSemverRequired)
v.IsDeployable, v.NonDeployableCause = isAppVersionDeployable(v, result, licenseChan.IsSemverRequired)
break
}

Expand Down Expand Up @@ -673,8 +687,21 @@ func (s *KOTSStore) AddDownstreamVersionsDetails(appID string, clusterID string,
if err != nil {
return errors.Wrap(err, "failed to get app license")
}
foundChannelID, err := s.GetAppChannelID(appID)
var licenseChan *kotsv1beta1.Channel
if foundChannelID == "" {
// TODO: Backfill app.ChannelID in the database, this is an install from before multi-channel was introduced
if licenseChan, err = kotsutil.FindChannelInLicense(license.Spec.ChannelID, license); err != nil {
errors.Wrap(err, "failed to find channel in license")
}
} else {
if licenseChan, err = kotsutil.FindChannelInLicense(foundChannelID, license); err != nil {
errors.Wrap(err, "failed to find channel in license")
}
}

for _, v := range versions {
v.IsDeployable, v.NonDeployableCause = isAppVersionDeployable(v, allVersions, license.Spec.IsSemverRequired)
v.IsDeployable, v.NonDeployableCause = isAppVersionDeployable(v, allVersions, licenseChan.IsSemverRequired)
}
}

Expand Down
15 changes: 14 additions & 1 deletion pkg/store/kotsstore/version_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,22 @@ func (s *KOTSStore) GetAppVersionBaseSequence(appID string, versionLabel string)
return -1, errors.Wrap(err, "failed to get app license")
}

foundChannelID, err := s.GetAppChannelID(appID)
var licenseChan *kotsv1beta1.Channel
if foundChannelID == "" {
// TODO: Backfill app.ChannelID in the database, this is an install from before multi-channel was introduced
if licenseChan, err = kotsutil.FindChannelInLicense(license.Spec.ChannelID, license); err != nil {
return -1, errors.Wrap(err, "failed to find channel in license")
}
} else {
if licenseChan, err = kotsutil.FindChannelInLicense(foundChannelID, license); err != nil {
return -1, errors.Wrap(err, "failed to find channel in license")
}
}

// add to the top of the list and sort
appVersions.AllVersions = append([]*downstreamtypes.DownstreamVersion{mockVersion}, appVersions.AllVersions...)
downstreamtypes.SortDownstreamVersions(appVersions.AllVersions, license.Spec.IsSemverRequired)
downstreamtypes.SortDownstreamVersions(appVersions.AllVersions, licenseChan.IsSemverRequired)

var baseVersion *downstreamtypes.DownstreamVersion
for i, v := range appVersions.AllVersions {
Expand Down

0 comments on commit 1355d88

Please sign in to comment.