Skip to content

Commit 4a78b72

Browse files
committed
pre-upgrade
1 parent 44248fd commit 4a78b72

29 files changed

Lines changed: 122 additions & 58 deletions

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ update-tools: clean-tools install-golangci-lint install-gotestsum install-boiler
191191
.PHONY: lint
192192
lint: install-golangci-lint install-logcheck ## Run golangci-lint
193193
@if [ -n "$(WHAT)" ]; then \
194-
$(GOLANGCI_LINT) run $(GOLANGCI_LINT_FLAGS) -c $(ROOT_DIR)/.golangci.yaml --timeout 20m $(WHAT); \
194+
$(GOLANGCI_LINT) run $(GOLANGCI_LINT_FLAGS) -c $(ROOT_DIR)/.golangci.yaml --timeout 20m $(WHAT) --fix; \
195195
else \
196196
for MOD in $(GOMODS); do \
197-
(cd $$MOD; echo "Linting $$MOD"; $(GOLANGCI_LINT) run $(GOLANGCI_LINT_FLAGS) -c $(ROOT_DIR)/.golangci.yaml --timeout 20m); \
197+
(cd $$MOD; echo "Linting $$MOD"; $(GOLANGCI_LINT) run $(GOLANGCI_LINT_FLAGS) -c $(ROOT_DIR)/.golangci.yaml --timeout 20m --fix); \
198198
done; \
199199
fi
200200

backend/auth/middleware.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,21 @@ func (am *AuthMiddleware) verifyState(next http.Handler) http.Handler {
179179

180180
state := authCtx.SessionState
181181
// Validate session fields are present
182-
if state.Token.Subject == "" || state.Token.Issuer == "" || state.SessionID == "" {
182+
switch {
183+
case state.Token.Subject == "" || state.Token.Issuer == "" || state.SessionID == "":
183184
logger.V(2).Info("Invalid session state: missing required fields")
184-
} else if state.IsExpired() {
185+
writeErrorResponse(w, http.StatusUnauthorized, kubebindv1alpha2.ErrorCodeAuthenticationFailed, "Authentication required", "Invalid session state: missing required fields")
186+
return
187+
case state.IsExpired():
185188
logger.V(2).Info("Session expired", "sessionID", state.SessionID)
186-
} else if !am.isValidSession(state.SessionID) {
189+
writeErrorResponse(w, http.StatusUnauthorized, kubebindv1alpha2.ErrorCodeAuthenticationFailed, "Authentication required", "Session has expired")
190+
return
191+
case !am.isValidSession(state.SessionID):
187192
logger.V(2).Info("Session ID not found or expired", "sessionID", state.SessionID)
188-
} else {
193+
writeErrorResponse(w, http.StatusUnauthorized, kubebindv1alpha2.ErrorCodeAuthenticationFailed, "Authentication required", "Session ID not found or expired")
194+
return
195+
default:
196+
// Session is valid
189197
authCtx.IsValid = true
190198
}
191199
ctx := context.WithValue(r.Context(), AuthContextKey, authCtx)

backend/controllers/cluster/cluster_reconcile.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package cluster
1919
import (
2020
"context"
2121

22-
"github.com/davecgh/go-spew/spew"
2322
rbacv1 "k8s.io/api/rbac/v1"
2423
"k8s.io/apimachinery/pkg/api/errors"
2524
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -39,8 +38,6 @@ type reconciler struct {
3938
func (r *reconciler) reconcile(ctx context.Context, client client.Client, _ cache.Cache, cluster *kubebindv1alpha2.Cluster) error {
4039
var errs []error
4140

42-
spew.Dump("Reconciling cluster:", cluster.Name)
43-
4441
if r.isEmbeddedOIDC {
4542
if err := r.ensureEmbeddedOIDCRBAC(ctx, client, cluster); err != nil {
4643
errs = append(errs, err)
@@ -54,12 +51,7 @@ func (r *reconciler) ensureEmbeddedOIDCRBAC(ctx context.Context, client client.C
5451
if err := r.ensureEmbeddedUserClusterRole(ctx, client, cluster); err != nil {
5552
return err
5653
}
57-
58-
if err := r.ensureEmbeddedUserClusterRoleBinding(ctx, client, cluster); err != nil {
59-
return err
60-
}
61-
62-
return nil
54+
return r.ensureEmbeddedUserClusterRoleBinding(ctx, client, cluster)
6355
}
6456

6557
func (r *reconciler) ensureEmbeddedUserClusterRole(ctx context.Context, client client.Client, cluster *kubebindv1alpha2.Cluster) error {

backend/kubernetes/manager.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,21 @@ func (m *Manager) AuthorizeRequest(ctx context.Context, subject, cluster, method
290290
}
291291
return nil
292292
}
293+
294+
func (m *Manager) SeedDefaultCluster(ctx context.Context) error {
295+
logger := klog.FromContext(ctx)
296+
297+
cl, err := m.manager.GetCluster(ctx, "")
298+
if err != nil {
299+
return err
300+
}
301+
c := cl.GetClient()
302+
303+
_, err = kuberesources.CreateDefaultCluster(ctx, c)
304+
if err != nil && !errors.IsAlreadyExists(err) {
305+
logger.Error(err, "Failed to create default Cluster resource")
306+
return err
307+
}
308+
logger.Info("Default Cluster resource ensured")
309+
return nil
310+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2025 The Kube Bind 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 resources
18+
19+
import (
20+
"context"
21+
22+
apierrors "k8s.io/apimachinery/pkg/api/errors"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
"sigs.k8s.io/controller-runtime/pkg/client"
25+
26+
kubebindv1alpha2 "github.com/kube-bind/kube-bind/sdk/apis/kubebind/v1alpha2"
27+
)
28+
29+
func CreateDefaultCluster(ctx context.Context, c client.Client) (*kubebindv1alpha2.Cluster, error) {
30+
cluster := &kubebindv1alpha2.Cluster{
31+
ObjectMeta: metav1.ObjectMeta{
32+
Name: kubebindv1alpha2.DefaultClusterName,
33+
},
34+
Spec: kubebindv1alpha2.ClusterSpec{},
35+
}
36+
37+
err := c.Create(ctx, cluster)
38+
if err != nil && !apierrors.IsAlreadyExists(err) {
39+
return nil, err
40+
}
41+
42+
return cluster, nil
43+
}

backend/options/options.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,14 @@ func (options *Options) Complete() (*CompletedOptions, error) {
224224
},
225225
}
226226

227-
switch options.Provider {
228-
case "kcp":
227+
if options.Provider == "kcp" {
229228
opts, err := options.ProviderKcp.Complete()
230229
if err != nil {
231230
return nil, err
232231
}
233232
co.completedOptions.ProviderKcp = opts
234233
}
235234
return co, nil
236-
237235
}
238236

239237
func (options *CompletedOptions) Validate() error {

backend/provider/kcp/options/options.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ type CompletedOptions struct {
3737
}
3838

3939
func NewOptions() *Options {
40-
4140
return &Options{
4241
ExtraOptions: ExtraOptions{
4342
APIExportEndpointSliceName: "kube-bind.io",
@@ -47,7 +46,6 @@ func NewOptions() *Options {
4746

4847
func (options *Options) AddFlags(fs *pflag.FlagSet) {
4948
fs.StringVar(&options.ExtraOptions.APIExportEndpointSliceName, "api-export-endpoint-slice-name", options.ExtraOptions.APIExportEndpointSliceName, "name of the APIExport EndpointSlice to watch")
50-
5149
}
5250

5351
func (options *Options) Complete() (*CompletedOptions, error) {

backend/provider/kcp/provider.go

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

22-
"github.com/davecgh/go-spew/spew"
2322
"github.com/kcp-dev/logicalcluster/v3"
2423
provider "github.com/kcp-dev/multicluster-provider/apiexport"
2524
"github.com/kcp-dev/multicluster-provider/pkg/handlers"
@@ -116,15 +115,10 @@ func (a *awareWrapper) Engage(ctx context.Context, name string, cluster cluster.
116115
Spec: kubebindv1alpha2.ClusterSpec{},
117116
}
118117

119-
// TODO: Check if embedded and pre-create rbac
120-
// TODO: add group support
121-
122118
err := cl.Create(ctx, obj)
123119
if err != nil && !apierrors.IsAlreadyExists(err) {
124120
return err
125121
}
126-
spew.Dump("Created kube-bind Cluster object in logical cluster", logicalcluster.From(obj))
127-
128122
return a.Aware.Engage(ctx, name, cluster)
129123
}
130124

backend/server.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,25 @@ func (s *Server) Run(ctx context.Context) error {
283283
}
284284
}()
285285

286+
go func() {
287+
// When using native k8s provider/singelton cluster - provider is set to nil
288+
// so we need to seed the default cluster. If provider is set - it is provider responsibility
289+
// to seed the default cluster in each managed cluster either via provider machinery or via provider
290+
// wrapper (see kcp example).
291+
if s.Config.Provider == nil {
292+
if err := s.seedCluster(ctx); err != nil {
293+
logger.Error(err, "Failed to seed default cluster")
294+
}
295+
}
296+
}()
297+
286298
go func() {
287299
<-ctx.Done()
288300
logger.Info("Context done")
289301
}()
290302
return s.WebServer.Start(ctx)
291303
}
304+
305+
func (s *Server) seedCluster(ctx context.Context) error {
306+
return s.Kubernetes.SeedDefaultCluster(ctx)
307+
}

contrib/kcp/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ export UGET_DIRECTORY := $(ROOT_DIR)/hack/tools
1818
export UGET_CHECKSUMS := $(ROOT_DIR)/hack/tools.checksums
1919
export UGET_VERSIONED_BINARIES = true
2020

21-
KCP_APIGEN_VERSION := v0.28.0
21+
KCP_APIGEN_VERSION := 11364df3071c021e96b4ed8c48aa9cd63e8f0115
2222

2323
.PHONY: all
2424
all: build
2525

2626
.PHONY: install-apigen
2727
install-apigen:
28-
@GO_MODULE=true $(ROOT_DIR)/hack/uget.sh github.com/kcp-dev/kcp/sdk/cmd/apigen apigen $(KCP_APIGEN_VERSION)
28+
@GO_MODULE=true $(ROOT_DIR)/hack/uget.sh github.com/kcp-dev/sdk/cmd/apigen apigen $(KCP_APIGEN_VERSION)
2929

3030
codegen: ## Generate KCP API resources from CRDs
3131
./hack/update-kcp-codegen.sh

0 commit comments

Comments
 (0)