From c5c0cfd74e902cd9757ad7a804deaa36359ada68 Mon Sep 17 00:00:00 2001 From: Andrew Beekhof Date: Wed, 28 Aug 2019 10:06:32 +1000 Subject: [PATCH] Use 'Replica: 1' on clusters with a single master --- .../machineremediation/v1alpha1/BUILD.bazel | 2 ++ .../machineremediation/v1alpha1/replicas.go | 35 +++++++++++++++++++ pkg/operator/operator_test.go | 4 +-- pkg/operator/resources.go | 2 +- 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 pkg/apis/machineremediation/v1alpha1/replicas.go diff --git a/pkg/apis/machineremediation/v1alpha1/BUILD.bazel b/pkg/apis/machineremediation/v1alpha1/BUILD.bazel index bbac7197..71397451 100644 --- a/pkg/apis/machineremediation/v1alpha1/BUILD.bazel +++ b/pkg/apis/machineremediation/v1alpha1/BUILD.bazel @@ -9,6 +9,7 @@ go_library( "machineremediation_types.go", "machineremediationoperator_types.go", "register.go", + "replicas.go", "zz_generated.deepcopy.go", ], importpath = "kubevirt.io/machine-remediation-operator/pkg/apis/machineremediation/v1alpha1", @@ -19,5 +20,6 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library", "//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", + "//vendor/sigs.k8s.io/controller-runtime/pkg/client:go_default_library", ], ) diff --git a/pkg/apis/machineremediation/v1alpha1/replicas.go b/pkg/apis/machineremediation/v1alpha1/replicas.go new file mode 100644 index 00000000..87a30f12 --- /dev/null +++ b/pkg/apis/machineremediation/v1alpha1/replicas.go @@ -0,0 +1,35 @@ +// NOTE: Boilerplate only. Ignore this file. + +// Package v1alpha1 contains API Schema definitions for the healthchecking v1alpha1 API group +// +k8s:deepcopy-gen=package,register +// +groupName=machineremediation.kubevirt.io +package v1alpha1 + +import ( + "context" + corev1 "k8s.io/api/core/v1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +const ( + NamespaceOpenShiftMachineAPI = "openshift-machine-api" + MasterNodeRoleLabel = "node-role.kubernetes.io/master" +) + +func GetReplicaCount(c client.Client) int32 { + masterNodes := &corev1.NodeList{} + err := c.List( + context.TODO(), + masterNodes, + client.InNamespace(NamespaceOpenShiftMachineAPI), + client.MatchingLabels(map[string]string{MasterNodeRoleLabel: ""}), + ) + if err != nil { + return 1 + } + if len(masterNodes.Items) < 2 { + return 1 + } + return 2 +} + diff --git a/pkg/operator/operator_test.go b/pkg/operator/operator_test.go index 81b75476..79e994fb 100644 --- a/pkg/operator/operator_test.go +++ b/pkg/operator/operator_test.go @@ -138,8 +138,8 @@ func TestReconcile(t *testing.T) { // update all deployments status to have desired number of replicas for _, d := range deploys.Items { - d.Status.Replicas = 2 - d.Status.UpdatedReplicas = 2 + d.Status.Replicas = mrv1.GetReplicaCount(r.client) + d.Status.UpdatedReplicas = mrv1.GetReplicaCount(r.client) assert.NoError(t, r.client.Update(context.TODO(), &d)) } diff --git a/pkg/operator/resources.go b/pkg/operator/resources.go index 670af050..326f2747 100644 --- a/pkg/operator/resources.go +++ b/pkg/operator/resources.go @@ -48,7 +48,7 @@ func (r *ReconcileMachineRemediationOperator) createOrUpdateDeployment(data *com } newDeploy := components.NewDeployment(data) - newDeploy.Spec.Replicas = pointer.Int32Ptr(2) + newDeploy.Spec.Replicas = pointer.Int32Ptr(mrv1.GetReplicaCount(r.client)) oldDeploy, err := r.getDeployment(data.Name, data.Namespace) if errors.IsNotFound(err) {