Skip to content

Commit

Permalink
add rego test
Browse files Browse the repository at this point in the history
add conftest to build

bump client-go v0.23.3

fix release ver in docs

add test, fix workflow

update workflow name, quiet wget

add orderedmap to go.mod

add proper test

fix test again

test fix

proper failing test

fixes #11

run tests on k8s with api changes

+ goreleaser workflow

run tests on k8s with api changes

export gopath for test runs

manually delete kind cluster

manually delete kind cluster

use separate kind cluster names

increase verbosity

use master for kind action

try forking

revert kind changes
  • Loading branch information
Thomas Cooper authored and coopernetes committed Feb 6, 2022
1 parent cee6b34 commit 7b35841
Show file tree
Hide file tree
Showing 8 changed files with 584 additions and 31 deletions.
29 changes: 15 additions & 14 deletions .github/workflows/go.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
name: Go
on: [push]
name: Go & K8s build
on:
push:
branches:
- "*"
jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.13
- name: Set up Go 1.17.6
uses: actions/setup-go@v1
with:
go-version: 1.13
go-version: 1.17.6
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Get dependencies & install
run: |
go get -v -t -d ./...
Expand All @@ -23,13 +22,15 @@ jobs:
dep ensure
fi
go install -v .
- uses: engineerd/[email protected]
curl -fsSLO https://github.com/open-policy-agent/conftest/releases/download/v0.30.0/conftest_0.30.0_Linux_x86_64.tar.gz
tar -C /usr/local/bin -xzvf conftest_0.30.0_Linux_x86_64.tar.gz
wget -q https://github.com/instrumenta/kubeval/releases/latest/download/kubeval-linux-amd64.tar.gz
tar xf kubeval-linux-amd64.tar.gz
sudo cp kubeval /usr/local/bin
- name: Setup kind
uses: engineerd/[email protected]
- name: Run Kubernetes tests
run: |
kubectl cluster-info
wget -q --show-progress https://github.com/instrumenta/kubeval/releases/latest/download/kubeval-linux-amd64.tar.gz
tar xf kubeval-linux-amd64.tar.gz
sudo cp kubeval /usr/local/bin
export PATH="$(go env GOPATH)/bin:$PATH"
export PATH="$(go env GOPATH)/bin:$PATH"
tests/k8s.sh
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: release
on:
push:
tags:
- "v*"
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17.6
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
# either 'goreleaser' (default) or 'goreleaser-pro'
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ This utility was inspired by [this original bash implementation](https://stackov
Download the latest [release](https://github.com/coopernetes/kube-role-gen/releases):

```bash
wget https://github.com/coopernetes/kube-role-gen/releases/download/v0.0.1/kube-role-gen_0.0.1_Linux_x86_64.tar.gz
tar xf kube-role-gen_0.0.1_Linux_x86_64.tar.gz
wget https://github.com/coopernetes/kube-role-gen/releases/download/v0.0.2/kube-role-gen_0.0.2_Linux_x86_64.tar.gz
tar xf kube-role-gen_0.0.2_Linux_x86_64.tar.gz
mv kube-role-gen /usr/local/bin/
```

Expand Down
22 changes: 10 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ module github.com/coopernetes/kube-role-gen
go 1.13

require (
github.com/elliotchance/orderedmap v1.3.0
github.com/googleapis/gnostic v0.3.1 // indirect
golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
k8s.io/api v0.17.2
k8s.io/apimachinery v0.17.2
k8s.io/client-go v0.0.0-20200118233946-a432bd9ba7da
k8s.io/utils v0.0.0-20200117235808-5f6fbceb4c31 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96 // indirect
github.com/elliotchance/orderedmap v1.4.0 // indirect
github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501 // indirect
github.com/gophercloud/gophercloud v0.1.0 // indirect
k8s.io/api v0.23.3
k8s.io/apimachinery v0.23.3
k8s.io/client-go v0.23.3
k8s.io/klog v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e // indirect
)
500 changes: 500 additions & 0 deletions go.sum

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"sort"
"path/filepath"
"strings"
"github.com/elliotchance/orderedmap"
Expand Down Expand Up @@ -63,7 +64,6 @@ func main() {
groupOnly = "core"
}

resourceList := make([]string, 0)
resourcesByVerb := make(map[string][]string)
for _, apiResource := range apiResourceList.APIResources {
if enableVerboseLogging {
Expand All @@ -72,11 +72,11 @@ func main() {
apiResource.Verbs.String())
}

resourceList = append(resourceList, apiResource.Name)
verbList := make([]string, 0)
for _, verb := range apiResource.Verbs {
verbList = append(verbList, verb)
}
sort.Strings(verbList)
verbString := strings.Join(verbList[:], ",")
if value,ok := resourcesByVerb[verbString]; ok {
resourcesByVerb[verbString] = append(value, apiResource.Name)
Expand All @@ -90,7 +90,19 @@ func main() {
sb.WriteString(groupOnly)
sb.WriteString("!")
sb.WriteString(k)
resourcesByGroupAndVerb.Set(sb.String(), resourcesByVerb[k])
if resourceVal,exists := resourcesByGroupAndVerb.Get(sb.String()); exists {
resourceSetMap := make(map[string]bool);
for _,r := range resourceVal.([]string) {
resourceSetMap[r] = true
}
for _,r := range resourcesByVerb[k] {
resourceSetMap[r] = true
}
resourceSet := mapSetToList(resourceSetMap)
resourcesByGroupAndVerb.Set(sb.String(), resourceSet)
} else {
resourcesByGroupAndVerb.Set(sb.String(), resourcesByVerb[k])
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/gh-11.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

deny[msg] {
input.rules[i].apiGroups[_] == "batch"
not valid_batch(input.rules[i].resources)
msg := "Must contain all batch resources"
}

valid_batch(resources) {
startswith(resources[_], "cronjobs")
startswith(resources[_], "jobs")
}
1 change: 1 addition & 0 deletions tests/k8s.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ IFS=$'\n\t'

kube-role-gen | kubeval -
kube-role-gen | kubectl apply --validate -f -
kube-role-gen | conftest test --policy tests/gh-11.rego -

0 comments on commit 7b35841

Please sign in to comment.