Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address TODOs regarding cluster network discovery #3162

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions controllers/submariner/submariner_networkdiscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ func (r *Reconciler) discoverNetwork(ctx context.Context, submariner *submopv1a1

submariner.Status.NetworkPlugin = clusterNetwork.NetworkPlugin

// TODO: globalCIDR allocation if no global CIDR is assigned and enabled.
// currently the clusterNetwork discovers any existing operator setting,
// but that's not really helpful here
return clusterNetwork, err
}

Expand Down
49 changes: 25 additions & 24 deletions pkg/discovery/network/generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/fake"
"github.com/submariner-io/submariner-operator/api/v1alpha1"
"github.com/submariner-io/submariner-operator/pkg/discovery/network"
"github.com/submariner-io/submariner-operator/pkg/names"
"github.com/submariner-io/submariner/pkg/cni"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
controllerClient "sigs.k8s.io/controller-runtime/pkg/client"
fakeClient "sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand All @@ -40,9 +43,9 @@ const (
)

var _ = Describe("Generic Network", func() {
When("There is a kube-proxy with no expected parameters", func() {
var clusterNet *network.ClusterNetwork
var clusterNet *network.ClusterNetwork

When("There is a kube-proxy with no expected parameters", func() {
BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -65,8 +68,6 @@ var _ = Describe("Generic Network", func() {
})

When("There is a kube-controller with no expected parameters", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -89,8 +90,6 @@ var _ = Describe("Generic Network", func() {
})

When("There is a kube-api with no expected parameters", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -113,8 +112,6 @@ var _ = Describe("Generic Network", func() {
})

When("There is a kube-controller pod with the right parameter", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -137,8 +134,6 @@ var _ = Describe("Generic Network", func() {
})

When("There is a kube-proxy pod but no kube-controller", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -161,8 +156,6 @@ var _ = Describe("Generic Network", func() {
})

When("There is a kubeapi pod", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -185,8 +178,6 @@ var _ = Describe("Generic Network", func() {
})

When("There is a kube-proxy and api pods", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -207,8 +198,6 @@ var _ = Describe("Generic Network", func() {
})

When("No pod CIDR information exists on any node", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -231,8 +220,6 @@ var _ = Describe("Generic Network", func() {
})

When("Pod CIDR information exists on a single node cluster", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -254,8 +241,6 @@ var _ = Describe("Generic Network", func() {
})

When("Pod CIDR information exists on a multi node cluster", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand All @@ -270,8 +255,6 @@ var _ = Describe("Generic Network", func() {
})

When("Both pod and service CIDR information exists", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(
ctx,
Expand Down Expand Up @@ -312,8 +295,6 @@ var _ = Describe("Generic Network", func() {
})

When("No kube-api pod exists and invalid service creation returns the expected error", func() {
var clusterNet *network.ClusterNetwork

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(ctx)
})
Expand All @@ -330,6 +311,26 @@ var _ = Describe("Generic Network", func() {
Expect(clusterNet.ServiceCIDRs).To(Equal([]string{testServiceCIDRFromService}))
})
})

When("the Submariner resource exists", func() {
const globalCIDR = "242.112.0.0/24"

BeforeEach(func(ctx SpecContext) {
clusterNet = testDiscoverGenericWith(ctx, &v1alpha1.Submariner{
ObjectMeta: metav1.ObjectMeta{
Name: names.SubmarinerCrName,
},
Spec: v1alpha1.SubmarinerSpec{
GlobalCIDR: globalCIDR,
},
})
})

It("should return the ClusterNetwork structure with the global CIDR", func() {
Expect(clusterNet.GlobalCIDR).To(Equal(globalCIDR))
clusterNet.Show()
})
})
})

func testDiscoverGenericWith(ctx context.Context, objects ...controllerClient.Object) *network.ClusterNetwork {
Expand Down
31 changes: 11 additions & 20 deletions pkg/discovery/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,13 @@ func (cn *ClusterNetwork) IsComplete() bool {

func Discover(ctx context.Context, client controllerClient.Client, operatorNamespace string) (*ClusterNetwork, error) {
discovery, err := networkPluginsDiscovery(ctx, client)
if err != nil {
return nil, err
}

if discovery != nil {
// TODO: The other branch of this if will not try to find the globalCIDRs
globalCIDR, _ := getGlobalCIDRs(ctx, client, operatorNamespace)
discovery.GlobalCIDR = globalCIDR

if discovery.IsComplete() {
return discovery, nil
}

// If the info we got from the non-generic plugins is incomplete
// try to complete with the generic discovery mechanisms
if len(discovery.ServiceCIDRs) == 0 || len(discovery.PodCIDRs) == 0 {
aswinsuryan marked this conversation as resolved.
Show resolved Hide resolved
genericNet, err := discoverGenericNetwork(ctx, client)
if err != nil {
return nil, err
}
if !discovery.IsComplete() {
var genericNet *ClusterNetwork

genericNet, err = discoverGenericNetwork(ctx, client)
if genericNet != nil {
if len(discovery.ServiceCIDRs) == 0 {
discovery.ServiceCIDRs = genericNet.ServiceCIDRs
Expand All @@ -96,12 +82,17 @@ func Discover(ctx context.Context, client controllerClient.Client, operatorNames
}
}
}
} else {
// If nothing specific was discovered, use the generic discovery
discovery, err = discoverGenericNetwork(ctx, client)
}

return discovery, nil
if discovery != nil {
globalCIDR, _ := getGlobalCIDRs(ctx, client, operatorNamespace)
discovery.GlobalCIDR = globalCIDR
}

// If nothing specific was discovered, use the generic discovery
return discoverGenericNetwork(ctx, client)
return discovery, err
}

type pluginDiscoveryFn func(context.Context, controllerClient.Client) (*ClusterNetwork, error)
Expand Down
7 changes: 7 additions & 0 deletions pkg/discovery/network/network_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/submariner-io/submariner-operator/api/v1alpha1"
v1 "k8s.io/api/core/v1"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
)

func init() {
utilruntime.Must(v1alpha1.AddToScheme(scheme.Scheme))
}

func TestOpenShift4NetworkDiscovery(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Network Discovery")
Expand Down
Loading