-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
- Loading branch information
1 parent
a6600a1
commit ea8629e
Showing
50 changed files
with
906 additions
and
3 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
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
18 changes: 18 additions & 0 deletions
18
docs/book/src/cronjob-tutorial/testdata/project/config/policy/allow-cert-manager.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,18 @@ | ||
# Allow CertManager have communication across the cluster | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: allow-cert-manager | ||
namespace: cert-manager | ||
spec: | ||
podSelector: {} # This applies to all pods within the cert-manager namespace | ||
policyTypes: | ||
- Ingress | ||
- Egress | ||
ingress: | ||
# Allow ingress from all pods in the same namespace | ||
- from: | ||
- podSelector: {} # This effectively allows all pods within the namespace | ||
egress: | ||
# Allow egress to all destinations | ||
- {} |
20 changes: 20 additions & 0 deletions
20
docs/book/src/cronjob-tutorial/testdata/project/config/policy/allow-webhook.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,20 @@ | ||
# Allow webhooks usage in the manager namespace | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: allow-webhooks | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Ingress | ||
ingress: | ||
- from: | ||
ports: | ||
- protocol: TCP | ||
port: 443 |
5 changes: 5 additions & 0 deletions
5
docs/book/src/cronjob-tutorial/testdata/project/config/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,5 @@ | ||
resources: | ||
- protect-metrics.yaml | ||
- allow-webhook.yaml | ||
- allow-cert-manager.yaml | ||
|
26 changes: 26 additions & 0 deletions
26
docs/book/src/cronjob-tutorial/testdata/project/config/policy/protect-metrics.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 @@ | ||
# NetworkPolicy to protect metrics endpoint | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: protect-metrics | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Egress | ||
- Ingress | ||
egress: | ||
- {} | ||
ingress: | ||
- from: | ||
- namespaceSelector: | ||
matchLabels: | ||
monitoring: enabled # Only from namespaces with this label | ||
ports: | ||
- protocol: TCP | ||
port: 8080 # TCP port for metrics |
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
18 changes: 18 additions & 0 deletions
18
docs/book/src/getting-started/testdata/project/config/policy/allow-cert-manager.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,18 @@ | ||
# Allow CertManager have communication across the cluster | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: allow-cert-manager | ||
namespace: cert-manager | ||
spec: | ||
podSelector: {} # This applies to all pods within the cert-manager namespace | ||
policyTypes: | ||
- Ingress | ||
- Egress | ||
ingress: | ||
# Allow ingress from all pods in the same namespace | ||
- from: | ||
- podSelector: {} # This effectively allows all pods within the namespace | ||
egress: | ||
# Allow egress to all destinations | ||
- {} |
2 changes: 2 additions & 0 deletions
2
docs/book/src/getting-started/testdata/project/config/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: | ||
- protect-metrics.yaml |
26 changes: 26 additions & 0 deletions
26
docs/book/src/getting-started/testdata/project/config/policy/protect-metrics.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 @@ | ||
# NetworkPolicy to protect metrics endpoint | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: project | ||
app.kubernetes.io/managed-by: kustomize | ||
name: protect-metrics | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Egress | ||
- Ingress | ||
egress: | ||
- {} | ||
ingress: | ||
- from: | ||
- namespaceSelector: | ||
matchLabels: | ||
monitoring: enabled # Only from namespaces with this label | ||
ports: | ||
- protocol: TCP | ||
port: 8080 # TCP port for metrics |
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
Binary file added
BIN
+6 KB
pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/.DS_Store
Binary file not shown.
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
62 changes: 62 additions & 0 deletions
62
...gins/common/kustomize/v2/scaffolds/internal/templates/config/policy/allow-cert-manager.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,62 @@ | ||
/* | ||
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 policy | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/pkg/machinery" | ||
) | ||
|
||
var _ machinery.Template = &AllowCertManagerNetworkPolicy{} | ||
|
||
// AllowWebhookNetworkPolicy scaffolds a file that defines the NetworkPolicy to allow access to webhook service | ||
type AllowCertManagerNetworkPolicy struct { | ||
machinery.TemplateMixin | ||
machinery.ProjectNameMixin | ||
} | ||
|
||
// SetTemplateDefaults implements file.Template | ||
func (f *AllowCertManagerNetworkPolicy) SetTemplateDefaults() error { | ||
if f.Path == "" { | ||
f.Path = filepath.Join("config", "policy", "allow-cert-manager.yaml") | ||
} | ||
|
||
f.TemplateBody = certmanagerNetworkPolicyTemplate | ||
|
||
return nil | ||
} | ||
|
||
const certmanagerNetworkPolicyTemplate = `# Allow CertManager have communication across the cluster | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
name: allow-cert-manager | ||
namespace: cert-manager | ||
spec: | ||
podSelector: {} # This applies to all pods within the cert-manager namespace | ||
policyTypes: | ||
- Ingress | ||
- Egress | ||
ingress: | ||
# Allow ingress from all pods in the same namespace | ||
- from: | ||
- podSelector: {} # This effectively allows all pods within the namespace | ||
egress: | ||
# Allow egress to all destinations | ||
- {} | ||
` |
64 changes: 64 additions & 0 deletions
64
pkg/plugins/common/kustomize/v2/scaffolds/internal/templates/config/policy/allow-webhooks.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,64 @@ | ||
/* | ||
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 policy | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"sigs.k8s.io/kubebuilder/v3/pkg/machinery" | ||
) | ||
|
||
var _ machinery.Template = &AllowWebhookNetworkPolicy{} | ||
|
||
// AllowWebhookNetworkPolicy scaffolds a file that defines the NetworkPolicy to allow access to webhook service | ||
type AllowWebhookNetworkPolicy struct { | ||
machinery.TemplateMixin | ||
machinery.ProjectNameMixin | ||
} | ||
|
||
// SetTemplateDefaults implements file.Template | ||
func (f *AllowWebhookNetworkPolicy) SetTemplateDefaults() error { | ||
if f.Path == "" { | ||
f.Path = filepath.Join("config", "policy", "allow-webhook.yaml") | ||
} | ||
|
||
f.TemplateBody = webhookNetworkPolicyTemplate | ||
|
||
return nil | ||
} | ||
|
||
const webhookNetworkPolicyTemplate = `# Allow webhooks usage in the manager namespace | ||
apiVersion: networking.k8s.io/v1 | ||
kind: NetworkPolicy | ||
metadata: | ||
labels: | ||
app.kubernetes.io/name: {{ .ProjectName }} | ||
app.kubernetes.io/managed-by: kustomize | ||
name: allow-webhooks | ||
namespace: system | ||
spec: | ||
podSelector: | ||
matchLabels: | ||
control-plane: controller-manager | ||
policyTypes: | ||
- Ingress | ||
ingress: | ||
- from: | ||
ports: | ||
- protocol: TCP | ||
port: 443 | ||
` |
Oops, something went wrong.