Skip to content

Commit

Permalink
Add unit test for multus admission controller for HyperShift
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan-cox committed Mar 22, 2024
1 parent 55994db commit f05474d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
5 changes: 2 additions & 3 deletions pkg/network/multus_admission_controller.go
Expand Up @@ -46,11 +46,10 @@ func getOpenshiftNamespaces(client cnoclient.Client) (string, error) {
}

// renderMultusAdmissonControllerConfig returns the manifests of Multus Admisson Controller
func renderMultusAdmissonControllerConfig(manifestDir string, externalControlPlane bool, bootstrapResult *bootstrap.BootstrapResult, client cnoclient.Client) ([]*uns.Unstructured, error) {
func renderMultusAdmissonControllerConfig(manifestDir string, externalControlPlane bool, bootstrapResult *bootstrap.BootstrapResult, client cnoclient.Client, hsc *hypershift.HyperShiftConfig, clientName string) ([]*uns.Unstructured, error) {
objs := []*uns.Unstructured{}
var err error

hsc := hypershift.NewHyperShiftConfig()
replicas := getMultusAdmissionControllerReplicas(bootstrapResult, hsc.Enabled)
if ignoredNamespaces == "" {
ignoredNamespaces, err = getOpenshiftNamespaces(client)
Expand Down Expand Up @@ -85,7 +84,7 @@ func renderMultusAdmissonControllerConfig(manifestDir string, externalControlPla
data.Data["CAConfigMapKey"] = hsc.CAConfigMapKey

serviceCA := &corev1.ConfigMap{}
err := client.ClientFor(names.ManagementClusterName).CRClient().Get(
err := client.ClientFor(clientName).CRClient().Get(
context.TODO(), types.NamespacedName{Namespace: hsc.Namespace, Name: hsc.CAConfigMap}, serviceCA)
if err != nil {
return nil, fmt.Errorf("failed to get managments clusters service CA: %v", err)
Expand Down
79 changes: 79 additions & 0 deletions pkg/network/multus_admission_controller_test.go
@@ -1,6 +1,7 @@
package network

import (
"github.com/openshift/cluster-network-operator/pkg/hypershift"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -84,6 +85,84 @@ func TestRenderMultusAdmissionController(t *testing.T) {
g.Expect(objs).To(ContainElement(HaveKubernetesID("Deployment", "openshift-multus", "multus-admission-controller")))
}

// TestRenderMultusAdmissionController has some simple rendering tests
func TestRenderMultusAdmissonControllerConfigForHyperShift(t *testing.T) {
g := NewGomegaWithT(t)

crd := MultusAdmissionControllerConfig.DeepCopy()
config := &crd.Spec
disabled := true
config.DisableMultiNetwork = &disabled
fillDefaults(config, nil)

fakeClient := cnofake.NewFakeClient(
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test1-ignored",
Labels: map[string]string{
"openshift.io/cluster-monitoring": "true",
},
},
},
&corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "test2-not-ignored",
},
},
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "MyCM",
Namespace: "test1-ignored",
},
Data: map[string]string{
"MyCMKey": "key",
},
},
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "openshift-service-ca.crt",
Namespace: "test1-ignored",
},
Data: map[string]string{
"MyCMKey": "key",
},
},
&corev1.Namespace{ObjectMeta: metav1.ObjectMeta{
Name: "test3-ignored",
Labels: map[string]string{
"openshift.io/cluster-monitoring": "true",
},
},
})
bootstrap := fakeBootstrapResultWithHyperShift()

hsc := hypershift.NewHyperShiftConfig()
hsc.Enabled = true
hsc.CAConfigMap = "MyCM"
hsc.CAConfigMapKey = "MyCMKey"
hsc.Name = "MyCluster"
hsc.Namespace = "test1-ignored"
hsc.RunAsUser = "1001"
hsc.ReleaseImage = "MyImage"
hsc.ControlPlaneImage = "MyCPOImage"

objs, err := renderMultusAdmissonControllerConfig(manifestDir, false, bootstrap, fakeClient, hsc, "")
g.Expect(err).NotTo(HaveOccurred())

// Check rendered object
for _, obj := range objs {
if obj.GetKind() == "Service" && obj.GetName() == "multus-admission-controller" {
labels := obj.GetLabels()
g.Expect(len(labels)).To(Equal(2))
g.Expect(labels["hypershift.openshift.io/allow-guest-webhooks"]).To(Equal("true"))

annotations := obj.GetAnnotations()
g.Expect(len(annotations)).To(Equal(1))
g.Expect(annotations["network.operator.openshift.io/cluster-name"]).To(Equal("management"))
}
}
}

// TestRenderMultusAdmissionControllerGetNamespace tests getOpenshiftNamespaces()
func TestRenderMultusAdmissionControllerGetNamespace(t *testing.T) {
g := NewGomegaWithT(t)
Expand Down
4 changes: 3 additions & 1 deletion pkg/network/render.go
@@ -1,6 +1,7 @@
package network

import (
"github.com/openshift/cluster-network-operator/pkg/names"
"log"
"net"
"os"
Expand Down Expand Up @@ -770,8 +771,9 @@ func renderMultusAdmissionController(conf *operv1.NetworkSpec, manifestDir strin
var err error
out := []*uns.Unstructured{}

hsc := hypershift.NewHyperShiftConfig()
objs, err := renderMultusAdmissonControllerConfig(manifestDir, externalControlPlane,
bootstrapResult, client)
bootstrapResult, client, hsc, names.ManagementClusterName)
if err != nil {
return nil, err
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/network/testutil_test.go
Expand Up @@ -8,6 +8,7 @@ import (
configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/cluster-network-operator/pkg/bootstrap"
"github.com/openshift/cluster-network-operator/pkg/client"
"github.com/openshift/cluster-network-operator/pkg/hypershift"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
Expand Down Expand Up @@ -77,6 +78,27 @@ func fakeBootstrapResult() *bootstrap.BootstrapResult {
}
}

func fakeBootstrapResultWithHyperShift() *bootstrap.BootstrapResult {
return &bootstrap.BootstrapResult{
Infra: bootstrap.InfraStatus{
PlatformType: "GCP",
PlatformRegion: "moon-2",
ControlPlaneTopology: configv1.HighlyAvailableTopologyMode,
InfrastructureTopology: configv1.HighlyAvailableTopologyMode,
APIServers: map[string]bootstrap.APIServer{
bootstrap.APIServerDefault: {
Host: "testing.test",
Port: "8443",
},
},
HostedControlPlane: &hypershift.HostedControlPlane{
ClusterID: "test",
NodeSelector: map[string]string{},
},
},
}
}

// createProxy creates an empty proxy object.
func createProxy(client client.Client) error {
proxy := &configv1.Proxy{
Expand Down

0 comments on commit f05474d

Please sign in to comment.