Migrate ingress.class annotation to new IngressClassName field
This commit is contained in:
parent
461aa93d13
commit
04ef782c57
7 changed files with 46 additions and 27 deletions
|
@ -59,8 +59,8 @@ requests to the first port of this Service.`)
|
||||||
|
|
||||||
ingressClass = flags.String("ingress-class", "",
|
ingressClass = flags.String("ingress-class", "",
|
||||||
`Name of the ingress class this controller satisfies.
|
`Name of the ingress class this controller satisfies.
|
||||||
The class of an Ingress object is set using the annotation "kubernetes.io/ingress.class".
|
The class of an Ingress object is set using the field IngressClassName in Kubernetes clusters version v1.18.0 or higher or the annotation "kubernetes.io/ingress.class" (deprecated).
|
||||||
All ingress classes are satisfied if this parameter is left empty.`)
|
All ingress classes are satisfied if this parameter is not set.`)
|
||||||
|
|
||||||
configMap = flags.String("configmap", "",
|
configMap = flags.String("configmap", "",
|
||||||
`Name of the ConfigMap containing custom global configurations for the controller.`)
|
`Name of the ConfigMap containing custom global configurations for the controller.`)
|
||||||
|
|
|
@ -102,11 +102,15 @@ func main() {
|
||||||
conf.FakeCertificate = ssl.GetFakeSSLCert()
|
conf.FakeCertificate = ssl.GetFakeSSLCert()
|
||||||
klog.Infof("SSL fake certificate created %v", conf.FakeCertificate.PemFileName)
|
klog.Infof("SSL fake certificate created %v", conf.FakeCertificate.PemFileName)
|
||||||
|
|
||||||
k8s.IsNetworkingIngressAvailable = k8s.NetworkingIngressAvailable(kubeClient)
|
k8s.IsNetworkingIngressAvailable, k8s.IsIngressV1Ready = k8s.NetworkingIngressAvailable(kubeClient)
|
||||||
if !k8s.IsNetworkingIngressAvailable {
|
if !k8s.IsNetworkingIngressAvailable {
|
||||||
klog.Warningf("Using deprecated \"k8s.io/api/extensions/v1beta1\" package because Kubernetes version is < v1.14.0")
|
klog.Warningf("Using deprecated \"k8s.io/api/extensions/v1beta1\" package because Kubernetes version is < v1.14.0")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !k8s.IsIngressV1Ready {
|
||||||
|
klog.Infof("Enabling new Ingress features availables since v1.18.0")
|
||||||
|
}
|
||||||
|
|
||||||
conf.Client = kubeClient
|
conf.Client = kubeClient
|
||||||
|
|
||||||
reg := prometheus.NewRegistry()
|
reg := prometheus.NewRegistry()
|
||||||
|
|
|
@ -17,8 +17,6 @@ limitations under the License.
|
||||||
package class
|
package class
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"k8s.io/klog"
|
|
||||||
|
|
||||||
networking "k8s.io/api/networking/v1beta1"
|
networking "k8s.io/api/networking/v1beta1"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,10 +41,7 @@ var (
|
||||||
// the ingress.class annotation, or it's set to the configured in the
|
// the ingress.class annotation, or it's set to the configured in the
|
||||||
// ingress controller.
|
// ingress controller.
|
||||||
func IsValid(ing *networking.Ingress) bool {
|
func IsValid(ing *networking.Ingress) bool {
|
||||||
ingress, ok := ing.GetAnnotations()[IngressKey]
|
className := ing.Spec.IngressClassName
|
||||||
if !ok {
|
|
||||||
klog.V(3).Infof("annotation %v is not present in ingress %v/%v", IngressKey, ing.Namespace, ing.Name)
|
|
||||||
}
|
|
||||||
|
|
||||||
// we have 2 valid combinations
|
// we have 2 valid combinations
|
||||||
// 1 - ingress with default class | blank annotation on ingress
|
// 1 - ingress with default class | blank annotation on ingress
|
||||||
|
@ -55,9 +50,17 @@ func IsValid(ing *networking.Ingress) bool {
|
||||||
// and 2 invalid combinations
|
// and 2 invalid combinations
|
||||||
// 3 - ingress with default class | fixed annotation on ingress
|
// 3 - ingress with default class | fixed annotation on ingress
|
||||||
// 4 - ingress with specific class | different annotation on ingress
|
// 4 - ingress with specific class | different annotation on ingress
|
||||||
if ingress == "" && IngressClass == DefaultClass {
|
if className != nil {
|
||||||
|
return *className == IngressClass
|
||||||
|
}
|
||||||
|
|
||||||
|
if IngressClass == DefaultClass {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return ingress == IngressClass
|
if IngressClass == "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,10 +54,10 @@ func TestIsValidClass(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
data := map[string]string{}
|
|
||||||
ing.SetAnnotations(data)
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
ing.Annotations[IngressKey] = test.ingress
|
if test.ingress != "" {
|
||||||
|
ing.Spec.IngressClassName = &test.ingress
|
||||||
|
}
|
||||||
|
|
||||||
IngressClass = test.controller
|
IngressClass = test.controller
|
||||||
DefaultClass = test.defClass
|
DefaultClass = test.defClass
|
||||||
|
|
|
@ -190,7 +190,8 @@ func TestCheckIngress(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("When the ingress class differs from nginx", func(t *testing.T) {
|
t.Run("When the ingress class differs from nginx", func(t *testing.T) {
|
||||||
ing.ObjectMeta.Annotations["kubernetes.io/ingress.class"] = "different"
|
class := "different"
|
||||||
|
ing.Spec.IngressClassName = &class
|
||||||
nginx.command = testNginxTestCommand{
|
nginx.command = testNginxTestCommand{
|
||||||
t: t,
|
t: t,
|
||||||
err: fmt.Errorf("test error"),
|
err: fmt.Errorf("test error"),
|
||||||
|
@ -201,7 +202,8 @@ func TestCheckIngress(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("when the class is the nginx one", func(t *testing.T) {
|
t.Run("when the class is the nginx one", func(t *testing.T) {
|
||||||
ing.ObjectMeta.Annotations["kubernetes.io/ingress.class"] = "nginx"
|
class := "nginx"
|
||||||
|
ing.Spec.IngressClassName = &class
|
||||||
nginx.command = testNginxTestCommand{
|
nginx.command = testNginxTestCommand{
|
||||||
t: t,
|
t: t,
|
||||||
err: nil,
|
err: nil,
|
||||||
|
|
|
@ -949,12 +949,22 @@ func toIngress(obj interface{}) (*networkingv1beta1.Ingress, bool) {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ing.Spec.IngressClassName = extractClassName(ing)
|
||||||
return ing, true
|
return ing, true
|
||||||
}
|
}
|
||||||
|
|
||||||
if ing, ok := obj.(*networkingv1beta1.Ingress); ok {
|
if ing, ok := obj.(*networkingv1beta1.Ingress); ok {
|
||||||
|
ing.Spec.IngressClassName = extractClassName(ing)
|
||||||
return ing, true
|
return ing, true
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractClassName(ing *networkingv1beta1.Ingress) *string {
|
||||||
|
if c, ok := ing.Annotations[class.IngressKey]; ok {
|
||||||
|
return &c
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
|
@ -118,26 +118,26 @@ func MetaNamespaceKey(obj interface{}) string {
|
||||||
// IsNetworkingIngressAvailable indicates if package "k8s.io/api/networking/v1beta1" is available or not
|
// IsNetworkingIngressAvailable indicates if package "k8s.io/api/networking/v1beta1" is available or not
|
||||||
var IsNetworkingIngressAvailable bool
|
var IsNetworkingIngressAvailable bool
|
||||||
|
|
||||||
// NetworkingIngressAvailable checks if the package "k8s.io/api/networking/v1beta1" is available or not
|
// IsIngressV1Ready indicates if the running Kubernetes version is at least v1.18.0
|
||||||
func NetworkingIngressAvailable(client clientset.Interface) bool {
|
var IsIngressV1Ready bool
|
||||||
|
|
||||||
|
// NetworkingIngressAvailable checks if the package "k8s.io/api/networking/v1beta1"
|
||||||
|
// is available or not and if Ingress V1 is supported (k8s >= v1.18.0)
|
||||||
|
func NetworkingIngressAvailable(client clientset.Interface) (bool, bool) {
|
||||||
// check kubernetes version to use new ingress package or not
|
// check kubernetes version to use new ingress package or not
|
||||||
version114, err := version.ParseGeneric("v1.14.0")
|
version114, _ := version.ParseGeneric("v1.14.0")
|
||||||
if err != nil {
|
version118, _ := version.ParseGeneric("v1.18.0")
|
||||||
klog.Errorf("unexpected error parsing version: %v", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
serverVersion, err := client.Discovery().ServerVersion()
|
serverVersion, err := client.Discovery().ServerVersion()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("unexpected error parsing Kubernetes version: %v", err)
|
return false, false
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
runningVersion, err := version.ParseGeneric(serverVersion.String())
|
runningVersion, err := version.ParseGeneric(serverVersion.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
klog.Errorf("unexpected error parsing running Kubernetes version: %v", err)
|
klog.Errorf("unexpected error parsing running Kubernetes version: %v", err)
|
||||||
return false
|
return false, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return runningVersion.AtLeast(version114)
|
return runningVersion.AtLeast(version114), runningVersion.AtLeast(version118)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue