├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── build.sh ├── env.sh ├── install.sh ├── manifest-nsxt.yml ├── manifest-vds.yml └── proxy.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.tar.gz 2 | *.tar -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM photon:3.0 2 | 3 | COPY proxy.sh /proxy.sh 4 | 5 | RUN tdnf update -y && tdnf install -y jq openssh-clients shadow && \ 6 | curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl" && \ 7 | chmod +x ./kubectl && \ 8 | mv ./kubectl /usr/local/bin/kubectl && \ 9 | chmod +x /proxy.sh && \ 10 | groupadd inject && useradd -G inject inject 11 | 12 | ENTRYPOINT [ "/proxy.sh" ] 13 | 14 | 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TKGs Proxy Injector 2 | 3 | This can be used to add a proxy and/or a CA cert to guest clusters automatically. This will run as a native pod in the supervsior cluster and continously ssh out to the guest cluster nodes and make sure they have the proxy and cert configured.When using this with VDS networking the pod will run on the control plane since natiev pods are unavailable. This will run on a per namespace basis due to some limitiations with the default firewall rules applied between namespaces with NSX-T. This also leverages the `docker-registry` running in the supervisor cluster to store the `proxy-inject` docker image to reduce external dependencies on internal regsitries existing. 4 | 5 | **NOTE: when upgrading the Supervisor Cluster it will role the nodes so the image for proxy-inject that is stored on the local registry will be removed. you will need to re-run the install script to re-upload the image otherwise you will get image pull errors** 6 | 7 | 8 | ## Compatibility 9 | 10 | if you are running a supervisor cluster that is `v1.18.2-vsc0.0.6-17224208` or higher please use `1.3.0` and up. if you are only an older version use `1.2.0` 11 | 12 | 13 | ## Usage 14 | 15 | 1. ssh to vcenter and hop into shell 16 | 2. **be sure to do a DCLI login otherwise the script will hang waiting for a password** 17 | 3. copy this repo over to your vcenter 18 | 4. grab the `proxy-inject.tar.gz` from the releases and upload it to your vcenter VM. you can do this scp or if you have internet connection out from vcenter just pull it down to the vm. copy it into the newly created repo directory 19 | 5. open `env.sh` and fill in the variables 20 | 1. if you do not want to have a proxy installed and just want to add a cert you can remove the proxy specific vars and it will skip the proxy. 21 | 2. if you do not want a cert to be added you can leave out the `REG_CERT` variable and it will be skipped. 22 | 6. execute `install.sh` 23 | 24 | ## Upgrading 25 | 26 | 1. ssh to vcenter and hop into shell 27 | 2. copy your `env.sh` out of the root repo folder 28 | 3. pull down the latest release of the code base to replace the existing one 29 | 4. pull down the latest release of `proxy-inject.tar.gz` to replace the existing one 30 | 5. copy your `env.sh` back into the root of the repo replacing the default one 31 | 6. update any new env vars 32 | 7. execute `install.sh` 33 | 34 | ## Vars 35 | 36 | all vars are set in `env.sh` 37 | 38 | * `VSPHERE_CLUSTER` - the vsphere cluster name that wcp is enabled on 39 | * `DEPLOY_NS` - namespace that the proxy pod will be deployed into 40 | * `TKC_HTTPS_PROXY` - valid http proxy that you want to use 41 | * `TKC_HTTP_PROXY` - valid https proxy that you want to use 42 | * `TKC_NO_PROXY` - no proxy list 43 | * `REG_CERT` - the registry ca cert to trust an untrusted registry 44 | * `INTERVAL` - interval to run the script 45 | 46 | 47 | ## Authenticated proxies 48 | 49 | if your proxy uses auth you can add the username and pass inline in the env var. 50 | ex. 51 | 52 | `TKC_HTTPS_PROXY='http://someuser:somepassword@proxy.com'` 53 | 54 | if your proxy password has a `$` be sure to escape it. you will need to use `\\` since it needs to be escaped for the k8s manifest as well as for the environment. 55 | 56 | ex. 57 | 58 | `pa\\$sword` 59 | 60 | **NOTE: NOT TESTED FOR PRODUCTION USE** -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | docker build -t proxy-inject:1.3.0 . 4 | # docker save proxy-inject:1.3.0 | gzip > proxy-inject.tar.gz -------------------------------------------------------------------------------- /env.sh: -------------------------------------------------------------------------------- 1 | export VSPHERE_CLUSTER="Cluster" #this is not the id this is the name you see in vcenter 2 | export DEPLOY_NS="test-ns2" 3 | export TKC_HTTPS_PROXY="http://someproxy:8080" 4 | export TKC_HTTP_PROXY="http://someproxy:8080" 5 | export TKC_NO_PROXY="localhost" 6 | export REG_CERT="-----BEGIN CERTIFICATE----- 7 | MIIEFzCCAv+gAwIBAgIJAPGReZlu7FEvMA0GCSqGSIb3DQEBCwUAMIGWMQswCQYD 8 | VQQDDAJDQTEXMBUGCgmSJomT8ixkARkWB3ZzcGhlcmUxFTATBgoJkiaJk/IsZAEZ 9 | FgVsb2NhbDELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExGDAWBgNV 10 | BAoMD3Zjc2E3LmxhYi5sb2NhbDEbMBkGA1UECwwSVk13YXJlIEVuZ2luZWVyaW5n 11 | MB4XDTIwMDcyNDIzMDc1N1oXDTMwMDcyMjIzMDc1N1owgZYxCzAJBgNVBAMMAkNB 12 | MRcwFQYKCZImiZPyLGQBGRYHdnNwaGVyZTEVMBMGCgmSJomT8ixkARkWBWxvY2Fs 13 | MQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEYMBYGA1UECgwPdmNz 14 | YTcubGFiLmxvY2FsMRswGQYDVQQLDBJWTXdhcmUgRW5naW5lZXJpbmcwggEiMA0G 15 | CSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCyIB1EjMlAVIBN9/ebMr13f7yPvfQs 16 | u3ZwEE+aq90t+ovIoTEgIFNUvsBkxlXF0xPD5IYmyIeF6adRznkaVOuWeJ9TwGzf 17 | 6DqJZJu1RG59ODCIMKFCsvak5SwmGM6VHarnmyc72e45YoAvAgUQkTlrdV8a1bGH 18 | F0UiL34ITprks4GNtcHd+e3VAZMAm37/Jgyzp9ICNKTkOA71pp0t7PZ/XRwQCxBB 19 | 0WsE9uNE5+MunXrz02AVZrk3+BtiqrPcnbXoQgx08ikhQIQ6Az2O6OlUd2+YmST4 20 | ST8GsHsKSjZEX//tNAZTHPC5gB0z8BTXQ72JmgPQAVnaHxXqI7MRD+KPAgMBAAGj 21 | ZjBkMB0GA1UdDgQWBBQiASNxq4LA0DC0KdGu0Xo+7Kw/tDAfBgNVHREEGDAWgQ5l 22 | bWFpbEBhY21lLmNvbYcEfwAAATAOBgNVHQ8BAf8EBAMCAQYwEgYDVR0TAQH/BAgw 23 | BgEB/wIBADANBgkqhkiG9w0BAQsFAAOCAQEAUtAMjGVLpJHggEIy3nR836bxkjN8 24 | Sn6JUD4KLBV8EsT9KIg9eQ+jBFmdPcJJrPNoz/ZeVfPGeM7cKPCH+ASV+DTE9q4A 25 | nhrHZDYG3s8TLE4H39MbcOqWysWQ+ZpB2fZAM7nq27WblroOodKI8y43+gKX7huX 26 | +TZ7azgxLrPVXQkjC6Pr5byxAFlke7FSjINZnq7DXY48UsOwvkCeXFTMVWqLCNEL 27 | KFXW5I1HLVT62aMv72+0m5lfKRL+Y0FZSIs7g9SYhHvBxyUCABJictGDkxMxcAwG 28 | 4shrtA8wP+zx/UvtVYPR+egaZeD/5SYsrgLRvsxYXOvGJV+9+PdLMoSnaA== 29 | -----END CERTIFICATE-----" #the registry ca cert 30 | #Optional 31 | #export INTERVAL="30" -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -e 3 | 4 | source env.sh 5 | 6 | nextip(){ 7 | IP=$1 8 | IP_HEX=$(printf '%.2X%.2X%.2X%.2X\n' `echo $IP | sed -e 's/\./ /g'`) 9 | NEXT_IP_HEX=$(printf %.8X `echo $(( 0x$IP_HEX + 1 ))`) 10 | NEXT_IP=$(printf '%d.%d.%d.%d\n' `echo $NEXT_IP_HEX | sed -r 's/(..)/0x\1 /g'`) 11 | echo "$NEXT_IP" 12 | } 13 | 14 | 15 | export REG_CERT=$(echo "$REG_CERT" | base64 -w 0) 16 | 17 | #get the cluster id 18 | CLUSTER=$(dcli com vmware vcenter cluster list --names ${VSPHERE_CLUSTER} +formatter yaml | sed -n -e 's/^.*cluster: //p') 19 | NETWORK=$(dcli com vmware vcenter namespacemanagement clusters get --cluster ${CLUSTER} | sed -n -e 's/^.*network_provider: //p' | tail -n1) 20 | #get all supervisor ips, there may be a better way? 21 | echo "getting supervisor vm creds" 22 | 23 | /usr/lib/vmware-wcp/decryptK8Pwd.py > ./sv-info 24 | 25 | cat ./sv-info | grep ${CLUSTER} -A2 > ./${CLUSTER}-info 26 | 27 | sv_ip=$(cat ./${CLUSTER}-info | sed -n -e 's/^.*IP: //p') 28 | sv_pass=$(cat ./${CLUSTER}-info| sed -n -e 's/^.*PWD: //p') 29 | 30 | #loop over each sv and upload the image tar 31 | set +e 32 | 33 | NUM=5 34 | ip=${sv_ip} 35 | success=0 36 | for i in $(seq 1 $NUM); 37 | do 38 | echo "checking if ip is in use" 39 | if nc -z $ip 22 2>/dev/null; then 40 | echo "$ip is up" 41 | else 42 | echo "$ip is not in use skipping" 43 | continue 44 | fi 45 | echo "copying image tar to ${ip}" 46 | sshpass -p "${sv_pass}" scp -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ./proxy-inject.tar.gz root@"${ip}":./proxy-inject.tar.gz >> /dev/null 47 | if [ $? -eq 0 ] ; 48 | then 49 | echo "copied image tar successfully" 50 | else 51 | echo "error copying image tar to supervisor node ${ip}" 52 | exit 2 53 | fi 54 | echo "importing image into local registry" 55 | sshpass -p "${sv_pass}" ssh -t -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@"${ip}" << EOF 56 | source /etc/profile 57 | gunzip -f proxy-inject.tar.gz 58 | ctr image import proxy-inject.tar 59 | ctr image tag --force proxy-inject:1.3.0 localhost:5002/vmware/proxy-inject:1.3.0 60 | ctr image push localhost:5002/vmware/proxy-inject:1.3.0 61 | EOF 62 | if [ $? -eq 0 ] ; 63 | then 64 | echo "image loaded successfully" 65 | else 66 | echo "error loading image into to supervisor node ${ip}" 67 | exit 2 68 | fi 69 | echo "cleanup image tar" 70 | sshpass -p "${sv_pass}" ssh -t -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@"${ip}" << EOF 71 | rm ./proxy-inject.tar.gz 72 | rm ./proxy-inject.tar 73 | EOF 74 | ip=$(nextip $ip) 75 | success=$((success+1)) 76 | done 77 | 78 | if [ $success -lt 4 ]; 79 | then 80 | echo "unable to upload image to all SV VMs please check their connectivity" 81 | exit 2 82 | fi 83 | 84 | manifest=./manifest-nsxt.yml 85 | if [ "${NETWORK}" = "VSPHERE_NETWORK" ]; 86 | then 87 | echo "using VDS networking" 88 | manifest=./manifest-vds.yml 89 | fi 90 | 91 | echo "injecting environment vars into manifest file" 92 | envsubst < ${manifest} > newman.yml 93 | 94 | echo "copying manifest file to supervisor node ${sv_ip}" 95 | sshpass -p "${sv_pass}" scp -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ./newman.yml root@"${sv_ip}":./manifest.yml >> /dev/null 96 | if [ $? -eq 0 ] ; 97 | then 98 | echo "manifest copied sucessfully" 99 | else 100 | echo "error copying manifest into to supervisor node ${ip}" 101 | exit 2 102 | fi 103 | 104 | 105 | 106 | echo "creating k8s deployment in namespace ${DEPLOY_NS}" 107 | sshpass -p "${sv_pass}" ssh -t -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@"${sv_ip}" << EOF 108 | kubectl apply -f manifest.yml 109 | EOF 110 | if [ $? -eq 0 ] ; 111 | then 112 | echo "manifest applied successfully" 113 | else 114 | echo "error applying manifest" 115 | exit 2 116 | fi 117 | -------------------------------------------------------------------------------- /manifest-nsxt.yml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ServiceAccount 3 | metadata: 4 | name: internal-kubectl 5 | namespace: ${DEPLOY_NS} 6 | 7 | --- 8 | 9 | apiVersion: rbac.authorization.k8s.io/v1 10 | kind: ClusterRole 11 | metadata: 12 | name: proxy-inject 13 | rules: 14 | - apiGroups: ["","vmoperator.vmware.com"] 15 | resources: 16 | - virtualmachines 17 | - secrets 18 | verbs: 19 | - get 20 | - list 21 | 22 | --- 23 | apiVersion: rbac.authorization.k8s.io/v1 24 | kind: RoleBinding 25 | metadata: 26 | name: proxy-kubectl 27 | namespace: ${DEPLOY_NS} 28 | subjects: 29 | - kind: ServiceAccount 30 | name: internal-kubectl 31 | roleRef: 32 | kind: ClusterRole 33 | name: proxy-inject 34 | apiGroup: rbac.authorization.k8s.io 35 | 36 | --- 37 | apiVersion: apps/v1 38 | kind: Deployment 39 | metadata: 40 | name: proxy-inject 41 | namespace: ${DEPLOY_NS} 42 | labels: 43 | app: proxy-inject 44 | spec: 45 | replicas: 1 46 | selector: 47 | matchLabels: 48 | app: proxy-inject 49 | template: 50 | metadata: 51 | labels: 52 | app: proxy-inject 53 | spec: 54 | serviceAccountName: internal-kubectl 55 | containers: 56 | - image: docker-registry.kube-system.svc:5000/vmware/proxy-inject:1.3.0 57 | imagePullPolicy: Always 58 | name: proxy-inject 59 | env: 60 | - name: TKC_HTTP_PROXY 61 | value: "${TKC_HTTP_PROXY}" 62 | - name: TKC_HTTPS_PROXY 63 | value: "${TKC_HTTPS_PROXY}" 64 | - name: TKC_NO_PROXY 65 | value: "${TKC_NO_PROXY}" 66 | - name: REG_CERT 67 | value: "${REG_CERT}" 68 | resources: 69 | requests: 70 | memory: "64Mi" 71 | cpu: "250m" 72 | limits: 73 | memory: "256Mi" 74 | cpu: "500m" -------------------------------------------------------------------------------- /manifest-vds.yml: -------------------------------------------------------------------------------- 1 | apiVersion: policy/v1beta1 2 | kind: PodSecurityPolicy 3 | metadata: 4 | name: proxy-inject 5 | spec: 6 | fsGroup: 7 | rule: MustRunAs 8 | ranges: 9 | - min: 1 10 | max: 65535 11 | privileged: false 12 | runAsUser: 13 | rule: MustRunAsNonRoot 14 | seLinux: 15 | rule: RunAsAny 16 | supplementalGroups: 17 | rule: MustRunAs 18 | ranges: 19 | - min: 1 20 | max: 65535 21 | hostNetwork: true 22 | volumes: 23 | - '*' 24 | --- 25 | 26 | apiVersion: v1 27 | kind: ServiceAccount 28 | metadata: 29 | name: internal-kubectl 30 | namespace: ${DEPLOY_NS} 31 | 32 | --- 33 | 34 | apiVersion: rbac.authorization.k8s.io/v1 35 | kind: ClusterRole 36 | metadata: 37 | name: proxy-inject 38 | rules: 39 | - apiGroups: ["","vmoperator.vmware.com"] 40 | resources: 41 | - virtualmachines 42 | - secrets 43 | verbs: 44 | - get 45 | - list 46 | - apiGroups: ['policy'] 47 | resources: ['podsecuritypolicies'] 48 | verbs: ['use'] 49 | resourceNames: 50 | - proxy-inject 51 | 52 | --- 53 | apiVersion: rbac.authorization.k8s.io/v1 54 | kind: RoleBinding 55 | metadata: 56 | name: proxy-kubectl 57 | namespace: ${DEPLOY_NS} 58 | subjects: 59 | - kind: ServiceAccount 60 | name: internal-kubectl 61 | roleRef: 62 | kind: ClusterRole 63 | name: proxy-inject 64 | apiGroup: rbac.authorization.k8s.io 65 | 66 | --- 67 | apiVersion: apps/v1 68 | kind: Deployment 69 | metadata: 70 | name: proxy-inject 71 | namespace: ${DEPLOY_NS} 72 | labels: 73 | app: proxy-inject 74 | spec: 75 | replicas: 1 76 | selector: 77 | matchLabels: 78 | app: proxy-inject 79 | template: 80 | metadata: 81 | labels: 82 | app: proxy-inject 83 | spec: 84 | serviceAccountName: internal-kubectl 85 | securityContext: 86 | runAsUser: 1000 87 | runAsGroup: 1000 88 | fsGroup: 1000 89 | containers: 90 | - image: localhost:5000/vmware/proxy-inject:1.3.0 91 | imagePullPolicy: Always 92 | name: proxy-inject 93 | env: 94 | - name: TKC_HTTP_PROXY 95 | value: "${TKC_HTTP_PROXY}" 96 | - name: TKC_HTTPS_PROXY 97 | value: "${TKC_HTTPS_PROXY}" 98 | - name: TKC_NO_PROXY 99 | value: "${TKC_NO_PROXY}" 100 | - name: REG_CERT 101 | value: "${REG_CERT}" 102 | resources: 103 | requests: 104 | memory: "64Mi" 105 | cpu: "250m" 106 | limits: 107 | memory: "256Mi" 108 | cpu: "500m" 109 | volumeMounts: 110 | - mountPath: /tmp 111 | name: tmp 112 | volumes: 113 | - emptyDir: {} 114 | name: tmp 115 | dnsPolicy: ClusterFirstWithHostNet 116 | hostNetwork: true 117 | nodeSelector: 118 | node-role.kubernetes.io/master: "" 119 | tolerations: 120 | - effect: NoSchedule 121 | key: node-role.kubernetes.io/master 122 | operator: Exists 123 | - key: CriticalAddonsOnly 124 | operator: Exists 125 | - effect: NoExecute 126 | key: node.alpha.kubernetes.io/notReady 127 | operator: Exists 128 | - effect: NoExecute 129 | key: node.alpha.kubernetes.io/unreachable 130 | operator: Exists 131 | - effect: NoSchedule 132 | key: kubeadmNode 133 | operator: Equal 134 | value: master -------------------------------------------------------------------------------- /proxy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Logging function that will redirect to stderr with timestamp: 4 | logerr() { echo "$(date) ERROR: $@" 1>&2; } 5 | # Logging function that will redirect to stdout with timestamp 6 | loginfo() { echo "$(date) INFO: $@" ;} 7 | 8 | 9 | 10 | run_interval=${INTERVAL:=30} 11 | 12 | 13 | function inject_ca() 14 | { 15 | touch /etc/ssl/certs/regcert.pem 16 | echo "checking if cert exists" 17 | if cmp -s "/etc/ssl/certs/regcert.pem.new" "/etc/ssl/certs/regcert.pem"; then 18 | echo "the cert already exists and has not changed" 19 | else 20 | echo "updating the certs" 21 | mv /etc/ssl/certs/regcert.pem.new /etc/ssl/certs/regcert.pem 22 | /usr/bin/rehash_ca_certificates.sh 23 | systemctl restart containerd 24 | echo "certs updated!" 25 | fi 26 | 27 | } 28 | 29 | # function insecure_reg() { 30 | # #TODO 31 | # } 32 | 33 | function run() 34 | { 35 | 36 | #get the machines 37 | machines=$(kubectl get virtualmachines -o json) 38 | 39 | for row in $(echo "${machines}" | jq -r '.items[] | @base64'); do 40 | _jq() { 41 | echo ${row} | base64 -d | jq -r ${1} 42 | } 43 | loginfo "-------------------" 44 | #get the namespace 45 | ns=$(_jq '.metadata.namespace') 46 | loginfo "namespace: ${ns}" 47 | 48 | #get the cluster name for the machine 49 | cluster=$(_jq '.metadata.labels."capw.vmware.com/cluster.name"') 50 | loginfo "cluster: ${cluster}" 51 | 52 | #get the ip for the machine 53 | ip=$(_jq '.status.vmIp') 54 | loginfo "ip: ${ip}" 55 | 56 | #get the secret for the machine and create a file 57 | loginfo "getting ssh key for ${cluster}" 58 | kubectl get secret ${cluster}-ssh -n ${ns} -o jsonpath="{.data.ssh-privatekey}" | base64 -d > /tmp/sshkey.pem 59 | chmod 600 /tmp/sshkey.pem 60 | 61 | loginfo "attempting ssh to ${ip}" 62 | ssh -q -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /tmp/sshkey.pem vmware-system-user@${ip} << EOF 63 | sudo -i 64 | if [[ -z "${TKC_HTTP_PROXY}" || -z "${TKC_HTTPS_PROXY}" || -z "${TKC_NO_PROXY}" ]] 65 | then 66 | echo no PROXY vars set skipping proxy... 67 | else 68 | mkdir -p /etc/systemd/system/containerd.service.d 69 | touch /etc/systemd/system/containerd.service.d/http-proxy.conf 70 | touch /etc/sysconfig/proxy 71 | 72 | 73 | echo "creating temp containerd proxy file" 74 | 75 | echo '[Service]' > /etc/systemd/system/containerd.service.d/http-proxy.conf.new 76 | echo 'Environment="HTTP_PROXY='${TKC_HTTP_PROXY}'"' >> /etc/systemd/system/containerd.service.d/http-proxy.conf.new 77 | echo 'Environment="HTTPS_PROXY='${TKC_HTTPS_PROXY}'"' >> /etc/systemd/system/containerd.service.d/http-proxy.conf.new 78 | echo 'Environment="NO_PROXY='${TKC_NO_PROXY}'"' >> /etc/systemd/system/containerd.service.d/http-proxy.conf.new 79 | 80 | echo "comparing containerd proxy settings" 81 | if cmp -s "/etc/systemd/system/containerd.service.d/http-proxy.conf.new" "/etc/systemd/system/containerd.service.d/http-proxy.conf"; then 82 | echo "the containerd proxy is already set and unchanged" 83 | else 84 | echo "containerd proxy changed updating..." 85 | mv /etc/systemd/system/containerd.service.d/http-proxy.conf.new /etc/systemd/system/containerd.service.d/http-proxy.conf 86 | systemctl daemon-reload 87 | systemctl restart containerd 88 | fi 89 | 90 | 91 | echo "creating temp system proxy file" 92 | 93 | echo 'PROXY_ENABLED="yes"' > /etc/sysconfig/proxy.new 94 | echo 'HTTP_PROXY='"'${TKC_HTTP_PROXY}'" >> /etc/sysconfig/proxy.new 95 | echo 'HTTPS_PROXY='"'${TKC_HTTPS_PROXY}'" >> /etc/sysconfig/proxy.new 96 | echo 'NO_PROXY='"'${TKC_NO_PROXY}'" >> /etc/sysconfig/proxy.new 97 | 98 | echo "comparing system proxy settings" 99 | if cmp -s "/etc/sysconfig/proxy" "/etc/sysconfig/proxy.new"; then 100 | echo "the system proxy is already set and unchanged" 101 | else 102 | echo "system proxy changed updating..." 103 | mv /etc/sysconfig/proxy.new /etc/sysconfig/proxy 104 | fi 105 | fi 106 | 107 | if [[ -z "${REG_CERT}" ]] 108 | then 109 | echo no CA cert providied skipping cert injection... 110 | else 111 | $(typeset -f inject_ca) 112 | echo "${REG_CERT}" | base64 -d > /etc/ssl/certs/regcert.pem.new 113 | inject_ca 114 | fi 115 | 116 | 117 | EOF 118 | 119 | if [ $? -eq 0 ] ; 120 | then 121 | loginfo "script ran successfully!" 122 | else 123 | logerr "There was an error running the script Exiting..." 124 | fi 125 | 126 | 127 | loginfo "-------------------" 128 | done 129 | } 130 | 131 | while true 132 | do 133 | set +e 134 | echo "running script in a loop" 135 | run 136 | sleep $run_interval 137 | done 138 | --------------------------------------------------------------------------------