resources.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package aci
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/hashicorp/go-multierror"
  18. "github.com/docker/compose-cli/aci/convert"
  19. "github.com/docker/compose-cli/api/resources"
  20. "github.com/docker/compose-cli/context/store"
  21. )
  22. type aciResourceService struct {
  23. aciContext store.AciContext
  24. }
  25. func (cs *aciResourceService) Prune(ctx context.Context, request resources.PruneRequest) (resources.PruneResult, error) {
  26. res, err := getACIContainerGroups(ctx, cs.aciContext.SubscriptionID, cs.aciContext.ResourceGroup)
  27. result := resources.PruneResult{}
  28. if err != nil {
  29. return result, err
  30. }
  31. multierr := &multierror.Error{}
  32. deleted := []string{}
  33. cpus := 0.
  34. mem := 0.
  35. for _, containerGroup := range res {
  36. if !request.Force && convert.GetGroupStatus(containerGroup) == "Node "+convert.StatusRunning {
  37. continue
  38. }
  39. for _, container := range *containerGroup.Containers {
  40. hostConfig := convert.ToHostConfig(container, containerGroup)
  41. cpus += hostConfig.CPUReservation
  42. mem += convert.BytesToGB(float64(hostConfig.MemoryReservation))
  43. }
  44. if !request.DryRun {
  45. _, err := deleteACIContainerGroup(ctx, cs.aciContext, *containerGroup.Name)
  46. multierr = multierror.Append(multierr, err)
  47. }
  48. deleted = append(deleted, *containerGroup.Name)
  49. }
  50. result.DeletedIDs = deleted
  51. result.Summary = fmt.Sprintf("Total CPUs reclaimed: %.2f, total memory reclaimed: %.2f GB", cpus, mem)
  52. return result, multierr.ErrorOrNil()
  53. }