Skip to content
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

Add controller tests and CI #97

Merged
merged 12 commits into from
Jul 9, 2024
91 changes: 91 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
name: tests

on:
push:
branches:
- main
- release-v*
pull_request:
branches:
- main

jobs:
generate-checks:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: '1.22'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
go-version: '1.22'
go-version-file: go.mod

One less thing to maintain.

- uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: generate-check-go-${{ runner.os }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
generate-check-go-${{ runner.os }}-
- name: Verify generated source is up-to-date
run: |
make generate
if [ ! -z "$(git status --porcelain)" ]; then
echo "make generate must be invoked and the result committed"
git status
git diff
exit 1
fi
- name: Verify generated manifests are up-to-date
run: |
make manifests
if [ ! -z "$(git status --porcelain)" ]; then
echo "make manifests must be invoked and the result committed"
git status
git diff
exit 1
fi
- name: Verify generated api-docs are up-to-date
run: |
make api-docs
if [ ! -z "$(git status --porcelain)" ]; then
echo "make api-docs must be invoked and the result committed"
git status
git diff
exit 1
fi
- name: Verify go.mod and go.sum are clean
run: |
go mod tidy
if [ ! -z "$(git status --porcelain)" ]; then
echo "go mod tidy must be invoked and the result committed"
git status
git diff
exit 1
fi

tests:
runs-on: ubuntu-22.04
name: tests
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
go-version: '1.22'
go-version-file: go.mod

- uses: actions/setup-python@v5
with:
python-version: '3.11'
- uses: actions/cache@v4
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: tests-go-${{ runner.os }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
tests-go-${{ runner.os }}-
- name: Run tests
shell: bash
run: |
make test
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ vet: ## Run go vet against code.

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out --ginkgo.v

##@ Build

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/objecttemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type ObjectTemplateSpec struct {

// Matrix specifies the input matrix
// +required
Matrix []*MatrixEntry `json:"matrix"`
Matrix []MatrixEntry `json:"matrix"`

// Templates specifies a list of templates to render and deploy
// +required
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/texttemplate_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type TextTemplateSpec struct {
ServiceAccountName string `json:"serviceAccountName,omitempty"`

// +optional
Inputs []*TextTemplateInput `json:"inputs,omitempty"`
Inputs []TextTemplateInput `json:"inputs,omitempty"`

// +optional
Template *string `json:"template,omitempty"`
Expand Down
16 changes: 4 additions & 12 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/crd/bases/templates.kluctl.io_githubcomments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.15.0
name: githubcomments.templates.kluctl.io
spec:
group: templates.kluctl.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/templates.kluctl.io_gitlabcomments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.15.0
name: gitlabcomments.templates.kluctl.io
spec:
group: templates.kluctl.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/templates.kluctl.io_gitprojectors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.15.0
name: gitprojectors.templates.kluctl.io
spec:
group: templates.kluctl.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.15.0
name: listgithubpullrequests.templates.kluctl.io
spec:
group: templates.kluctl.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.15.0
name: listgitlabmergerequests.templates.kluctl.io
spec:
group: templates.kluctl.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/templates.kluctl.io_objecttemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.15.0
name: objecttemplates.templates.kluctl.io
spec:
group: templates.kluctl.io
Expand Down
2 changes: 1 addition & 1 deletion config/crd/bases/templates.kluctl.io_texttemplates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.14.0
controller-gen.kubebuilder.io/version: v0.15.0
name: texttemplates.templates.kluctl.io
spec:
group: templates.kluctl.io
Expand Down
6 changes: 1 addition & 5 deletions controllers/base_template_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -33,10 +32,7 @@ type BaseTemplateReconciler struct {
}

func (r *BaseTemplateReconciler) getClientForObjects(serviceAccountName string, objNamespace string) (client.Client, error) {
restConfig, err := config.GetConfig()
if err != nil {
return nil, err
}
restConfig := rest.CopyConfig(r.Manager.GetConfig())

name := "default"
if serviceAccountName != "" {
Expand Down
Loading
Loading