Don't remove ingress config on API call failure

This commit is contained in:
Daniel Tomcej 2021-07-19 12:06:07 -06:00 committed by GitHub
parent 8d4620dc53
commit c2c4dc9b58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 19 deletions

View file

@ -60,7 +60,7 @@ type Client interface {
GetSecret(namespace, name string) (*corev1.Secret, bool, error) GetSecret(namespace, name string) (*corev1.Secret, bool, error)
GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error) GetEndpoints(namespace, name string) (*corev1.Endpoints, bool, error)
UpdateIngressStatus(ing *networkingv1beta1.Ingress, ingStatus []corev1.LoadBalancerIngress) error UpdateIngressStatus(ing *networkingv1beta1.Ingress, ingStatus []corev1.LoadBalancerIngress) error
GetServerVersion() (*version.Version, error) GetServerVersion() *version.Version
} }
type clientWrapper struct { type clientWrapper struct {
@ -72,6 +72,7 @@ type clientWrapper struct {
ingressLabelSelector string ingressLabelSelector string
isNamespaceAll bool isNamespaceAll bool
watchedNamespaces []string watchedNamespaces []string
serverVersion *version.Version
} }
// newInClusterClient returns a new Provider client that is expected to run // newInClusterClient returns a new Provider client that is expected to run
@ -208,12 +209,18 @@ func (c *clientWrapper) WatchAll(namespaces []string, stopCh <-chan struct{}) (<
} }
} }
serverVersion, err := c.GetServerVersion() // Get and store the serverVersion for future use.
serverVersionInfo, err := c.clientset.Discovery().ServerVersion()
if err != nil { if err != nil {
log.WithoutContext().Errorf("Failed to get server version: %v", err) return eventCh, fmt.Errorf("could not retrieve server version: %w", err)
return eventCh, nil
} }
serverVersion, err := version.NewVersion(serverVersionInfo.GitVersion)
if err != nil {
return eventCh, fmt.Errorf("could not parse server version: %w", err)
}
c.serverVersion = serverVersion
if supportsIngressClass(serverVersion) { if supportsIngressClass(serverVersion) {
c.clusterFactory = informers.NewSharedInformerFactoryWithOptions(c.clientset, resyncPeriod) c.clusterFactory = informers.NewSharedInformerFactoryWithOptions(c.clientset, resyncPeriod)
c.clusterFactory.Networking().V1beta1().IngressClasses().Informer().AddEventHandler(eventHandler) c.clusterFactory.Networking().V1beta1().IngressClasses().Informer().AddEventHandler(eventHandler)
@ -426,13 +433,8 @@ func (c *clientWrapper) lookupNamespace(ns string) string {
} }
// GetServerVersion returns the cluster server version, or an error. // GetServerVersion returns the cluster server version, or an error.
func (c *clientWrapper) GetServerVersion() (*version.Version, error) { func (c *clientWrapper) GetServerVersion() *version.Version {
serverVersion, err := c.clientset.Discovery().ServerVersion() return c.serverVersion
if err != nil {
return nil, fmt.Errorf("could not retrieve server version: %w", err)
}
return version.NewVersion(serverVersion.GitVersion)
} }
// eventHandlerFunc will pass the obj on to the events channel or drop it. // eventHandlerFunc will pass the obj on to the events channel or drop it.

View file

@ -73,8 +73,8 @@ func (c clientMock) GetIngresses() []*networkingv1beta1.Ingress {
return c.ingresses return c.ingresses
} }
func (c clientMock) GetServerVersion() (*version.Version, error) { func (c clientMock) GetServerVersion() *version.Version {
return c.serverVersion, nil return c.serverVersion
} }
func (c clientMock) GetService(namespace, name string) (*corev1.Service, bool, error) { func (c clientMock) GetService(namespace, name string) (*corev1.Service, bool, error) {

View file

@ -1,6 +1,7 @@
package ingress package ingress
import ( import (
"errors"
"fmt" "fmt"
"testing" "testing"
"time" "time"
@ -154,7 +155,8 @@ func TestClientIgnoresHelmOwnedSecrets(t *testing.T) {
stopCh := make(chan struct{}) stopCh := make(chan struct{})
eventCh, err := client.WatchAll(nil, stopCh) eventCh, err := client.WatchAll(nil, stopCh)
require.NoError(t, err) // Fake clientset always returns this exact serverVersion that fails our validation.
require.Error(t, errors.New(`could not parse server version: Malformed version: v0.0.0-master+$Format:%h$`), err)
select { select {
case event := <-eventCh: case event := <-eventCh:

View file

@ -189,11 +189,7 @@ func (p *Provider) loadConfigurationFromIngresses(ctx context.Context, client Cl
TCP: &dynamic.TCPConfiguration{}, TCP: &dynamic.TCPConfiguration{},
} }
serverVersion, err := client.GetServerVersion() serverVersion := client.GetServerVersion()
if err != nil {
log.FromContext(ctx).Errorf("Failed to get server version: %v", err)
return conf
}
var ingressClasses []*networkingv1beta1.IngressClass var ingressClasses []*networkingv1beta1.IngressClass