Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Linting
on:
pull_request:

jobs:
golangci:
name: Lint golang files
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
repository: ${{github.event.pull_request.head.repo.full_name}}
persist-credentials: false

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
cache: false

- name: golangci-lint
uses: golangci/[email protected]
with:
only-new-issues: true
version: v1.62.2
args: --timeout=900s

gomodtidy:
name: Enforce go.mod tidiness
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: "${{ github.event.pull_request.head.sha }}"
repository: ${{github.event.pull_request.head.repo.full_name}}
persist-credentials: false

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.23'

- name: Execute go mod tidy and check the outcome
working-directory: ./
run: |
go mod tidy
exit_code=$(git diff --exit-code)
exit ${exit_code}

- name: Issue a comment in case the of failure
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
body: |
The `go.mod` and/or `go.sum` files appear not to be correctly tidied.

Please, rerun `go mod tidy` to fix the issues.
reactions: confused
if: |
failure() && github.event.pull_request.head.repo.full_name == github.repository

shelllint:
name: Lint bash files
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: "${{ github.event.pull_request.head.sha }}"
repository: ${{github.event.pull_request.head.repo.full_name}}
persist-credentials: false
- name: Run Shellcheck
uses: ludeeus/[email protected]
env:
# Allow 'source' outside of FILES
SHELLCHECK_OPTS: -x

markdownlint:
name: Lint markdown files
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4
with:
ref: "${{ github.event.pull_request.head.sha }}"
repository: ${{github.event.pull_request.head.repo.full_name}}
persist-credentials: false
- name: Lint markdown files
uses: avto-dev/markdown-lint@v1
with:
config: '.markdownlint.yml'
args: '**/*.md'
ignore: .github/ISSUE_TEMPLATE/*.md
140 changes: 140 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
run:
timeout: 10m

linters-settings:
exhaustive:
check-generated: false
default-signifies-exhaustive: true

lll:
line-length: 150
gomodguard:
blocked:
modules:
- github.com/go-logr/logr:
recommendations:
- k8s.io/klog/v2
gci:
sections:
- standard # Captures all standard packages if they do not match another section.
- default # Contains all imports that could not be matched to another section type.
- prefix(github.com/liqotech/resource-slice-class-controller-template) # Groups all imports with the specified Prefix.
goconst:
min-len: 2
min-occurrences: 2
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
# Conflicts with govet check-shadowing
- sloppyReassign
goimports:
local-prefixes: github.com/liqotech/resource-slice-class-controller-template
govet:
enable:
- shadow
- nilness
- nilfunc
misspell:
locale: US
nolintlint:
allow-unused: false # report any unused nolint directives
require-explanation: true # require an explanation for nolint directives
require-specific: true # require nolint directives to be specific about which linter is being skipped
dupl:
threshold: 300

linters:
disable-all: true
enable:
- asciicheck
- bodyclose
- copyloopvar
# - depguard
- dogsled
- dupl
- errcheck
- errorlint
- exhaustive
# - funlen
# - gochecknoglobals
# - gochecknoinits
# - gocognit
- gci
- goconst
- gocritic
- gocyclo
- godot
# - godox
# - goerr113
- gofmt
- goheader
- goimports
- gomodguard
# - gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
# - maligned
- misspell
- nakedret
# - nestif
- noctx
- nolintlint
# - prealloc
- revive
- rowserrcheck
- staticcheck
- stylecheck
# - testpackage
- typecheck
- unconvert
- unparam
- unused
- whitespace
# - wsl

issues:
#fix: true

max-issues-per-linter: 0
max-same-issues: 0

# Disable the default exclude patterns (as they disable the mandatory comments)
exclude-use-default: false
exclude:
# errcheck: Almost all programs ignore errors on these functions and in most cases it's ok
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*printf?|os\.(Un)?Setenv). is not checked

exclude-rules:
- linters:
- govet
text: 'declaration of "(err|ctx)" shadows declaration at'
- linters:
- gosec
# Disable the check to test that HTTP clients are not using an insecure TLS connection.
# We need it to contact the remote authentication services exposing a self-signed certificate
text: TLS InsecureSkipVerify set true.
- linters:
- errorlint
# Disable the check to test errors type assertion on switches.
text: type switch on error will fail on wrapped errors. Use errors.As to check for specific errors

# Exclude the following linters from running on tests files.
- path: _test\.go
linters:
- whitespace
- revive # it does not allow dot imports (e.g. . "github.com/onsi/ginkgo")

exclude-files:
- "zz_generated.*.go"

exclude-dirs:
- "pkg/client"
5 changes: 5 additions & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
default: true
line-length: false
no-inline-html: false
no-duplicate-header:
siblings_only: true
92 changes: 47 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ The controller manages the ResourceSlice status updates and conditions, while th
## Installation

1. Clone the repository:

```bash
git clone https://github.com/liqotech/resource-slice-class-controller-template.git
cd resource-slice-class-controller-template
```

2. Build the controller:

```bash
go build -o bin/manager main.go
```
Expand All @@ -40,13 +42,15 @@ The controller requires a class name to be specified:
```

Additional flags:

- `--metrics-bind-address`: The address to bind the metrics endpoint (default: ":8080")
- `--health-probe-bind-address`: The address to bind the health probe endpoint (default: ":8081")
- `--leader-elect`: Enable leader election for controller manager

### Example Implementation

The repository includes an example handler implementation in `example/resourceslice/handler.go` that:

- Generates CPU resources between 1 and 10 cores
- Generates Memory resources between 1 and 5 GB
- Allocates 110 pods
Expand All @@ -58,56 +62,56 @@ To implement a custom handler for your ResourceSlice class:

1. Create a new type that implements the `handler.Handler` interface:

```go
package myhandler
```go
package myhandler

import (
"context"
authv1beta1 "github.com/liqotech/liqo/apis/authentication/v1beta1"
"github.com/liqotech/resource-slice-classes/pkg/resourceslice/handler"
ctrl "sigs.k8s.io/controller-runtime"
)
import (
"context"
authv1beta1 "github.com/liqotech/liqo/apis/authentication/v1beta1"
rshandler "github.com/liqotech/resource-slice-class-controller-template/pkg/resourceslice/handler"
ctrl "sigs.k8s.io/controller-runtime"
)

type MyHandler struct {}
type MyHandler struct {}

func NewMyHandler() handler.Handler {
return &MyHandler{}
}
func NewMyHandler() rshandler.Handler {
return &MyHandler{}
}

func (h *MyHandler) Handle(ctx context.Context, resourceSlice *authv1beta1.ResourceSlice) (ctrl.Result, error) {
// Implement your custom resource allocation logic here
// Update resourceSlice.Status.Resources with your allocated resources

return ctrl.Result{}, nil
}
```
func (h *MyHandler) Handle(ctx context.Context, resourceSlice *authv1beta1.ResourceSlice) (ctrl.Result, error) {
// Implement your custom resource allocation logic here
// Update resourceSlice.Status.Resources with your allocated resources
return ctrl.Result{}, nil
}
```

2. Update `main.go` to use your custom handler:

```go
import (
"github.com/your-org/your-module/pkg/myhandler"
)

func main() {
// ...

// Create your custom handler
customHandler := myhandler.NewMyHandler()

if err = controller.NewResourceSliceReconciler(
mgr.GetClient(),
mgr.GetScheme(),
mgr.GetEventRecorderFor("resource-slice-controller"),
className,
customHandler,
).SetupWithManager(mgr); err != nil {
```go
import (
"github.com/your-org/your-module/pkg/myhandler"
)

func main() {
// ...

// Create your custom handler
customHandler := myhandler.NewMyHandler()

if err = controller.NewResourceSliceReconciler(
mgr.GetClient(),
mgr.GetScheme(),
mgr.GetEventRecorderFor("resource-slice-controller"),
className,
customHandler,
).SetupWithManager(mgr); err != nil {
// ...
}

// ...
}

// ...
}
```
```

## Handler Interface

Expand All @@ -120,11 +124,13 @@ type Handler interface {
```

Your handler implementation should:

1. Implement your resource allocation strategy
2. Set the allocated resources in `resourceSlice.Status.Resources`
3. Return appropriate reconciliation results and errors

Note: The controller, not the handler, is responsible for:

- Updating the ResourceSlice status in the API server
- Managing ResourceSlice conditions
- Recording events
Expand All @@ -150,7 +156,3 @@ Note: The controller, not the handler, is responsible for:
## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Loading
Loading