Skip to content

Commit

Permalink
Merge pull request #71 from WyriHaximus/3.x-make-existing-kubeconfig-…
Browse files Browse the repository at this point in the history
…overruleable

Make existing kubeconfig overruleable
  • Loading branch information
WyriHaximus authored Jul 19, 2023
2 parents 8c8247c + ed97a6d commit fc4ba26
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 2 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,114 @@ jobs:
php -r "exit(trim('${COUNT}') === '0' ? 0 : 255);"
env:
COUNT: ${{ steps.helmlistcountthird.outputs.count }}
tests-helm-install-input-overruled:
name: "Testing Helm Install (input overruled) ${{ matrix.runner }}"
runs-on: ${{ matrix.runner }}
needs:
- runner-matrix
strategy:
matrix:
runner: ${{ fromJson(needs.runner-matrix.outputs.runner) }}
steps:
- uses: actions/checkout@v1
- name: Create k8s Kind Cluster
uses: helm/[email protected]
- name: Get running image ID
id: ps
run: printf "::set-output name=id::%s" $(docker ps --format "{{.ID}}")
env:
IMAGE: ${{ steps.build.outputs.tag }}
- name: Get running image IP
id: inspect
run: printf "::set-output name=ip::%s" $(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${IMAGE_ID})
env:
IMAGE_ID: ${{ steps.ps.outputs.id }}
- run: sed -i "s/127.0.0.1:32768/${KIND_IP}:6443/g" ~/.kube/config
env:
KIND_IP: ${{ steps.inspect.outputs.ip }}
- name: Make kubeconfig available to Helm3 action
run: |
kubeconfig=$(cat ~/.kube/config)
kubeconfig="${kubeconfig//'%'/'%25'}"
kubeconfig="${kubeconfig//$'\n'/'%0A'}"
kubeconfig="${kubeconfig//$'\r'/'%0D'}"
echo "::set-output name=kubeconfig::$kubeconfig"
id: kubeconfig
- name: List installed packages
uses: ./
id: helmlistcountfirst
with:
exec: |
printf "count=%s\n" $(helm list | grep ducker-hub-exporter | wc -l) >> $GITHUB_OUTPUT
kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }}
- run: |
php -r "exit(trim('${COUNT}') === '0' ? 0 : 255);"
env:
COUNT: ${{ steps.helmlistcountfirst.outputs.count }}
- name: Install ducker-hub-exporter
uses: ./
id: helm3
with:
exec: |
helm repo add WyriHaximusNet https://helm.wyrihaximus.net/
helm install ducker-hub-exporter WyriHaximusNet/docker-hub-exporter --atomic
kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }}
- name: List installed packages
uses: ./
with:
exec: |
helm list
kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }}
- run: |
rm ~/.kube -Rf
mkdir ~/.kube
echo "junk" > ~/.kube/config
- name: Get kubeconfig contents
uses: ./
id: getkubeconfigcontents
with:
exec: |
helm list
printf "::set-output name=cnt::%s" $(cat ~/.kube/config)
kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }}
- name: "Assert kubeconfig really contains: junk"
uses: nick-fields/assert-action@v1
with:
expected: junk
actual: ${{ steps.getkubeconfigcontents.outputs.cnt }}
- name: List installed packages through a overruled kubeconfig
uses: ./
id: helmlistcountoverruledkubeconfig
with:
exec: |
helm list
printf "::set-output name=count::%s" $(helm list | grep ducker-hub-exporter | wc -l)
kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }}
overrule_existing_kubeconfig: "true"
- run: |
php -r "exit(trim('${COUNT}') === '1' ? 0 : 255);"
env:
COUNT: ${{ steps.helmlistcountoverruledkubeconfig.outputs.count }}
- run: |
rm ~/.kube -Rf
- name: UnInstall ducker-hub-exporter
uses: ./
with:
exec: |
helm uninstall ducker-hub-exporter
kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }}
- name: List installed packages
uses: ./
id: helmlistcountthird
with:
exec: |
helm list
printf "::set-output name=count::%s" $(helm list | grep ducker-hub-exporter | wc -l)
kubeconfig: ${{ steps.kubeconfig.outputs.kubeconfig }}
- run: |
php -r "exit(trim('${COUNT}') === '0' ? 0 : 255);"
env:
COUNT: ${{ steps.helmlistcountthird.outputs.count }}
tests-helm-install-file:
name: "Testing Helm Install (file) ${{ matrix.runner }}"
runs-on: ${{ matrix.runner }}
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ be appended to `~/.kube/config`, and will always be removed afterwards.*
* *Required*: `no`
* *Type*: `string`

## overrule_existing_kubeconfig

When this input is set to `"true"` it will swap out the `.kube/config` with the one provided.

* *Required*: `no`
* *Type*: `string`

## Output

This action has only one output and that's the `number` output. This is the number you see in the HTML URL of the
Expand All @@ -50,6 +57,7 @@ jobs:
with:
exec: helm upgrade APP_NAME ./.helm/app/ --install --wait --atomic --namespace=APP_NAMESPACE --values=./.helm/app/values.yaml
kubeconfig: '${{ secrets.KUBECONFIG }}'
overrule_existing_kubeconfig: "true"
```
The following example shows how you can use this action to lint your helm files in (for example) `./.helm/app/`.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ inputs:
kubeconfig:
description: 'The full helm command to run (including helm)'
required: false
overrule_existing_kubeconfig:
description: 'The full helm command to run (including helm)'
required: false
runs:
using: 'node16'
main: 'main.js'
25 changes: 23 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,20 @@ async function main() {
});

const kubeConfigLocation = homedir + '/.kube/config';
const kubeConfigLocationTempOld = kubeConfigLocation + '_tmp_f' + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 13) + 'f';
const kubeConfigExists = fs.existsSync(kubeConfigLocation);
if (kubeConfigExists) {
if (kubeConfigExists && process.env.INPUT_OVERRULE_EXISTING_KUBECONFIG === "true") {
console.log("\033[36mExisting kubeconfig found, but provided kubeconfig is overruling it\033[0m");
console.log("\033[36mWill be swapping out existing kubeconfig for the duration of the execution of this action\033[0m");
fs.renameSync(kubeConfigLocation, kubeConfigLocationTempOld);
fs.appendFileSync(
kubeConfigLocation,
"\r\n\r\n" + process.env.INPUT_KUBECONFIG + "\r\n\r\n",
{
mode: 0o644,
}
);
} else if (kubeConfigExists) {
console.log("\033[36mExisting kubeconfig found, using that and ignoring input\033[0m");
} else {
console.log("\033[36mUsing kubeconfig from input\033[0m");
Expand Down Expand Up @@ -104,10 +116,19 @@ async function main() {
fs.unlinkSync(execShFile.name);
fs.unlinkSync(dockerKubeConfig);
console.log("\033[36m - exec ✅ \033[0m");
if (!kubeConfigExists) {
if (
!kubeConfigExists ||
(
kubeConfigExists && process.env.INPUT_OVERRULE_EXISTING_KUBECONFIG === "true"
)
) {
fs.unlinkSync(kubeConfigLocation);
console.log("\033[36m - kubeconfig ✅ \033[0m");
}
if (kubeConfigExists && process.env.INPUT_OVERRULE_EXISTING_KUBECONFIG === "true") {
fs.renameSync(kubeConfigLocationTempOld, kubeConfigLocation);
console.log("\033[36m - kubeconfig restored ✅ \033[0m");
}
}
}

Expand Down

0 comments on commit fc4ba26

Please sign in to comment.