-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[RFC] Network policies #3611
[RFC] Network policies #3611
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package nodeimage | ||
|
||
/* | ||
The default network policy manifest and images are https://github.com/kubernetes-sigs/kube-network-policies | ||
*/ | ||
|
||
const networkPolicyImage = "registry.k8s.io/networking/kube-network-policies:v0.2.0" | ||
|
||
var defaultNetworkPolicyImage = []string{networkPolicyImage} | ||
|
||
const defaultNetworkPolicyManifest = ` | ||
--- | ||
kind: ClusterRole | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: kube-network-policies | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- pods | ||
- namespaces | ||
verbs: | ||
- list | ||
- watch | ||
- apiGroups: | ||
- "networking.k8s.io" | ||
resources: | ||
- networkpolicies | ||
verbs: | ||
- list | ||
- watch | ||
--- | ||
kind: ClusterRoleBinding | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
metadata: | ||
name: kube-network-policies | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: kube-network-policies | ||
subjects: | ||
- kind: ServiceAccount | ||
name: kube-network-policies | ||
namespace: kube-system | ||
--- | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: kube-network-policies | ||
namespace: kube-system | ||
--- | ||
apiVersion: apps/v1 | ||
kind: DaemonSet | ||
metadata: | ||
name: kube-network-policies | ||
namespace: kube-system | ||
labels: | ||
tier: node | ||
app: kube-network-policies | ||
k8s-app: kube-network-policies | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: kube-network-policies | ||
template: | ||
metadata: | ||
labels: | ||
tier: node | ||
app: kube-network-policies | ||
k8s-app: kube-network-policies | ||
spec: | ||
hostNetwork: true | ||
dnsPolicy: ClusterFirst | ||
nodeSelector: | ||
kubernetes.io/os: linux | ||
tolerations: | ||
- operator: Exists | ||
effect: NoSchedule | ||
serviceAccountName: kube-network-policies | ||
containers: | ||
- name: kube-network-policies | ||
image: ` + networkPolicyImage + ` | ||
args: | ||
- /bin/netpol | ||
- -v | ||
- "2" | ||
volumeMounts: | ||
- name: lib-modules | ||
mountPath: /lib/modules | ||
readOnly: true | ||
resources: | ||
requests: | ||
cpu: "100m" | ||
memory: "50Mi" | ||
securityContext: | ||
privileged: true | ||
capabilities: | ||
add: ["NET_ADMIN"] | ||
Comment on lines
+113
to
+115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't privileged=true make the NET_ADMIN cap redundant, i think it should be privileged=false and only the required caps should be added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i guess this is from the original repo manifest; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I think I started to get granularity and gave up :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think, it's fine to leave it privileged and remove the NET_ADMIN cap. maybe leave a TODO comment if someone wishes to experiment with caps and limit the access scope. |
||
volumes: | ||
- name: lib-modules | ||
hostPath: | ||
path: /lib/modules | ||
--- | ||
` |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,75 @@ | ||||||||||||||||||||||||
/* | ||||||||||||||||||||||||
Copyright 2019 The Kubernetes Authors. | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||
you may not use this file except in compliance with the License. | ||||||||||||||||||||||||
You may obtain a copy of the License at | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||
distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||
See the License for the specific language governing permissions and | ||||||||||||||||||||||||
limitations under the License. | ||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// Package installnetworkpolicies implements the install Network Policy action | ||||||||||||||||||||||||
package installnetworkpolicies | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||
"bytes" | ||||||||||||||||||||||||
"strings" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
"sigs.k8s.io/kind/pkg/errors" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
"sigs.k8s.io/kind/pkg/cluster/internal/create/actions" | ||||||||||||||||||||||||
"sigs.k8s.io/kind/pkg/cluster/nodeutils" | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
type action struct{} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// NewAction returns a new action for installing storage | ||||||||||||||||||||||||
func NewAction() actions.Action { | ||||||||||||||||||||||||
return &action{} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// Execute runs the action | ||||||||||||||||||||||||
func (a *action) Execute(ctx *actions.ActionContext) error { | ||||||||||||||||||||||||
ctx.Status.Start("Installing Network Policies π") | ||||||||||||||||||||||||
defer ctx.Status.End(false) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
allNodes, err := ctx.Nodes() | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// get the target node for this task | ||||||||||||||||||||||||
controlPlanes, err := nodeutils.ControlPlaneNodes(allNodes) | ||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
node := controlPlanes[0] // kind expects at least one always | ||||||||||||||||||||||||
Comment on lines
+47
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
// read the manifest from the node | ||||||||||||||||||||||||
var raw bytes.Buffer | ||||||||||||||||||||||||
if err := node.Command("cat", "/kind/manifests/default-network-policy.yaml").SetStdout(&raw).Run(); err != nil { | ||||||||||||||||||||||||
return errors.Wrap(err, "failed to read Network Policies manifest") | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
manifest := raw.String() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// apply the manifest | ||||||||||||||||||||||||
in := strings.NewReader(manifest) | ||||||||||||||||||||||||
cmd := node.Command( | ||||||||||||||||||||||||
"kubectl", | ||||||||||||||||||||||||
"--kubeconfig=/etc/kubernetes/admin.conf", "apply", "-f", "-", | ||||||||||||||||||||||||
) | ||||||||||||||||||||||||
cmd.SetStdin(in) | ||||||||||||||||||||||||
if err := cmd.Run(); err != nil { | ||||||||||||||||||||||||
return err | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// mark success | ||||||||||||||||||||||||
ctx.Status.End(true) | ||||||||||||||||||||||||
return nil | ||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given this is a bool i would put a verb in front of it, same as DisasbleDefaultCNI above.
e.g. InstallNetworkPolicies