Skip to content

Commit

Permalink
feat: plan for migrate command
Browse files Browse the repository at this point in the history
  • Loading branch information
deryrahman committed May 13, 2024
1 parent 2b53821 commit 21dd699
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions client/cmd/internal/plan/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
OperationDelete Operation = iota + 1
OperationCreate
OperationUpdate
OperationMigrate
)

func (o Operation) String() string {
Expand All @@ -16,6 +17,8 @@ func (o Operation) String() string {
return "create"
case OperationUpdate:
return "update"
case OperationMigrate:
return "migrate"
default:
return ""
}
Expand All @@ -29,6 +32,8 @@ func NewOperationByString(operation string) Operation {
return OperationCreate
case "update":
return OperationUpdate
case "migrate":
return OperationMigrate
default:
return 0
}
Expand Down
24 changes: 23 additions & 1 deletion client/cmd/job/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,24 @@ func (p *planCommand) RunE(_ *cobra.Command, _ []string) error {
plans := make(plan.Plans, 0, len(directories))
p.logger.Info("job plan found changed in directories: %+v", directories)

plansByName := map[string]*plan.Plan{}
for _, directory := range directories {
var jobPlan *plan.Plan
jobPlan, err = p.describePlanFromDirectory(ctx, directory)
if err != nil {
return err
}
if jobMigrated := p.getPlanMigrated(plansByName, jobPlan); jobMigrated != nil {
jobPlan = jobMigrated
}
plansByName[jobPlan.KindName] = jobPlan
}

for _, jobPlan := range plansByName {
plans = append(plans, jobPlan)
if p.verbose {
p.logger.Info("[%s] plan operation %s for job %s", jobPlan.NamespaceName, jobPlan.Operation, jobPlan.KindName)
}
plans = append(plans, jobPlan)
}

sort.SliceStable(plans, plans.SortByOperationPriority)
Expand Down Expand Up @@ -181,6 +189,20 @@ func (p *planCommand) describePlanFromDirectory(ctx context.Context, directory s
return jobPlan, nil
}

func (*planCommand) getPlanMigrated(plansByName map[string]*plan.Plan, targetPlan *plan.Plan) *plan.Plan {
existedPlan, ok := plansByName[targetPlan.KindName]
isMigrated := ok && existedPlan.NamespaceName != targetPlan.NamespaceName
jobPlan := targetPlan
if isMigrated {
if existedPlan.Operation == plan.OperationCreate {
jobPlan = existedPlan
}
jobPlan.Operation = plan.OperationMigrate
return jobPlan
}
return nil
}

func (*planCommand) appendDirectory(directory string, directoryExists map[string]bool, fileDirectories []string) []string {
index := strings.Index(directory, "/assets")
if !strings.HasSuffix(directory, "/"+jobFileName) && index < 1 {
Expand Down

0 comments on commit 21dd699

Please sign in to comment.