Skip to content

Commit

Permalink
add tests for kubernetes versions (#7035)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabelf5 authored Dec 23, 2024
1 parent becafe7 commit 93eec0d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
15 changes: 9 additions & 6 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
85 changes: 85 additions & 0 deletions cmd/nginx-ingress/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
}
})
}
}

0 comments on commit 93eec0d

Please sign in to comment.