Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt to refactored API
Browse files Browse the repository at this point in the history
jotak committed Mar 5, 2024

Verified

This commit was signed with the committer’s verified signature.
jonasbjoralt Jonas Bjøralt
1 parent 93543c5 commit 2e5bc67
Showing 3 changed files with 26 additions and 16 deletions.
20 changes: 13 additions & 7 deletions pkg/api/transform_network.go
Original file line number Diff line number Diff line change
@@ -63,13 +63,13 @@ func TransformNetworkOperationName(operation string) string {
}

type NetworkTransformRule struct {
Type string `yaml:"type,omitempty" json:"type,omitempty" enum:"TransformNetworkOperationEnum" doc:"one of the following:"`
KubernetesInfra *K8sInfraRule `yaml:"kubernetes_infra,omitempty" json:"kubernetes_infra,omitempty" doc:"Kubernetes infra rule configuration"`
Kubernetes *K8sRule `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty" doc:"Kubernetes rule configuration"`
AddSubnet *NetworkAddSubnetRule `yaml:"add_subnet,omitempty" json:"add_subnet,omitempty" doc:"Add subnet rule configuration"`
AddLocation *NetworkGenericRule `yaml:"add_location,omitempty" json:"add_location,omitempty" doc:"Add location rule configuration"`
AddSubnetLabel *NetworkGenericRule `yaml:"add_subnet_label,omitempty" json:"add_subnet_label,omitempty" doc:"Add subnet label rule configuration"`
AddService *NetworkAddServiceRule `yaml:"add_service,omitempty" json:"add_service,omitempty" doc:"Add service rule configuration"`
Type string `yaml:"type,omitempty" json:"type,omitempty" enum:"TransformNetworkOperationEnum" doc:"one of the following:"`
KubernetesInfra *K8sInfraRule `yaml:"kubernetes_infra,omitempty" json:"kubernetes_infra,omitempty" doc:"Kubernetes infra rule configuration"`
Kubernetes *K8sRule `yaml:"kubernetes,omitempty" json:"kubernetes,omitempty" doc:"Kubernetes rule configuration"`
AddSubnet *NetworkAddSubnetRule `yaml:"add_subnet,omitempty" json:"add_subnet,omitempty" doc:"Add subnet rule configuration"`
AddLocation *NetworkGenericRule `yaml:"add_location,omitempty" json:"add_location,omitempty" doc:"Add location rule configuration"`
AddSubnetLabel *NetworkAddSubnetLabelRule `yaml:"add_subnet_label,omitempty" json:"add_subnet_label,omitempty" doc:"Add subnet label rule configuration"`
AddService *NetworkAddServiceRule `yaml:"add_service,omitempty" json:"add_service,omitempty" doc:"Add service rule configuration"`
}

type K8sInfraRule struct {
@@ -103,6 +103,12 @@ type NetworkAddSubnetRule struct {
SubnetMask string `yaml:"subnet_mask,omitempty" json:"subnet_mask,omitempty" doc:"subnet mask field"`
}

type NetworkAddSubnetLabelRule struct {
Input string `yaml:"input,omitempty" json:"input,omitempty" doc:"entry input field"`
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
SkipIfFieldExists string `yaml:"skip_if_field_exists,omitempty" json:"skip_if_field_exists,omitempty" doc:"skip labelling flows when the given field exists, to avoid creating redundant information"`
}

type NetworkAddServiceRule struct {
Input string `yaml:"input,omitempty" json:"input,omitempty" doc:"entry input field"`
Output string `yaml:"output,omitempty" json:"output,omitempty" doc:"entry output field"`
8 changes: 4 additions & 4 deletions pkg/pipeline/transform/transform_network.go
Original file line number Diff line number Diff line change
@@ -129,18 +129,18 @@ func (n *Network) Transform(inputEntry config.GenericMap) (config.GenericMap, bo
lbl, ok := n.ipLabelCache.GetCacheEntry(strIP)
if !ok {
skip := false
if rule.Parameters != "" {
if rule.AddSubnetLabel.SkipIfFieldExists != "" {
// rule.Parameters holds the name of a field that must be absent to apply the rule (else, just skip applying)
_, skip = outputEntry[rule.Parameters]
_, skip = outputEntry[rule.AddSubnetLabel.SkipIfFieldExists]
lbl = ""
}
if !skip {
lbl = n.applySubnetLabel(strIP, rule.Parameters)
lbl = n.applySubnetLabel(strIP, rule.AddSubnetLabel.SkipIfFieldExists)
}
n.ipLabelCache.UpdateCacheEntry(strIP, lbl)
}
if lbl != "" {
outputEntry[rule.AddIPCategory.Output] = lbl
outputEntry[rule.AddSubnetLabel.Output] = lbl
}
}
}
14 changes: 9 additions & 5 deletions pkg/pipeline/transform/transform_network_test.go
Original file line number Diff line number Diff line change
@@ -227,10 +227,10 @@ func Test_Categorize(t *testing.T) {
Transform: &config.Transform{
Network: &api.TransformNetwork{
Rules: []api.NetworkTransformRule{
{Type: api.OpAddSubnetLabel, AddIPCategory: &api.NetworkGenericRule{Input: "addr1", Output: "cat1"}},
{Type: api.OpAddSubnetLabel, AddIPCategory: &api.NetworkGenericRule{Input: "addr2", Output: "cat2"}},
{Type: api.OpAddSubnetLabel, AddIPCategory: &api.NetworkGenericRule{Input: "addr3", Output: "cat3"}},
{Type: api.OpAddSubnetLabel, AddIPCategory: &api.NetworkGenericRule{Input: "addr4", Output: "cat4"}},
{Type: api.OpAddSubnetLabel, AddSubnetLabel: &api.NetworkAddSubnetLabelRule{Input: "addr1", Output: "cat1"}},
{Type: api.OpAddSubnetLabel, AddSubnetLabel: &api.NetworkAddSubnetLabelRule{Input: "addr2", Output: "cat2"}},
{Type: api.OpAddSubnetLabel, AddSubnetLabel: &api.NetworkAddSubnetLabelRule{Input: "addr3", Output: "cat3"}},
{Type: api.OpAddSubnetLabel, AddSubnetLabel: &api.NetworkAddSubnetLabelRule{Input: "addr4", Output: "cat4"}},
},
SubnetLabels: []api.NetworkTransformSubnetLabel{{
Name: "Pods overlay",
@@ -269,7 +269,11 @@ func Test_CategorizeOnlyUnknown(t *testing.T) {
Transform: &config.Transform{
Network: &api.TransformNetwork{
Rules: []api.NetworkTransformRule{
{Type: api.OpAddSubnetLabel, Input: "addr1", Output: "cat1", Parameters: "type"},
{Type: api.OpAddSubnetLabel, AddSubnetLabel: &api.NetworkAddSubnetLabelRule{
Input: "addr1",
Output: "cat1",
SkipIfFieldExists: "type",
}},
},
SubnetLabels: []api.NetworkTransformSubnetLabel{{
Name: "Pods overlay",

0 comments on commit 2e5bc67

Please sign in to comment.