Skip to content

Commit

Permalink
fix (performance): reduce installation check time consumption (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
l-qing authored Sep 4, 2024
1 parent 8549b14 commit 3a345d4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 5 deletions.
35 changes: 30 additions & 5 deletions check/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package check

import (
"context"
"time"

pkgclient "github.com/katanomi/pkg/client"
pipev1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"knative.dev/pkg/logging"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)

// InstalledTekton Check if tekton is installed
Expand All @@ -37,20 +40,42 @@ func InstalledTekton(ctx context.Context, clt client.Client) bool {
// Even if the resource does not exist, it's fine.
// Although the list operation won't fail, an error will still occur when importing resources.
// It also won't trigger the issue where client-go's cache fails to list the Task resource.
installed := InstallCheck(ctx, clt, &pipev1beta1.TaskList{}, &client.ListOptions{})
installed := InstallationCheck(ctx, clt, &pipev1beta1.TaskList{}, &client.ListOptions{})
if !installed {
return false
}

// If the PipelineRun does not exist, it is likely that the TaskRun does not exist either.
return InstallCheck(ctx, clt, &pipev1beta1.PipelineRunList{}, &client.ListOptions{})
return InstallationCheck(ctx, clt, &pipev1beta1.PipelineRunList{}, &client.ListOptions{})
}

// InstallCheck common check component installed method.
func InstallCheck(ctx context.Context, clt client.Client, value client.ObjectList, opts ...client.ListOption) bool {
// InstallationCheck common check component installed method.
func InstallationCheck(ctx context.Context, clt client.Client, value client.ObjectList, opts ...client.ListOption) bool {
log := logging.FromContext(ctx)

defer func(startTime time.Time) {
log.Debugw("Installation check", "gvk", value.GetObjectKind().GroupVersionKind(), "UsedTime", time.Since(startTime))
}(time.Now())

// We only need to check the first one to determine whether the crd exists and the component is ready.
// If set `resourceVersion` to 0, it will ignore the `limit` option.
opts = append(opts, pkgclient.NewListOptions().WithLimit(1).WithUnsafeDisableDeepCopy().Build())

if err := clt.List(ctx, value, opts...); err != nil {
log.Debugw("list operation failed", "err", err)
log.Infow("list operation failed", "err", err)
return false
}

if prList, ok := value.(*pipev1beta1.PipelineRunList); ok && prList != nil {
log.Debugw("list PipelineRun success", "count", len(prList.Items))
}

// Set the group version kind to the object, otherwise the log will not show the GVK.
gvk, err := apiutil.GVKForObject(value, clt.Scheme())
if err != nil {
log.Errorw("get GVK failed", "err", err)
}
value.GetObjectKind().SetGroupVersionKind(gvk)

return true
}
61 changes: 61 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,64 @@ func (opt *GetOptions) Build() *client.GetOptions {
func GetCachedOptions() *client.GetOptions {
return NewGetOptions().WithCached().Build()
}

// ListOptions is a wrapper for client.ListOptions
type ListOptions struct {
client.ListOptions
}

// NewListOptions returns a new ListOptions
func NewListOptions() *ListOptions {
return &ListOptions{}
}

// init returns a new ListOptions if opt is nil or opt.Raw is nil
func (opt *ListOptions) init() *ListOptions {
if opt != nil && opt.ListOptions.Raw != nil {
return opt
}
return &ListOptions{
ListOptions: client.ListOptions{
Raw: &metav1.ListOptions{},
},
}
}

// WithCached set the ResourceVersion to 0
func (opt *ListOptions) WithCached() *ListOptions {
opt = opt.init()
opt.Raw.ResourceVersion = "0"
return opt
}

// WithLimit set the limit
func (opt *ListOptions) WithLimit(limit int64) *ListOptions {
opt = opt.init()
opt.Limit = limit
return opt
}

// WithNamespace set the namespace
func (opt *ListOptions) WithNamespace(namespace string) *ListOptions {
opt = opt.init()
opt.Namespace = namespace
return opt
}

// WithUnsafeDisableDeepCopy set the UnsafeDisableDeepCopy to true
func (opt *ListOptions) WithUnsafeDisableDeepCopy() *ListOptions {
opt = opt.init()
True := true
opt.UnsafeDisableDeepCopy = &True
return opt
}

// Build returns the client.ListOptions
func (opt *ListOptions) Build() *client.ListOptions {
return &opt.ListOptions
}

// ListCachedOptions returns ListOptions with ResourceVersion set to 0
func ListCachedOptions() *client.ListOptions {
return NewListOptions().WithCached().Build()
}
74 changes: 74 additions & 0 deletions client/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,77 @@ var _ = Describe("GetCachedOptions", func() {
Expect(options.Raw).To(Equal(&metav1.GetOptions{ResourceVersion: "0"}))
})
})

var _ = Describe("Test.ListOptions", func() {
var (
opts *ListOptions
)

BeforeEach(func() {
opts = NewListOptions()
})

Describe("init", func() {
Context("when ListOptions is not nil and Raw is not nil", func() {
It("should return the input ListOptions", func() {
raw := &metav1.ListOptions{}
opt := &ListOptions{
ListOptions: client.ListOptions{
Raw: raw,
},
}
Expect(opt.init()).To(Equal(opt))
})
})

Context("when ListOptions is nil or Raw is nil", func() {
It("should return a new ListOptions with Raw set to a new instance of metav1.ListOptions", func() {
Expect(opts.init()).To(Equal(&ListOptions{
ListOptions: client.ListOptions{
Raw: &metav1.ListOptions{},
},
}))
})
})
})

Describe("WithCached", func() {
It("should set the ResourceVersion to '0'", func() {
Expect(opts.WithCached().Build().Raw.ResourceVersion).To(Equal("0"))
})
})

Describe("WithLimit", func() {
It("should set the Limit", func() {
Expect(opts.WithLimit(10).Build().Limit).To(Equal(int64(10)))
})
})

Describe("WithNamespace", func() {
It("should set the Namespace", func() {
Expect(opts.WithNamespace("NS").Build().Namespace).To(Equal("NS"))
})
})

Describe("WithUnsafeDisableDeepCopy", func() {
It("should set the UnsafeDisableDeepCopy to true", func() {
Expect(opts.WithUnsafeDisableDeepCopy().Build().UnsafeDisableDeepCopy).NotTo(BeNil())
Expect(*opts.WithUnsafeDisableDeepCopy().Build().UnsafeDisableDeepCopy).To(BeTrue())
})
})

Describe("Build", func() {
It("should return a pointer to the underlying client.ListOptions", func() {
Expect(opts.Build()).To(BeIdenticalTo(&opts.ListOptions))
})
})

})

var _ = Describe("ListCachedOptions", func() {
It("should return client.ListOptions with ResourceVersion 0", func() {
options := ListCachedOptions()
Expect(options).ToNot(BeNil())
Expect(options.Raw).To(Equal(&metav1.ListOptions{ResourceVersion: "0"}))
})
})

0 comments on commit 3a345d4

Please sign in to comment.