Skip to content

Commit

Permalink
fix: clean completed migration job before reinitialize on-prem demo (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
exu authored Oct 2, 2024
1 parent ce86ab6 commit 7d549a8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/api-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func runMigrations() (err error) {
return migrations.Migrator.Run(version.Version, migrator.MigrationTypeServer)
}

func runMongoMigrations(ctx context.Context, db *mongo.Database, migrationsDir string) error {
func runMongoMigrations(ctx context.Context, db *mongo.Database, _ string) error {
migrationsCollectionName := "__migrations"
activeMigrations, err := dbmigrator.GetDbMigrationsFromFs(dbmigrations.MongoMigrationsFs)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/kubectl-testkube/commands/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (
TKErrHelmCommandFailed ErrorCode = "TKERR-1301"
// TKErrKubectlCommandFailed is returned when a kubectl command fail.
TKErrKubectlCommandFailed ErrorCode = "TKERR-1302"

// TKErrCleanOldMigrationJobFailed is returned in case of issues with old migration jobs.
TKErrCleanOldMigrationJobFailed ErrorCode = "TKERR-1401"
)

const helpUrl = "https://testkubeworkspace.slack.com"
Expand Down
24 changes: 24 additions & 0 deletions cmd/kubectl-testkube/commands/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,30 @@ func updateHelmRepo(helmPath string, dryRun bool, isOnPrem bool) *CLIError {
return nil
}

// It cleans existing migrations job with long TTL
func CleanExistingCompletedMigrationJobs(namespace string) (cliErr *CLIError) {
kubectlPath, cliErr := lookupKubectlPath()
if cliErr != nil {
return cliErr
}

// Clean the job only when it's found and it's state is successful - ignore pending migrations.
succeeded, _ := runKubectlCommand(kubectlPath, []string{"get", "job", "testkube-enterprise-api-migrations", "-n", namespace, "-o", "jsonpath={.status.succeeded}"})
if succeeded == "1" {
_, err := runKubectlCommand(kubectlPath, []string{"delete", "job", "testkube-enterprise-api-migrations", "--namespace", namespace})
if err != nil {
return NewCLIError(
TKErrCleanOldMigrationJobFailed,
"Can't clean old migrations job",
"Migration job can't be deleted from some reason, check for errors in installation namespace, check execution. As a workaround try to delete job manually and retry installation/upgrade process",
err,
)
}
}

return nil
}

func runHelmCommand(helmPath string, args []string, dryRun bool) (commandOutput string, cliErr *CLIError) {
output, err := process.ExecuteWithOptions(process.Options{Command: helmPath, Args: args, DryRun: dryRun})
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions cmd/kubectl-testkube/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ func NewInitCmdDemo() *cobra.Command {
DemoValuesURL: demoValuesUrl,
DryRun: dryRun,
}

cliErr = common.CleanExistingCompletedMigrationJobs(options.Namespace)
if cliErr != nil {
spinner.Fail("Failed to install Testkube On-Prem Demo")
if cfg.TelemetryEnabled {
cliErr.AddTelemetry(cmd, "installing", "install_failed", license)
_, _ = telemetry.HandleCLIErrorTelemetry(common.Version, cliErr)
}
common.HandleCLIError(cliErr)
}

cliErr = common.HelmUpgradeOrInstallTestkubeOnPremDemo(options)
if cliErr != nil {
spinner.Fail("Failed to install Testkube On-Prem Demo")
Expand Down
2 changes: 1 addition & 1 deletion cmd/testworkflow-init/obfuscator/obfuscator.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (s *obfuscator) Write(p []byte) (n int, err error) {

// Write the rest of data
if len(p) > 0 {
nn, err = s.dst.Write(p)
_, err = s.dst.Write(p)
return size, err
}
return size, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (ag *Agent) runWorkers(ctx context.Context, numWorkers int) error {
return g.Wait()
}

func (ag *Agent) executeCommand(ctx context.Context, cmd *cloud.ExecuteRequest) *cloud.ExecuteResponse {
func (ag *Agent) executeCommand(_ context.Context, cmd *cloud.ExecuteRequest) *cloud.ExecuteResponse {
switch {
case cmd.Url == healthcheckCommand:
return &cloud.ExecuteResponse{MessageId: cmd.MessageId, Status: 0}
Expand Down

0 comments on commit 7d549a8

Please sign in to comment.