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 <[email protected]>
  • Loading branch information
georgethebeatle and danail-branekov committed Aug 15, 2023
1 parent 914d839 commit 60aa94a
Show file tree
Hide file tree
Showing 4 changed files with 38 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
36 changes: 36 additions & 0 deletions tests/helpers/k8s.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package helpers

import (
"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)
Expect(err).NotTo(HaveOccurred())

minorVersion, err := strconv.Atoi(serverVersion.Minor)
Expect(err).NotTo(HaveOccurred())

return majorVersion, minorVersion
}

0 comments on commit 60aa94a

Please sign in to comment.