Skip to content

Commit

Permalink
feat(platform): support ingress and storage event for v1 version (tke…
Browse files Browse the repository at this point in the history
…stack#1995)

Co-authored-by: xdonggao <[email protected]>
  • Loading branch information
GaoXiaodong and xdonggao committed Sep 14, 2022
1 parent 8c27de4 commit d935e22
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
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

0 comments on commit d935e22

Please sign in to comment.