networkinsightmatcher.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package opencost
  2. import (
  3. "fmt"
  4. ast "github.com/opencost/opencost/core/pkg/filter/ast"
  5. "github.com/opencost/opencost/core/pkg/filter/matcher"
  6. nfilter "github.com/opencost/opencost/core/pkg/filter/networkinsight"
  7. "github.com/opencost/opencost/core/pkg/filter/transform"
  8. )
  9. // NetworkInsightMatcher is a matcher implementation for NetworkInsightSet,
  10. // compiled using the matcher.MatchCompiler.
  11. type NetworkInsightMatcher matcher.Matcher[*NetworkInsight]
  12. // NewNetworkInsightMatchCompiler creates a new instance of a
  13. // matcher.MatchCompiler[NetworkInsight] which can be used to compile filter.Filter
  14. // ASTs into matcher.Matcher[NetworkInsight] implementations.
  15. //
  16. // If the label config is nil, the compiler will fail to compile alias filters
  17. // if any are present in the AST.
  18. //
  19. // If storage interfaces every support querying natively by alias (e.g. if a
  20. // data store contained a "product" attribute on an Asset row), that should
  21. // be handled by a purpose-built AST compiler.
  22. func NewNetworkInsightMatchCompiler() *matcher.MatchCompiler[*NetworkInsight] {
  23. passes := []transform.CompilerPass{}
  24. return matcher.NewMatchCompiler(
  25. networkInsightFieldMap,
  26. networkInsightSliceFieldMap,
  27. networkInsightMapFieldMap,
  28. passes...,
  29. )
  30. }
  31. // Maps fields from a network insight to a string value based on an identifier
  32. func networkInsightFieldMap(ni *NetworkInsight, identifier ast.Identifier) (string, error) {
  33. if ni == nil {
  34. return "", fmt.Errorf("cannot map field for nil Network insight")
  35. }
  36. if identifier.Field == nil {
  37. return "", fmt.Errorf("cannot map field from identifier with nil field")
  38. }
  39. switch nfilter.NetworkInsightField(identifier.Field.Name) {
  40. case nfilter.FieldClusterID:
  41. return ni.Cluster, nil
  42. case nfilter.FieldNamespace:
  43. return ni.Namespace, nil
  44. case nfilter.FieldPod:
  45. return ni.Pod, nil
  46. }
  47. return "", fmt.Errorf("Failed to find string identifier on Network Insight: %s", identifier.Field.Name)
  48. }
  49. // Maps slice fields from a network insight to a []string value based on an identifier
  50. func networkInsightSliceFieldMap(ni *NetworkInsight, identifier ast.Identifier) ([]string, error) {
  51. return nil, fmt.Errorf("NetworkInsights have no slice fields")
  52. }
  53. // Maps map fields from a network insight to a map[string]string value based on an identifier
  54. func networkInsightMapFieldMap(ni *NetworkInsight, identifier ast.Identifier) (map[string]string, error) {
  55. return nil, fmt.Errorf("NetworkInsights have no map fields")
  56. }
  57. // NetworkInsightMatcher is a matcher implementation for NetworkInsightSet,
  58. // compiled using the matcher.MatchCompiler.
  59. type NetworkInsightDetailMatcher matcher.Matcher[*NetworkDetail]
  60. // NewNetworkInsightMatchCompiler creates a new instance of a
  61. // matcher.MatchCompiler[NetworkInsight] which can be used to compile filter.Filter
  62. // ASTs into matcher.Matcher[NetworkInsight] implementations.
  63. //
  64. // If the label config is nil, the compiler will fail to compile alias filters
  65. // if any are present in the AST.
  66. //
  67. // If storage interfaces every support querying natively by alias (e.g. if a
  68. // data store contained a "product" attribute on an Asset row), that should
  69. // be handled by a purpose-built AST compiler.
  70. func NewNetworkInsightDetailMatchCompiler() *matcher.MatchCompiler[*NetworkDetail] {
  71. passes := []transform.CompilerPass{}
  72. return matcher.NewMatchCompiler(
  73. networkInsightDetailFieldMap,
  74. networkInsightDetailSliceFieldMap,
  75. networkInsightDetailMapFieldMap,
  76. passes...,
  77. )
  78. }
  79. // Maps fields from a network insight to a string value based on an identifier
  80. func networkInsightDetailFieldMap(nd *NetworkDetail, identifier ast.Identifier) (string, error) {
  81. if nd == nil {
  82. return "", fmt.Errorf("cannot map field for nil Network insight")
  83. }
  84. if identifier.Field == nil {
  85. return "", fmt.Errorf("cannot map field from identifier with nil field")
  86. }
  87. switch nfilter.NetworkInsightDetailField(identifier.Field.Name) {
  88. case nfilter.FieldEndPoint:
  89. return nd.EndPoint, nil
  90. }
  91. return "", fmt.Errorf("Failed to find string identifier on Network Insight: %s", identifier.Field.Name)
  92. }
  93. // Maps slice fields from a network insight to a []string value based on an identifier
  94. func networkInsightDetailSliceFieldMap(nd *NetworkDetail, identifier ast.Identifier) ([]string, error) {
  95. return nil, fmt.Errorf("NetworkInsights have no slice fields")
  96. }
  97. // Maps map fields from a network insight to a map[string]string value based on an identifier
  98. func networkInsightDetailMapFieldMap(nd *NetworkDetail, identifier ast.Identifier) (map[string]string, error) {
  99. return nil, fmt.Errorf("NetworkInsights have no map fields")
  100. }