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 c8be741
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
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
29 changes: 29 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,31 @@ 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) 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
}

0 comments on commit c8be741

Please sign in to comment.