-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Figure out ingress API before calling List/Get operations
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
1 parent
808e32f
commit 9e70a2a
Showing
5 changed files
with
202 additions
and
0 deletions.
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
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,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{}) | ||
} |
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,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{}) | ||
} |
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,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{}) | ||
} |
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,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") | ||
} |