Skip to content

Commit e71e393

Browse files
committed
address reviews
1 parent 902fee2 commit e71e393

File tree

9 files changed

+18
-73
lines changed

9 files changed

+18
-73
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ hack/tools/bin
2626
junit-report.xml
2727
/artifacts
2828

29-
examples/kcp/bin/*
29+
examples/kcp/.gitignore

.prow.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ presubmits:
1212
- make
1313
- test
1414

15-
- name: pull-kcp-test-e2e
15+
- name: pull-controller-runtime-example-e2e
1616
decorate: true
1717
# only run e2e tests if code changed.
18-
run_if_changed: "(cmd|config|pkg|sdk|test|go.mod|go.sum|Makefile|.prow.yaml)"
18+
run_if_changed: "(pkg|examples|go.mod|go.sum|Makefile|.prow.yaml)"
1919
clone_uri: "https://github.com/kcp-dev/controller-runtime"
2020
labels:
2121
preset-goproxy: "true"

examples/kcp/Makefile

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ GO_INSTALL = ./hack/go-install.sh
1010
LOCALBIN ?= $(shell pwd)/bin
1111
ARTIFACT_DIR ?= .test
1212

13-
KCP_APIGEN_VER := v0.23.0
14-
KCP_APIGEN_BIN := apigen
15-
KCP_APIGEN_GEN := $(LOCALBIN)/$(KCP_APIGEN_BIN)-$(KCP_APIGEN_VER)
16-
export KCP_APIGEN_GEN # so hack scripts can use it
17-
18-
CONTROLLER_GEN_VER := v0.14.0
19-
CONTROLLER_GEN_BIN := controller-gen
20-
CONTROLLER_GEN := $(LOCALBIN)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER)
21-
export CONTROLLER_GEN # so hack scripts can use it
22-
2313
KCP ?= $(LOCALBIN)/kcp
2414
KUBECTL_KCP ?= $(LOCALBIN)/kubectl-kcp
2515

@@ -33,27 +23,19 @@ $(KCP): ## Download kcp locally if necessary.
3323
curl -L -s -o - https://github.com/kcp-dev/kcp/releases/download/v$(KCP_VERSION)/kcp_$(KCP_VERSION)_$(OS)_$(ARCH).tar.gz | tar --directory $(LOCALBIN)/../ -xvzf - bin/kcp
3424
touch $(KCP) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir
3525

36-
$(KCP_APIGEN_GEN):
37-
GOBIN=$(LOCALBIN) $(GO_INSTALL) github.com/kcp-dev/kcp/sdk/cmd/apigen $(KCP_APIGEN_BIN) $(KCP_APIGEN_VER)
38-
39-
$(CONTROLLER_GEN):
40-
GOBIN=$(LOCALBIN) $(GO_INSTALL) sigs.k8s.io/controller-tools/cmd/controller-gen $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)
41-
4226
$(KUBECTL_KCP): ## Download kcp kubectl plugins locally if necessary.
4327
curl -L -s -o - https://github.com/kcp-dev/kcp/releases/download/v$(KCP_VERSION)/kubectl-kcp-plugin_$(KCP_VERSION)_$(OS)_$(ARCH).tar.gz | tar --directory $(LOCALBIN)/../ -xvzf - bin
4428
touch $(KUBECTL_KCP) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir
4529

46-
build: $(KCP) $(KCP_APIGEN_GEN) $(CONTROLLER_GEN) $(KUBECTL_KCP) build-controller
30+
build: $(KCP) $(KUBECTL_KCP) build-controller
4731

4832
ifeq (,$(shell go env GOBIN))
4933
GOBIN=$(shell go env GOPATH)/bin
5034
else
5135
GOBIN=$(shell go env GOBIN)
5236
endif
5337

54-
.PHONY:
5538
build-controller: ## Build the controller binary.
56-
rm -f $(LOCALBIN)/kcp-controller
5739
go build -o $(LOCALBIN)/kcp-controller ./main.go
5840

5941
.PHONY: kcp-server

examples/kcp/config/consumers/bootstrap.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"embed"
2222

23-
"k8s.io/apimachinery/pkg/util/sets"
2423
"sigs.k8s.io/controller-runtime/pkg/client"
2524
"sigs.k8s.io/controller-runtime/pkg/log"
2625

@@ -36,10 +35,9 @@ var fs embed.FS
3635
func Bootstrap(
3736
ctx context.Context,
3837
client client.Client,
39-
batteriesIncluded sets.Set[string],
4038
) error {
4139
log := log.FromContext(ctx)
4240

4341
log.Info("Bootstrapping consumers workspaces")
44-
return confighelpers.Bootstrap(ctx, client, fs, batteriesIncluded)
42+
return confighelpers.Bootstrap(ctx, client, fs)
4543
}

examples/kcp/config/helpers/bootstrap.go

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11-
"strings"
1211
"text/template"
1312
"time"
1413

@@ -17,7 +16,6 @@ import (
1716
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1817
"k8s.io/apimachinery/pkg/types"
1918
apimachineryerrors "k8s.io/apimachinery/pkg/util/errors"
20-
"k8s.io/apimachinery/pkg/util/sets"
2119
"k8s.io/apimachinery/pkg/util/wait"
2220
kubeyaml "k8s.io/apimachinery/pkg/util/yaml"
2321
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -27,10 +25,10 @@ import (
2725
// Bootstrap creates resources in a package's fs by
2826
// continuously retrying the list. This is blocking, i.e. it only returns (with error)
2927
// when the context is closed or with nil when the bootstrapping is successfully completed.
30-
func Bootstrap(ctx context.Context, client client.Client, fs embed.FS, batteriesIncluded sets.Set[string]) error {
28+
func Bootstrap(ctx context.Context, client client.Client, fs embed.FS) error {
3129
// bootstrap non-crd resources
3230
return wait.PollUntilContextCancel(ctx, time.Second, true, func(ctx context.Context) (bool, error) {
33-
if err := CreateResourcesFromFS(ctx, client, fs, batteriesIncluded); err != nil {
31+
if err := CreateResourcesFromFS(ctx, client, fs); err != nil {
3432
log.FromContext(ctx).WithValues("err", err).Info("failed to bootstrap resources, retrying")
3533
return false, nil
3634
}
@@ -39,7 +37,7 @@ func Bootstrap(ctx context.Context, client client.Client, fs embed.FS, batteries
3937
}
4038

4139
// CreateResourcesFromFS creates all resources from a filesystem.
42-
func CreateResourcesFromFS(ctx context.Context, client client.Client, fs embed.FS, batteriesIncluded sets.Set[string]) error {
40+
func CreateResourcesFromFS(ctx context.Context, client client.Client, fs embed.FS) error {
4341
files, err := fs.ReadDir(".")
4442
if err != nil {
4543
return err
@@ -50,15 +48,15 @@ func CreateResourcesFromFS(ctx context.Context, client client.Client, fs embed.F
5048
if f.IsDir() {
5149
continue
5250
}
53-
if err := CreateResourceFromFS(ctx, client, f.Name(), fs, batteriesIncluded); err != nil {
51+
if err := CreateResourceFromFS(ctx, client, f.Name(), fs); err != nil {
5452
errs = append(errs, err)
5553
}
5654
}
5755
return apimachineryerrors.NewAggregate(errs)
5856
}
5957

6058
// CreateResourceFromFS creates given resource file.
61-
func CreateResourceFromFS(ctx context.Context, client client.Client, filename string, fs embed.FS, batteriesIncluded sets.Set[string]) error {
59+
func CreateResourceFromFS(ctx context.Context, client client.Client, filename string, fs embed.FS) error {
6260
raw, err := fs.ReadFile(filename)
6361
if err != nil {
6462
return fmt.Errorf("could not read %s: %w", filename, err)
@@ -81,17 +79,14 @@ func CreateResourceFromFS(ctx context.Context, client client.Client, filename st
8179
continue
8280
}
8381

84-
if err := createResourceFromFS(ctx, client, doc, batteriesIncluded); err != nil {
82+
if err := createResourceFromFS(ctx, client, doc); err != nil {
8583
errs = append(errs, fmt.Errorf("failed to create resource %s doc %d: %w", filename, i, err))
8684
}
8785
}
8886
return apimachineryerrors.NewAggregate(errs)
8987
}
9088

91-
const annotationCreateOnlyKey = "bootstrap.kcp.io/create-only"
92-
const annotationBattery = "bootstrap.kcp.io/battery"
93-
94-
func createResourceFromFS(ctx context.Context, client client.Client, raw []byte, batteriesIncluded sets.Set[string]) error {
89+
func createResourceFromFS(ctx context.Context, client client.Client, raw []byte) error {
9590
log := log.FromContext(ctx)
9691

9792
type Input struct {
@@ -100,9 +95,6 @@ func createResourceFromFS(ctx context.Context, client client.Client, raw []byte,
10095
input := Input{
10196
Batteries: map[string]bool{},
10297
}
103-
for _, b := range sets.List[string](batteriesIncluded) {
104-
input.Batteries[b] = true
105-
}
10698
tmpl, err := template.New("manifest").Parse(string(raw))
10799
if err != nil {
108100
return fmt.Errorf("failed to parse manifest: %w", err)
@@ -121,21 +113,6 @@ func createResourceFromFS(ctx context.Context, client client.Client, raw []byte,
121113
return fmt.Errorf("decoded into incorrect type, got %T, wanted %T", obj, &unstructured.Unstructured{})
122114
}
123115

124-
if v, found := u.GetAnnotations()[annotationBattery]; found {
125-
partOf := strings.Split(v, ",")
126-
included := false
127-
for _, p := range partOf {
128-
if batteriesIncluded.Has(strings.TrimSpace(p)) {
129-
included = true
130-
break
131-
}
132-
}
133-
if !included {
134-
log.V(4).WithValues("resource", u.GetName(), "batteriesRequired", v, "batteriesIncluded", batteriesIncluded).Info("skipping resource because required batteries are not among included batteries")
135-
return nil
136-
}
137-
}
138-
139116
key := types.NamespacedName{
140117
Namespace: u.GetNamespace(),
141118
Name: u.GetName(),
@@ -148,12 +125,6 @@ func createResourceFromFS(ctx context.Context, client client.Client, raw []byte,
148125
return err
149126
}
150127

151-
if _, exists := u.GetAnnotations()[annotationCreateOnlyKey]; exists {
152-
log.Info("skipping update of object because it has the create-only annotation")
153-
154-
return nil
155-
}
156-
157128
u.SetResourceVersion(u.GetResourceVersion())
158129
err = client.Update(ctx, u)
159130
if err != nil {

examples/kcp/config/main.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/kcp-dev/logicalcluster/v3"
2626
"k8s.io/apimachinery/pkg/runtime"
2727
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
28-
"k8s.io/apimachinery/pkg/util/sets"
2928
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3029
"k8s.io/client-go/rest"
3130
ctrl "sigs.k8s.io/controller-runtime"
@@ -75,7 +74,6 @@ func main() {
7574

7675
ctx := ctrl.SetupSignalHandler()
7776
log := log.FromContext(ctx)
78-
fakeBatteries := sets.New("")
7977

8078
restConfig, err := config.GetConfigWithContext("base")
8179
if err != nil {
@@ -100,17 +98,17 @@ func main() {
10098
log.Error(err, "unable to create client")
10199
}
102100

103-
err = widgets.Bootstrap(ctx, clientRoot, fakeBatteries)
101+
err = widgets.Bootstrap(ctx, clientRoot)
104102
if err != nil {
105103
log.Error(err, "failed to bootstrap widgets")
106104
}
107105

108-
err = resources.Bootstrap(ctx, clientWidgets, fakeBatteries)
106+
err = resources.Bootstrap(ctx, clientWidgets)
109107
if err != nil {
110108
log.Error(err, "failed to bootstrap resources")
111109
}
112110

113-
err = consumers.Bootstrap(ctx, clientRoot, fakeBatteries)
111+
err = consumers.Bootstrap(ctx, clientRoot)
114112
if err != nil {
115113
log.Error(err, "failed to bootstrap consumers")
116114
}

examples/kcp/config/widgets/bootstrap.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"embed"
2222

23-
"k8s.io/apimachinery/pkg/util/sets"
2423
"sigs.k8s.io/controller-runtime/pkg/client"
2524
"sigs.k8s.io/controller-runtime/pkg/log"
2625

@@ -36,10 +35,9 @@ var fs embed.FS
3635
func Bootstrap(
3736
ctx context.Context,
3837
client client.Client,
39-
batteriesIncluded sets.Set[string],
4038
) error {
4139
log := log.FromContext(ctx)
4240

4341
log.Info("Bootstrapping widgets workspace")
44-
return confighelpers.Bootstrap(ctx, client, fs, batteriesIncluded)
42+
return confighelpers.Bootstrap(ctx, client, fs)
4543
}

examples/kcp/config/widgets/resources/bootstrap.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"embed"
2222

23-
"k8s.io/apimachinery/pkg/util/sets"
2423
"sigs.k8s.io/controller-runtime/pkg/client"
2524
"sigs.k8s.io/controller-runtime/pkg/log"
2625

@@ -36,12 +35,11 @@ var fs embed.FS
3635
func Bootstrap(
3736
ctx context.Context,
3837
client client.Client,
39-
batteriesIncluded sets.Set[string],
4038
) error {
4139
log := log.FromContext(ctx)
4240

4341
log.Info("Bootstrapping widgets resources")
44-
if err := confighelpers.Bootstrap(ctx, client, fs, batteriesIncluded); err != nil {
42+
if err := confighelpers.Bootstrap(ctx, client, fs); err != nil {
4543
return err
4644
}
4745

examples/kcp/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/kcp-dev/controller-runtime/examples/kcp
33
go 1.21.8
44

55
// IMPORTANT: This is only an example replace directive. This is so examples can be run with the latest version of controller-runtime.
6-
// In your own projects, you should not use replace directives like this. Instead, you should use the latest version of controller-runtime.
6+
// In your own projects, you should not use replace directives like this. Instead, you should replace, but with kcp-dev/controller-runtime instead of ../../
77
replace sigs.k8s.io/controller-runtime => ../../
88

99
require (

0 commit comments

Comments
 (0)