forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix tackle GCE ingress incompatibility, kubernetes#13492
- Loading branch information
Travis Clarke
committed
Jul 25, 2019
1 parent
ab96149
commit 85338af
Showing
4 changed files
with
248 additions
and
9 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,81 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 main | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
extensions "k8s.io/api/extensions/v1beta1" | ||
networking "k8s.io/api/networking/v1beta1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/version" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// https://github.com/kubernetes/enhancements/blob/master/keps/sig-network/20190125-ingress-api-group.md#116 | ||
// NOTE: GCE ingress controller does not target "networking.k8s.io/v1beta1": https://github.com/kubernetes/ingress-gce/issues/770 | ||
const networkingIngressKubeVersion = "v1.16.0" | ||
|
||
var runtimeScheme = runtime.NewScheme() | ||
|
||
func init() { | ||
extensions.AddToScheme(runtimeScheme) | ||
networking.AddToScheme(runtimeScheme) | ||
} | ||
|
||
// networkingIngressAvailable checks if the "networking.k8s.io/v1beta1" resource is available based on Kubernetes version. | ||
func networkingIngressAvailable(kc kubernetes.Interface) bool { | ||
serverVersion, err := kc.Discovery().ServerVersion() | ||
if err != nil { | ||
logrus.WithError(err).Fatal("unexpected error parsing Kubernetes version") | ||
return false | ||
} | ||
|
||
return version.CompareKubeAwareVersionStrings(serverVersion.String(), networkingIngressKubeVersion) <= 0 | ||
} | ||
|
||
// fromExtensions converts a legacy "extensions/v1beta1" IngressList to the newer "networking.k8s.io/v1beta1" IngressList. | ||
func fromExtensions(old *extensions.IngressList) (*networking.IngressList, error) { | ||
networkingIngress := &networking.IngressList{} | ||
|
||
err := runtimeScheme.Convert(old, networkingIngress, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return networkingIngress, nil | ||
} | ||
|
||
// toIngress normalizes a given IngressList to the newer "networking.k8s.io/v1beta1" type. | ||
func toIngress(obj interface{}) (*networking.IngressList, bool) { | ||
oldVersion, inExtension := obj.(*extensions.IngressList) | ||
|
||
if inExtension { | ||
ing, err := fromExtensions(oldVersion) | ||
if err != nil { | ||
logrus.WithError(err).Fatal("unexpected error converting Ingress from extensions package") | ||
return nil, false | ||
} | ||
|
||
return ing, true | ||
} | ||
|
||
if ing, ok := obj.(*networking.IngressList); ok { | ||
return ing, true | ||
} | ||
|
||
return nil, false | ||
} |
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,123 @@ | ||
/* | ||
Copyright 2018 The Kubernetes 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 main | ||
|
||
import ( | ||
"bytes" | ||
extensions "k8s.io/api/extensions/v1beta1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
"k8s.io/apimachinery/pkg/version" | ||
discoveryFake "k8s.io/client-go/discovery/fake" | ||
k8sFake "k8s.io/client-go/kubernetes/fake" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestNetworkingIngressDetectLess(t *testing.T) { | ||
fakeClient := k8sFake.NewSimpleClientset() | ||
|
||
fakeClient.Discovery().(*discoveryFake.FakeDiscovery).FakedServerVersion = &version.Info{ | ||
GitVersion: "v1.10", | ||
} | ||
|
||
actual := networkingIngressAvailable(fakeClient) | ||
|
||
if actual != false { | ||
t.Fatalf("networkingIngressAvailable should return `false` for versions < %s", networkingIngressKubeVersion) | ||
} | ||
} | ||
|
||
func TestNetworkingIngressDetectEqual(t *testing.T) { | ||
fakeClient := k8sFake.NewSimpleClientset() | ||
|
||
fakeClient.Discovery().(*discoveryFake.FakeDiscovery).FakedServerVersion = &version.Info{ | ||
GitVersion: networkingIngressKubeVersion, | ||
} | ||
|
||
actual := networkingIngressAvailable(fakeClient) | ||
|
||
if actual != true { | ||
t.Fatalf("networkingIngressAvailable should return `true` for versions >= %s", networkingIngressKubeVersion) | ||
} | ||
} | ||
|
||
func TestNetworkingIngressDetectGreater(t *testing.T) { | ||
fakeClient := k8sFake.NewSimpleClientset() | ||
|
||
fakeClient.Discovery().(*discoveryFake.FakeDiscovery).FakedServerVersion = &version.Info{ | ||
GitVersion: "v1.20", | ||
} | ||
|
||
actual := networkingIngressAvailable(fakeClient) | ||
|
||
if actual != true { | ||
t.Fatalf("networkingIngressAvailable should return `true` for versions >= %s", networkingIngressKubeVersion) | ||
} | ||
} | ||
|
||
func TestIngressConversion(t *testing.T) { | ||
ings := &extensions.IngressList{ | ||
Items: []extensions.Ingress{ | ||
{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "old-ingress", | ||
Namespace: "demo", | ||
CreationTimestamp: metav1.NewTime(time.Now()), | ||
}, | ||
Spec: extensions.IngressSpec{ | ||
Rules: []extensions.IngressRule{ | ||
{ | ||
Host: "foo.bar", | ||
IngressRuleValue: extensions.IngressRuleValue{ | ||
HTTP: &extensions.HTTPIngressRuleValue{ | ||
Paths: []extensions.HTTPIngressPath{ | ||
{ | ||
Backend: extensions.IngressBackend{ | ||
ServiceName: "demo", | ||
ServicePort: intstr.FromInt(80), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
new, err := fromExtensions(ings) | ||
if err != nil { | ||
t.Fatalf("unexpected error converting ingress: %v", err) | ||
} | ||
|
||
m1, err := new.Marshal() | ||
if err != nil { | ||
t.Fatalf("unexpected error marshalling Ingress: %v", err) | ||
} | ||
|
||
m2, err := ings.Marshal() | ||
if err != nil { | ||
t.Fatalf("unexpected error marshalling Ingress: %v", err) | ||
} | ||
|
||
if bytes.Compare(m1, m2) != 0 { | ||
t.Fatalf("Expected marshalling of types should be equal") | ||
} | ||
} |
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