Skip to content

Commit 70593e7

Browse files
committed
Logging info
- Updated configuration.md with info about how to set and adjust log levels - added .idea/ to .gitignore - minor typo fixes in controller.py replacing k8s-network-policy tier with default - Replaced NET_POL_TIER_NAME with NET_POL_TIER_DEFAULT - Added code to try and remove "k8s-network-policy" tier if it exists - some minor pep8 and typo fixes Removing constants and some now unnecessary checks - Constants for tier name moved to literals in code - Removed checks for per-namespace policies to remove as it should be unnecessary now Moved removal of "k8s-network-policy" tier above creation of default
1 parent a276be8 commit 70593e7

File tree

6 files changed

+18
-17
lines changed

6 files changed

+18
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ dist/
55
*.tar
66
*.created
77
*.coverage
8+
.idea/

configuration.md

+2
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ the root directory of the container. This can be done by mounting a file from th
5858

5959
### Other configuration
6060

61+
* `LOG_LEVEL`: Supports the standard Python log levels. e.g. `LOG_LEVEL=debug`, defaults to `info`
62+
6163
More information on leader election can be found in the [kubernetes/contrib](https://github.com/kubernetes/contrib/tree/master/election#simple-leader-election-with-kubernetes-and-docker) repository.

constants.py

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
NS_POLICY_ANNOTATION = "net.beta.kubernetes.io/network-policy"
3030

3131
# Tier name /order to use for policies.
32-
NET_POL_TIER_NAME = "k8s-network-policy"
3332
NET_POL_TIER_ORDER = 1000
3433

3534
# The priority assigned to network policies created by the controller.

controller.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
_log = logging.getLogger(__name__)
2222

23+
2324
# Raised upon receiving an error from the Kubernetes API.
2425
class KubernetesApiError(Exception):
2526
pass
@@ -60,7 +61,7 @@ def __init__(self):
6061
"""
6162

6263
elect = os.environ.get("LEADER_ELECTION", "false")
63-
self._leader_elect = elect.lower() == "true"
64+
self._leader_elect = elect.lower() == "true"
6465
"""
6566
Whether or not leader election is enabled. If set to False, this
6667
policy controller will assume it is the only instance.
@@ -133,17 +134,24 @@ def run(self):
133134
self._wait_for_leadership()
134135
self._start_leader_thread()
135136

137+
# Remove old tier if it exists
138+
try:
139+
_log.debug("Attempting to remove old tier k8s-network-policy")
140+
self._client.delete_policy_tier("k8s-network-policy")
141+
except KeyError:
142+
pass
143+
136144
# Ensure the tier exists.
137145
metadata = {"order": NET_POL_TIER_ORDER}
138-
self._client.set_policy_tier_metadata(NET_POL_TIER_NAME, metadata)
146+
self._client.set_policy_tier_metadata("default", metadata)
139147

140-
# Ensure the backstop policy exists. This policy fowards
148+
# Ensure the backstop policy exists. This policy forwards
141149
# any traffic to Kubernetes pods which doesn't match another policy
142150
# to the next-tier (i.e the per-namespace Profiles).
143151
selector = "has(%s)" % K8S_NAMESPACE_LABEL
144152
rules = Rules(inbound_rules=[Rule(action="next-tier")],
145153
outbound_rules=[Rule(action="next-tier")])
146-
self._client.create_policy(NET_POL_TIER_NAME,
154+
self._client.create_policy("default",
147155
"k8s-policy-no-match",
148156
selector,
149157
order=NET_POL_BACKSTOP_ORDER,

handlers/namespace.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ def add_update_namespace(namespace):
6363
# update it if it already exists.
6464
client.create_profile(profile_name, rules, labels)
6565

66-
# Delete any per-namespace policy. Older versions of the policy-controller
67-
# used to install these, but they're not relevant any more.
68-
name = "calico-%s" % profile_name
69-
try:
70-
client.remove_policy(NET_POL_TIER_NAME, name)
71-
except KeyError:
72-
# Policy doesn't exist, we're all good.
73-
pass
74-
7566
_log.debug("Created/updated profile for namespace %s", namespace_name)
7667

7768

@@ -80,7 +71,7 @@ def delete_namespace(namespace):
8071
Takes a deleted namespace and removes the corresponding
8172
configuration from the Calico datastore.
8273
"""
83-
# Delete the Calico policy which represnets this namespace.
74+
# Delete the Calico policy which represents this namespace.
8475
namespace_name = namespace["metadata"]["name"]
8576
profile_name = NS_PROFILE_FMT % namespace_name
8677
_log.debug("Deleting namespace profile: %s", profile_name)

handlers/network_policy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def add_update_network_policy(policy):
3737
outbound_rules=[Rule(action="allow")])
3838

3939
# Create the network policy using the calculated selector and rules.
40-
client.create_policy(NET_POL_TIER_NAME,
40+
client.create_policy("default",
4141
name,
4242
selector,
4343
order=NET_POL_ORDER,
@@ -57,6 +57,6 @@ def delete_network_policy(policy):
5757

5858
# Delete the corresponding Calico policy
5959
try:
60-
client.remove_policy(NET_POL_TIER_NAME, name)
60+
client.remove_policy("default", name)
6161
except KeyError:
6262
_log.info("Unable to find policy '%s' - already deleted", name)

0 commit comments

Comments
 (0)