Skip to content

Commit

Permalink
e2e: cluster infrastructure getter layer
Browse files Browse the repository at this point in the history
Abstract from a specific way of getting information about a cluster.
Provide two ways of getting e.g. clusterplatform type.
Either by reading infrastructure.config.io/cluster object.
Or, be reading corresponding environment variables.
  • Loading branch information
ingvagabund committed Feb 9, 2023
1 parent 47e582a commit 67b4bd9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 32 deletions.
9 changes: 4 additions & 5 deletions test/extended/baremetal/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (
func skipIfNotBaremetal(oc *exutil.CLI) {
g.By("checking platform type")

infra, err := oc.AdminConfigClient().ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{})
platformType, err := exutil.ClusterInfrastructure(oc.AdminConfigClient()).PlatformType()
o.Expect(err).NotTo(o.HaveOccurred())

if infra.Status.PlatformStatus.Type != configv1.BareMetalPlatformType {
if platformType != configv1.BareMetalPlatformType {
e2eskipper.Skipf("No baremetal platform detected")
}
}
Expand All @@ -35,10 +34,10 @@ func skipIfNotBaremetal(oc *exutil.CLI) {
func skipIfUnsupportedPlatformOrConfig(oc *exutil.CLI, dc dynamic.Interface) {
g.By("checking supported platforms")

infra, err := oc.AdminConfigClient().ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{})
platformType, err := exutil.ClusterInfrastructure(oc.AdminConfigClient()).PlatformType()
o.Expect(err).NotTo(o.HaveOccurred())

switch infra.Status.PlatformStatus.Type {
switch platformType {
case configv1.BareMetalPlatformType:
return
case configv1.OpenStackPlatformType:
Expand Down
6 changes: 3 additions & 3 deletions test/extended/baremetal/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = g.Describe("[sig-installer][Feature:baremetal] Baremetal/OpenStack/vSphere/None/AWS platforms [apigroup:config.openshift.io]", func() {
var _ = g.Describe("[sig-installer][Feature:baremetal] Baremetal/OpenStack/vSphere/None/AWS platforms", func() {
defer g.GinkgoRecover()

var (
Expand All @@ -37,7 +37,7 @@ var _ = g.Describe("[sig-installer][Feature:baremetal] Baremetal/OpenStack/vSphe
})
})

var _ = g.Describe("[sig-installer][Feature:baremetal] Baremetal platform should [apigroup:config.openshift.io]", func() {
var _ = g.Describe("[sig-installer][Feature:baremetal] Baremetal platform should", func() {
defer g.GinkgoRecover()

oc := exutil.NewCLI("baremetal")
Expand Down Expand Up @@ -147,7 +147,7 @@ var _ = g.Describe("[sig-installer][Feature:baremetal] Baremetal platform should

// This block must be used for the serial tests. Any eventual extra worker deployed will be
// automatically deleted during the AfterEach
var _ = g.Describe("[sig-installer][Feature:baremetal][Serial] Baremetal platform should [apigroup:config.openshift.io]", func() {
var _ = g.Describe("[sig-installer][Feature:baremetal][Serial] Baremetal platform should", func() {
defer g.GinkgoRecover()

var (
Expand Down
15 changes: 7 additions & 8 deletions test/extended/ci/job_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,18 @@ var _ = g.Describe("[sig-ci] [Early] prow job name", func() {
}
isPeriodic := strings.HasPrefix(jobName, "periodic-")

g.It("should match platform type [apigroup:config.openshift.io]", func() {
g.It("should match platform type", func() {
if jobName == "" {
e2eskipper.Skipf("JOB_NAME env var not set, skipping")
} else if strings.Contains(jobName, "agnostic") {
e2eskipper.Skipf("JOB_NAME contains agnostic, not expecting platform in name")
}

infra, err := oc.AdminConfigClient().ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{})
platformType, err := exutil.ClusterInfrastructure(oc.AdminConfigClient()).PlatformType()
o.Expect(err).NotTo(o.HaveOccurred())

hasPlatform := true
platform := infra.Status.PlatformStatus.Type
switch platform {
switch platformType {
case v1.AlibabaCloudPlatformType:
if !strings.Contains(jobName, "alibaba") {
hasPlatform = false
Expand All @@ -55,13 +54,13 @@ var _ = g.Describe("[sig-ci] [Early] prow job name", func() {
hasPlatform = false
}
default:
if !strings.Contains(jobName, strings.ToLower(string(platform))) {
if !strings.Contains(jobName, strings.ToLower(string(platformType))) {
hasPlatform = false
}
}

if !hasPlatform {
result.Flakef("job name %q does not contain platform type in name (%s)", originalJobName, platform)
result.Flakef("job name %q does not contain platform type in name (%s)", originalJobName, platformType)
}

})
Expand All @@ -71,9 +70,9 @@ var _ = g.Describe("[sig-ci] [Early] prow job name", func() {
e2eskipper.Skipf("JOB_NAME env var not set, skipping")
}

network, err := oc.AdminConfigClient().ConfigV1().Networks().Get(context.Background(), "cluster", metav1.GetOptions{})
networkType, err := exutil.ClusterInfrastructure(oc.AdminConfigClient()).NetworkType()
o.Expect(err).NotTo(o.HaveOccurred())
switch network.Status.NetworkType {
switch networkType {
case "OpenShiftSDN":
if strings.Contains(jobName, "ovn") {
e2e.Failf("job name %q has mismatched network type in name (expected `sdn`, found `ovn`)", jobName)
Expand Down
14 changes: 7 additions & 7 deletions test/extended/networking/egress_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var _ = g.Describe("[sig-network][Feature:EgressFirewall]", func() {
)
noegFwoc := exutil.NewCLIWithPodSecurityLevel(noEgressFWE2E, admissionapi.LevelBaseline)
noegFwf := noegFwoc.KubeFramework()
g.It("egressFirewall should have no impact outside its namespace [apigroup:config.openshift.io]", func() {
g.It("egressFirewall should have no impact outside its namespace", func() {
g.By("creating test pod")
pod := "dummy"
o.Expect(createTestEgressFw(noegFwf, pod)).To(o.Succeed())
Expand All @@ -61,10 +61,10 @@ var _ = g.Describe("[sig-network][Feature:EgressFirewall]", func() {
return
}
g.By("sending traffic should all pass with no egress firewall impact")
infra, err := noegFwoc.AdminConfigClient().ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred(), "failed to get cluster-wide infrastructure")
platformType, err := exutil.ClusterInfrastructure(noegFwoc.AdminConfigClient()).PlatformType()
o.Expect(err).NotTo(o.HaveOccurred(), "failed to get cluster-wide infrastructure platform type")

if platformSupportICMP(infra.Status.PlatformStatus.Type) {
if platformSupportICMP(platformType) {
_, err := noegFwoc.Run("exec").Args(pod, "--", "ping", "-c", "1", "8.8.8.8").Output()
expectNoError(err)

Expand Down Expand Up @@ -105,10 +105,10 @@ func doEgressFwTest(f *e2e.Framework, oc *exutil.CLI, manifest string) error {
}

func sendEgressFwTraffic(f *e2e.Framework, oc *exutil.CLI, pod string) error {
infra, err := oc.AdminConfigClient().ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{})
o.Expect(err).NotTo(o.HaveOccurred(), "failed to get cluster-wide infrastructure")
platformType, err := exutil.ClusterInfrastructure(oc.AdminConfigClient()).PlatformType()
o.Expect(err).NotTo(o.HaveOccurred(), "failed to get cluster-wide infrastructure platform type")

if platformSupportICMP(infra.Status.PlatformStatus.Type) {
if platformSupportICMP(platformType) {
// Test ICMP / Ping to Google’s DNS IP (8.8.8.8) should pass
// because we have allow cidr rule for 8.8.8.8
g.By("sending traffic that matches allow cidr rule")
Expand Down
10 changes: 1 addition & 9 deletions test/extended/networking/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,6 @@ func openshiftSDNMode() string {
return *cachedNetworkPluginName
}

func platformType(configClient configv1client.Interface) (configv1.PlatformType, error) {
infrastructure, err := configClient.ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{})
if err != nil {
return "", err
}
return infrastructure.Status.PlatformStatus.Type, nil
}

func networkPluginName() string {
if cachedNetworkPluginName == nil {
// We don't use exutil.NewCLI() here because it can't be called from BeforeEach()
Expand Down Expand Up @@ -519,7 +511,7 @@ func InBareMetalIPv4ClusterContext(oc *exutil.CLI, body func()) {
Context("when running openshift ipv4 cluster on bare metal [apigroup:config.openshift.io]",
func() {
BeforeEach(func() {
pType, err := platformType(oc.AdminConfigClient())
pType, err := exutil.ClusterInfrastructure(oc.AdminConfigClient()).PlatformType()
expectNoError(err)
if pType != configv1.BareMetalPlatformType || getIPFamilyForCluster(oc.KubeFramework()) != IPv4 {
e2eskipper.Skipf("Not running in bare metal ipv4 cluster")
Expand Down
45 changes: 45 additions & 0 deletions test/extended/util/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
g "github.com/onsi/ginkgo/v2"
o "github.com/onsi/gomega"

configv1client "github.com/openshift/client-go/config/clientset/versioned"
configclient "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
authorizationapi "k8s.io/api/authorization/v1"
batchv1 "k8s.io/api/batch/v1"
Expand Down Expand Up @@ -2074,3 +2075,47 @@ func DoesApiResourceExist(config *rest.Config, apiResourceName, groupVersionName
func groupName(groupVersionName string) string {
return strings.Split(groupVersionName, "/")[0]
}

// Status.PlatformStatus.Type -> PLATFORM_TYPE
type clusterInfrastructure struct {
configClient configv1client.Interface
restConfig *rest.Config
}

func ClusterInfrastructure(configClient configv1client.Interface) *clusterInfrastructure {
return &clusterInfrastructure{
configClient: configClient,
}
}

func (ci *clusterInfrastructure) getInfrastructureConfig(client configv1client.Interface) (*configv1.Infrastructure, error) {
return ci.configClient.ConfigV1().Infrastructures().Get(context.Background(), "cluster", metav1.GetOptions{})
}

func (ci *clusterInfrastructure) getNetworkConfig(client configv1client.Interface) (*configv1.Network, error) {
return ci.configClient.ConfigV1().Networks().Get(context.Background(), "cluster", metav1.GetOptions{})
}

func (ci *clusterInfrastructure) PlatformType() (configv1.PlatformType, error) {
if os.Getenv("PLATFORM_TYPE") != "" {
return configv1.PlatformType(os.Getenv("PLATFORM_TYPE")), nil
}
// will return an error if infrastructures.config.openshift.io does not exist
infra, err := ci.getInfrastructureConfig(ci.configClient)
if err != nil {
return "", nil
}
return infra.Status.PlatformStatus.Type, nil
}

func (ci *clusterInfrastructure) NetworkType() (string, error) {
if os.Getenv("NETWORK_TYPE") != "" {
return os.Getenv("NETWORK_TYPE"), nil
}
// will return an error if infrastructures.config.openshift.io does not exist
infra, err := ci.getNetworkConfig(ci.configClient)
if err != nil {
return "", nil
}
return infra.Status.NetworkType, nil
}

0 comments on commit 67b4bd9

Please sign in to comment.