-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move code being used by kafka plugin from client #67
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f72442d
Move code being used by kafka plugin from client
vyasgun 35f364b
moved the test code for copied files
vyasgun e7cdee7
moved update-codegen and related scripts
vyasgun f4c9eaa
extracted only relevant code from plugin package
vyasgun dbc50af
added files related to e2e tests in kafka plugin
vyasgun 5f413b5
moved lib to pkg/util
vyasgun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2020 The Knative Authors | ||
|
||
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 v1alpha1 | ||
|
||
import ( | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
servingv1 "knative.dev/serving/pkg/apis/serving/v1" | ||
) | ||
|
||
// +genclient:noStatus | ||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// Export is a specification for a Export resource | ||
type Export struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ObjectMeta `json:"metadata,omitempty"` | ||
|
||
Spec ExportSpec `json:"spec"` | ||
} | ||
|
||
// ExportSpec is the spec for a Export resource | ||
type ExportSpec struct { | ||
Service servingv1.Service `json:"service"` | ||
Revisions []servingv1.Revision `json:"revisions"` | ||
} | ||
|
||
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object | ||
|
||
// ExportList is a list of Export resources | ||
type ExportList struct { | ||
metav1.TypeMeta `json:",inline"` | ||
metav1.ListMeta `json:"metadata"` | ||
|
||
Items []Export `json:"items"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,272 @@ | ||
// Copyright © 2019 The Knative Authors | ||
// | ||
// 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 dynamic | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"strings" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/dynamic" | ||
"knative.dev/client-pkg/pkg/util" | ||
"knative.dev/eventing/pkg/apis/messaging" | ||
) | ||
|
||
const ( | ||
crdGroup = "apiextensions.k8s.io" | ||
crdVersion = "v1" | ||
crdKind = "CustomResourceDefinition" | ||
crdKinds = "customresourcedefinitions" | ||
sourcesLabelKey = "duck.knative.dev/source" | ||
sourcesLabelValue = "true" | ||
sourceListGroup = "client.knative.dev" | ||
sourceListVersion = "v1alpha1" | ||
sourceListKind = "SourceList" | ||
channelLabelValue = "true" | ||
channelListVersion = "v1" | ||
channelListKind = "ChannelList" | ||
channelKind = "Channel" | ||
) | ||
|
||
// KnDynamicClient to client-go Dynamic client. All methods are relative to the | ||
// namespace specified during construction | ||
type KnDynamicClient interface { | ||
// Namespace in which this client is operating for | ||
Namespace() string | ||
|
||
// ListCRDs returns list of CRDs with their type and name | ||
ListCRDs(ctx context.Context, options metav1.ListOptions) (*unstructured.UnstructuredList, error) | ||
|
||
// ListSourcesTypes returns list of eventing sources CRDs | ||
ListSourcesTypes(ctx context.Context) (*unstructured.UnstructuredList, error) | ||
|
||
// ListSources returns list of available source objects | ||
ListSources(ctx context.Context, types ...WithType) (*unstructured.UnstructuredList, error) | ||
|
||
// ListSourcesUsingGVKs returns list of available source objects using given list of GVKs | ||
ListSourcesUsingGVKs(context.Context, *[]schema.GroupVersionKind, ...WithType) (*unstructured.UnstructuredList, error) | ||
|
||
// ListChannelsTypes returns installed knative channel CRDs | ||
ListChannelsTypes(ctx context.Context) (*unstructured.UnstructuredList, error) | ||
|
||
// ListChannelsUsingGVKs returns list of available channel objects using given list of GVKs | ||
ListChannelsUsingGVKs(context.Context, *[]schema.GroupVersionKind, ...WithType) (*unstructured.UnstructuredList, error) | ||
|
||
// RawClient returns the raw dynamic client interface | ||
RawClient() dynamic.Interface | ||
} | ||
|
||
// knDynamicClient is a combination of client-go Dynamic client interface and namespace | ||
type knDynamicClient struct { | ||
client dynamic.Interface | ||
namespace string | ||
} | ||
|
||
// NewKnDynamicClient is to invoke Eventing Sources Client API to create object | ||
func NewKnDynamicClient(client dynamic.Interface, namespace string) KnDynamicClient { | ||
return &knDynamicClient{ | ||
client: client, | ||
namespace: namespace, | ||
} | ||
} | ||
|
||
// Return the client's namespace | ||
func (c *knDynamicClient) Namespace() string { | ||
return c.namespace | ||
} | ||
|
||
// TODO(navidshaikh): Use ListConfigs here instead of ListOptions | ||
// ListCRDs returns list of installed CRDs in the cluster and filters based on the given options | ||
func (c *knDynamicClient) ListCRDs(ctx context.Context, options metav1.ListOptions) (*unstructured.UnstructuredList, error) { | ||
gvr := schema.GroupVersionResource{ | ||
Group: crdGroup, | ||
Version: crdVersion, | ||
Resource: crdKinds, | ||
} | ||
|
||
uList, err := c.client.Resource(gvr).List(ctx, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return uList, nil | ||
} | ||
|
||
// ListSourcesTypes returns installed knative eventing sources CRDs | ||
func (c *knDynamicClient) ListSourcesTypes(ctx context.Context) (*unstructured.UnstructuredList, error) { | ||
options := metav1.ListOptions{} | ||
sourcesLabels := labels.Set{sourcesLabelKey: sourcesLabelValue} | ||
options.LabelSelector = sourcesLabels.String() | ||
return c.ListCRDs(ctx, options) | ||
} | ||
|
||
// ListChannelsTypes returns installed knative channel CRDs | ||
func (c *knDynamicClient) ListChannelsTypes(ctx context.Context) (*unstructured.UnstructuredList, error) { | ||
var ChannelTypeList unstructured.UnstructuredList | ||
options := metav1.ListOptions{} | ||
channelsLabels := labels.Set{messaging.SubscribableDuckVersionAnnotation: channelLabelValue} | ||
options.LabelSelector = channelsLabels.String() | ||
uList, err := c.ListCRDs(ctx, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ChannelTypeList.Object = uList.Object | ||
for _, channelType := range uList.Items { | ||
content := channelType.UnstructuredContent() | ||
channelTypeKind, _, err := unstructured.NestedString(content, "spec", "names", "kind") | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !util.SliceContainsIgnoreCase([]string{channelKind}, channelTypeKind) { | ||
ChannelTypeList.Items = append(ChannelTypeList.Items, channelType) | ||
} | ||
} | ||
return &ChannelTypeList, nil | ||
} | ||
|
||
func (c knDynamicClient) RawClient() dynamic.Interface { | ||
return c.client | ||
} | ||
|
||
// ListSources returns list of available sources objects | ||
// Provide the list of source types as for example: WithTypes("pingsource", "apiserversource"...) to list | ||
// only given types of source objects | ||
func (c *knDynamicClient) ListSources(ctx context.Context, types ...WithType) (*unstructured.UnstructuredList, error) { | ||
var ( | ||
sourceList unstructured.UnstructuredList | ||
options metav1.ListOptions | ||
) | ||
sourceTypes, err := c.ListSourcesTypes(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if sourceTypes == nil || len(sourceTypes.Items) == 0 { | ||
return nil, errors.New("no sources found on the backend, please verify the installation") | ||
} | ||
|
||
namespace := c.Namespace() | ||
filters := WithTypes(types).List() | ||
// For each source type available, find out each source types objects | ||
for i := range sourceTypes.Items { | ||
source := &sourceTypes.Items[i] | ||
// find source kind before hand to fail early | ||
sourceKind, err := kindFromUnstructured(source) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(filters) > 0 && !util.SliceContainsIgnoreCase(filters, sourceKind) { | ||
continue | ||
} | ||
|
||
// find source's GVR from unstructured source type object | ||
gvr, err := gvrFromUnstructured(source) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// list objects of source type with this GVR | ||
sList, err := c.client.Resource(gvr).Namespace(namespace).List(ctx, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(sList.Items) > 0 { | ||
sourceList.Items = append(sourceList.Items, sList.Items...) | ||
} | ||
} | ||
if len(sourceList.Items) > 0 { | ||
sourceList.SetGroupVersionKind(schema.GroupVersionKind{Group: sourceListGroup, Version: sourceListVersion, Kind: sourceListKind}) | ||
} | ||
return &sourceList, nil | ||
} | ||
|
||
// ListSourcesUsingGVKs returns list of available source objects using given list of GVKs | ||
func (c *knDynamicClient) ListSourcesUsingGVKs(ctx context.Context, gvks *[]schema.GroupVersionKind, types ...WithType) (*unstructured.UnstructuredList, error) { | ||
if gvks == nil { | ||
return nil, nil | ||
} | ||
|
||
var ( | ||
sourceList unstructured.UnstructuredList | ||
options metav1.ListOptions | ||
) | ||
namespace := c.Namespace() | ||
filters := WithTypes(types).List() | ||
|
||
for _, gvk := range *gvks { | ||
if len(filters) > 0 && !util.SliceContainsIgnoreCase(filters, gvk.Kind) { | ||
continue | ||
} | ||
|
||
gvr := gvk.GroupVersion().WithResource(strings.ToLower(gvk.Kind) + "s") | ||
|
||
// list objects of source type with this GVR | ||
sList, err := c.client.Resource(gvr).Namespace(namespace).List(ctx, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(sList.Items) > 0 { | ||
sourceList.Items = append(sourceList.Items, sList.Items...) | ||
} | ||
} | ||
if len(sourceList.Items) > 0 { | ||
sourceList.SetGroupVersionKind(schema.GroupVersionKind{Group: sourceListGroup, Version: sourceListVersion, Kind: sourceListKind}) | ||
} | ||
return &sourceList, nil | ||
} | ||
|
||
// ListChannelsUsingGVKs returns list of available channel objects using given list of GVKs | ||
func (c *knDynamicClient) ListChannelsUsingGVKs(ctx context.Context, gvks *[]schema.GroupVersionKind, types ...WithType) (*unstructured.UnstructuredList, error) { | ||
if gvks == nil { | ||
return nil, nil | ||
} | ||
|
||
var ( | ||
channelList unstructured.UnstructuredList | ||
options metav1.ListOptions | ||
) | ||
namespace := c.Namespace() | ||
filters := WithTypes(types).List() | ||
|
||
for _, gvk := range *gvks { | ||
if len(filters) > 0 && !util.SliceContainsIgnoreCase(filters, gvk.Kind) { | ||
continue | ||
} | ||
|
||
gvr := gvk.GroupVersion().WithResource(strings.ToLower(gvk.Kind) + "s") | ||
|
||
// list objects of channel type with this GVR | ||
cList, err := c.client.Resource(gvr).Namespace(namespace).List(ctx, options) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(cList.Items) > 0 { | ||
channelList.Items = append(channelList.Items, cList.Items...) | ||
} | ||
} | ||
if len(channelList.Items) > 0 { | ||
channelList.SetGroupVersionKind(schema.GroupVersionKind{Group: messaging.GroupName, Version: channelListVersion, Kind: channelListKind}) | ||
} | ||
return &channelList, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'll need to bring in
hack/update-codege.sh
script as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will copy the scripts for this.