Skip to content

Commit

Permalink
Kotsadm gitops cluster tracers (#515)
Browse files Browse the repository at this point in the history
* gitops cluster tracers
  • Loading branch information
sgalsaleh committed May 8, 2020
1 parent 2a6721f commit 66333cf
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 20 deletions.
2 changes: 1 addition & 1 deletion kotsadm/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/nwaples/rardecode v1.1.0 // indirect
github.com/pierrec/lz4 v2.4.1+incompatible // indirect
github.com/pkg/errors v0.9.1
github.com/replicatedhq/kots v1.15.3-0.20200506223221-57cb35299f1a
github.com/replicatedhq/kots v1.15.3-0.20200508174743-846a053fa0d2
github.com/replicatedhq/troubleshoot v0.9.31
github.com/replicatedhq/yaml/v3 v3.0.0-beta5-replicatedhq
github.com/segmentio/ksuid v1.0.2
Expand Down
4 changes: 2 additions & 2 deletions kotsadm/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -752,8 +752,8 @@ github.com/quobyte/api v0.1.2/go.mod h1:jL7lIHrmqQ7yh05OJ+eEEdHr0u/kmT1Ff9iHd+4H
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be h1:ta7tUOvsPHVHGom5hKW5VXNc2xZIkfCKP8iaqOyYtUQ=
github.com/rainycape/unidecode v0.0.0-20150907023854-cb7f23ec59be/go.mod h1:MIDFMn7db1kT65GmV94GzpX9Qdi7N/pQlwb+AN8wh+Q=
github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uYEyJGbgTkfkS4+E/PavXkNJcbFIpEtjt2B0KDQ5+9M=
github.com/replicatedhq/kots v1.15.3-0.20200506223221-57cb35299f1a h1:Mv4363GkURRHnY2Gc4yff5DdHxri2VvidnA+zRlUljA=
github.com/replicatedhq/kots v1.15.3-0.20200506223221-57cb35299f1a/go.mod h1:75+q98hCN7KFP/wnsZes5762UZ6tR3Up+EAdbAck7RU=
github.com/replicatedhq/kots v1.15.3-0.20200508174743-846a053fa0d2 h1:BuRsOAtWupuYasdwxkY0jVoVNDThqO46MxcJ6T0UXuc=
github.com/replicatedhq/kots v1.15.3-0.20200508174743-846a053fa0d2/go.mod h1:75+q98hCN7KFP/wnsZes5762UZ6tR3Up+EAdbAck7RU=
github.com/replicatedhq/kurl/kurlkinds v0.0.0-20200306230415-b6d377a48a56 h1:W4lzQxpdUPFVTjs6zCDFGyAbPfow28zbFU65QCQf+SY=
github.com/replicatedhq/kurl/kurlkinds v0.0.0-20200306230415-b6d377a48a56/go.mod h1:Vp5X4tM0KmahplYOvdRqs2NY2qy1amZ89zZBm9CDENw=
github.com/replicatedhq/troubleshoot v0.9.27 h1:51ouEai9xSp7E9+Jbd+Oz1wxu5jFJbXSvLr3Vq6ivTk=
Expand Down
2 changes: 2 additions & 0 deletions kotsadm/pkg/airgap/airgap.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ func CreateAppFromAirgap(pendingApp *PendingApp, airgapBundle multipart.File, re
Username: username,
Password: password,
},
AppSlug: pendingApp.Slug,
AppSequence: 0,
}

if _, err := pull.Pull(fmt.Sprintf("replicated://%s", license.Spec.AppSlug), pullOptions); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions kotsadm/pkg/airgap/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func UpdateAppFromAirgap(a *app.App, airgapBundle multipart.File) error {
appNamespace = os.Getenv("KOTSADM_TARGET_NAMESPACE")
}

appSequence, err := version.GetNextAppSequence(a.ID, &a.CurrentSequence)
if err != nil {
return errors.Wrap(err, "failed to get new app sequence")
}

pipeReader, pipeWriter := io.Pipe()
go func() {
scanner := bufio.NewScanner(pipeReader)
Expand Down Expand Up @@ -159,6 +164,8 @@ func UpdateAppFromAirgap(a *app.App, airgapBundle multipart.File) error {
Username: registrySettings.Username,
Password: string(decryptedPassword),
},
AppSlug: a.Slug,
AppSequence: appSequence,
}

if _, err := pull.Pull(fmt.Sprintf("replicated://%s", beforeKotsKinds.License.Spec.AppSlug), pullOptions); err != nil {
Expand Down
11 changes: 10 additions & 1 deletion kotsadm/pkg/handlers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,21 @@ func updateAppConfig(updateApp *app.App, sequence int64, req UpdateAppConfigRequ
return updateAppConfigResponse, err
}

appSequence := updateApp.CurrentSequence
if req.CreateNewVersion {
appSequence, err = version.GetNextAppSequence(updateApp.ID, &updateApp.CurrentSequence)
if err != nil {
updateAppConfigResponse.Error = "failed to get next app sequence"
return updateAppConfigResponse, errors.Wrap(err, "failed to get new app sequence")
}
}

registrySettings, err := registry.GetRegistrySettingsForApp(updateApp.ID)
if err != nil {
updateAppConfigResponse.Error = "failed to get registry settings"
return updateAppConfigResponse, err
}
err = render.RenderDir(archiveDir, updateApp.ID, registrySettings)
err = render.RenderDir(archiveDir, updateApp.ID, appSequence, registrySettings)
if err != nil {
updateAppConfigResponse.Error = "failed to render archive directory"
return updateAppConfigResponse, err
Expand Down
7 changes: 6 additions & 1 deletion kotsadm/pkg/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ func Sync(a *app.App, licenseData string) (*kotsv1beta1.License, error) {
return nil, errors.Wrap(err, "update app license")
}

appSequence, err := version.GetNextAppSequence(a.ID, &a.CurrentSequence)
if err != nil {
return nil, errors.Wrap(err, "failed to get new app sequence")
}

registrySettings, err := registry.GetRegistrySettingsForApp(a.ID)
if err != nil {
return nil, errors.Wrap(err, "failed to get registry settings for app")
}

if err := render.RenderDir(archiveDir, a.ID, registrySettings); err != nil {
if err := render.RenderDir(archiveDir, a.ID, appSequence, registrySettings); err != nil {
return nil, errors.Wrap(err, "failed to render new version")
}

Expand Down
2 changes: 2 additions & 0 deletions kotsadm/pkg/online/online.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func CreateAppFromOnline(pendingApp *PendingApp, upstreamURI string) (*kotsutil.
CreateAppDir: false,
ConfigFile: configFile,
ReportWriter: pipeWriter,
AppSlug: pendingApp.Slug,
AppSequence: 0,
}

if _, err := pull.Pull(upstreamURI, pullOptions); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions kotsadm/pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ func RewriteImages(appID string, sequence int64, hostname string, username strin
appNamespace = os.Getenv("KOTSADM_TARGET_NAMESPACE")
}

appSequence, err := version.GetNextAppSequence(a.ID, &a.CurrentSequence)
if err != nil {
return errors.Wrap(err, "failed to get new app sequence")
}

pipeReader, pipeWriter := io.Pipe()
go func() {
scanner := bufio.NewScanner(pipeReader)
Expand Down Expand Up @@ -196,6 +201,8 @@ func RewriteImages(appID string, sequence int64, hostname string, username strin
RegistryUsername: username,
RegistryPassword: password,
RegistryNamespace: namespace,
AppSlug: a.Slug,
AppSequence: appSequence,
}

if err := rewrite.Rewrite(options); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion kotsadm/pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func RenderFile(kotsKinds *kotsutil.KotsKinds, registrySettings *registrytypes.R

// RenderDir renders an app archive dir
// this is useful for when the license/config have updated, and template functions need to be evaluated again
func RenderDir(archiveDir string, appID string, registrySettings *registrytypes.RegistrySettings) error {
func RenderDir(archiveDir string, appID string, appSequence int64, registrySettings *registrytypes.RegistrySettings) error {
installation, err := kotsutil.LoadInstallationFromPath(filepath.Join(archiveDir, "upstream", "userdata", "installation.yaml"))
if err != nil {
return errors.Wrap(err, "failed to load installation from path")
Expand Down Expand Up @@ -135,6 +135,8 @@ func RenderDir(archiveDir string, appID string, registrySettings *registrytypes.
K8sNamespace: appNamespace,
CopyImages: false,
IsAirgap: a.IsAirgap,
AppSlug: a.Slug,
AppSequence: appSequence,
}

if registrySettings != nil {
Expand Down
17 changes: 12 additions & 5 deletions kotsadm/pkg/upstream/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,21 @@ func DownloadUpdate(appID string, archiveDir string, toCursor string) error {
pipeReader.CloseWithError(scanner.Err())
}()

a, err := app.Get(appID)
if err != nil {
return errors.Wrap(err, "failed to get app")
}

appNamespace := os.Getenv("POD_NAMESPACE")
if os.Getenv("KOTSADM_TARGET_NAMESPACE") != "" {
appNamespace = os.Getenv("KOTSADM_TARGET_NAMESPACE")
}

appSequence, err := version.GetNextAppSequence(a.ID, &a.CurrentSequence)
if err != nil {
return errors.Wrap(err, "failed to get new app sequence")
}

pullOptions := kotspull.PullOptions{
LicenseFile: filepath.Join(archiveDir, "upstream", "userdata", "license.yaml"),
Namespace: appNamespace,
Expand All @@ -85,6 +95,8 @@ func DownloadUpdate(appID string, archiveDir string, toCursor string) error {
ExcludeAdminConsole: true,
CreateAppDir: false,
ReportWriter: pipeWriter,
AppSlug: a.Slug,
AppSequence: appSequence,
}

registrySettings, err := registry.GetRegistrySettingsForApp(appID)
Expand Down Expand Up @@ -133,11 +145,6 @@ func DownloadUpdate(appID string, archiveDir string, toCursor string) error {
return nil // ?
}

a, err := app.Get(appID)
if err != nil {
return errors.Wrap(err, "failed to get app")
}

newSequence, err := version.CreateVersion(appID, archiveDir, "Upstream Update", a.CurrentSequence)
if err != nil {
finalError = err
Expand Down
28 changes: 19 additions & 9 deletions kotsadm/pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ import (
"github.com/replicatedhq/kots/kotsadm/pkg/version/types"
)

// GetNextAppSequence determines next available sequence for this app
// we shouldn't assume that a.CurrentSequence is accurate. Returns 0 if currentSequence is nil
func GetNextAppSequence(appID string, currentSequence *int64) (int64, error) {
newSequence := 0
if currentSequence != nil {
db := persistence.MustGetPGSession()
row := db.QueryRow(`select max(sequence) from app_version where app_id = $1`, appID)
if err := row.Scan(&newSequence); err != nil {
return 0, errors.Wrap(err, "failed to find current max sequence in row")
}
newSequence++
}
return int64(newSequence), nil
}

// CreateFirstVersion works much likst CreateVersion except that it assumes version 0
// and never attempts to calculate a diff, or look at previous versions
func CreateFirstVersion(appID string, filesInDir string, source string) (int64, error) {
Expand Down Expand Up @@ -81,16 +96,11 @@ func createVersion(appID string, filesInDir string, source string, currentSequen
}
defer tx.Rollback()

newSequence := 0
if currentSequence != nil {
// determine next available sequence for this app - we shouldn't assume that a.CurrentSequence is accurate
row := tx.QueryRow(`select max(sequence) from app_version where app_id = $1`, appID)
err = row.Scan(&newSequence)
if err != nil {
return 0, errors.Wrap(err, "failed to find current max sequence in row")
}
newSequence++
n, err := GetNextAppSequence(appID, currentSequence)
if err != nil {
return 0, errors.Wrap(err, "failed to get new app sequence")
}
newSequence := int(n)

query := `insert into app_version (app_id, sequence, created_at, version_label, release_notes, update_cursor, channel_name, encryption_key,
supportbundle_spec, analyzer_spec, preflight_spec, app_spec, kots_app_spec, kots_license, config_spec, config_values, backup_spec)
Expand Down

0 comments on commit 66333cf

Please sign in to comment.