Skip to content

Commit 4301422

Browse files
committed
ensure network policies for eventing mock
1 parent 24c8c1d commit 4301422

File tree

4 files changed

+189
-34
lines changed

4 files changed

+189
-34
lines changed

config/serverless/templates/ingress-network-policy.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apiVersion: networking.k8s.io/v1
22
kind: NetworkPolicy
33
metadata:
44
namespace: {{ .Release.Namespace }}
5-
name: {{ template "fullname" . }}.kyma-project.io--allow-to-registry
5+
name: {{ template "fullname" . }}.kyma-project.io--allow-to-registry-port
66
spec:
77
podSelector:
88
matchLabels:
@@ -12,9 +12,6 @@ spec:
1212
- Ingress
1313
ingress:
1414
- from:
15-
# - podSelector:
16-
# matchLabels:
17-
# serverless.kyma-project.io/managed-by: function-controller
1815
ports:
1916
- protocol: TCP
2017
port: {{ .Values.global.registryServicePort }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package networkpolicy
2+
3+
import (
4+
"context"
5+
6+
"github.com/kyma-project/serverless/tests/serverless/internal/utils"
7+
"github.com/pkg/errors"
8+
"github.com/sirupsen/logrus"
9+
networkingv1 "k8s.io/api/networking/v1"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
networkingclient "k8s.io/client-go/kubernetes/typed/networking/v1"
12+
)
13+
14+
const (
15+
componentLabel = "component"
16+
)
17+
18+
type NetworkPolicy struct {
19+
name string
20+
namespace string
21+
networkingCli networkingclient.NetworkPolicyInterface
22+
log *logrus.Entry
23+
spec networkingv1.NetworkPolicySpec
24+
}
25+
26+
func NewNetworkPolicy(name, namespace string, spec networkingv1.NetworkPolicySpec, networkPolicies networkingclient.NetworkPolicyInterface, log *logrus.Entry) NetworkPolicy {
27+
return NetworkPolicy{
28+
name: name,
29+
namespace: namespace,
30+
networkingCli: networkPolicies,
31+
log: log,
32+
spec: spec,
33+
}
34+
}
35+
36+
func (n NetworkPolicy) Create(spec networkingv1.NetworkPolicySpec) error {
37+
38+
networkPolicy := &networkingv1.NetworkPolicy{
39+
ObjectMeta: metav1.ObjectMeta{
40+
Name: n.name,
41+
Namespace: n.namespace,
42+
Labels: map[string]string{
43+
componentLabel: n.name,
44+
},
45+
},
46+
Spec: spec,
47+
}
48+
_, err := n.networkingCli.Create(context.Background(), networkPolicy, metav1.CreateOptions{})
49+
return errors.Wrapf(err, "while creating NetworkPolicy %s in namespace %s", n.name, n.namespace)
50+
}
51+
52+
func (n NetworkPolicy) Delete() error {
53+
return n.networkingCli.Delete(context.Background(), n.name, metav1.DeleteOptions{})
54+
}
55+
56+
func (n NetworkPolicy) LogResource() error {
57+
policy, err := n.Get()
58+
if err != nil {
59+
return err
60+
}
61+
62+
out, err := utils.PrettyMarshall(policy)
63+
if err != nil {
64+
return err
65+
}
66+
67+
n.log.Infof("%s", out)
68+
return nil
69+
}
70+
71+
func (n NetworkPolicy) Get() (*networkingv1.NetworkPolicy, error) {
72+
u, err := n.networkingCli.Get(context.Background(), n.name, metav1.GetOptions{})
73+
if err != nil {
74+
return nil, errors.Wrapf(err, "while getting %s", n.name)
75+
}
76+
77+
return u, nil
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package networkpolicy
2+
3+
import (
4+
"github.com/kyma-project/serverless/tests/serverless/internal/executor"
5+
"github.com/sirupsen/logrus"
6+
networkingv1 "k8s.io/api/networking/v1"
7+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8+
networkingclient "k8s.io/client-go/kubernetes/typed/networking/v1"
9+
)
10+
11+
type newNetworkPolicyStep struct {
12+
name string
13+
namespace string
14+
networkPolicies []*NetworkPolicy
15+
// resCli *resources.Resource
16+
log *logrus.Entry
17+
}
18+
19+
// Cleanup implements executor.Step.
20+
func (n newNetworkPolicyStep) Cleanup() error {
21+
for _, networkPolicy := range n.networkPolicies {
22+
err := networkPolicy.Delete()
23+
if err != nil {
24+
return err
25+
}
26+
}
27+
return nil
28+
}
29+
30+
// Name implements executor.Step.
31+
func (n newNetworkPolicyStep) Name() string {
32+
return n.name
33+
}
34+
35+
// OnError implements executor.Step.
36+
func (n newNetworkPolicyStep) OnError() error {
37+
for _, networkPolicy := range n.networkPolicies {
38+
err := networkPolicy.LogResource()
39+
if err != nil {
40+
return err
41+
}
42+
}
43+
return nil
44+
}
45+
46+
// Run implements executor.Step.
47+
func (n newNetworkPolicyStep) Run() error {
48+
for _, networkPolicy := range n.networkPolicies {
49+
err := networkPolicy.Create(networkPolicy.spec)
50+
if err != nil {
51+
return err
52+
}
53+
}
54+
return nil
55+
}
56+
57+
var _ executor.Step = newNetworkPolicyStep{}
58+
59+
func CreateNetworkPoliciesStep(log *logrus.Entry, name, namespace string, networkCli networkingclient.NetworkPolicyInterface) executor.Step {
60+
61+
allowEgressFromMockSpec := networkingv1.NetworkPolicySpec{
62+
PodSelector: metav1.LabelSelector{
63+
MatchLabels: map[string]string{
64+
"serverless.kyma-project.io/managed-by": "function-controller",
65+
},
66+
},
67+
PolicyTypes: []networkingv1.PolicyType{
68+
networkingv1.PolicyTypeEgress,
69+
},
70+
Egress: []networkingv1.NetworkPolicyEgressRule{
71+
networkingv1.NetworkPolicyEgressRule{},
72+
},
73+
}
74+
75+
allowIngressToMockSpec := networkingv1.NetworkPolicySpec{
76+
PodSelector: metav1.LabelSelector{
77+
MatchLabels: map[string]string{
78+
"app.kubernetes.io/name": "eventing-publisher-proxy",
79+
},
80+
},
81+
PolicyTypes: []networkingv1.PolicyType{
82+
networkingv1.PolicyTypeIngress,
83+
},
84+
Ingress: []networkingv1.NetworkPolicyIngressRule{
85+
networkingv1.NetworkPolicyIngressRule{},
86+
},
87+
}
88+
89+
allowEgressFromMock := NewNetworkPolicy("allow-all-egress-from-eventing-mock", "kyma-system", allowEgressFromMockSpec, networkCli, log)
90+
allowIngressToMock := NewNetworkPolicy("allow-all-ingress-from-eventing-mock", "kyma-system", allowIngressToMockSpec, networkCli, log)
91+
92+
return newNetworkPolicyStep{
93+
name: name,
94+
namespace: namespace,
95+
networkPolicies: []*NetworkPolicy{
96+
&allowEgressFromMock,
97+
&allowIngressToMock,
98+
},
99+
log: log,
100+
}
101+
102+
}

tests/serverless/internal/testsuite/cloud_events.go

+8-30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/kyma-project/serverless/tests/serverless/internal/executor"
1111
"github.com/kyma-project/serverless/tests/serverless/internal/resources/function"
1212
"github.com/kyma-project/serverless/tests/serverless/internal/resources/namespace"
13+
"github.com/kyma-project/serverless/tests/serverless/internal/resources/networkpolicy"
1314
"github.com/kyma-project/serverless/tests/serverless/internal/resources/runtimes"
1415
"github.com/kyma-project/serverless/tests/serverless/internal/utils"
1516

@@ -18,6 +19,7 @@ import (
1819
"github.com/sirupsen/logrus"
1920
"k8s.io/client-go/dynamic"
2021
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
22+
networkingv1 "k8s.io/client-go/kubernetes/typed/networking/v1"
2123
"k8s.io/client-go/rest"
2224
)
2325

@@ -35,6 +37,11 @@ func FunctionCloudEventsTest(restConfig *rest.Config, cfg internal.Config, logf
3537
return nil, errors.Wrap(err, "while creating k8s CoreV1Client")
3638
}
3739

40+
networkingCli, err := networkingv1.NewForConfig(restConfig)
41+
if err != nil {
42+
return nil, errors.Wrap(err, "while creating k8s NetworkingV1Client")
43+
}
44+
3845
python312Logger := logf.WithField(runtimeKey, "python312")
3946
nodejs20Logger := logf.WithField(runtimeKey, "nodejs20")
4047
nodejs22Logger := logf.WithField(runtimeKey, "nodejs22")
@@ -56,6 +63,7 @@ func FunctionCloudEventsTest(restConfig *rest.Config, cfg internal.Config, logf
5663

5764
return executor.NewSerialTestRunner(logf, "Runtime test",
5865
namespace.NewNamespaceStep(logf, fmt.Sprintf("Create %s namespace", genericContainer.Namespace), genericContainer.Namespace, coreCli),
66+
networkpolicy.CreateNetworkPoliciesStep(logf, "Create network policies for publisher proxy mock", "kyma-system", networkingCli.NetworkPolicies("kyma-system")),
5967
function.CreateFunction(logf, publisherProxyMock, "Create publisher proxy mock", runtimes.PythonPublisherProxyMock()),
6068
executor.NewParallelRunner(logf, "Fn tests",
6169
executor.NewSerialTestRunner(python312Logger, "Python312 test",
@@ -79,33 +87,3 @@ func FunctionCloudEventsTest(restConfig *rest.Config, cfg internal.Config, logf
7987
),
8088
), nil
8189
}
82-
83-
// Define those as part of this test:
84-
85-
// kind: NetworkPolicy
86-
// apiVersion: networking.k8s.io/v1
87-
// metadata:
88-
// namespace: kyma-system
89-
// name: temp1
90-
// spec:
91-
// podSelector:
92-
// matchLabels:
93-
// serverless.kyma-project.io/managed-by: function-controller
94-
// policyTypes:
95-
// - Egress
96-
// egress:
97-
// - {}
98-
// ---
99-
// apiVersion: networking.k8s.io/v1
100-
// kind: NetworkPolicy
101-
// metadata:
102-
// namespace: kyma-system
103-
// name: temp2
104-
// spec:
105-
// podSelector:
106-
// matchLabels:
107-
// app.kubernetes.io/name: eventing-publisher-proxy
108-
// policyTypes:
109-
// - Ingress
110-
// ingress:
111-
// - {}

0 commit comments

Comments
 (0)