Skip to content

Commit 3e983ef

Browse files
committed
wire in tests
1 parent f122712 commit 3e983ef

File tree

6 files changed

+59
-63
lines changed

6 files changed

+59
-63
lines changed

examples/kcp/Makefile

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ $(KCP_APIGEN_GEN):
3232
$(CONTROLLER_GEN):
3333
GOBIN=$(LOCALBIN) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
3434

35-
build: $(KCP) $(KCP_APIGEN_GEN) $(CONTROLLER_GEN)
35+
build: $(KCP) $(KCP_APIGEN_GEN) $(CONTROLLER_GEN) build-controller
36+
37+
.PHONY:
38+
build-controller: ## Build the controller binary.
39+
rm -f $(LOCALBIN)/kcp-controller
40+
go build -o $(LOCALBIN)/kcp-controller ./main.go
3641

3742
.PHONY: kcp-server
3843
kcp-server: $(KCP) $(ARTIFACT_DIR)/kcp ## Run the kcp server.
@@ -48,20 +53,27 @@ test-cleanup: ## Clean up processes and directories from an end-to-end test run.
4853
rm -rf $(ARTIFACT_DIR) || true
4954
pkill -sigterm kcp || true
5055
pkill -sigterm kubectl || true
56+
pkill -sigterm kcp-controller || true
5157

5258
$(ARTIFACT_DIR)/kcp: ## Create a directory for the kcp server data.
5359
mkdir -p $(ARTIFACT_DIR)/kcp
5460

5561
generate: build
5662
./hack/update-codegen-crds.sh
5763

58-
5964
bootstrap:
6065
export KUBECONFIG=./.test/kcp.kubeconfig
6166
@go run ./config/main.go
6267

68+
run-controller-background: build bootstrap
69+
export KUBECONFIG=./.test/kcp.kubeconfig
70+
kubectl ws use widgets
71+
@if [[ ! -s $(ARTIFACT_DIR)/controller.log ]]; then ( ./bin/kcp-controller >$(ARTIFACT_DIR)/controller.log 2>&1 & ); fi
6372

64-
run:
73+
run: build-controller bootstrap
6574
export KUBECONFIG=./.test/kcp.kubeconfig
6675
kubectl ws use widgets
67-
@go run ./main.go
76+
./bin/kcp-controller
77+
78+
test:
79+
go test ./... --workspace=root --kubeconfig=$(CURDIR)/$(ARTIFACT_DIR)/kcp.kubeconfig

examples/kcp/config/widgets/resources/apiexport-data.my.domain.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ spec:
1111
resource: configmaps
1212
- all: true
1313
resource: secrets
14+
- all: true
15+
resource: namespaces
1416
status: {}

examples/kcp/controllers/helper.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package controllers
1919
import (
2020
"context"
2121

22+
"github.com/davecgh/go-spew/spew"
2223
"github.com/kcp-dev/logicalcluster/v3"
2324
"sigs.k8s.io/controller-runtime/pkg/kontext"
2425
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -28,6 +29,7 @@ import (
2829
// cluster clients and cache work out of the box.
2930
func WithClusterInContext(r reconcile.Reconciler) reconcile.Reconciler {
3031
return reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
32+
spew.Dump(req.NamespacedName, req.ClusterName)
3133
ctx = kontext.WithCluster(ctx, logicalcluster.Name(req.ClusterName))
3234
return r.Reconcile(ctx, req)
3335
})

examples/kcp/main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ func init() {
5959
}
6060

6161
func main() {
62+
ctx := ctrl.SetupSignalHandler()
63+
if err := runController(ctx); err != nil {
64+
setupLog.Error(err, "problem running controller")
65+
os.Exit(1)
66+
}
67+
}
68+
69+
func runController(ctx context.Context) error {
6270
var metricsAddr string
6371
var enableLeaderElection bool
6472
var probeAddr string
@@ -81,8 +89,6 @@ func main() {
8189

8290
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
8391

84-
ctx := ctrl.SetupSignalHandler()
85-
8692
restConfig := ctrl.GetConfigOrDie()
8793

8894
setupLog = setupLog.WithValues("api-export-name", apiExportName)
@@ -145,10 +151,7 @@ func main() {
145151

146152
setupLog.Info("starting manager")
147153

148-
if err := mgr.Start(ctx); err != nil {
149-
setupLog.Error(err, "problem running manager")
150-
os.Exit(1)
151-
}
154+
return mgr.Start(ctx)
152155
}
153156

154157
// restConfigForAPIExport returns a *rest.Config properly configured to communicate with the endpoint for the

examples/kcp/test/e2e/apibinding.yaml

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

examples/kcp/test/e2e/controller_test.go

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ import (
55
"flag"
66
"fmt"
77
"math/rand"
8+
"strings"
89
"testing"
910
"time"
1011

1112
"github.com/google/go-cmp/cmp"
1213
apierrors "k8s.io/apimachinery/pkg/api/errors"
1314

1415
kcpclienthelper "github.com/kcp-dev/apimachinery/pkg/client"
15-
16-
apisv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/apis/v1alpha1"
17-
tenancyv1alpha1 "github.com/kcp-dev/kcp/pkg/apis/tenancy/v1alpha1"
18-
"github.com/kcp-dev/kcp/pkg/apis/third_party/conditions/util/conditions"
16+
apisv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/apis/v1alpha1"
17+
corev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1"
18+
tenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1"
19+
"github.com/kcp-dev/kcp/sdk/apis/third_party/conditions/util/conditions"
1920

2021
"github.com/kcp-dev/logicalcluster/v2"
2122

@@ -35,11 +36,7 @@ import (
3536

3637
// The tests in this package expect to be called when:
3738
// - kcp is running
38-
// - a kind cluster is up and running
39-
// - it is hosting a syncer, and the SyncTarget is ready to go
40-
// - the controller-manager from this repo is deployed to kcp
41-
// - that deployment is synced to the kind cluster
42-
// - the deployment is rolled out & ready
39+
// - the controller-manager from this repo is running
4340
//
4441
// We can then check that the controllers defined here are working as expected.
4542

@@ -51,7 +48,6 @@ func init() {
5148
}
5249

5350
func parentWorkspace(t *testing.T) logicalcluster.Name {
54-
flag.Parse()
5551
if workspaceName == "" {
5652
t.Fatal("--workspace cannot be empty")
5753
}
@@ -65,7 +61,7 @@ func loadClusterConfig(t *testing.T, clusterName logicalcluster.Name) *rest.Conf
6561
if err != nil {
6662
t.Fatalf("failed to load *rest.Config: %v", err)
6763
}
68-
return rest.AddUserAgent(kcpclienthelper.ConfigWithCluster(restConfig, clusterName), t.Name())
64+
return rest.AddUserAgent(kcpclienthelper.SetCluster(restConfig, clusterName), t.Name())
6965
}
7066

7167
func loadClient(t *testing.T, clusterName logicalcluster.Name) client.Client {
@@ -98,13 +94,13 @@ func createWorkspace(t *testing.T, clusterName logicalcluster.Name) client.Clien
9894
}
9995
c := loadClient(t, parent)
10096
t.Logf("creating workspace %s", clusterName)
101-
if err := c.Create(context.TODO(), &tenancyv1alpha1.ClusterWorkspace{
97+
if err := c.Create(context.TODO(), &tenancyv1alpha1.Workspace{
10298
ObjectMeta: metav1.ObjectMeta{
10399
Name: clusterName.Base(),
104100
},
105-
Spec: tenancyv1alpha1.ClusterWorkspaceSpec{
106-
Type: tenancyv1alpha1.ClusterWorkspaceTypeReference{
107-
Name: "universal",
101+
Spec: tenancyv1alpha1.WorkspaceSpec{
102+
Type: tenancyv1alpha1.WorkspaceTypeReference{
103+
Name: "widgets",
108104
Path: "root",
109105
},
110106
},
@@ -113,15 +109,15 @@ func createWorkspace(t *testing.T, clusterName logicalcluster.Name) client.Clien
113109
}
114110

115111
t.Logf("waiting for workspace %s to be ready", clusterName)
116-
var workspace tenancyv1alpha1.ClusterWorkspace
112+
var workspace tenancyv1alpha1.Workspace
117113
if err := wait.PollImmediate(100*time.Millisecond, wait.ForeverTestTimeout, func() (done bool, err error) {
118114
fetchErr := c.Get(context.TODO(), client.ObjectKey{Name: clusterName.Base()}, &workspace)
119115
if fetchErr != nil {
120116
t.Logf("failed to get workspace %s: %v", clusterName, err)
121117
return false, fetchErr
122118
}
123119
var reason string
124-
if actual, expected := workspace.Status.Phase, tenancyv1alpha1.ClusterWorkspacePhaseReady; actual != expected {
120+
if actual, expected := workspace.Status.Phase, corev1alpha1.LogicalClusterPhaseReady; actual != expected {
125121
reason = fmt.Sprintf("phase is %s, not %s", actual, expected)
126122
t.Logf("not done waiting for workspace %s to be ready: %s", clusterName, reason)
127123
}
@@ -130,32 +126,26 @@ func createWorkspace(t *testing.T, clusterName logicalcluster.Name) client.Clien
130126
t.Fatalf("workspace %s never ready: %v", clusterName, err)
131127
}
132128

133-
return createAPIBinding(t, clusterName)
129+
return waitingForAPIBinding(t, clusterName)
134130
}
135131

136-
func createAPIBinding(t *testing.T, workspaceCluster logicalcluster.Name) client.Client {
132+
func waitingForAPIBinding(t *testing.T, workspaceCluster logicalcluster.Name) client.Client {
137133
c := loadClient(t, workspaceCluster)
138-
apiName := "controller-runtime-example-data.my.domain"
139-
t.Logf("creating APIBinding %s|%s", workspaceCluster, apiName)
140-
if err := c.Create(context.TODO(), &apisv1alpha1.APIBinding{
141-
ObjectMeta: metav1.ObjectMeta{
142-
Name: apiName,
143-
},
144-
Spec: apisv1alpha1.APIBindingSpec{
145-
Reference: apisv1alpha1.ExportReference{
146-
Workspace: &apisv1alpha1.WorkspaceExportReference{
147-
Path: parentWorkspace(t).String(),
148-
ExportName: apiName,
149-
},
150-
},
151-
AcceptedPermissionClaims: []apisv1alpha1.PermissionClaim{
152-
{GroupResource: apisv1alpha1.GroupResource{Resource: "configmaps"}},
153-
{GroupResource: apisv1alpha1.GroupResource{Resource: "secrets"}},
154-
{GroupResource: apisv1alpha1.GroupResource{Resource: "namespaces"}},
155-
},
156-
},
157-
}); err != nil {
158-
t.Fatalf("could not create APIBinding %s|%s: %v", workspaceCluster, apiName, err)
134+
ctx := context.TODO()
135+
apiNamePrefix := "data.my.domain" // matches bootstrapped name
136+
137+
list := &apisv1alpha1.APIBindingList{}
138+
err := c.List(ctx, list)
139+
if err != nil {
140+
t.Fatalf("failed to list APIBindings: %v", err)
141+
}
142+
143+
apiName := ""
144+
for _, apiBinding := range list.Items {
145+
if strings.HasPrefix(apiBinding.Name, apiNamePrefix) {
146+
apiName = apiBinding.Name
147+
break
148+
}
159149
}
160150

161151
t.Logf("waiting for APIBinding %s|%s to be bound", workspaceCluster, apiName)

0 commit comments

Comments
 (0)