Skip to content

Commit

Permalink
feat: update k8s to 1.29
Browse files Browse the repository at this point in the history
Signed-off-by: phantomnat <[email protected]>
  • Loading branch information
phantomnat committed Nov 21, 2024
1 parent eaacca6 commit 763908c
Show file tree
Hide file tree
Showing 31 changed files with 430 additions and 599 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ jobs:
- name: install Kubebuilder
uses: RyanSiu1995/[email protected]
with:
version: 3.1.0
version: 3.15.1
kubebuilderOnly: false
kubernetesVersion: v1.22.0
kubernetesVersion: v1.29.0


- name: Run unit-test
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@
.vscode
.history

**apiserver.local.config/
**apiserver.local.config/
bin/
coverage.txt
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)
ENVTEST ?= $(LOCALBIN)/setup-envtest
ENVTEST_K8S_VERSION = 1.29.0

generate:
go generate ./apis/...

Expand All @@ -10,10 +16,15 @@ vet:
tidy:
go mod tidy

unit-test:
go test -v -coverpkg=./... -coverprofile=coverage.txt ./...
unit-test: envtest
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -v -coverpkg=./... -coverprofile=coverage.txt ./...

lint:
golangci-lint run ./...

reviewable: generate fmt vet tidy lint

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
1 change: 0 additions & 1 deletion apis/cue/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion apis/oam/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions controller/client/controller_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import (

"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

"github.com/kubevela/pkg/multicluster"
)
Expand All @@ -34,20 +32,22 @@ var (
CachedGVKs = ""
)

var _ client.NewClientFunc = DefaultNewControllerClient

// DefaultNewControllerClient function for creating controller client
func DefaultNewControllerClient(cache cache.Cache, config *rest.Config, options client.Options, uncachedObjects ...client.Object) (c client.Client, err error) {
func DefaultNewControllerClient(config *rest.Config, options client.Options) (c client.Client, err error) {
rawClient, err := multicluster.NewDefaultClient(config, options)
if err != nil {
return nil, fmt.Errorf("failed to get raw client: %w", err)
}
rawClient = WrapDefaultTimeoutClient(rawClient)

mClient := &monitorClient{rawClient}
mCache := &monitorCache{cache}
mCache := &monitorCache{options.Cache.Reader}

uncachedStructuredGVKs := map[schema.GroupVersionKind]struct{}{}
for _, obj := range uncachedObjects {
gvk, err := apiutil.GVKForObject(obj, mClient.Scheme())
for _, obj := range options.Cache.DisableFor {
gvk, err := mClient.GroupVersionKindFor(obj)
if err != nil {
return nil, err
}
Expand All @@ -69,6 +69,7 @@ func DefaultNewControllerClient(cache cache.Cache, config *rest.Config, options
dClient := &delegatingClient{
scheme: mClient.Scheme(),
mapper: mClient.RESTMapper(),
client: mClient,
Reader: &delegatingReader{
CacheReader: mCache,
ClientReader: mClient,
Expand Down
11 changes: 11 additions & 0 deletions controller/client/delegating_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type delegatingClient struct {

scheme *runtime.Scheme
mapper meta.RESTMapper
client client.Client
}

// Scheme returns the scheme this client is using.
Expand All @@ -52,6 +53,16 @@ func (d *delegatingClient) RESTMapper() meta.RESTMapper {
return d.mapper
}

// RESTMapper returns the rest mapper this client is using.
func (d *delegatingClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
return d.client.GroupVersionKindFor(obj)
}

// RESTMapper returns the rest mapper this client is using.
func (d *delegatingClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
return d.client.IsObjectNamespaced(obj)
}

// delegatingReader extend the delegatingReader from controller-runtime/pkg/client
// 1. for requests not in local cluster, disable cache
// 2. for structured types, inherit the cache blacklist
Expand Down
23 changes: 23 additions & 0 deletions controller/client/delegating_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import (
"testing"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

testobj "github.com/kubevela/pkg/util/test/object"
)
Expand All @@ -32,3 +36,22 @@ func TestDelegatingReaderSchemeErr(t *testing.T) {
require.Error(t, dr.Get(context.Background(), client.ObjectKey{}, &testobj.UnknownObject{}))
require.Error(t, dr.List(context.Background(), &testobj.UnknownObjectList{}))
}

func TestDelegatingClient(t *testing.T) {
mapper := meta.NewDefaultRESTMapper([]schema.GroupVersion{
{Group: "", Version: "v1"},
})
mapper.Add(schema.GroupVersionKind{Group: "", Version: "v1", Kind: "ConfigMap"}, meta.RESTScopeNamespace)
c := fake.NewClientBuilder().
WithRESTMapper(mapper).
Build()
_client := &delegatingClient{
client: c,
}
ok, err := _client.IsObjectNamespaced(&corev1.ConfigMap{})
require.NoError(t, err)
require.True(t, ok)
gvk, err := _client.GroupVersionKindFor(&corev1.ConfigMap{})
require.NoError(t, err)
require.Equal(t, schema.GroupVersionKind{Kind: "ConfigMap", Version: "v1"}, gvk)
}
7 changes: 3 additions & 4 deletions controller/client/monitor_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/prometheus/client_golang/prometheus"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"

Expand Down Expand Up @@ -76,19 +75,19 @@ func monitor(ctx context.Context, verb string, obj runtime.Object) func() {

// monitorCache records time costs in metrics when execute function calls
type monitorCache struct {
cache.Cache
client.Reader
}

func (c *monitorCache) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
cb := monitor(ctx, "GetCache", obj)
defer cb()
return c.Cache.Get(ctx, key, obj, opts...)
return c.Reader.Get(ctx, key, obj, opts...)
}

func (c *monitorCache) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
cb := monitor(ctx, "ListCache", list)
defer cb()
return c.Cache.List(ctx, list, opts...)
return c.Reader.List(ctx, list, opts...)
}

// monitorClient records time costs in metrics when execute function calls
Expand Down
9 changes: 8 additions & 1 deletion controller/client/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package client_test
import (
"context"
"testing"
"time"

corev1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -56,8 +57,14 @@ var _ = Describe("Test clients", func() {
Ω(_cache.Start(ctx)).To(Succeed())
}()

Eventually(func() bool { return _cache.WaitForCacheSync(ctx) }).WithTimeout(time.Second).Should(BeTrue(), "cache should be synced within a second")
velaclient.CachedGVKs = "Deployment.apps.v1"
_client, err := velaclient.DefaultNewControllerClient(_cache, cfg, client.Options{}, &corev1.Secret{})
_client, err := velaclient.DefaultNewControllerClient(cfg, client.Options{
Cache: &client.CacheOptions{
Reader: _cache,
DisableFor: []client.Object{&corev1.Secret{}},
},
})
Ω(err).To(Succeed())
tester.TestClientFunctions(_client)
obj := &unstructured.Unstructured{Object: map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion controller/reconciler/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = Describe("Test clients", func() {

const path = "/trigger"
ch := reconciler.RegisterTriggerHandler(mgr, path, 1024)
svr := httptest.NewServer(mgr.GetWebhookServer().WebhookMux)
svr := httptest.NewServer(mgr.GetWebhookServer().WebhookMux())
defer svr.Close()

resp, err := http.Get(svr.URL + path)
Expand Down
26 changes: 14 additions & 12 deletions controller/sharding/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,28 @@ package sharding

import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// BuildCache add shard-id label selector for given typed object
func BuildCache(scheme *runtime.Scheme, shardingObjects ...client.Object) cache.NewCacheFunc {
return BuildCacheWithOptions(cache.Options{Scheme: scheme}, shardingObjects...)
func BuildCache(shardingObjects ...client.Object) cache.NewCacheFunc {
return BuildCacheWithOptions(shardingObjects...)
}

// BuildCacheWithOptions add shard-id label selector to sharding objects with options
func BuildCacheWithOptions(opts cache.Options, shardingObjects ...client.Object) cache.NewCacheFunc {
if EnableSharding {
ls := labels.SelectorFromSet(map[string]string{LabelKubeVelaScheduledShardID: ShardID})
if opts.SelectorsByObject == nil {
opts.SelectorsByObject = map[client.Object]cache.ObjectSelector{}
}
for _, obj := range shardingObjects {
opts.SelectorsByObject[obj] = cache.ObjectSelector{Label: ls}
func BuildCacheWithOptions(shardingObjects ...client.Object) cache.NewCacheFunc {
return func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
if EnableSharding {
ls := labels.SelectorFromSet(map[string]string{LabelKubeVelaScheduledShardID: ShardID})
if opts.ByObject == nil {
opts.ByObject = map[client.Object]cache.ByObject{}
}
for _, obj := range shardingObjects {
opts.ByObject[obj] = cache.ByObject{Label: ls}
}
}
return cache.New(config, opts)
}
return cache.BuilderWithOptions(opts)
}
2 changes: 1 addition & 1 deletion controller/sharding/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ var _ = Describe("Test sharding", func() {
Ω(cli.Create(ctx, cm2)).To(Succeed())

By("Test cache")
store, err := sharding.BuildCache(scheme.Scheme, &corev1.ConfigMap{})(cfg, cache.Options{})
store, err := sharding.BuildCache(&corev1.ConfigMap{})(cfg, cache.Options{Scheme: scheme.Scheme})
Ω(err).To(Succeed())
go func() { _ = store.Start(ctx) }()
Eventually(func(g Gomega) {
Expand Down
27 changes: 15 additions & 12 deletions crds/core.oam.dev_definitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.3
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.14.0
name: definitions.core.oam.dev
spec:
group: core.oam.dev
Expand All @@ -24,20 +23,24 @@ spec:
name: v1alpha1
schema:
openAPIV3Schema:
description: Definition is a internal storage for KubeVela definitions, it
will never be exposed directly to end users. It will just like a configmap
as internal usage, using a standalone CRD can help us optimize the efficiency
for informer.
description: |-
Definition is a internal storage for KubeVela definitions, it will never be exposed directly to end users.
It will just like a configmap as internal usage, using a standalone CRD can help us optimize the efficiency for informer.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
Expand Down
20 changes: 12 additions & 8 deletions crds/cue.oam.dev_packages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.3
creationTimestamp: null
controller-gen.kubebuilder.io/version: v0.14.0
name: packages.cue.oam.dev
spec:
group: cue.oam.dev
Expand Down Expand Up @@ -36,14 +35,19 @@ spec:
description: Package is an extension for cuex engine
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
Expand Down
Loading

0 comments on commit 763908c

Please sign in to comment.