Skip to content

Commit 25bf91e

Browse files
authored
Merge pull request #85 from xrstf/split-controllers
Split controllers into dedicated packages, remove Ginkgo
2 parents ff40aad + 753d6d6 commit 25bf91e

21 files changed

+580
-724
lines changed

Makefile

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
# for checking import statements.
33
OPENSHIFT_GOIMPORTS_VER := c72f1dc2e3aacfa00aece3391d938c9bc734e791
44
RECONCILER_GEN_VER := v0.5.0
5-
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
6-
ENVTEST_K8S_VERSION = 1.31.0
75
## Tool Versions
86
KUBECTL_VERSION ?= v1.32.0
97
KUSTOMIZE_VERSION ?= v5.4.3
108
CONTROLLER_TOOLS_VERSION ?= v0.16.1
11-
ENVTEST_VERSION ?= release-0.19
129
GOLANGCI_LINT_VERSION ?= 2.1.6
1310
PROTOKOL_VERSION ?= 0.7.2
1411

@@ -63,8 +60,8 @@ vet: ## Run go vet against code.
6360
go vet ./...
6461

6562
.PHONY: test
66-
test: fmt vet envtest ## Run tests.
67-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(TOOLS_DIR) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
63+
test: fmt vet ## Run tests.
64+
go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
6865

6966
# Utilize Kind or modify the e2e tests to load the image locally, enabling compatibility with other vendors.
7067
.PHONY: test-e2e # Run the e2e tests against a Kind k8s instance that is spun up.
@@ -152,7 +149,6 @@ undeploy: kubectl kustomize ## Undeploy controller from the K8s cluster specifie
152149
## Tool Binaries
153150
KUBECTL ?= $(TOOLS_DIR)/kubectl
154151
KUSTOMIZE ?= $(TOOLS_DIR)/kustomize
155-
ENVTEST ?= $(TOOLS_DIR)/setup-envtest
156152
GOLANGCI_LINT = $(TOOLS_DIR)/golangci-lint
157153
PROTOKOL = $(TOOLS_DIR)/protokol
158154
RECONCILER_GEN := $(TOOLS_DIR)/reconciler-gen
@@ -172,13 +168,6 @@ kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
172168
$(KUSTOMIZE):
173169
@GO_MODULE=true hack/download-tool.sh sigs.k8s.io/kustomize/kustomize/v5 kustomize $(KUSTOMIZE_VERSION)
174170

175-
.PHONY: envtest
176-
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
177-
178-
.PHONY: $(ENVTEST)
179-
$(ENVTEST):
180-
@GO_MODULE=true hack/download-tool.sh sigs.k8s.io/controller-runtime/tools/setup-envtest setup-envtest $(ENVTEST_VERSION)
181-
182171
.PHONY: golangci-lint
183172
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
184173

cmd/main.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ import (
3636
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3737
"sigs.k8s.io/controller-runtime/pkg/webhook"
3838

39-
"github.com/kcp-dev/kcp-operator/internal/controller"
39+
"github.com/kcp-dev/kcp-operator/internal/controller/cacheserver"
40+
"github.com/kcp-dev/kcp-operator/internal/controller/frontproxy"
41+
"github.com/kcp-dev/kcp-operator/internal/controller/kubeconfig"
42+
"github.com/kcp-dev/kcp-operator/internal/controller/rootshard"
43+
"github.com/kcp-dev/kcp-operator/internal/controller/shard"
4044
"github.com/kcp-dev/kcp-operator/internal/reconciling"
4145
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
4246
)
@@ -149,35 +153,35 @@ func main() {
149153
os.Exit(1)
150154
}
151155

152-
if err = (&controller.RootShardReconciler{
156+
if err = (&rootshard.RootShardReconciler{
153157
Client: mgr.GetClient(),
154158
Scheme: mgr.GetScheme(),
155159
}).SetupWithManager(mgr); err != nil {
156160
setupLog.Error(err, "unable to create controller", "controller", "KCPInstance")
157161
os.Exit(1)
158162
}
159-
if err = (&controller.FrontProxyReconciler{
163+
if err = (&frontproxy.FrontProxyReconciler{
160164
Client: mgr.GetClient(),
161165
Scheme: mgr.GetScheme(),
162166
}).SetupWithManager(mgr); err != nil {
163167
setupLog.Error(err, "unable to create controller", "controller", "FrontProxy")
164168
os.Exit(1)
165169
}
166-
if err = (&controller.ShardReconciler{
170+
if err = (&shard.ShardReconciler{
167171
Client: mgr.GetClient(),
168172
Scheme: mgr.GetScheme(),
169173
}).SetupWithManager(mgr); err != nil {
170174
setupLog.Error(err, "unable to create controller", "controller", "Shard")
171175
os.Exit(1)
172176
}
173-
if err = (&controller.CacheServerReconciler{
177+
if err = (&cacheserver.CacheServerReconciler{
174178
Client: mgr.GetClient(),
175179
Scheme: mgr.GetScheme(),
176180
}).SetupWithManager(mgr); err != nil {
177181
setupLog.Error(err, "unable to create controller", "controller", "CacheServer")
178182
os.Exit(1)
179183
}
180-
if err = (&controller.KubeconfigReconciler{
184+
if err = (&kubeconfig.KubeconfigReconciler{
181185
Client: mgr.GetClient(),
182186
Scheme: mgr.GetScheme(),
183187
}).SetupWithManager(mgr); err != nil {

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ require (
1212
github.com/kcp-dev/code-generator/v2 v2.3.1
1313
github.com/kcp-dev/kcp-operator/sdk v0.0.0-00010101000000-000000000000
1414
github.com/kcp-dev/kcp/sdk v0.27.1
15-
github.com/onsi/ginkgo/v2 v2.20.0
16-
github.com/onsi/gomega v1.34.1
1715
github.com/stretchr/testify v1.9.0
1816
go.uber.org/zap v1.27.0
1917
k8c.io/reconciler v0.5.0
@@ -46,7 +44,6 @@ require (
4644
github.com/go-openapi/jsonpointer v0.21.0 // indirect
4745
github.com/go-openapi/jsonreference v0.21.0 // indirect
4846
github.com/go-openapi/swag v0.23.0 // indirect
49-
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
5047
github.com/gobuffalo/flect v1.0.2 // indirect
5148
github.com/gogo/protobuf v1.3.2 // indirect
5249
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
@@ -55,7 +52,6 @@ require (
5552
github.com/google/gnostic-models v0.6.8 // indirect
5653
github.com/google/go-cmp v0.6.0 // indirect
5754
github.com/google/gofuzz v1.2.1-0.20210504230335-f78f29fc09ea // indirect
58-
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8 // indirect
5955
github.com/google/uuid v1.6.0 // indirect
6056
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
6157
github.com/imdario/mergo v0.3.16 // indirect
@@ -70,6 +66,7 @@ require (
7066
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
7167
github.com/modern-go/reflect2 v1.0.2 // indirect
7268
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
69+
github.com/onsi/gomega v1.34.1 // indirect
7370
github.com/pkg/errors v0.9.1 // indirect
7471
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
7572
github.com/prometheus/client_golang v1.20.4 // indirect
@@ -104,6 +101,7 @@ require (
104101
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
105102
google.golang.org/grpc v1.66.2 // indirect
106103
google.golang.org/protobuf v1.34.2 // indirect
104+
gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
107105
gopkg.in/inf.v0 v0.9.1 // indirect
108106
gopkg.in/yaml.v2 v2.4.0 // indirect
109107
gopkg.in/yaml.v3 v3.0.1 // indirect

internal/controller/cacheserver_controller.go renamed to internal/controller/cacheserver/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package controller
17+
package cacheserver
1818

1919
import (
2020
"context"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright 2024 The KCP Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cacheserver
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
27+
ctrlruntimefakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
28+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
29+
30+
"github.com/kcp-dev/kcp-operator/internal/controller/util"
31+
operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1"
32+
)
33+
34+
func TestReconciling(t *testing.T) {
35+
const namespace = "cacheserver-tests"
36+
37+
testcases := []struct {
38+
name string
39+
cacheServer *operatorv1alpha1.CacheServer
40+
}{
41+
{
42+
name: "vanilla",
43+
cacheServer: &operatorv1alpha1.CacheServer{
44+
ObjectMeta: metav1.ObjectMeta{
45+
Name: "test-resource",
46+
Namespace: namespace,
47+
},
48+
Spec: operatorv1alpha1.CacheServerSpec{
49+
Etcd: operatorv1alpha1.EtcdConfig{
50+
Endpoints: []string{"https://localhost:2379"},
51+
},
52+
},
53+
},
54+
},
55+
}
56+
57+
scheme := util.GetTestScheme()
58+
59+
for _, testcase := range testcases {
60+
t.Run(testcase.name, func(t *testing.T) {
61+
client := ctrlruntimefakeclient.
62+
NewClientBuilder().
63+
WithScheme(scheme).
64+
WithStatusSubresource(testcase.cacheServer).
65+
WithObjects(testcase.cacheServer).
66+
Build()
67+
68+
ctx := context.Background()
69+
70+
controllerReconciler := &CacheServerReconciler{
71+
Client: client,
72+
Scheme: client.Scheme(),
73+
}
74+
75+
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
76+
NamespacedName: ctrlruntimeclient.ObjectKeyFromObject(testcase.cacheServer),
77+
})
78+
require.NoError(t, err)
79+
})
80+
}
81+
}

internal/controller/cacheserver_controller_test.go

Lines changed: 0 additions & 89 deletions
This file was deleted.

internal/controller/frontproxy_controller.go renamed to internal/controller/frontproxy/controller.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package controller
17+
package frontproxy
1818

1919
import (
2020
"context"
@@ -38,6 +38,7 @@ import (
3838
"sigs.k8s.io/controller-runtime/pkg/log"
3939
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4040

41+
"github.com/kcp-dev/kcp-operator/internal/controller/util"
4142
"github.com/kcp-dev/kcp-operator/internal/reconciling"
4243
"github.com/kcp-dev/kcp-operator/internal/resources"
4344
"github.com/kcp-dev/kcp-operator/internal/resources/frontproxy"
@@ -118,7 +119,7 @@ func (r *FrontProxyReconciler) reconcile(ctx context.Context, frontProxy *operat
118119
conditions []metav1.Condition
119120
)
120121

121-
cond, rootShard := fetchRootShard(ctx, r.Client, frontProxy.Namespace, frontProxy.Spec.RootShard.Reference)
122+
cond, rootShard := util.FetchRootShard(ctx, r.Client, frontProxy.Namespace, frontProxy.Spec.RootShard.Reference)
122123
conditions = append(conditions, cond)
123124

124125
if rootShard == nil {
@@ -178,7 +179,7 @@ func (r *FrontProxyReconciler) reconcileStatus(ctx context.Context, oldFrontProx
178179
var errs []error
179180

180181
depKey := types.NamespacedName{Namespace: frontProxy.Namespace, Name: resources.GetFrontProxyDeploymentName(frontProxy)}
181-
cond, err := getDeploymentAvailableCondition(ctx, r.Client, depKey)
182+
cond, err := util.GetDeploymentAvailableCondition(ctx, r.Client, depKey)
182183
if err != nil {
183184
errs = append(errs, err)
184185
} else {
@@ -187,19 +188,18 @@ func (r *FrontProxyReconciler) reconcileStatus(ctx context.Context, oldFrontProx
187188

188189
for _, condition := range conditions {
189190
condition.ObservedGeneration = frontProxy.Generation
190-
frontProxy.Status.Conditions = updateCondition(frontProxy.Status.Conditions, condition)
191+
frontProxy.Status.Conditions = util.UpdateCondition(frontProxy.Status.Conditions, condition)
191192
}
192193

193-
availableCond := apimeta.FindStatusCondition(frontProxy.Status.Conditions, string(operatorv1alpha1.ConditionTypeAvailable))
194-
switch {
195-
case availableCond.Status == metav1.ConditionTrue:
196-
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseRunning
197-
198-
case frontProxy.DeletionTimestamp != nil:
194+
if frontProxy.DeletionTimestamp != nil {
199195
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseDeleting
200-
201-
case frontProxy.Status.Phase == "":
202-
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseProvisioning
196+
} else {
197+
availableCond := apimeta.FindStatusCondition(frontProxy.Status.Conditions, string(operatorv1alpha1.ConditionTypeAvailable))
198+
if availableCond != nil && availableCond.Status == metav1.ConditionTrue {
199+
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseRunning
200+
} else {
201+
frontProxy.Status.Phase = operatorv1alpha1.FrontProxyPhaseProvisioning
202+
}
203203
}
204204

205205
// only patch the status if there are actual changes.

0 commit comments

Comments
 (0)