Skip to content

Commit

Permalink
Check kubernetes server version from within the test
Browse files Browse the repository at this point in the history
Thus we simplify e2e tests setup

Co-authored-by: Danail Branekov <danailster@gmail.com>
Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com>
  • Loading branch information
danail-branekov and georgethebeatle committed Aug 15, 2023
1 parent a426011 commit 85e5026
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
6 changes: 0 additions & 6 deletions tests/e2e/e2e_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -50,8 +49,6 @@ var (
commonTestOrgGUID string
commonTestOrgName string
assetsTmpDir string
clusterVersionMinor int
clusterVersionMajor int
defaultAppBitsFile string
multiProcessAppBitsFile string
)
Expand Down Expand Up @@ -1030,9 +1027,6 @@ func commonTestSetup() {

appFQDN = helpers.GetRequiredEnvVar("APP_FQDN")

clusterVersionMinor, _ = strconv.Atoi(helpers.GetRequiredEnvVar("CLUSTER_VERSION_MINOR"))
clusterVersionMajor, _ = strconv.Atoi(helpers.GetRequiredEnvVar("CLUSTER_VERSION_MAJOR"))

ensureServerIsUp()

serviceAccountFactory = helpers.NewServiceAccountFactory(rootNamespace)
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/orgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ var _ = Describe("Orgs", func() {
})

It("doesn't set an HTTP warning header for long certs", func() {
clusterVersionMajor, clusterVersionMinor := helpers.GetClusterVersion()
if clusterVersionMajor < 1 || (clusterVersionMajor == 1 && clusterVersionMinor < 22) {
GinkgoWriter.Printf("Skipping certificate warning test as k8s v%d.%d doesn't support creation of short lived test client certificates\n", clusterVersionMajor, clusterVersionMinor)
return
Expand Down
8 changes: 1 addition & 7 deletions tests/helpers/cert_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
certv1 "k8s.io/api/certificates/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -149,11 +148,6 @@ func generateCSR(k8sClient client.Client, key *rsa.PrivateKey, userName string,
func approveCSR(csr *certv1.CertificateSigningRequest) {
GinkgoHelper()

config, err := controllerruntime.GetConfig()
Expect(err).NotTo(HaveOccurred())
clientSet, err := kubernetes.NewForConfig(config)
Expect(err).NotTo(HaveOccurred())

csr.Status.Conditions = append(csr.Status.Conditions, certv1.CertificateSigningRequestCondition{
Type: certv1.RequestConditionType(certv1.CertificateApproved),
Status: corev1.ConditionTrue,
Expand All @@ -162,7 +156,7 @@ func approveCSR(csr *certv1.CertificateSigningRequest) {
LastUpdateTime: metav1.Now(),
})

_, err = clientSet.CertificatesV1().CertificateSigningRequests().UpdateApproval(
_, err := getClientSet().CertificatesV1().CertificateSigningRequests().UpdateApproval(
context.Background(),
csr.Name,
csr,
Expand Down
41 changes: 41 additions & 0 deletions tests/helpers/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package helpers

import (
"fmt"
"strconv"

. "github.com/onsi/ginkgo/v2" //lint:ignore ST1001 this is a test file
. "github.com/onsi/gomega" //lint:ignore ST1001 this is a test file
"k8s.io/client-go/kubernetes"
controllerruntime "sigs.k8s.io/controller-runtime"
)

func getClientSet() *kubernetes.Clientset {
GinkgoHelper()

config, err := controllerruntime.GetConfig()
Expect(err).NotTo(HaveOccurred())
clientSet, err := kubernetes.NewForConfig(config)
Expect(err).NotTo(HaveOccurred())

return clientSet
}

func GetClusterVersion() (int, int) {
GinkgoHelper()

serverVersion, err := getClientSet().ServerVersion()
Expect(err).NotTo(HaveOccurred())

majorVersion, err := strconv.Atoi(serverVersion.Major)
if err != nil {
Skip(fmt.Sprintf("cannot determine kubernetes server major version: %v", err))
}

minorVersion, err := strconv.Atoi(serverVersion.Minor)
if err != nil {
Skip(fmt.Sprintf("cannot determine kubernetes server minor version: %v", err))
}

return majorVersion, minorVersion
}

0 comments on commit 85e5026

Please sign in to comment.