Skip to content

Commit

Permalink
fix(deps): update controller-runtime to v0.12.3 (#129)
Browse files Browse the repository at this point in the history
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
nakamasato and renovate[bot] committed Mar 12, 2023
1 parent 656b80a commit 7e39913
Show file tree
Hide file tree
Showing 18 changed files with 648 additions and 377 deletions.
29 changes: 18 additions & 11 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ jobs:
e2e-with-kuttl:
runs-on: ubuntu-latest
steps:
- name: set up
uses: actions/setup-go@v3
with:
go-version: 1.18

- uses: actions/checkout@master
- uses: actions/checkout@v3

# https://krew.sigs.k8s.io/docs/user-guide/setup/install/
- name: krew - install
Expand All @@ -44,23 +39,35 @@ jobs:
kubectl krew install kuttl
kubectl kuttl -v
- name: e2e-with-kuttl
run: make e2e-with-kuttl
- name: docker build
run: docker build -t mysql-operator .

- name: kuttl test
run: make kuttl

e2e-with-ginkgo:
runs-on: ubuntu-latest
steps:
- name: set up
- uses: actions/checkout@v3

- name: setup go
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version-file: ./go.mod
cache: true

- name: install skaffold # TODO: #69 Enable to install skaffold in e2e
run: |
curl -Lo skaffold https://storage.googleapis.com/skaffold/releases/latest/skaffold-linux-amd64 && \
sudo install skaffold /usr/local/bin/
- uses: actions/checkout@master
- name: create kind cluster
working-directory: e2e
run: kind create cluster --name mysql-operator-e2e --kubeconfig kubeconfig --config kind-config.yml --wait 30s

- name: skaffold run
working-directory: e2e
run: skaffold run --kubeconfig kubeconfig

- name: e2e-with-ginkgo
run: make e2e-with-ginkgo
67 changes: 65 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# Code Style

- [golangci-lint](https://golangci-lint.run)
[golangci-lint](https://golangci-lint.run)

Install:
```
brew install golangci-lint
```
Run lint:
```
golangci-lint run ./...
```


# Run mysql-operator

Expand Down Expand Up @@ -53,6 +63,33 @@
kubectl delete -f config/samples/mysql_v1alpha1_mysqluser.yaml
kubectl delete -f config/samples/mysql_v1alpha1_mysql.yaml
```

TODO: get stuck in deletion.

<details>

```
1.6780545572555468e+09 INFO [FetchMySQL] Not found {"controller": "mysql", "controllerGroup": "mysql.nakamasato.com", "controllerKind": "MySQL", "mySQL": {"name":"mysql-sample","namespace":"default"}, "namespace": "default", "name": "mysql-sample", "reconcileID": "0b6db5c6-8b3b-43ce-b903-a4959d55064e", "mysql.Name": "", "mysql.Namespace": ""}
1.678054557255548e+09 INFO [FetchMySQLUser] Found. {"controller": "mysqluser", "controllerGroup": "mysql.nakamasato.com", "controllerKind": "MySQLUser", "mySQLUser": {"name":"nakamasato","namespace":"default"}, "namespace": "default", "name": "nakamasato", "reconcileID": "78d4a7cf-5be0-4d47-82c0-38c7fdcf675b", "name": "nakamasato", "mysqlUser.Namespace": "default"}
1.678054557255587e+09 ERROR [FetchMySQL] Failed {"controller": "mysqluser", "controllerGroup": "mysql.nakamasato.com", "controllerKind": "MySQLUser", "mySQLUser": {"name":"nakamasato","namespace":"default"}, "namespace": "default", "name": "nakamasato", "reconcileID": "78d4a7cf-5be0-4d47-82c0-38c7fdcf675b", "error": "MySQL.mysql.nakamasato.com \"mysql-sample\" not found"}
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Reconcile
/Users/m.naka/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.12.3/pkg/internal/controller/controller.go:121
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler
/Users/m.naka/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.12.3/pkg/internal/controller/controller.go:320
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem
/Users/m.naka/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.12.3/pkg/internal/controller/controller.go:273
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).Start.func2.2
/Users/m.naka/go/pkg/mod/sigs.k8s.io/controller-runtime@v0.12.3/pkg/internal/controller/controller.go:234
```

When getting stuck:

```
kubectl patch mysqluser nakamasato -p '{"metadata":{"finalizers": []}}' --type=merge
```

</details>

1. Secret is deleted.
```
kubectl get secret
Expand Down Expand Up @@ -83,6 +120,7 @@

```
make uninstall
docker rm -f $(docker ps | grep mysql | head -1 |awk '{print $1}')
```

## Local kubernetes
Expand Down Expand Up @@ -135,6 +173,14 @@ make uninstall
kubectl delete -f config/samples-on-k8s/mysql_v1alpha1_mysqluser.yaml
kubectl delete -f config/samples-on-k8s/mysql_v1alpha1_mysql.yaml
```

TODO: Get stuck:

```
kubectl exec -it $(kubectl get po | grep mysql | head -1 | awk '{print $1}') -- mysql -uroot -ppassword -e 'delete from mysql.user where User = "nakamasato";'
kubectl patch mysqluser nakamasato -p '{"metadata":{"finalizers": []}}' --type=merge
```

1. Stop the `skaffold dev` by `ctrl-c` -> will clean up the controller, CRDs, and installed resources.
# Test

Expand Down Expand Up @@ -206,7 +252,7 @@ make e2e-with-ginkgo
```
1. Deploy `CRD`, `mysql-operator`, and MySQL with `Deployment`:
```
cd e2e && skaffold run --kubeconfig kubeconfig
cd e2e && skaffold run --kubeconfig kubeconfig --tail
```

</details>
Expand Down Expand Up @@ -382,5 +428,22 @@ https://cloud.redhat.com/blog/kubernetes-operators-best-practices
1. Return the error in the status of the object. https://pkg.go.dev/github.com/shivanshs9/operator-utils@v1.0.1#section-readme
1. Generate an event describing the error.

### Error1: `Operation cannot be fulfilled on mysqlusers.mysql.nakamasato.com \"john\": StorageError: invalid object, Code: 4, Key: /registry/mysql.nakamasato.com/mysqlusers/default/john, ResourceVersion: 0, AdditionalErrorMsg: Precondition failed: UID in precondition: cd9c94d1-992a-457d-8fab-489b21ed02e9, UID in object meta:`

```
[manager] 1.6781410047933352e+09 ERROR Reconciler error {"controller": "mysqluser", "controllerGroup": "mysql.nakamasato.com", "controllerKind": "MySQLUser", "mySQLUser": {"name":"john","namespace":"default"}, "namespace": "default", "name": "john", "reconcileID": "85fc0e64-f2b9-413f-af44-46ff1daad7f7", "error": "Operation cannot be fulfilled on mysqlusers.mysql.nakamasato.com \"john\": StorageError: invalid object, Code: 4, Key: /registry/mysql.nakamasato.com/mysqlusers/default/john, ResourceVersion: 0, AdditionalErrorMsg: Precondition failed: UID in precondition: cd9c94d1-992a-457d-8fab-489b21ed02e9, UID in object meta: "}
```

UID in precondition and UID in object meta are different?

https://github.com/kubernetes-sigs/controller-runtime/issues/2209

## Slow build

```
time CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go 161.57s user 24.90s system 283% cpu 1:05.76 total
```

## MySQL
- http://go-database-sql.org/index.html
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ IMG ?= ghcr.io/nakamasato/mysql-operator:latest
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.21
ENVTEST_K8S_VERSION = 1.24

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -88,7 +88,7 @@ vet: ## Run go vet against code.
go vet ./...

test: ginkgo manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) -cover -coverprofile cover.out -covermode=atomic -skipPackage=e2e ./...
ACK_GINKGO_DEPRECATIONS=1.16.5 KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) -cover -coverprofile cover.out -covermode=atomic -skipPackage=e2e ./...

##@ Build

Expand Down Expand Up @@ -216,4 +216,4 @@ e2e-with-kuttl:

.PHONY: e2e-with-ginkgo
e2e-with-ginkgo: ginkgo
$(GINKGO) e2e
ACK_GINKGO_RC=true $(GINKGO) e2e
7 changes: 3 additions & 4 deletions controllers/mysql_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/go-logr/logr"
mysqlv1alpha1 "github.com/nakamasato/mysql-operator/api/v1alpha1"
)

Expand All @@ -51,7 +50,7 @@ type MySQLReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.9.2/pkg/reconcile
func (r *MySQLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
log := log.FromContext(ctx).WithName("MySQLReconciler")

// Fetch MySQL
mysql := &mysqlv1alpha1.MySQL{}
Expand All @@ -67,7 +66,7 @@ func (r *MySQLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
}

// Get referenced number
referencedNum, err := r.countReferencesByMySQLUser(ctx, log, mysql)
referencedNum, err := r.countReferencesByMySQLUser(ctx, mysql)
if err != nil {
log.Error(err, "[countReferences] Failed get referencedNum")
return ctrl.Result{}, err
Expand Down Expand Up @@ -96,7 +95,7 @@ func (r *MySQLReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *MySQLReconciler) countReferencesByMySQLUser(ctx context.Context, log logr.Logger, mysql *mysqlv1alpha1.MySQL) (int, error) {
func (r *MySQLReconciler) countReferencesByMySQLUser(ctx context.Context, mysql *mysqlv1alpha1.MySQL) (int, error) {
// 1. Get the referenced MySQLUser instances.
// 2. Return the number of referencing MySQLUser.
mysqlUserList := &mysqlv1alpha1.MySQLUserList{}
Expand Down
3 changes: 1 addition & 2 deletions controllers/mysql_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -22,7 +21,7 @@ var _ = Describe("MySQL controller", func() {

BeforeEach(func() {
k8sManager, err := ctrl.NewManager(cfg, ctrl.Options{
Scheme: scheme.Scheme,
Scheme: scheme,
})
Expect(err).ToNot(HaveOccurred())

Expand Down
Loading

0 comments on commit 7e39913

Please sign in to comment.