Skip to content

Commit

Permalink
support external control plane
Browse files Browse the repository at this point in the history
Signed-off-by: Zenghui Shi <zshi@redhat.com>
  • Loading branch information
zshi-redhat committed May 26, 2022
1 parent 7f732b8 commit 519ff98
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 6 deletions.
8 changes: 8 additions & 0 deletions bindata/manifests/operator-webhook/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ spec:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
{{ if .ExternalControlPlane }}
- key: node-role.kubernetes.io/worker
{{ else }}
- key: node-role.kubernetes.io/master
{{ end }}
operator: Exists
tolerations:
{{ if .ExternalControlPlane }}
- key: "node-role.kubernetes.io/worker"
{{ else }}
- key: "node-role.kubernetes.io/master"
{{ end }}
operator: Exists
effect: NoSchedule
- key: "node.kubernetes.io/not-ready"
Expand Down
10 changes: 9 additions & 1 deletion bindata/manifests/webhook/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ metadata:
namespace: {{.Namespace}}
annotations:
kubernetes.io/description: |
This daemon set launches the network resource injector component on master nodes.
This daemon set launches the network resource injector component on master or worker nodes.
release.openshift.io/version: "{{.ReleaseVersion}}"
spec:
selector:
Expand All @@ -35,10 +35,18 @@ spec:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
{{ if .ExternalControlPlane }}
- key: node-role.kubernetes.io/worker
{{ else }}
- key: node-role.kubernetes.io/master
{{ end }}
operator: Exists
tolerations:
{{ if .ExternalControlPlane }}
- key: "node-role.kubernetes.io/worker"
{{ else }}
- key: "node-role.kubernetes.io/master"
{{ end }}
operator: Exists
effect: NoSchedule
- key: "node.kubernetes.io/not-ready"
Expand Down
6 changes: 6 additions & 0 deletions controllers/sriovoperatorconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,12 @@ func (r *SriovOperatorConfigReconciler) syncWebhookObjs(dc *sriovnetworkv1.Sriov
data.Data["ClusterType"] = utils.ClusterType
data.Data["CaBundle"] = os.Getenv("WEBHOOK_CA_BUNDLE")
data.Data["DevMode"] = os.Getenv("DEV_MODE")
external, err := utils.IsExternalControlPlaneCluster(r.Client)
if err != nil {
logger.Error(err, "Fail to get control plane topology")
return err
}
data.Data["ExternalControlPlane"] = external
objs, err := render.RenderDir(path, &data)
if err != nil {
logger.Error(err, "Fail to render webhook manifests")
Expand Down
14 changes: 14 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
. "github.com/onsi/ginkgo"
"github.com/onsi/ginkgo/config"
. "github.com/onsi/gomega"
openshiftconfigv1 "github.com/openshift/api/config/v1"
mcfgv1 "github.com/openshift/machine-config-operator/pkg/apis/machineconfiguration.openshift.io/v1"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -80,6 +81,8 @@ var _ = BeforeSuite(func(done Done) {
Expect(err).NotTo(HaveOccurred())
err = mcfgv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
err = openshiftconfigv1.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

//+kubebuilder:scaffold:scheme

Expand Down Expand Up @@ -158,6 +161,17 @@ var _ = BeforeSuite(func(done Done) {
}
Expect(k8sClient.Create(context.TODO(), config)).Should(Succeed())

infra := &openshiftconfigv1.Infrastructure{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster",
},
Spec: openshiftconfigv1.InfrastructureSpec{},
Status: openshiftconfigv1.InfrastructureStatus{
ControlPlaneTopology: openshiftconfigv1.HighlyAvailableTopologyMode,
},
}
Expect(k8sClient.Create(context.TODO(), infra)).Should(Succeed())

poolConfig := &sriovnetworkv1.SriovNetworkPoolConfig{}
poolConfig.SetNamespace(testNamespace)
poolConfig.SetName(constants.DEFAULT_CONFIG_NAME)
Expand Down
33 changes: 28 additions & 5 deletions pkg/utils/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,34 @@ const (

func IsSingleNodeCluster(c client.Client) (bool, error) {
if os.Getenv("CLUSTER_TYPE") == ClusterTypeOpenshift {
return openshiftSingleNodeClusterStatus(c)
topo, err := openshiftControlPlaneTopologyStatus(c)
if err != nil {
return false, err
}
switch topo {
case configv1.SingleReplicaTopologyMode:
return true, nil
}
return false, nil
}
return k8sSingleNodeClusterStatus(c)
}

func IsExternalControlPlaneCluster(c client.Client) (bool, error) {
if os.Getenv("CLUSTER_TYPE") == ClusterTypeOpenshift {
topo, err := openshiftControlPlaneTopologyStatus(c)
if err != nil {
return false, err
}
switch topo {
case configv1.ExternalTopologyMode:
return true, nil
}
return false, nil
}
return false, nil
}

func k8sSingleNodeClusterStatus(c client.Client) (bool, error) {
nodeList := &corev1.NodeList{}
err := c.List(context.TODO(), nodeList)
Expand All @@ -40,14 +63,14 @@ func k8sSingleNodeClusterStatus(c client.Client) (bool, error) {
return false, nil
}

func openshiftSingleNodeClusterStatus(c client.Client) (bool, error) {
func openshiftControlPlaneTopologyStatus(c client.Client) (configv1.TopologyMode, error) {
infra := &configv1.Infrastructure{}
err := c.Get(context.TODO(), types.NamespacedName{Name: infraResourceName}, infra)
if err != nil {
return false, err
return "", err
}
if infra == nil {
return false, fmt.Errorf("getting resource Infrastructure (name: %s) succeeded but object was nil", infraResourceName)
return "", fmt.Errorf("getting resource Infrastructure (name: %s) succeeded but object was nil", infraResourceName)
}
return infra.Status.ControlPlaneTopology == configv1.SingleReplicaTopologyMode, nil
return infra.Status.ControlPlaneTopology, nil
}
Loading

0 comments on commit 519ff98

Please sign in to comment.