Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
66e022e1
Commit
66e022e1
authored
Sep 30, 2021
by
Sashi Kumar Kumaresan
Committed by
Etienne Baqué
Sep 30, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix network policies update to support name updation
parent
1f1314a7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
100 additions
and
22 deletions
+100
-22
ee/app/services/network_policies/deploy_resource_service.rb
ee/app/services/network_policies/deploy_resource_service.rb
+14
-7
ee/spec/services/network_policies/deploy_resource_service_spec.rb
...services/network_policies/deploy_resource_service_spec.rb
+86
-15
No files found.
ee/app/services/network_policies/deploy_resource_service.rb
View file @
66e022e1
...
...
@@ -36,7 +36,6 @@ module NetworkPolicies
def
setup_resource
@resource
=
policy
.
generate
resource
[
:metadata
][
:namespace
]
=
kubernetes_namespace
resource
[
:metadata
][
:name
]
=
resource_name
if
resource_name
end
def
load_policy_from_resource
...
...
@@ -56,18 +55,26 @@ module NetworkPolicies
end
def
deploy_cilium_network_policy
if
resource_name
platform
.
kubeclient
.
update_cilium_network_policy
(
resource
)
else
return
platform
.
kubeclient
.
create_cilium_network_policy
(
resource
)
unless
resource_name
if
resource_name
!=
resource
.
dig
(
:metadata
,
:name
)
platform
.
kubeclient
.
delete_cilium_network_policy
(
resource_name
,
kubernetes_namespace
)
resource
[
:metadata
][
:resourceVersion
]
=
nil
platform
.
kubeclient
.
create_cilium_network_policy
(
resource
)
else
platform
.
kubeclient
.
update_cilium_network_policy
(
resource
)
end
end
def
deploy_network_policy
if
resource_name
platform
.
kubeclient
.
update_network_policy
(
resource
)
else
return
platform
.
kubeclient
.
create_network_policy
(
resource
)
unless
resource_name
if
resource_name
!=
resource
.
dig
(
:metadata
,
:name
)
platform
.
kubeclient
.
delete_network_policy
(
resource_name
,
kubernetes_namespace
)
resource
[
:metadata
][
:resourceVersion
]
=
nil
platform
.
kubeclient
.
create_network_policy
(
resource
)
else
platform
.
kubeclient
.
update_network_policy
(
resource
)
end
end
end
...
...
ee/spec/services/network_policies/deploy_resource_service_spec.rb
View file @
66e022e1
...
...
@@ -59,16 +59,39 @@ RSpec.describe NetworkPolicies::DeployResourceService do
end
context
'with resource_name'
do
let
(
:resource_name
)
{
'policy
2
'
}
let
(
:resource_name
)
{
'policy'
}
it
'updates resource in the deployment namespace and returns success response with a policy
'
do
namespaced_policy
=
policy
.
generate
namespaced_policy
[
:metadata
][
:namespace
]
=
environment
.
deployment_namespac
e
namespaced_policy
[
:metadata
][
:name
]
=
'policy2'
context
'when name is not updated
'
do
it
'updates resource in the deployment namespace and returns success response with a policy'
do
namespaced_policy
=
policy
.
generat
e
namespaced_policy
[
:metadata
][
:namespace
]
=
environment
.
deployment_namespace
expect
(
kubeclient
).
to
receive
(
:update_network_policy
).
with
(
namespaced_policy
)
{
policy
.
generate
}
expect
(
subject
).
to
be_success
expect
(
subject
.
payload
.
as_json
).
to
eq
(
policy
.
as_json
)
expect
(
kubeclient
).
to
receive
(
:update_network_policy
).
with
(
namespaced_policy
)
expect
(
subject
).
to
be_success
expect
(
subject
.
payload
.
as_json
).
to
eq
(
policy
.
as_json
)
end
end
context
'when name is updated'
do
let
(
:policy
)
do
Gitlab
::
Kubernetes
::
NetworkPolicy
.
new
(
name:
'policy2'
,
namespace:
'another'
,
selector:
{
matchLabels:
{
role:
'db'
}
},
ingress:
[{
from:
[{
namespaceSelector:
{
matchLabels:
{
project:
'myproject'
}
}
}]
}]
)
end
it
'destroys and recreates resource in the deployment namespace and returns success response with a policy'
do
namespaced_policy
=
policy
.
generate
namespaced_policy
[
:metadata
][
:namespace
]
=
environment
.
deployment_namespace
namespaced_policy
[
:metadata
][
:resourceVersion
]
=
nil
expect
(
kubeclient
).
to
receive
(
:delete_network_policy
).
with
(
resource_name
,
environment
.
deployment_namespace
)
expect
(
kubeclient
).
to
receive
(
:create_network_policy
).
with
(
namespaced_policy
)
expect
(
subject
).
to
be_success
expect
(
subject
.
payload
.
as_json
).
to
eq
(
policy
.
as_json
)
end
end
end
...
...
@@ -137,14 +160,62 @@ RSpec.describe NetworkPolicies::DeployResourceService do
context
'with resource_name'
do
let
(
:resource_name
)
{
'policy'
}
it
'updates resource in the deployment namespace and returns success response with a policy'
do
namespaced_policy
=
policy
.
generate
namespaced_policy
[
:metadata
][
:namespace
]
=
environment
.
deployment_namespace
namespaced_policy
[
:metadata
][
:name
]
=
resource_name
before
do
allow
(
Gitlab
::
Kubernetes
::
CiliumNetworkPolicy
).
to
receive
(
:from_resource
).
and_return
policy
allow
(
Gitlab
::
Kubernetes
::
CiliumNetworkPolicy
).
to
receive
(
:from_yaml
).
and_return
policy
end
expect
(
kubeclient
).
to
receive
(
:update_cilium_network_policy
).
with
(
namespaced_policy
)
{
policy
.
generate
}
expect
(
subject
).
to
be_success
expect
(
subject
.
payload
.
as_json
).
to
eq
(
policy
.
as_json
)
context
'when name is not updated'
do
it
'updates resource in the deployment namespace and returns success response with a policy'
do
namespaced_policy
=
policy
.
generate
namespaced_policy
[
:metadata
][
:namespace
]
=
environment
.
deployment_namespace
expect
(
kubeclient
).
to
receive
(
:update_cilium_network_policy
).
with
(
namespaced_policy
)
expect
(
subject
).
to
be_success
expect
(
subject
.
payload
.
as_json
).
to
eq
(
policy
.
as_json
)
end
end
context
'when name is updated'
do
let
(
:policy
)
do
Gitlab
::
Kubernetes
::
CiliumNetworkPolicy
.
new
(
name:
'policy2'
,
namespace:
'namespace'
,
resource_version:
101
,
selector:
{
matchLabels:
{
role:
'db'
}
},
ingress:
[{
fromEndpoints:
[{
matchLabels:
{
project:
'myproject'
}
}]
}]
)
end
let
(
:manifest
)
do
<<~
POLICY
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: policy2
namespace: another
resourceVersion: 101
spec:
endpointSelector:
matchLabels:
role: db
ingress:
- fromEndpoints:
- matchLabels:
project: myproject
POLICY
end
it
'destroys and recreates resource in the deployment namespace and returns success response with a policy'
do
namespaced_policy
=
policy
.
generate
namespaced_policy
[
:metadata
][
:namespace
]
=
environment
.
deployment_namespace
namespaced_policy
[
:metadata
][
:resourceVersion
]
=
nil
expect
(
kubeclient
).
to
receive
(
:delete_cilium_network_policy
).
with
(
resource_name
,
environment
.
deployment_namespace
)
expect
(
kubeclient
).
to
receive
(
:create_cilium_network_policy
).
with
(
namespaced_policy
)
expect
(
subject
).
to
be_success
expect
(
subject
.
payload
.
as_json
).
to
eq
(
policy
.
as_json
)
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment