diff --git a/agent/pkg/rules/rulesHTTP.go b/agent/pkg/rules/rulesHTTP.go index 855768344..d2096fa83 100644 --- a/agent/pkg/rules/rulesHTTP.go +++ b/agent/pkg/rules/rulesHTTP.go @@ -101,7 +101,7 @@ func MatchRequestPolicy(harEntry har.Entry, service string) (resultPolicyToSend func PassedValidationRules(rulesMatched []RulesMatched) (bool, int64, int) { var numberOfRulesMatched = len(rulesMatched) - var latency int64 = -1 + var responseTime int64 = -1 if numberOfRulesMatched == 0 { return false, 0, numberOfRulesMatched @@ -109,15 +109,15 @@ func PassedValidationRules(rulesMatched []RulesMatched) (bool, int64, int) { for _, rule := range rulesMatched { if rule.Matched == false { - return false, latency, numberOfRulesMatched + return false, responseTime, numberOfRulesMatched } else { - if strings.ToLower(rule.Rule.Type) == "latency" { - if rule.Rule.Latency < latency || latency == -1 { - latency = rule.Rule.Latency + if strings.ToLower(rule.Rule.Type) == "responseTime" { + if rule.Rule.ResponseTime < responseTime || responseTime == -1 { + responseTime = rule.Rule.ResponseTime } } } } - return true, latency, numberOfRulesMatched + return true, responseTime, numberOfRulesMatched } diff --git a/cli/cmd/tapRunner.go b/cli/cmd/tapRunner.go index 4f4ce6d37..5549296d8 100644 --- a/cli/cmd/tapRunner.go +++ b/cli/cmd/tapRunner.go @@ -76,7 +76,7 @@ func RunMizuTap() { targetNamespaces := getNamespaces(kubernetesProvider) if config.Config.IsNsRestrictedMode() { - if len(targetNamespaces) != 1 || !mizu.Contains(targetNamespaces, config.Config.MizuResourcesNamespace) { + if len(targetNamespaces) != 1 || !shared.Contains(targetNamespaces, config.Config.MizuResourcesNamespace) { logger.Log.Errorf("Not supported mode. Mizu can't resolve IPs in other namespaces when running in namespace restricted mode.\n"+ "You can use the same namespace for --%s and --%s", configStructs.NamespacesTapName, config.MizuResourcesNamespaceConfigName) return @@ -84,7 +84,7 @@ func RunMizuTap() { } var namespacesStr string - if !mizu.Contains(targetNamespaces, mizu.K8sAllNamespaces) { + if !shared.Contains(targetNamespaces, mizu.K8sAllNamespaces) { namespacesStr = fmt.Sprintf("namespaces \"%s\"", strings.Join(targetNamespaces, "\", \"")) } else { namespacesStr = "all namespaces" @@ -99,7 +99,7 @@ func RunMizuTap() { if len(state.currentlyTappedPods) == 0 { var suggestionStr string - if !mizu.Contains(targetNamespaces, mizu.K8sAllNamespaces) { + if !shared.Contains(targetNamespaces, mizu.K8sAllNamespaces) { suggestionStr = ". Select a different namespace with -n or tap all namespaces with -A" } logger.Log.Warningf(uiUtils.Warning, fmt.Sprintf("Did not find any pods matching the regex argument%s", suggestionStr)) @@ -639,7 +639,7 @@ func getNamespaces(kubernetesProvider *kubernetes.Provider) []string { if config.Config.Tap.AllNamespaces { return []string{mizu.K8sAllNamespaces} } else if len(config.Config.Tap.Namespaces) > 0 { - return mizu.Unique(config.Config.Tap.Namespaces) + return shared.Unique(config.Config.Tap.Namespaces) } else { return []string{kubernetesProvider.CurrentNamespace()} } diff --git a/cli/config/config.go b/cli/config/config.go index b75e79cb8..8a65bdf28 100644 --- a/cli/config/config.go +++ b/cli/config/config.go @@ -4,7 +4,7 @@ import ( "errors" "fmt" "github.com/up9inc/mizu/cli/logger" - "github.com/up9inc/mizu/cli/mizu" + "github.com/up9inc/mizu/shared" "io/ioutil" "os" "reflect" @@ -89,7 +89,7 @@ func initFlag(f *pflag.Flag) { configElemValue := reflect.ValueOf(&Config).Elem() var flagPath []string - if mizu.Contains([]string{ConfigFilePathCommandName}, f.Name) { + if shared.Contains([]string{ConfigFilePathCommandName}, f.Name) { flagPath = []string{f.Name} } else { flagPath = []string{cmdName, f.Name} diff --git a/shared/models.go b/shared/models.go index 5dac6e9cc..73ab4041c 100644 --- a/shared/models.go +++ b/shared/models.go @@ -1,8 +1,8 @@ package shared import ( - "fmt" "io/ioutil" + "log" "strings" "gopkg.in/yaml.v3" @@ -83,14 +83,14 @@ type RulesPolicy struct { } type RulePolicy struct { - Type string `yaml:"type"` - Service string `yaml:"service"` - Path string `yaml:"path"` - Method string `yaml:"method"` - Key string `yaml:"key"` - Value string `yaml:"value"` - Latency int64 `yaml:"latency"` - Name string `yaml:"name"` + Type string `yaml:"type"` + Service string `yaml:"service"` + Path string `yaml:"path"` + Method string `yaml:"method"` + Key string `yaml:"key"` + Value string `yaml:"value"` + ResponseTime int64 `yaml:"response-time"` + Name string `yaml:"name"` } type RulesMatched struct { @@ -99,14 +99,17 @@ type RulesMatched struct { } func (r *RulePolicy) validateType() bool { - permitedTypes := []string{"json", "header", "latency"} + permitedTypes := []string{"json", "header", "slo"} _, found := Find(permitedTypes, r.Type) if !found { - fmt.Printf("\nRule with name %s will be ignored. Err: only json, header and latency types are supported on rule definition.\n", r.Name) + log.Printf("Error: %s. ", r.Name) + log.Printf("Only json, header and slo types are supported on rule definition. This rule will be ignored\n") + found = false } - if strings.ToLower(r.Type) == "latency" { - if r.Latency == 0 { - fmt.Printf("\nRule with name %s will be ignored. Err: when type=latency, the field Latency should be specified and have a value >= 1\n\n", r.Name) + if strings.ToLower(r.Type) == "slo" { + if r.ResponseTime <= 0 { + log.Printf("Error: %s. ", r.Name) + log.Printf("When type=slo, the field response-time should be specified and have a value >= 1\n\n") found = false } } @@ -124,10 +127,6 @@ func (rules *RulesPolicy) ValidateRulesPolicy() []int { return invalidIndex } -func (rules *RulesPolicy) RemoveRule(idx int) { - rules.Rules = append(rules.Rules[:idx], rules.Rules[idx+1:]...) -} - func Find(slice []string, val string) (int, bool) { for i, item := range slice { if item == val { @@ -148,10 +147,15 @@ func DecodeEnforcePolicy(path string) (RulesPolicy, error) { return enforcePolicy, err } invalidIndex := enforcePolicy.ValidateRulesPolicy() + var k = 0 if len(invalidIndex) != 0 { - for i := range invalidIndex { - enforcePolicy.RemoveRule(invalidIndex[i]) + for i, rule := range enforcePolicy.Rules { + if !ContainsInt(invalidIndex, i) { + enforcePolicy.Rules[k] = rule + k++ + } } + enforcePolicy.Rules = enforcePolicy.Rules[:k] } return enforcePolicy, nil } diff --git a/cli/mizu/sliceUtils.go b/shared/sliceUtils.go similarity index 69% rename from cli/mizu/sliceUtils.go rename to shared/sliceUtils.go index 94e253225..6d08c70b5 100644 --- a/cli/mizu/sliceUtils.go +++ b/shared/sliceUtils.go @@ -1,4 +1,4 @@ -package mizu +package shared func Contains(slice []string, containsValue string) bool { for _, sliceValue := range slice { @@ -10,6 +10,16 @@ func Contains(slice []string, containsValue string) bool { return false } +func ContainsInt(slice []int, containsValue int) bool { + for _, sliceValue := range slice { + if sliceValue == containsValue { + return true + } + } + return false +} + + func Unique(slice []string) []string { keys := make(map[string]bool) var list []string diff --git a/cli/mizu/sliceUtils_test.go b/shared/sliceUtils_test.go similarity index 90% rename from cli/mizu/sliceUtils_test.go rename to shared/sliceUtils_test.go index d5e7efe6d..97d79b0bb 100644 --- a/cli/mizu/sliceUtils_test.go +++ b/shared/sliceUtils_test.go @@ -1,8 +1,8 @@ -package mizu_test +package shared_test import ( "fmt" - "github.com/up9inc/mizu/cli/mizu" + "github.com/up9inc/mizu/shared" "reflect" "testing" ) @@ -21,7 +21,7 @@ func TestContainsExists(t *testing.T) { for _, test := range tests { t.Run(test.ContainsValue, func(t *testing.T) { - actual := mizu.Contains(test.Slice, test.ContainsValue) + actual := shared.Contains(test.Slice, test.ContainsValue) if actual != test.Expected { t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual) } @@ -43,7 +43,7 @@ func TestContainsNotExists(t *testing.T) { for _, test := range tests { t.Run(test.ContainsValue, func(t *testing.T) { - actual := mizu.Contains(test.Slice, test.ContainsValue) + actual := shared.Contains(test.Slice, test.ContainsValue) if actual != test.Expected { t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual) } @@ -63,7 +63,7 @@ func TestContainsEmptySlice(t *testing.T) { for _, test := range tests { t.Run(test.ContainsValue, func(t *testing.T) { - actual := mizu.Contains(test.Slice, test.ContainsValue) + actual := shared.Contains(test.Slice, test.ContainsValue) if actual != test.Expected { t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual) } @@ -83,7 +83,7 @@ func TestContainsNilSlice(t *testing.T) { for _, test := range tests { t.Run(test.ContainsValue, func(t *testing.T) { - actual := mizu.Contains(test.Slice, test.ContainsValue) + actual := shared.Contains(test.Slice, test.ContainsValue) if actual != test.Expected { t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual) } @@ -102,7 +102,7 @@ func TestUniqueNoDuplicateValues(t *testing.T) { for index, test := range tests { t.Run(fmt.Sprintf("%v", index), func(t *testing.T) { - actual := mizu.Unique(test.Slice) + actual := shared.Unique(test.Slice) if !reflect.DeepEqual(test.Expected, actual) { t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual) } @@ -121,7 +121,7 @@ func TestUniqueDuplicateValues(t *testing.T) { for index, test := range tests { t.Run(fmt.Sprintf("%v", index), func(t *testing.T) { - actual := mizu.Unique(test.Slice) + actual := shared.Unique(test.Slice) if !reflect.DeepEqual(test.Expected, actual) { t.Errorf("unexpected result - Expected: %v, actual: %v", test.Expected, actual) } diff --git a/ui/src/components/EntryListItem/EntryListItem.tsx b/ui/src/components/EntryListItem/EntryListItem.tsx index 81da0a56c..0e8c04546 100644 --- a/ui/src/components/EntryListItem/EntryListItem.tsx +++ b/ui/src/components/EntryListItem/EntryListItem.tsx @@ -68,7 +68,7 @@ export const EntryItem: React.FC = ({entry, setFocusedEntryId, isSel let rule = 'latency' in entry.rules if (rule) { if (entry.rules.latency !== -1) { - if (entry.rules.latency >= entry.latency) { + if (entry.rules.latency >= entry.latency || !('latency' in entry)) { additionalRulesProperties = styles.ruleSuccessRow ruleSuccess = true } else {