Skip to content

Commit

Permalink
Figure out ingress API before calling List/Get operations
Browse files Browse the repository at this point in the history
This commit adds wrapper around the std API's get and list
methods so that we can first figure out which api version
ingresses are part of, and then can call the respective methods.
  • Loading branch information
viveksinghggits committed Nov 16, 2021
1 parent 808e32f commit 9e70a2a
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/kube/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ func IsOSRouteGroupAvailable(ctx context.Context, cli discovery.DiscoveryInterfa
return false, nil
}

func IsResAvailableInGroupVersion(ctx context.Context, cli discovery.DiscoveryInterface, groupName, version, resource string) (bool, error) {
resList, err := cli.ServerPreferredResources()
if err != nil {
return false, err
}
for _, res := range resList {
for _, r := range res.APIResources {
if r.Name == resource && fmt.Sprintf("%s/%s", groupName, version) == res.GroupVersion {
return true, nil
}
}
}
return false, nil
}

// IsGroupVersionAvailable returns true if given group/version is registered.
func IsGroupVersionAvailable(ctx context.Context, cli discovery.DiscoveryInterface, groupName, version string) (bool, error) {
sgs, err := cli.ServerGroups()
Expand Down
41 changes: 41 additions & 0 deletions pkg/kube/ingress/ingress_extbeta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2019 The Kanister 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 ingress

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
)

type IngressExtBeta struct {
kubeCli kubernetes.Interface
}

func NewIngressExtBeta(cli kubernetes.Interface) *IngressExtBeta {
return &IngressExtBeta{
kubeCli: cli,
}
}

func (i *IngressExtBeta) List(ctx context.Context, ns string) (runtime.Object, error) {
return i.kubeCli.ExtensionsV1beta1().Ingresses(ns).List(ctx, metav1.ListOptions{})
}

func (i *IngressExtBeta) Get(ctx context.Context, ns, name string) (runtime.Object, error) {
return i.kubeCli.ExtensionsV1beta1().Ingresses(ns).Get(ctx, name, metav1.GetOptions{})
}
41 changes: 41 additions & 0 deletions pkg/kube/ingress/ingress_netbeta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2019 The Kanister 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 ingress

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
)

type IngressNetBeta struct {
kubeCli kubernetes.Interface
}

func NewIngressNetBeta(cli kubernetes.Interface) *IngressNetBeta {
return &IngressNetBeta{
kubeCli: cli,
}
}

func (i *IngressNetBeta) List(ctx context.Context, ns string) (runtime.Object, error) {
return i.kubeCli.NetworkingV1beta1().Ingresses(ns).List(ctx, metav1.ListOptions{})
}

func (i *IngressNetBeta) Get(ctx context.Context, ns, name string) (runtime.Object, error) {
return i.kubeCli.NetworkingV1beta1().Ingresses(ns).Get(ctx, name, metav1.GetOptions{})
}
41 changes: 41 additions & 0 deletions pkg/kube/ingress/ingress_netv1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2019 The Kanister 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 ingress

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
)

type IngressNetV1 struct {
kubeCli kubernetes.Interface
}

func NewIngressNetV1(cli kubernetes.Interface) *IngressNetV1 {
return &IngressNetV1{
kubeCli: cli,
}
}

func (i *IngressNetV1) List(ctx context.Context, ns string) (runtime.Object, error) {
return i.kubeCli.NetworkingV1().Ingresses(ns).List(ctx, metav1.ListOptions{})
}

func (i *IngressNetV1) Get(ctx context.Context, ns, name string) (runtime.Object, error) {
return i.kubeCli.NetworkingV1().Ingresses(ns).Get(ctx, name, metav1.GetOptions{})
}
64 changes: 64 additions & 0 deletions pkg/kube/ingress/ingressmgr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2019 The Kanister 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 ingress

import (
"context"

"github.com/pkg/errors"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
netv1 "k8s.io/api/networking/v1"
netv1beta1 "k8s.io/api/networking/v1beta1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"

"github.com/kanisterio/kanister/pkg/kube"
)

const (
ingressRes = "ingresses"
)

type IngressMgr interface {
List(ctx context.Context, ns string) (runtime.Object, error)
Get(ctx context.Context, ns, name string) (runtime.Object, error)
}

func NewIngressMgr(ctx context.Context, kubeCli kubernetes.Interface) (IngressMgr, error) {
exists, err := kube.IsResAvailableInGroupVersion(ctx, kubeCli.Discovery(), netv1.GroupName, netv1.SchemeGroupVersion.Version, ingressRes)
if err != nil {
return nil, errors.Errorf("Failed to call discovery APIs: %v", err)
}
if exists {
return NewIngressNetV1(kubeCli), nil
}

exists, err = kube.IsResAvailableInGroupVersion(ctx, kubeCli.Discovery(), extensionsv1beta1.GroupName, extensionsv1beta1.SchemeGroupVersion.Version, ingressRes)
if err != nil {
return nil, errors.Errorf("Failed to call discovery APIs: %v", err)
}
if exists {
return NewIngressExtBeta(kubeCli), nil
}

exists, err = kube.IsResAvailableInGroupVersion(ctx, kubeCli.Discovery(), netv1beta1.GroupName, netv1beta1.SchemeGroupVersion.Version, ingressRes)
if err != nil {
return nil, errors.Errorf("Failed to call discovery APIs: %v", err)
}
if exists {
return NewIngressNetBeta(kubeCli), nil
}
return nil, errors.New("Ingress resources are not available")
}

0 comments on commit 9e70a2a

Please sign in to comment.