From 93eec0dc5998872a2ac8552ec5034a6f96c5bcde Mon Sep 17 00:00:00 2001 From: Paul Abel <128620221+pdabelf5@users.noreply.github.com> Date: Mon, 23 Dec 2024 09:20:40 +0000 Subject: [PATCH] add tests for kubernetes versions (#7035) --- cmd/nginx-ingress/main.go | 15 +++--- cmd/nginx-ingress/main_test.go | 85 ++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 6 deletions(-) diff --git a/cmd/nginx-ingress/main.go b/cmd/nginx-ingress/main.go index ccca8288a3..850920eee5 100644 --- a/cmd/nginx-ingress/main.go +++ b/cmd/nginx-ingress/main.go @@ -92,7 +92,9 @@ func main() { podName := os.Getenv("POD_NAME") config, kubeClient := mustCreateConfigAndKubeClient(ctx) - mustValidateKubernetesVersionInfo(ctx, kubeClient) + if err := validateKubernetesVersionInfo(ctx, kubeClient); err != nil { + nl.Fatal(l, err) + } pod, err := kubeClient.CoreV1().Pods(controllerNamespace).Get(context.TODO(), podName, meta_v1.GetOptions{}) if err != nil { nl.Fatalf(l, "Failed to get pod: %v", err) @@ -399,24 +401,25 @@ func mustCreateConfigAndKubeClient(ctx context.Context) (*rest.Config, *kubernet return config, kubeClient } -// mustValidateKubernetesVersionInfo calls internally os.Exit if +// validateKubernetesVersionInfo returns an Error if // the k8s version can not be retrieved or the version is not supported. -func mustValidateKubernetesVersionInfo(ctx context.Context, kubeClient kubernetes.Interface) { +func validateKubernetesVersionInfo(ctx context.Context, kubeClient kubernetes.Interface) error { l := nl.LoggerFromContext(ctx) k8sVersion, err := k8s.GetK8sVersion(kubeClient) if err != nil { - nl.Fatalf(l, "error retrieving k8s version: %v", err) + return fmt.Errorf("error retrieving k8s version: %w", err) } nl.Infof(l, "Kubernetes version: %v", k8sVersion) minK8sVersion, err := util_version.ParseGeneric("1.22.0") if err != nil { - nl.Fatalf(l, "unexpected error parsing minimum supported version: %v", err) + return fmt.Errorf("unexpected error parsing minimum supported version: %w", err) } if !k8sVersion.AtLeast(minK8sVersion) { - nl.Fatalf(l, "Versions of Kubernetes < %v are not supported, please refer to the documentation for details on supported versions and legacy controller support.", minK8sVersion) + return fmt.Errorf("versions of kubernetes < %v are not supported, please refer to the documentation for details on supported versions and legacy controller support", minK8sVersion) } + return nil } // mustValidateIngressClass calls internally os.Exit diff --git a/cmd/nginx-ingress/main_test.go b/cmd/nginx-ingress/main_test.go index ccd3fec928..8e62435911 100644 --- a/cmd/nginx-ingress/main_test.go +++ b/cmd/nginx-ingress/main_test.go @@ -2,11 +2,18 @@ package main import ( "bytes" + "context" + "io" + "log/slog" "regexp" "testing" nl "github.com/nginxinc/kubernetes-ingress/internal/logger" + nic_glog "github.com/nginxinc/kubernetes-ingress/internal/logger/glog" "github.com/nginxinc/kubernetes-ingress/internal/logger/levels" + pkgversion "k8s.io/apimachinery/pkg/version" + fakediscovery "k8s.io/client-go/discovery/fake" + "k8s.io/client-go/kubernetes/fake" ) func TestLogFormats(t *testing.T) { @@ -46,3 +53,81 @@ func TestLogFormats(t *testing.T) { }) } } + +func TestK8sVersionValidation(t *testing.T) { + testCases := []struct { + name string + kubeVersion string + }{ + { + name: "Earliest version 1.22.0", + kubeVersion: "1.22.0", + }, + { + name: "Minor version 1.22.5", + kubeVersion: "1.22.5", + }, + { + name: "Close to current 1.32.0", + kubeVersion: "1.32.0", + }, + } + t.Parallel() + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // setup logger + l := slog.New(nic_glog.New(io.Discard, &nic_glog.Options{Level: levels.LevelInfo})) + ctx := nl.ContextWithLogger(context.Background(), l) + + // setup kube client with version + clientset := fake.NewSimpleClientset() + fakeDiscovery, _ := clientset.Discovery().(*fakediscovery.FakeDiscovery) + fakeDiscovery.FakedServerVersion = &pkgversion.Info{GitVersion: tc.kubeVersion} + + // run test + err := validateKubernetesVersionInfo(ctx, clientset) + if err != nil { + t.Errorf("%v", err) + } + }) + } +} + +func TestK8sVersionValidationBad(t *testing.T) { + testCases := []struct { + name string + kubeVersion string + }{ + { + name: "Before earliest version 1.21.0", + kubeVersion: "1.21.0", + }, + { + name: "Empty version", + kubeVersion: "", + }, + { + name: "Garbage", + kubeVersion: "xyzabc", + }, + } + t.Parallel() + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // setup logger + l := slog.New(nic_glog.New(io.Discard, &nic_glog.Options{Level: levels.LevelInfo})) + ctx := nl.ContextWithLogger(context.Background(), l) + + // setup kube client with version + clientset := fake.NewSimpleClientset() + fakeDiscovery, _ := clientset.Discovery().(*fakediscovery.FakeDiscovery) + fakeDiscovery.FakedServerVersion = &pkgversion.Info{GitVersion: tc.kubeVersion} + + // run test + err := validateKubernetesVersionInfo(ctx, clientset) + if err == nil { + t.Error("Wanted an error here") + } + }) + } +}