Skip to content

Commit

Permalink
feat(compose): Introduce RemoveOrphans function
Browse files Browse the repository at this point in the history
The removeOrphans function goes through all machines
linked to a project and removes the ones that are no
longer part of the compose file.

Signed-off-by: Luca Seritan <luca.seritan@gmail.com>
  • Loading branch information
LucaSeri committed Apr 23, 2024
1 parent 8631528 commit 0635cae
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions internal/cli/kraft/compose/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package utils

import (
"context"

"kraftkit.sh/compose"
"kraftkit.sh/internal/cli/kraft/remove"
"kraftkit.sh/log"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
composeapi "kraftkit.sh/api/compose/v1"
machineapi "kraftkit.sh/api/machine/v1alpha1"
mplatform "kraftkit.sh/machine/platform"
)

func RemoveOrphans(ctx context.Context, project *compose.Project) error {
composeController, err := compose.NewComposeProjectV1(ctx)
if err != nil {
return err
}

embeddedProject, err := composeController.Get(ctx, &composeapi.Compose{
ObjectMeta: metav1.ObjectMeta{
Name: project.Name,
},
})
if err != nil {
return err
}

machineController, err := mplatform.NewMachineV1alpha1ServiceIterator(ctx)
if err != nil {
return err
}

machines, err := machineController.List(ctx, &machineapi.MachineList{})
if err != nil {
return err
}

orphanMachines := []string{}
for _, machine := range embeddedProject.Status.Machines {
isService := false
for _, service := range project.Services {
if service.Name == machine.Name {
isService = true
break
}
}

for _, m := range machines.Items {
if m.Name == machine.Name {
if !isService && m.Status.State == machineapi.MachineStateRunning {
orphanMachines = append(orphanMachines, machine.Name)
}
}
}
}

if len(orphanMachines) == 0 {
return nil
}

log.G(ctx).Info("removing orphan machines...")
removeOptions := remove.RemoveOptions{
Platform: "auto",
}

return removeOptions.Run(ctx, orphanMachines)
}

0 comments on commit 0635cae

Please sign in to comment.