v1.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package filterutil
  2. import (
  3. "reflect"
  4. "github.com/opencost/opencost/pkg/kubecost"
  5. "github.com/opencost/opencost/pkg/log"
  6. "github.com/opencost/opencost/pkg/util/mapper"
  7. )
  8. func ConvertFilterQueryParams(qp mapper.PrimitiveMapReader, labelConfig *kubecost.LabelConfig) AllocationFilterV1 {
  9. filter := AllocationFilterV1{
  10. Annotations: qp.GetList(ParamFilterAnnotations, ","),
  11. Containers: qp.GetList(ParamFilterContainers, ","),
  12. Controllers: qp.GetList(ParamFilterControllers, ","),
  13. ControllerKinds: qp.GetList(ParamFilterControllerKinds, ","),
  14. Clusters: qp.GetList(ParamFilterClusters, ","),
  15. Labels: qp.GetList(ParamFilterLabels, ","),
  16. Namespaces: qp.GetList(ParamFilterNamespaces, ","),
  17. Nodes: qp.GetList(ParamFilterNodes, ","),
  18. Pods: qp.GetList(ParamFilterPods, ","),
  19. Services: qp.GetList(ParamFilterServices, ","),
  20. }
  21. if labelConfig != nil {
  22. filter.Departments = qp.GetList(ParamFilterDepartments, ",")
  23. filter.Environments = qp.GetList(ParamFilterEnvironments, ",")
  24. filter.Owners = qp.GetList(ParamFilterOwners, ",")
  25. filter.Products = qp.GetList(ParamFilterProducts, ",")
  26. filter.Teams = qp.GetList(ParamFilterTeams, ",")
  27. } else {
  28. log.Debugf("No label config is available. Not creating filters for label-mapped 'fields'.")
  29. }
  30. return filter
  31. }
  32. type AllocationFilterV1 struct {
  33. Annotations []string `json:"annotations,omitempty"`
  34. Containers []string `json:"containers,omitempty"`
  35. Controllers []string `json:"controllers,omitempty"`
  36. ControllerKinds []string `json:"controllerKinds,omitempty"`
  37. Clusters []string `json:"clusters,omitempty"`
  38. Departments []string `json:"departments,omitempty"`
  39. Environments []string `json:"environments,omitempty"`
  40. Labels []string `json:"labels,omitempty"`
  41. Namespaces []string `json:"namespaces,omitempty"`
  42. Nodes []string `json:"nodes,omitempty"`
  43. Owners []string `json:"owners,omitempty"`
  44. Pods []string `json:"pods,omitempty"`
  45. Products []string `json:"products,omitempty"`
  46. Services []string `json:"services,omitempty"`
  47. Teams []string `json:"teams,omitempty"`
  48. }
  49. func (f AllocationFilterV1) Equals(that AllocationFilterV1) bool {
  50. return reflect.DeepEqual(f.Annotations, that.Annotations) &&
  51. reflect.DeepEqual(f.Containers, that.Containers) &&
  52. reflect.DeepEqual(f.Controllers, that.Controllers) &&
  53. reflect.DeepEqual(f.ControllerKinds, that.ControllerKinds) &&
  54. reflect.DeepEqual(f.Clusters, that.Clusters) &&
  55. reflect.DeepEqual(f.Departments, that.Departments) &&
  56. reflect.DeepEqual(f.Environments, that.Environments) &&
  57. reflect.DeepEqual(f.Labels, that.Labels) &&
  58. reflect.DeepEqual(f.Namespaces, that.Namespaces) &&
  59. reflect.DeepEqual(f.Nodes, that.Nodes) &&
  60. reflect.DeepEqual(f.Owners, that.Owners) &&
  61. reflect.DeepEqual(f.Pods, that.Pods) &&
  62. reflect.DeepEqual(f.Products, that.Products) &&
  63. reflect.DeepEqual(f.Services, that.Services) &&
  64. reflect.DeepEqual(f.Teams, that.Teams)
  65. }