Skip to content
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

feat(platform): support ingress and storage event for v1 version #1995

Merged
merged 1 commit into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions pkg/platform/proxy/extensions/ingress/storage/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ package storage
import (
"context"

"tkestack.io/tke/pkg/platform/proxy"
"tkestack.io/tke/pkg/util/apiclient"

corev1 "k8s.io/api/core/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -36,6 +34,8 @@ import (
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/kubernetes"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/platform/proxy"
"tkestack.io/tke/pkg/util/apiclient"
)

// EventREST implements the REST endpoint for find events by a daemonset.
Expand Down Expand Up @@ -74,6 +74,9 @@ func (r *EventREST) Get(ctx context.Context, name string, options *metav1.GetOpt
if apiclient.ClusterVersionIsBefore116(client) {
return listEventsByExtensions(ctx, client, namespaceName, name, options)
}
if apiclient.ClusterVersionIsAfter122(client) {
return listEventsByNetworkingsV1(ctx, client, namespaceName, name, options)
}
return listEventsByNetworkings(ctx, client, namespaceName, name, options)
}

Expand Down Expand Up @@ -110,3 +113,20 @@ func listEventsByNetworkings(ctx context.Context, client *kubernetes.Clientset,
}
return client.CoreV1().Events(namespaceName).List(ctx, listOptions)
}

func listEventsByNetworkingsV1(ctx context.Context, client *kubernetes.Clientset, namespaceName, name string, options *metav1.GetOptions) (runtime.Object, error) {
ingress, err := client.NetworkingV1().Ingresses(namespaceName).Get(ctx, name, *options)
if err != nil {
return nil, errors.NewNotFound(networkingv1.Resource("ingresses/events"), name)
}

selector := fields.AndSelectors(
fields.OneTermEqualSelector("involvedObject.uid", string(ingress.UID)),
fields.OneTermEqualSelector("involvedObject.name", ingress.Name),
fields.OneTermEqualSelector("involvedObject.namespace", ingress.Namespace),
fields.OneTermEqualSelector("involvedObject.kind", "Ingress"))
listOptions := metav1.ListOptions{
FieldSelector: selector.String(),
}
return client.CoreV1().Events(namespaceName).List(ctx, listOptions)
}
25 changes: 25 additions & 0 deletions pkg/platform/proxy/storage/storageclass/storage/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/kubernetes"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/platform/proxy"
"tkestack.io/tke/pkg/util/apiclient"
)

// EventREST implements the REST endpoint for find events by a storageclass.
Expand Down Expand Up @@ -61,6 +63,13 @@ func (r *EventREST) Get(ctx context.Context, name string, options *metav1.GetOpt
return nil, err
}

if apiclient.ClusterVersionIsAfter122(client) {
return listEventsByV1(ctx, client, name, options)
}
return listEventsByV1beta1(ctx, client, name, options)
}

func listEventsByV1beta1(ctx context.Context, client *kubernetes.Clientset, name string, options *metav1.GetOptions) (runtime.Object, error) {
storageClass, err := client.StorageV1beta1().StorageClasses().Get(ctx, name, *options)
if err != nil {
return nil, errors.NewNotFound(extensionsv1beta1.Resource("storageclasses/events"), name)
Expand All @@ -75,3 +84,19 @@ func (r *EventREST) Get(ctx context.Context, name string, options *metav1.GetOpt
}
return client.CoreV1().Events("").List(ctx, listOptions)
}

func listEventsByV1(ctx context.Context, client *kubernetes.Clientset, name string, options *metav1.GetOptions) (runtime.Object, error) {
storageClass, err := client.StorageV1().StorageClasses().Get(ctx, name, *options)
if err != nil {
return nil, errors.NewNotFound(extensionsv1beta1.Resource("storageclasses/events"), name)
}

selector := fields.AndSelectors(
fields.OneTermEqualSelector("involvedObject.uid", string(storageClass.UID)),
fields.OneTermEqualSelector("involvedObject.name", storageClass.Name),
fields.OneTermEqualSelector("involvedObject.kind", "StorageClass"))
listOptions := metav1.ListOptions{
FieldSelector: selector.String(),
}
return client.CoreV1().Events("").List(ctx, listOptions)
}
9 changes: 9 additions & 0 deletions pkg/util/apiclient/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ func ClusterVersionIsBefore116(client kubernetes.Interface) bool {
return result
}

func ClusterVersionIsAfter122(client kubernetes.Interface) bool {
result, err := CheckClusterVersion(client, ">= 1.22")
if err != nil {
return false
}

return result
}

func ClusterVersionIsBefore118(client kubernetes.Interface) bool {
result, err := CheckClusterVersion(client, "< 1.18")
if err != nil {
Expand Down