-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor code structure for improved readability and maintainability #477
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
Open
hamzabouissi
wants to merge
6
commits into
main
Choose a base branch
from
kubeapi-access-doc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
85c910b
Refactor code structure for improved readability and maintainability
hamzabouissi 397eb29
fix: Potential fix for pull request finding
hamzabouissi 0f9f9db
fix: Potential fix for pull request finding
hamzabouissi d1edf25
fix: Potential fix for pull request finding
hamzabouissi bca98c2
fix: Potential fix for pull request finding
hamzabouissi 34430c2
fix: Potential fix for pull request finding
hamzabouissi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
219 changes: 219 additions & 0 deletions
219
docs/deploy-applications/traefik/expose-kubeapi-server.md
This file contains hidden or 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,219 @@ | ||
| --- | ||
| id: expose-kubeapi-server | ||
| title: "Expose Kubernetes API access publicly with OIDC and an IP allowlist" | ||
| --- | ||
|
|
||
| # Expose Kubernetes API access publicly with OIDC and an IP allowlist | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| The client must have the following tools installed: | ||
|
|
||
| ```bash | ||
| ( | ||
| set -x; cd "$(mktemp -d)" && | ||
| OS="$(uname | tr '[:upper:]' '[:lower:]')" && | ||
| ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" && | ||
| KREW="krew-${OS}_${ARCH}" && | ||
| curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" && | ||
| tar zxvf "${KREW}.tar.gz" && | ||
| ./${KREW} install krew | ||
| ) | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| # install kube-oidc-login plugin | ||
| export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" | ||
| kubectl krew install oidc-login | ||
|
Copilot marked this conversation as resolved.
|
||
|
|
||
| ``` | ||
|
|
||
| ## Setup RBAC | ||
|
|
||
| for each customer we're going to have custom rbac permissions applied, here is the default manifest. | ||
|
|
||
| the following one allows: readonly, port-forward and exec permissions to pods under default namespace. | ||
|
|
||
|
|
||
| ```yaml | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: ClusterRole | ||
| metadata: | ||
| name: port-forwarder | ||
| rules: | ||
| - apiGroups: [""] | ||
| resources: ["pods", "pods/portforward"] | ||
| verbs: ["get", "list", "create"] | ||
|
hamzabouissi marked this conversation as resolved.
Comment on lines
+39
to
+41
|
||
|
|
||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: RoleBinding | ||
| metadata: | ||
| name: oidc-kubectl-port-forward | ||
| namespace: default | ||
| roleRef: | ||
| apiGroup: rbac.authorization.k8s.io | ||
| kind: ClusterRole | ||
| name: port-forwarder | ||
| subjects: | ||
| - kind: Group | ||
| name: oidc:glueops-rocks:captain_domain_glueops-kubectl-pfwd | ||
| apiGroup: rbac.authorization.k8s.io | ||
|
Comment on lines
+54
to
+56
|
||
|
|
||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: ClusterRole | ||
| metadata: | ||
| name: pod-exec | ||
| rules: | ||
| - apiGroups: [""] | ||
| resources: ["pods"] | ||
| verbs: ["get", "list"] | ||
| - apiGroups: [""] | ||
| resources: ["pods/exec"] | ||
| verbs: ["create", "get"] | ||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: RoleBinding | ||
| metadata: | ||
| name: oidc-kubectl-exec | ||
| namespace: default | ||
| roleRef: | ||
| apiGroup: rbac.authorization.k8s.io | ||
| kind: ClusterRole | ||
| name: pod-exec | ||
| subjects: | ||
| - kind: Group | ||
| name: oidc:glueops-rocks:captain_domain_glueops-kubectl-exec | ||
| apiGroup: rbac.authorization.k8s.io | ||
|
Comment on lines
+81
to
+83
|
||
|
|
||
|
|
||
| --- | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: RoleBinding | ||
| metadata: | ||
| name: oidc-kubectl-readonly | ||
| namespace: default | ||
| roleRef: | ||
| apiGroup: rbac.authorization.k8s.io | ||
| kind: ClusterRole | ||
| name: view | ||
| subjects: | ||
| - kind: Group | ||
| name: oidc:glueops-rocks:captain_domain_glueops-kubectl-reader | ||
| apiGroup: rbac.authorization.k8s.io | ||
|
Comment on lines
+97
to
+99
|
||
| ``` | ||
|
|
||
| ## Set IngressRouteTCP with middleware | ||
|
|
||
| Now to expose kube-api to the public world, we need to use `IngressRouteTCP`, but first we must create a `MiddlewareTCP` to restrict access to certain IP addresses. | ||
|
|
||
|
Comment on lines
+102
to
+105
|
||
| ```yaml | ||
| apiVersion: traefik.io/v1alpha1 | ||
| kind: MiddlewareTCP | ||
| metadata: | ||
| name: kube-apiserver-ip-allowlist | ||
| namespace: glueops-core-platform-traefik | ||
| spec: | ||
| ipAllowList: | ||
| sourceRange: | ||
| - "x.x.x.x/32" | ||
| ``` | ||
|
|
||
| replace the `x.x.x.x` with IP address you want, note you can create as much IP address as you can | ||
|
|
||
| then let's have the `IngressRouteTCP` created | ||
|
|
||
| ```yaml | ||
| apiVersion: traefik.io/v1alpha1 | ||
| kind: IngressRouteTCP | ||
| metadata: | ||
| name: kube-apiserver-passthrough | ||
| namespace: glueops-core-platform-traefik | ||
| annotations: | ||
| kubernetes.io/ingress.class: "platform-traefik" | ||
| # ExternalDNS will create/update this DNS name to point at the Traefik LB target. | ||
| external-dns.alpha.kubernetes.io/target: "platform-v2.CAPTAIN_DOMAIN" | ||
| spec: | ||
| entryPoints: | ||
| - websecure | ||
| routes: | ||
| - match: HostSNI(`kube-api.CAPTAIN_DOMAIN`) | ||
| middlewares: | ||
| - name: kube-apiserver-ip-allowlist | ||
| namespace: glueops-core-platform-traefik | ||
| services: | ||
| - name: kube-apiserver-proxy | ||
| port: 443 | ||
| tls: | ||
| passthrough: true | ||
|
|
||
| --- | ||
| apiVersion: v1 | ||
| kind: Service | ||
| metadata: | ||
| name: kube-apiserver-proxy | ||
| namespace: glueops-core-platform-traefik | ||
| spec: | ||
| ports: | ||
| - name: https | ||
| port: 443 | ||
| protocol: TCP | ||
| targetPort: 443 | ||
|
|
||
| --- | ||
| apiVersion: v1 | ||
| kind: Endpoints | ||
| metadata: | ||
| name: kube-apiserver-proxy | ||
| namespace: glueops-core-platform-traefik | ||
| subsets: | ||
| - addresses: | ||
| - ip: 192.168.0.1 | ||
| ports: | ||
|
Comment on lines
+166
to
+168
|
||
| - name: https | ||
| port: 443 | ||
| protocol: TCP | ||
|
|
||
| ``` | ||
|
|
||
| ## Setup config for kubectl access | ||
|
|
||
| In the current cluster, run the following. It will create a file `kubeconfig.yaml`, which you can then hand to the customer. | ||
|
|
||
| ```bash | ||
|
|
||
| # 1. Grab the certificate data | ||
| CERT_DATA=$(kubectl config view --raw --minify -o jsonpath='{.clusters[0].cluster.certificate-authority-data}') | ||
|
|
||
| # 2. Write the config to a file | ||
| cat <<EOF > kubeconfig.yaml | ||
| apiVersion: v1 | ||
| kind: Config | ||
| clusters: | ||
| - name: kubernetes | ||
| cluster: | ||
| server: https://kube-api.CAPTAIN_DOMAIN | ||
| certificate-authority-data: "$CERT_DATA" | ||
| contexts: | ||
| - name: kubectl-oidc@kubernetes | ||
| context: | ||
| cluster: kubernetes | ||
| user: kubectl-oidc | ||
| current-context: kubectl-oidc@kubernetes | ||
| users: | ||
| - name: kubectl-oidc | ||
| user: | ||
| exec: | ||
| apiVersion: client.authentication.k8s.io/v1 | ||
| command: kubectl | ||
| args: | ||
| - oidc-login | ||
| - get-token | ||
| - --grant-type=device-code | ||
| - --oidc-issuer-url=https://dex.CAPTAIN_DOMAIN | ||
| - --oidc-client-id=kubectl | ||
| - --oidc-extra-scope=profile | ||
| - --oidc-extra-scope=email | ||
| - --oidc-extra-scope=groups | ||
| EOF | ||
|
|
||
| ``` | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.