-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add networkpolices to protect metrics endpoint and allow communicatio…
…n with webhooks
- Loading branch information
1 parent
4c4103e
commit 14274c5
Showing
38 changed files
with
747 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ok/src/cronjob-tutorial/testdata/project/config/network-policy/allow-metrics-traffic.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This NetworkPolicy allows ingress and egress traffic | ||
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those | ||
# namespaces are able to gathering data from the metrics endpoint. | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: allow-metrics-traffic | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Ingress | ||
ingress: | ||
# This allows ingress traffic from any namespace with the label metrics: enabled | ||
- from: | ||
- namespaceSelector: | ||
matchLabels: | ||
metrics: enabled # Only from namespaces with this label | ||
ports: | ||
- port: 8443 | ||
protocol: TCP |
26 changes: 26 additions & 0 deletions
26
...ok/src/cronjob-tutorial/testdata/project/config/network-policy/allow-webhook-traffic.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This NetworkPolicy allows ingress traffic to your webhook server running | ||
# as part of the controller-manager from specific namespaces and pods. CR(s) which uses webhooks | ||
# will only work when applied in namespaces labeled with 'webhook: enabled' | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: allow-webhook-traffic | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Ingress | ||
ingress: | ||
# This allows ingress traffic from any namespace with the label webhook: enabled | ||
- from: | ||
- namespaceSelector: | ||
matchLabels: | ||
webhook: enabled # Only from namespaces with this label | ||
ports: | ||
- port: 443 | ||
protocol: TCP |
3 changes: 3 additions & 0 deletions
3
docs/book/src/cronjob-tutorial/testdata/project/config/network-policy/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
resources: | ||
- allow-webhook-traffic.yaml | ||
- allow-metrics-traffic.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ook/src/getting-started/testdata/project/config/network-policy/allow-metrics-traffic.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This NetworkPolicy allows ingress and egress traffic | ||
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those | ||
# namespaces are able to gathering data from the metrics endpoint. | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: allow-metrics-traffic | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Ingress | ||
ingress: | ||
# This allows ingress traffic from any namespace with the label metrics: enabled | ||
- from: | ||
- namespaceSelector: | ||
matchLabels: | ||
metrics: enabled # Only from namespaces with this label | ||
ports: | ||
- port: 8443 | ||
protocol: TCP |
2 changes: 2 additions & 0 deletions
2
docs/book/src/getting-started/testdata/project/config/network-policy/kustomization.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
resources: | ||
- allow-metrics-traffic.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../kustomize/v2/scaffolds/internal/templates/config/network-policy/allow-metrics-traffic.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
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 network_policy | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v4/pkg/machinery" | ||
) | ||
|
||
var _ machinery.Template = &NetworkPolicyAllowMetrics{} | ||
|
||
// NetworkPolicyAllowMetrics scaffolds a file that defines the NetworkPolicy | ||
// to allow access to the metrics endpoint | ||
type NetworkPolicyAllowMetrics struct { | ||
machinery.TemplateMixin | ||
machinery.ProjectNameMixin | ||
} | ||
|
||
// SetTemplateDefaults implements file.Template | ||
func (f *NetworkPolicyAllowMetrics) SetTemplateDefaults() error { | ||
if f.Path == "" { | ||
f.Path = filepath.Join("config", "network-policy", "allow-metrics-traffic.yaml") | ||
} | ||
|
||
f.TemplateBody = metricsNetworkPolicyTemplate | ||
|
||
return nil | ||
} | ||
|
||
const metricsNetworkPolicyTemplate = `# This NetworkPolicy allows ingress and egress traffic | ||
# with Pods running on namespaces labeled with 'metrics: enabled'. Only Pods on those | ||
# namespaces are able to gathering data from the metrics endpoint. | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: {{ .ProjectName }} | ||
app.kubernetes.io/managed-by: kustomize | ||
name: allow-metrics-traffic | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Ingress | ||
ingress: | ||
# This allows ingress traffic from any namespace with the label metrics: enabled | ||
- from: | ||
- namespaceSelector: | ||
matchLabels: | ||
metrics: enabled # Only from namespaces with this label | ||
ports: | ||
- port: 8443 | ||
protocol: TCP | ||
` |
71 changes: 71 additions & 0 deletions
71
.../kustomize/v2/scaffolds/internal/templates/config/network-policy/allow-webhook-traffic.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
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 network_policy | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v4/pkg/machinery" | ||
) | ||
|
||
var _ machinery.Template = &NetworkPolicyAllowWebhooks{} | ||
|
||
// NetworkPolicyAllowWebhooks in scaffolds a file that defines the NetworkPolicy | ||
// to allow the webhook server can communicate | ||
type NetworkPolicyAllowWebhooks struct { | ||
machinery.TemplateMixin | ||
machinery.ProjectNameMixin | ||
} | ||
|
||
// SetTemplateDefaults implements file.Template | ||
func (f *NetworkPolicyAllowWebhooks) SetTemplateDefaults() error { | ||
if f.Path == "" { | ||
f.Path = filepath.Join("config", "network-policy", "allow-webhook-traffic.yaml") | ||
} | ||
|
||
f.TemplateBody = webhooksNetworkPolicyTemplate | ||
|
||
return nil | ||
} | ||
|
||
const webhooksNetworkPolicyTemplate = `# This NetworkPolicy allows ingress traffic to your webhook server running | ||
# as part of the controller-manager from specific namespaces and pods. CR(s) which uses webhooks | ||
# will only work when applied in namespaces labeled with 'webhook: enabled' | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: {{ .ProjectName }} | ||
app.kubernetes.io/managed-by: kustomize | ||
name: allow-webhook-traffic | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Ingress | ||
ingress: | ||
# This allows ingress traffic from any namespace with the label webhook: enabled | ||
- from: | ||
- namespaceSelector: | ||
matchLabels: | ||
webhook: enabled # Only from namespaces with this label | ||
ports: | ||
- port: 443 | ||
protocol: TCP | ||
` |
45 changes: 45 additions & 0 deletions
45
...s/common/kustomize/v2/scaffolds/internal/templates/config/network-policy/kustomization.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
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 network_policy | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v4/pkg/machinery" | ||
) | ||
|
||
var _ machinery.Template = &Kustomization{} | ||
|
||
// Kustomization scaffolds a file that defines the kustomization scheme for the prometheus folder | ||
type Kustomization struct { | ||
machinery.TemplateMixin | ||
} | ||
|
||
// SetTemplateDefaults implements file.Template | ||
func (f *Kustomization) SetTemplateDefaults() error { | ||
if f.Path == "" { | ||
f.Path = filepath.Join("config", "network-policy", "kustomization.yaml") | ||
} | ||
|
||
f.TemplateBody = kustomizationTemplate | ||
|
||
return nil | ||
} | ||
|
||
const kustomizationTemplate = `resources: | ||
- allow-metrics-traffic.yaml | ||
` |
Oops, something went wrong.