ianbelcher / eks-kubectl-action

30 stars 43 forks source link

Add script input param #29

Open DavidMakin opened 1 year ago

DavidMakin commented 1 year ago

wanted to run a little bash script that would run 1 kubectl command, parse the results with jq, loop around those results and run a 2nd based on the results

        uses: ianbelcher/eks-kubectl-action
        with:
          cluster_name: test-cluster
          script: |
            #!/bin/sh
            maxAge="${{ inputs.max_age }}"

            dateNow=$(date +%s --date="now")
            uniqDomainPrefix=$(kubectl get deployments -n ${{ inputs.env }} -o json | jq -r ".items[].metadata.labels.domain_prefix" | sort -u)

            for domainPrefix in $uniqDomainPrefix; do
                latestCreationTimestamp="0"
                # revision 0 gets all. Can not find a way to get only the latest
                for creationTimestamp in $(kubectl rollout history deployment -l domain_prefix="$domainPrefix" -n test --revision 0 -o json | jq -r '.metadata.creationTimestamp'); do
                    tmpLatestCreationTimestamp=$(date --utc --date="${creationTimestamp}" +"%s")
                    if [ "$tmpLatestCreationTimestamp" -gt "$latestCreationTimestamp" ]; then
                        latestCreationTimestamp=$tmpLatestCreationTimestamp
                    fi
                done

                daysDiff=$(((dateNow - latestCreationTimestamp) / 86400))
                if [ "$daysDiff" -gt $maxAge ]; then
                    staleDeployments="${staleDeployments:+${staleDeployments}, }\"${domainPrefix}\""
                else
                    freshDeployments="${freshDeployments:+${freshDeployments}, }\"${domainPrefix}\""
                fi
            done

            echo "[${staleDeployments}]"
markwellis commented 8 months ago

any chance of getting this merged? it would be very helpful for a problem I have currently:

I am trying to delete all resources matching a label, and that needs to first run a kubectl command to get a list of all resources types that can be deleted