Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for NetworkPolicies #422

Merged
merged 1 commit into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/kubernetes-deploy/deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
persistent_volume_claim
pod
redis
network_policy
memcached
service
pod_template
Expand Down Expand Up @@ -63,6 +64,7 @@ class DeployTask
def predeploy_sequence
before_crs = %w(
ResourceQuota
NetworkPolicy
)
after_crs = %w(
ConfigMap
Expand All @@ -86,6 +88,7 @@ def prune_whitelist
extensions/v1beta1/DaemonSet
extensions/v1beta1/Deployment
extensions/v1beta1/Ingress
networking.k8s.io/v1/NetworkPolicy
apps/v1beta1/StatefulSet
autoscaling/v1/HorizontalPodAutoscaler
policy/v1beta1/PodDisruptionBudget
Expand Down
8 changes: 8 additions & 0 deletions lib/kubernetes-deploy/kubeclient_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def build_rbac_v1_kubeclient(context)
)
end

def build_networking_v1_kubeclient(context)
_build_kubeclient(
api_version: "v1",
context: context,
endpoint_path: "/apis/networking.k8s.io"
)
end

def _build_kubeclient(api_version:, context:, endpoint_path: nil)
# Find a context defined in kube conf files that matches the input context by name
configs = config_files.map { |f| KubeConfig.read(f) }
Expand Down
22 changes: 22 additions & 0 deletions lib/kubernetes-deploy/kubernetes_resource/network_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
module KubernetesDeploy
class NetworkPolicy < KubernetesResource
TIMEOUT = 30.seconds

def status
exists? ? "Created" : "Not Found"
end

def deploy_succeeded?
exists?
end

def deploy_failed?
false
end

def timeout_message
UNUSUAL_FAILURE_MESSAGE
end
end
end
10 changes: 10 additions & 0 deletions test/fixtures/hello-cloud/network_policy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-all-network-policy
spec:
podSelector: {}
ingress:
- {}
policyTypes:
- Ingress
6 changes: 6 additions & 0 deletions test/helpers/fixture_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ def assert_stateful_set_present(name)
desired = stateful_sets.find { |ss| ss.metadata.name == name }
assert(desired.present?, "Stateful set #{name} does not exist")
end

def assert_network_policy_present(name)
network_policies = networking_v1_kubeclient.get_network_policies(namespace: namespace)
desired = network_policies.find { |np| np.metadata.name == name }
assert(desired.present?, "Network #{name} does not exist")
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions test/helpers/fixture_sets/hello_cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def assert_all_up
assert_daemon_set_up
assert_stateful_set_up
assert_job_up
assert_network_policy_up
end

def assert_unmanaged_pod_statuses(status, count = 1)
Expand Down Expand Up @@ -109,5 +110,9 @@ def assert_stateful_set_up
def assert_job_up
assert_job_exists("hello-job")
end

def assert_network_policy_up
assert_network_policy_present("allow-all-network-policy")
end
end
end
4 changes: 4 additions & 0 deletions test/helpers/kubeclient_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ def autoscaling_v1_kubeclient
def rbac_v1_kubeclient
@rbac_v1_kubeclient ||= build_rbac_v1_kubeclient(TEST_CONTEXT)
end

def networking_v1_kubeclient
@networking_v1_kubeclient ||= build_networking_v1_kubeclient(TEST_CONTEXT)
end
end
16 changes: 14 additions & 2 deletions test/integration/kubernetes_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_full_hello_cloud_set_deploy_succeeds
%r{Deploying Pod/unmanaged-pod-[-\w]+ \(timeout: 60s\)}, # annotation timeout override
"Hello from the command runner!", # unmanaged pod logs
"Result: SUCCESS",
"Successfully deployed 21 resources",
"Successfully deployed 22 resources",
], in_order: true)

num_ds = expected_daemonset_pod_count
Expand Down Expand Up @@ -101,8 +101,9 @@ def test_pruning_works
prune_matcher("statefulset", "apps", "stateful-busybox"),
prune_matcher("job", "batch", "hello-job"),
prune_matcher("poddisruptionbudget", "policy", "test"),
prune_matcher("networkpolicy", "networking.k8s.io", "allow-all-network-policy"),
] # not necessarily listed in this order
expected_msgs = [/Pruned 10 resources and successfully deployed 6 resources/]
expected_msgs = [/Pruned 11 resources and successfully deployed 6 resources/]
expected_pruned.map do |resource|
expected_msgs << /The following resources were pruned:.*#{resource}/
end
Expand Down Expand Up @@ -1080,6 +1081,17 @@ def test_not_apply_resource_can_be_pruned
])
end

def test_network_policies_are_deployed_first
deploy_fixtures('hello-cloud', subset: ['network_policy.yml'])
assert_logs_match_all([
"Predeploying priority resources",
"Deploying NetworkPolicy/allow-all-network-policy (timeout: 30s)",
"Successfully deployed 1 resource",
"Successful resources",
"NetworkPolicy/allow-all-network-policy",
], in_order: true)
end

private

def expected_daemonset_pod_count
Expand Down
1 change: 1 addition & 0 deletions test/integration/render_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def test_render_task_rendering_all_files
assert_match(/name: redis/, output)
assert_match(/name: role-binding/, output)
assert_match(/name: resource-quotas/, output)
assert_match(/name: allow-all-network-policy/, output)
assert_match(/name: build-robot/, output)
assert_match(/name: stateful-busybox/, output)
assert_match(/name: hello-cloud-template-runner/, output)
Expand Down