-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create warning event if client has internet intent without egress policy
If the egress policy is not enabled, we can't really handle any internet intent, since all internet intents are egress. Add a warning to notify about this configuration invalid status.
- Loading branch information
Showing
4 changed files
with
101 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/operator/controllers/intents_reconcilers/networkpolicy/reconciler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package networkpolicy | ||
|
||
import ( | ||
"context" | ||
"github.com/otterize/intents-operator/src/operator/api/v2alpha1" | ||
otterizev2alpha1 "github.com/otterize/intents-operator/src/operator/api/v2alpha1" | ||
"github.com/otterize/intents-operator/src/operator/controllers/intents_reconcilers/consts" | ||
"github.com/otterize/intents-operator/src/operator/effectivepolicy" | ||
"github.com/otterize/intents-operator/src/shared/injectablerecorder" | ||
"github.com/otterize/intents-operator/src/shared/serviceidresolver/serviceidentity" | ||
"github.com/otterize/intents-operator/src/shared/testbase" | ||
"github.com/stretchr/testify/suite" | ||
"go.uber.org/mock/gomock" | ||
v1 "k8s.io/api/networking/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"testing" | ||
) | ||
|
||
type NetworkPolicyReconcilerTestSuite struct { | ||
testbase.MocksSuiteBase | ||
} | ||
|
||
func (s *NetworkPolicyReconcilerTestSuite) SetupTest() { | ||
s.MocksSuiteBase.SetupTest() | ||
} | ||
|
||
func (s *NetworkPolicyReconcilerTestSuite) TearDownTest() { | ||
s.MocksSuiteBase.TearDownTest() | ||
} | ||
|
||
func (s *NetworkPolicyReconcilerTestSuite) TestAddingWarningForInternetIntentWhenEgressPolicyIsDisabled() { | ||
networkPolicyReconciler := NewReconciler( | ||
s.Client, | ||
scheme.Scheme, | ||
nil, | ||
nil, | ||
nil, | ||
true, | ||
true, | ||
true, | ||
nil, | ||
nil, // This means that the egress policy is disabled | ||
) | ||
|
||
calls := []effectivepolicy.Call{{ | ||
Target: otterizev2alpha1.Target{ | ||
Internet: &otterizev2alpha1.Internet{ | ||
Domains: []string{"example.com"}, | ||
}, | ||
}, | ||
ReferencingKubernetesServices: nil, | ||
EventRecorder: nil, | ||
}} | ||
|
||
effectivePolicies := []effectivepolicy.ServiceEffectivePolicy{{ | ||
Service: serviceidentity.ServiceIdentity{ | ||
Name: "test-service", | ||
Namespace: "test-namespace", | ||
Kind: "Service", | ||
OwnerObject: nil, | ||
}, | ||
CalledBy: nil, | ||
Calls: calls, | ||
ClientIntentsStatus: v2alpha1.IntentsStatus{}, | ||
ClientIntentsEventRecorder: injectablerecorder.NewObjectEventRecorder( | ||
&injectablerecorder.InjectableRecorder{Recorder: s.Recorder}, | ||
nil), | ||
}} | ||
|
||
s.Client.EXPECT().List(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( | ||
func(ctx context.Context, list *v1.NetworkPolicyList, opts ...client.ListOption) error { | ||
return nil | ||
}).AnyTimes() | ||
|
||
_, errs := networkPolicyReconciler.ReconcileEffectivePolicies(context.Background(), effectivePolicies) | ||
for _, err := range errs { | ||
s.NoError(err) | ||
} | ||
s.ExpectEventsForRecorder(s.Recorder, consts.ReasonInternetEgressNetworkPolicyWithEgressPolicyDisabled) | ||
|
||
} | ||
|
||
func TestNetworkPolicyReconcilerTestSuite(t *testing.T) { | ||
suite.Run(t, new(NetworkPolicyReconcilerTestSuite)) | ||
} |