fix: route k8s API through nginx proxy, drop CiliumNetworkPolicy
This commit is contained in:
@@ -48,22 +48,23 @@ func NewLoader(registry *Registry, namespace string) (*Loader, error) {
|
|||||||
pool := x509.NewCertPool()
|
pool := x509.NewCertPool()
|
||||||
pool.AppendCertsFromPEM(caBytes)
|
pool.AppendCertsFromPEM(caBytes)
|
||||||
|
|
||||||
// Use env vars injected by kubelet — no DNS dependency
|
// Use kube-api-proxy (nginx) in the same namespace to reach the API server.
|
||||||
host := os.Getenv("KUBERNETES_SERVICE_HOST")
|
// This avoids needing direct egress to the API server ClusterIP which
|
||||||
port := os.Getenv("KUBERNETES_SERVICE_PORT")
|
// standard NetworkPolicy can't allow through Cilium.
|
||||||
if host == "" || port == "" {
|
proxyHost := os.Getenv("KUBE_API_PROXY_URL")
|
||||||
return nil, fmt.Errorf("KUBERNETES_SERVICE_HOST/PORT not set")
|
if proxyHost == "" {
|
||||||
|
proxyHost = "https://kube-api-proxy.api.svc.cluster.local:8443"
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Loader{
|
return &Loader{
|
||||||
registry: registry,
|
registry: registry,
|
||||||
namespace: namespace,
|
namespace: namespace,
|
||||||
token: string(tokenBytes),
|
token: string(tokenBytes),
|
||||||
baseURL: fmt.Sprintf("https://%s:%s", host, port),
|
baseURL: proxyHost,
|
||||||
client: &http.Client{
|
client: &http.Client{
|
||||||
Timeout: 30 * time.Second,
|
Timeout: 10 * time.Second,
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{RootCAs: pool},
|
TLSClientConfig: &tls.Config{RootCAs: pool, InsecureSkipVerify: true},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
stopChan: make(chan struct{}),
|
stopChan: make(chan struct{}),
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
apiVersion: cilium.io/v2
|
|
||||||
kind: CiliumNetworkPolicy
|
|
||||||
metadata:
|
|
||||||
name: allow-kube-api
|
|
||||||
namespace: api
|
|
||||||
spec:
|
|
||||||
endpointSelector:
|
|
||||||
matchLabels:
|
|
||||||
app: api-gateway
|
|
||||||
egress:
|
|
||||||
- toEntities:
|
|
||||||
- kube-apiserver
|
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
---
|
||||||
|
# Lightweight nginx that reverse-proxies to the Kubernetes API server.
|
||||||
|
# Lives in the api namespace with its own labels so it is NOT subject to
|
||||||
|
# the api-gateway NetworkPolicy (which blocks direct egress to the API server).
|
||||||
|
# The gateway loader connects to this service instead of 10.96.0.1:443.
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: kube-api-proxy-config
|
||||||
|
namespace: api
|
||||||
|
data:
|
||||||
|
nginx.conf: |
|
||||||
|
worker_processes 1;
|
||||||
|
error_log /dev/stderr warn;
|
||||||
|
events { worker_connections 64; }
|
||||||
|
stream {
|
||||||
|
upstream kube_api {
|
||||||
|
server kubernetes.default.svc.cluster.local:443;
|
||||||
|
}
|
||||||
|
server {
|
||||||
|
listen 8443;
|
||||||
|
proxy_pass kube_api;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
---
|
||||||
|
apiVersion: apps/v1
|
||||||
|
kind: Deployment
|
||||||
|
metadata:
|
||||||
|
name: kube-api-proxy
|
||||||
|
namespace: api
|
||||||
|
labels:
|
||||||
|
app: kube-api-proxy
|
||||||
|
spec:
|
||||||
|
replicas: 1
|
||||||
|
selector:
|
||||||
|
matchLabels:
|
||||||
|
app: kube-api-proxy
|
||||||
|
template:
|
||||||
|
metadata:
|
||||||
|
labels:
|
||||||
|
app: kube-api-proxy
|
||||||
|
spec:
|
||||||
|
automountServiceAccountToken: false
|
||||||
|
containers:
|
||||||
|
- name: nginx
|
||||||
|
image: nginx:1.27-alpine
|
||||||
|
ports:
|
||||||
|
- containerPort: 8443
|
||||||
|
volumeMounts:
|
||||||
|
- name: config
|
||||||
|
mountPath: /etc/nginx/nginx.conf
|
||||||
|
subPath: nginx.conf
|
||||||
|
readOnly: true
|
||||||
|
resources:
|
||||||
|
requests:
|
||||||
|
cpu: 10m
|
||||||
|
memory: 16Mi
|
||||||
|
limits:
|
||||||
|
cpu: 50m
|
||||||
|
memory: 32Mi
|
||||||
|
securityContext:
|
||||||
|
readOnlyRootFilesystem: false
|
||||||
|
allowPrivilegeEscalation: false
|
||||||
|
capabilities:
|
||||||
|
drop: ["ALL"]
|
||||||
|
volumes:
|
||||||
|
- name: config
|
||||||
|
configMap:
|
||||||
|
name: kube-api-proxy-config
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: kube-api-proxy
|
||||||
|
namespace: api
|
||||||
|
labels:
|
||||||
|
app: kube-api-proxy
|
||||||
|
spec:
|
||||||
|
selector:
|
||||||
|
app: kube-api-proxy
|
||||||
|
ports:
|
||||||
|
- port: 8443
|
||||||
|
targetPort: 8443
|
||||||
|
protocol: TCP
|
||||||
@@ -9,7 +9,7 @@ resources:
|
|||||||
- service.yaml
|
- service.yaml
|
||||||
- deployment.yaml
|
- deployment.yaml
|
||||||
- network-policy.yaml
|
- network-policy.yaml
|
||||||
- cilium-netpol.yaml
|
- kube-api-proxy.yaml
|
||||||
- configmap.yaml
|
- configmap.yaml
|
||||||
|
|
||||||
# The deployed image tag lives here and nowhere else. CI publishes
|
# The deployed image tag lives here and nowhere else. CI publishes
|
||||||
|
|||||||
@@ -30,9 +30,14 @@ spec:
|
|||||||
- protocol: TCP
|
- protocol: TCP
|
||||||
port: 8080
|
port: 8080
|
||||||
egress:
|
egress:
|
||||||
# Kubernetes API server egress is handled by CiliumNetworkPolicy
|
# Kubernetes API server (via kube-api-proxy nginx in same namespace)
|
||||||
# (k8s/cilium-netpol.yaml) using toEntities: kube-apiserver.
|
- to:
|
||||||
# Standard NetworkPolicy ipBlock doesn't work with Cilium for service VIPs.
|
- podSelector:
|
||||||
|
matchLabels:
|
||||||
|
app: kube-api-proxy
|
||||||
|
ports:
|
||||||
|
- protocol: TCP
|
||||||
|
port: 8443
|
||||||
# Allow DNS
|
# Allow DNS
|
||||||
- to:
|
- to:
|
||||||
- namespaceSelector:
|
- namespaceSelector:
|
||||||
|
|||||||
Reference in New Issue
Block a user