apiserver cilium rules.

This commit is contained in:
Erik Brakkee 2025-01-02 19:01:05 +01:00
parent 933b46c68c
commit 5659d7c18c
8 changed files with 64 additions and 27 deletions

View File

@ -102,8 +102,9 @@ func (c Config) Validate() error {
} }
// application names must be unique and may not conflict with predefined applications // application names must be unique and may not conflict with predefined applications
apps := map[string]bool{ apps := make(map[string]bool)
"apiserver": true, for _, predefined := range PREDEFINED_APPS {
apps[predefined] = true
} }
// application names may also not conflict with network names. // application names may also not conflict with network names.
for _, network := range c.Networks { for _, network := range c.Networks {

View File

@ -107,7 +107,10 @@ func Generate(writer io.Writer, generator Generator, config *Config) error {
fmt.Fprintf(os.Stderr, "RULE %s\n", app) fmt.Fprintf(os.Stderr, "RULE %s\n", app)
fmt.Fprintf(os.Stderr, " IN %s\n", ingress) fmt.Fprintf(os.Stderr, " IN %s\n", ingress)
fmt.Fprintf(os.Stderr, " OUT %s\n", egress) fmt.Fprintf(os.Stderr, " OUT %s\n", egress)
generator.GenerateCommunicationRule(writer, applications[app], ingress, egress) err := generator.GenerateCommunicationRule(writer, applications[app], ingress, egress)
if err != nil {
return err
}
} }
} }

View File

@ -32,7 +32,10 @@ func execute(files []string, options *Options) error {
config: config, config: config,
policyTemplates: policyTemplates, policyTemplates: policyTemplates,
} }
Generate(os.Stdout, generator, config) err = Generate(os.Stdout, generator, config)
if err != nil {
return err
}
} }
return nil return nil

View File

@ -5,6 +5,7 @@ import (
"io" "io"
"log" "log"
"os" "os"
"slices"
) )
type NetworkPolicyGenerrator struct { type NetworkPolicyGenerrator struct {
@ -40,21 +41,49 @@ func (g NetworkPolicyGenerrator) GenerateCommunicationRule(
// non-trivial regular network policy // non-trivial regular network policy
tmpl := g.policyTemplates.ApplicationTemplate("netpol") tmpl := g.policyTemplates.ApplicationTemplate("netpol")
log.Printf("Found template %v for pod %s", tmpl, app.Name) if tmpl == nil {
if tmpl != nil { return fmt.Errorf("Could not find policy template for 'netpol'")
}
err := tmpl.Execute(writer, map[string]any{ err := tmpl.Execute(writer, map[string]any{
"app": app, "app": app,
"ingress": ingress, "ingress": ingress,
"egress": egress, "egress": egress,
"labels": map[string]string{ "labels": map[string]string{
"policy-generator": "1", "policy-generator": "1",
}, },
}) })
if err != nil { if err != nil {
return err return err
}
} }
} }
allPredefined := make(map[string]bool)
for _, pre := range ingress.Predefined {
allPredefined[pre] = true
}
for _, pre := range egress.Predefined {
allPredefined[pre] = true
}
log.Printf("ALl PREDEFINED %v", allPredefined)
for predefined, _ := range allPredefined {
tmpl := g.policyTemplates.PredefineApplicationPolicyTemplate("netpol", predefined)
if tmpl == nil {
return fmt.Errorf("Could not find predefined template for netpol/%s", predefined)
}
log.Printf("PREDEFINED FOR %s", app.Name)
err := tmpl.Execute(writer, map[string]any{
"app": app,
"ingress": slices.Contains(ingress.Predefined, predefined),
"egress": slices.Contains(egress.Predefined, predefined),
"labels": map[string]string{
"policy-generator": "1",
},
})
if err != nil {
return err
}
}
return nil return nil
} }

View File

@ -121,6 +121,6 @@ func (t *PolicyTemplates) ApplicationTemplate(policyType string) *template.Templ
} }
func (t *PolicyTemplates) PredefineApplicationPolicyTemplate(policyType string, predefined string) *template.Template { func (t *PolicyTemplates) PredefineApplicationPolicyTemplate(policyType string, predefined string) *template.Template {
tmpl := t.templates.Lookup(fmt.Sprintf("templates/pod/%s/%s.yaml", policyType, predefined)) tmpl := t.templates.Lookup(fmt.Sprintf("templates/%s/pod/%s.yaml", policyType, predefined))
return tmpl return tmpl
} }

View File

@ -1,20 +1,20 @@
---
kind: CiliumNetworkPolicy kind: CiliumNetworkPolicy
apiVersion: cilium.io/v2 apiVersion: cilium.io/v2
metadata: metadata:
name: {{.name}} name: {{.app.Name}}
namespace: {{.namespace}} namespace: {{.app.Namespace.Name}}
labels: "{{ .labels | toYaml | nindent 4 }}" labels: {{ .labels | toYaml | nindent 4 }}
spec: spec:
endpointSelector: endpointSelector: {{ .app.MatchLabels | toYaml | nindent 4 }}
{{ .selector }} {{- if .ingress }}
{{- if .from }}
ingress: ingress:
- fromEntities: - fromEntities:
- kube-apiserver - kube-apiserver
# See https://github.com/cilium/cilium/issues/35401 # See https://github.com/cilium/cilium/issues/35401
- remote-node - remote-node
{{- end }} {{- end }}
{{- if .to }} {{- if .egress }}
egress: egress:
- toEntities: - toEntities:
- kube-apiserver - kube-apiserver

View File

@ -13,7 +13,7 @@
matchLabels: {{ .MatchLabels | toYaml | nindent 12 }} matchLabels: {{ .MatchLabels | toYaml | nindent 12 }}
namespaceSelector: namespaceSelector:
matchLabels: matchLabels:
kubernetes.io/metadata.name: {{ .Namespace }} kubernetes.io/metadata.name: {{ .Namespace.Name }}
{{- if .Ports }} {{- if .Ports }}
ports: ports:
{{- template "ports" .Ports }} {{- template "ports" .Ports }}

View File

@ -42,6 +42,7 @@ communications:
- from: # can we support both string and list of strings? - from: # can we support both string and list of strings?
- httpd-wamblee-org - httpd-wamblee-org
- internet - internet
- apiserver
to: to:
- nexus-server - nexus-server