Skip to content

Commit

Permalink
feat: add executor and webhook listers and informers [TKC-631] (#205)
Browse files Browse the repository at this point in the history
* feat: add listeners and informers

* feat: lister and informer for test source

* fix: improve resources in groupversion for executor group

* fix: fix permission error while testing locally
  • Loading branch information
vLia authored Dec 6, 2023
1 parent 2fc74ce commit 83394de
Show file tree
Hide file tree
Showing 33 changed files with 1,732 additions and 13 deletions.
20 changes: 19 additions & 1 deletion api/executor/v1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,26 @@ import (
)

var (
// Group represents the API Group
Group = "executor.testkube.io"

// Version represents the Resource version
Version = "v1"

// ExecutorResource corresponds to the CRD Kind
ExecutorResource = "Executor"

// WebhookResource corresponds to the CRD Kind
WebhookResource = "Webhook"

// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "executor.testkube.io", Version: "v1"}
GroupVersion = schema.GroupVersion{Group: Group, Version: Version}

// ExecutorGroupVersionResource is group, version and resource used to register these objects
ExecutorGroupVersionResource = schema.GroupVersionResource{Group: Group, Version: Version, Resource: ExecutorResource}

// WebhookGroupVersionResource is group, version and resource used to register these objects
WebhookGroupVersionResource = schema.GroupVersionResource{Group: Group, Version: Version, Resource: WebhookResource}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
Expand Down
14 changes: 13 additions & 1 deletion api/testsource/v1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,20 @@ import (
)

var (
// Group represents the API Group
Group = "tests.testkube.io"

// Version represents the Resource version
Version = "v1"

// Resource corresponds to the CRD Kind
Resource = "TestSource"

// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "tests.testkube.io", Version: "v1"}
GroupVersion = schema.GroupVersion{Group: Group, Version: Version}

// GroupVersionResource is group, version and resource used to register these objects
GroupVersionResource = schema.GroupVersionResource{Group: Group, Version: Version, Resource: Resource}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
Expand Down
20 changes: 17 additions & 3 deletions pkg/clientset/versioned/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/http"

executorv1 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/executor/v1"
v1 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v1"
v2 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v2"
v3 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v3"
Expand All @@ -34,14 +35,16 @@ type Interface interface {
TestsV1() v1.TestsV1Interface
TestsV2() v2.TestsV2Interface
TestsV3() v3.TestsV3Interface
ExecutorV1() executorv1.ExecutorV1Interface
}

// Clientset contains the clients for groups. Each group has exactly one version included in a Clientset.
type Clientset struct {
*discovery.DiscoveryClient
testsV1 *v1.TestsV1Client
testsV2 *v2.TestsV2Client
testsV3 *v3.TestsV3Client
testsV1 *v1.TestsV1Client
testsV2 *v2.TestsV2Client
testsV3 *v3.TestsV3Client
executorV1 *executorv1.ExecutorV1Client
}

// TestsV1 retrieves the TestsV1Client
Expand All @@ -59,6 +62,11 @@ func (c *Clientset) TestsV3() v3.TestsV3Interface {
return c.testsV3
}

// ExecutorV1 retrieves the ExecutorV1Client
func (c *Clientset) ExecutorV1() executorv1.ExecutorV1Interface {
return c.executorV1
}

// Discovery retrieves the DiscoveryClient
func (c *Clientset) Discovery() discovery.DiscoveryInterface {
if c == nil {
Expand Down Expand Up @@ -118,6 +126,11 @@ func NewForConfigAndClient(c *rest.Config, httpClient *http.Client) (*Clientset,
return nil, err
}

cs.executorV1, err = executorv1.NewForConfigAndClient(&configShallowCopy, httpClient)
if err != nil {
return nil, err
}

cs.DiscoveryClient, err = discovery.NewDiscoveryClientForConfigAndClient(&configShallowCopy, httpClient)
if err != nil {
return nil, err
Expand All @@ -141,6 +154,7 @@ func New(c rest.Interface) *Clientset {
cs.testsV1 = v1.New(c)
cs.testsV2 = v2.New(c)
cs.testsV3 = v3.New(c)
cs.executorV1 = executorv1.New(c)

cs.DiscoveryClient = discovery.NewDiscoveryClient(c)
return &cs
Expand Down
15 changes: 11 additions & 4 deletions pkg/clientset/versioned/fake/clientset_generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package fake

import (
"github.com/kubeshop/testkube-operator/pkg/clientset/versioned"
v1 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v1"
fakev1 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v1/fake"
executorv1 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/executor/v1"
fakeexecutorv1 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/executor/v1/fake"
testsv1 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v1"
faketestsv1 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v1/fake"
v2 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v2"
fakev2 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v2/fake"
v3 "github.com/kubeshop/testkube-operator/pkg/clientset/versioned/typed/tests/v3"
Expand Down Expand Up @@ -82,8 +84,8 @@ var (
)

// TestsV1 retrieves the TestsV1Client
func (c *Clientset) TestsV1() v1.TestsV1Interface {
return &fakev1.FakeTestsV1{Fake: &c.Fake}
func (c *Clientset) TestsV1() testsv1.TestsV1Interface {
return &faketestsv1.FakeTestsV1{Fake: &c.Fake}
}

// TestsV2 retrieves the TestsV2Client
Expand All @@ -95,3 +97,8 @@ func (c *Clientset) TestsV2() v2.TestsV2Interface {
func (c *Clientset) TestsV3() v3.TestsV3Interface {
return &fakev3.FakeTestsV3{Fake: &c.Fake}
}

// ExecutorV1 retrieves the ExecutorV1Client
func (c *Clientset) ExecutorV1() executorv1.ExecutorV1Interface {
return &fakeexecutorv1.FakeExecutorV1{Fake: &c.Fake}
}
2 changes: 2 additions & 0 deletions pkg/clientset/versioned/fake/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package fake

import (
executorv1 "github.com/kubeshop/testkube-operator/api/executor/v1"
testsv3 "github.com/kubeshop/testkube-operator/api/tests/v3"
testsuitev3 "github.com/kubeshop/testkube-operator/api/testsuite/v3"
testtriggersv1 "github.com/kubeshop/testkube-operator/api/testtriggers/v1"
Expand All @@ -34,6 +35,7 @@ var localSchemeBuilder = runtime.SchemeBuilder{
testtriggersv1.AddToScheme,
testsuitev3.AddToScheme,
testsv3.AddToScheme,
executorv1.AddToScheme,
}

// AddToScheme adds all types of this clientset into the given scheme. This allows composition
Expand Down
2 changes: 2 additions & 0 deletions pkg/clientset/versioned/scheme/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package scheme

import (
executorv1 "github.com/kubeshop/testkube-operator/api/executor/v1"
testsv3 "github.com/kubeshop/testkube-operator/api/tests/v3"
testsuitev3 "github.com/kubeshop/testkube-operator/api/testsuite/v3"
testtriggersv1 "github.com/kubeshop/testkube-operator/api/testtriggers/v1"
Expand All @@ -34,6 +35,7 @@ var localSchemeBuilder = runtime.SchemeBuilder{
testtriggersv1.AddToScheme,
testsuitev3.AddToScheme,
testsv3.AddToScheme,
executorv1.AddToScheme,
}

// AddToScheme adds all types of this clientset into the given scheme. This allows composition
Expand Down
17 changes: 17 additions & 0 deletions pkg/clientset/versioned/typed/executor/v1/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1
88 changes: 88 additions & 0 deletions pkg/clientset/versioned/typed/executor/v1/executor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"context"
"time"

executorv1 "github.com/kubeshop/testkube-operator/api/executor/v1"
"github.com/kubeshop/testkube-operator/pkg/clientset/versioned/scheme"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/rest"
)

// ExecutorGetter has a method to return a ExecutorInterface.
// A group's client should implement this interface.
type ExecutorGetter interface {
Executor(namespace string) ExecutorInterface
}

// ExecutorInterface has methods to work with Executor resources.
type ExecutorInterface interface {
List(ctx context.Context, opts v1.ListOptions) (*executorv1.ExecutorList, error)
Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error)
ExecutorExpansion
}

// executors implements ExecutorInterface
type executors struct {
client rest.Interface
ns string
}

// newExecutor returns a Executor
func newExecutor(c *ExecutorV1Client, namespace string) *executors {
return &executors{
client: c.RESTClient(),
ns: namespace,
}
}

// List takes label and field selectors, and returns the list of Executor that match those selectors.
func (c *executors) List(ctx context.Context, opts v1.ListOptions) (result *executorv1.ExecutorList, err error) {
var timeout time.Duration
if opts.TimeoutSeconds != nil {
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
}
result = &executorv1.ExecutorList{}
err = c.client.Get().
Namespace(c.ns).
Resource("executors").
VersionedParams(&opts, scheme.ParameterCodec).
Timeout(timeout).
Do(ctx).
Into(result)
return
}

// Watch returns a watch.Interface that watches the requested executors.
func (c *executors) Watch(ctx context.Context, opts v1.ListOptions) (watch.Interface, error) {
var timeout time.Duration
if opts.TimeoutSeconds != nil {
timeout = time.Duration(*opts.TimeoutSeconds) * time.Second
}
opts.Watch = true
return c.client.Get().
Namespace(c.ns).
Resource("executors").
VersionedParams(&opts, scheme.ParameterCodec).
Timeout(timeout).
Watch(ctx)
}
111 changes: 111 additions & 0 deletions pkg/clientset/versioned/typed/executor/v1/executor_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
"net/http"

"github.com/kubeshop/testkube-operator/pkg/clientset/versioned/scheme"

executorv1 "github.com/kubeshop/testkube-operator/api/executor/v1"
"k8s.io/client-go/rest"
)

type ExecutorV1Interface interface {
RESTClient() rest.Interface
ExecutorGetter
WebhookGetter
}

// ExecutorV1Client is used to interact with features provided by the executor.testkube.io group.
type ExecutorV1Client struct {
restClient rest.Interface
}

func (c *ExecutorV1Client) Executor(namespace string) ExecutorInterface {
return newExecutor(c, namespace)
}

func (c *ExecutorV1Client) Webhook(namespace string) WebhookInterface {
return newWebhook(c, namespace)
}

// NewForConfig creates a new ExecutorV1Client for the given config.
// NewForConfig is equivalent to NewForConfigAndClient(c, httpClient),
// where httpClient was generated with rest.HTTPClientFor(c).
func NewForConfig(c *rest.Config) (*ExecutorV1Client, error) {
config := *c
if err := setConfigDefaults(&config); err != nil {
return nil, err
}
httpClient, err := rest.HTTPClientFor(&config)
if err != nil {
return nil, err
}
return NewForConfigAndClient(&config, httpClient)
}

// NewForConfigAndClient creates a new ExecutorV1Client for the given config and http client.
// Note the http client provided takes precedence over the configured transport values.
func NewForConfigAndClient(c *rest.Config, h *http.Client) (*ExecutorV1Client, error) {
config := *c
if err := setConfigDefaults(&config); err != nil {
return nil, err
}
client, err := rest.RESTClientForConfigAndClient(&config, h)
if err != nil {
return nil, err
}
return &ExecutorV1Client{client}, nil
}

// NewForConfigOrDie creates a new ExecutorV1Client for the given config and
// panics if there is an error in the config.
func NewForConfigOrDie(c *rest.Config) *ExecutorV1Client {
client, err := NewForConfig(c)
if err != nil {
panic(err)
}
return client
}

// New creates a new ExecutorV1Client for the given RESTClient.
func New(c rest.Interface) *ExecutorV1Client {
return &ExecutorV1Client{c}
}

func setConfigDefaults(config *rest.Config) error {
gv := executorv1.GroupVersion
config.GroupVersion = &gv
config.APIPath = "/apis"
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()

if config.UserAgent == "" {
config.UserAgent = rest.DefaultKubernetesUserAgent()
}

return nil
}

// RESTClient returns a RESTClient that is used to communicate
// with API server by this client implementation.
func (c *ExecutorV1Client) RESTClient() rest.Interface {
if c == nil {
return nil
}
return c.restClient
}
Loading

0 comments on commit 83394de

Please sign in to comment.