Label vBMHs with SIP name along with namespace

Relates-To: #5

Change-Id: Ia3e553f5032ba199abc18345cd3fedbfe381578c
This commit is contained in:
Ian Howell 2021-03-11 15:50:39 -06:00
parent 44ee037f5b
commit 58faa0d491
2 changed files with 32 additions and 28 deletions

View File

@ -242,7 +242,7 @@ func (ml *MachineList) identifyNodes(sip airshipv1.SIPCluster,
return err return err
} }
logger.Info("Matching hosts against constraints") logger.Info("Matching hosts against constraints")
err = ml.scheduleIt(nodeRole, nodeCfg, bmhList, scheduleSetMap, c, sip.GetNamespace()) err = ml.scheduleIt(nodeRole, nodeCfg, bmhList, scheduleSetMap, c, GetClusterLabel(sip))
if err != nil { if err != nil {
return err return err
} }
@ -273,12 +273,12 @@ func (ml *MachineList) initScheduleMaps(role airshipv1.BMHRole,
} }
func (ml *MachineList) countScheduledAndTobeScheduled(nodeRole airshipv1.BMHRole, func (ml *MachineList) countScheduledAndTobeScheduled(nodeRole airshipv1.BMHRole,
c client.Client, namespace string) int { c client.Client, clusterName string) int {
bmhList := &metal3.BareMetalHostList{} bmhList := &metal3.BareMetalHostList{}
scheduleLabels := map[string]string{ scheduleLabels := map[string]string{
SipScheduleLabel: "true", SipScheduleLabel: "true",
SipClusterLabel: namespace, SipClusterLabel: clusterName,
SipNodeTypeLabel: string(nodeRole), SipNodeTypeLabel: string(nodeRole),
} }
@ -318,14 +318,14 @@ func (ml *MachineList) countScheduledAndTobeScheduled(nodeRole airshipv1.BMHRole
func (ml *MachineList) scheduleIt(nodeRole airshipv1.BMHRole, nodeCfg airshipv1.NodeSet, func (ml *MachineList) scheduleIt(nodeRole airshipv1.BMHRole, nodeCfg airshipv1.NodeSet,
bmList *metal3.BareMetalHostList, scheduleSet *ScheduleSet, bmList *metal3.BareMetalHostList, scheduleSet *ScheduleSet,
c client.Client, namespace string) error { c client.Client, clusterName string) error {
logger := ml.Log.WithValues("role", nodeRole) logger := ml.Log.WithValues("role", nodeRole)
validBmh := true validBmh := true
// Count the expectations stated in the CR // Count the expectations stated in the CR
// Reduce from the list of BMH's already scheduled and labeled with the Cluster Name // Reduce from the list of BMH's already scheduled and labeled with the Cluster Name
// Reduce from the number of Machines I have identified already to be Labeled // Reduce from the number of Machines I have identified already to be Labeled
totalNodes := nodeCfg.Count.Active + nodeCfg.Count.Standby totalNodes := nodeCfg.Count.Active + nodeCfg.Count.Standby
nodeTarget := totalNodes - ml.countScheduledAndTobeScheduled(nodeRole, c, namespace) nodeTarget := totalNodes - ml.countScheduledAndTobeScheduled(nodeRole, c, clusterName)
logger.Info("BMH count that need to be scheduled for SIP cluster discouting nodes ready to be scheduled", logger.Info("BMH count that need to be scheduled for SIP cluster discouting nodes ready to be scheduled",
"BMH count to be scheduled", nodeTarget) "BMH count to be scheduled", nodeTarget)
@ -748,7 +748,7 @@ func (ml *MachineList) ApplyLabels(sip airshipv1.SIPCluster, c client.Client) er
if machine.ScheduleStatus == ToBeScheduled { if machine.ScheduleStatus == ToBeScheduled {
bmh := &machine.BMH bmh := &machine.BMH
fmt.Printf("ApplyLabels bmh.ObjectMeta.Name:%s\n", bmh.ObjectMeta.Name) fmt.Printf("ApplyLabels bmh.ObjectMeta.Name:%s\n", bmh.ObjectMeta.Name)
bmh.Labels[SipClusterLabel] = sip.GetNamespace() bmh.Labels[SipClusterLabel] = GetClusterLabel(sip)
bmh.Labels[SipScheduleLabel] = "true" bmh.Labels[SipScheduleLabel] = "true"
bmh.Labels[SipNodeTypeLabel] = string(machine.BMHRole) bmh.Labels[SipNodeTypeLabel] = string(machine.BMHRole)
@ -794,7 +794,7 @@ func (ml *MachineList) GetCluster(sip airshipv1.SIPCluster, c client.Client) err
bmhList := &metal3.BareMetalHostList{} bmhList := &metal3.BareMetalHostList{}
scheduleLabels := map[string]string{ scheduleLabels := map[string]string{
SipScheduleLabel: "true", SipScheduleLabel: "true",
SipClusterLabel: sip.GetNamespace(), SipClusterLabel: GetClusterLabel(sip),
} }
err := c.List(context.Background(), bmhList, client.MatchingLabels(scheduleLabels)) err := c.List(context.Background(), bmhList, client.MatchingLabels(scheduleLabels))
@ -816,3 +816,7 @@ func (ml *MachineList) GetCluster(sip airshipv1.SIPCluster, c client.Client) err
fmt.Printf("GetCluster %s \n", ml.String()) fmt.Printf("GetCluster %s \n", ml.String())
return nil return nil
} }
func GetClusterLabel(sip airshipv1.SIPCluster) string {
return fmt.Sprintf("%s_%s", sip.GetNamespace(), sip.GetName())
}

View File

@ -70,8 +70,8 @@ var _ = Describe("SIPCluster controller", func() {
} }
// Create SIP cluster // Create SIP cluster
name := "subcluster-test1" clusterName := "subcluster-test1"
sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(name, testNamespace, 3, 4) sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(clusterName, testNamespace, 3, 4)
Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed())
Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed())
@ -79,7 +79,7 @@ var _ = Describe("SIPCluster controller", func() {
Eventually(func() error { Eventually(func() error {
expectedLabels := map[string]string{ expectedLabels := map[string]string{
bmhpkg.SipScheduleLabel: "true", bmhpkg.SipScheduleLabel: "true",
bmhpkg.SipClusterLabel: testNamespace, bmhpkg.SipClusterLabel: bmhpkg.GetClusterLabel(*sipCluster),
} }
var bmh metal3.BareMetalHost var bmh metal3.BareMetalHost
@ -107,8 +107,8 @@ var _ = Describe("SIPCluster controller", func() {
} }
// Create SIP cluster // Create SIP cluster
name := "subcluster-test2" clusterName := "subcluster-test2"
sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(name, testNamespace, 3, 4) sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(clusterName, testNamespace, 3, 4)
Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed())
Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed())
@ -132,7 +132,7 @@ var _ = Describe("SIPCluster controller", func() {
// Validate SIP CR ready condition has been updated // Validate SIP CR ready condition has been updated
var sipCR airshipv1.SIPCluster var sipCR airshipv1.SIPCluster
Expect(k8sClient.Get(context.Background(), types.NamespacedName{ Expect(k8sClient.Get(context.Background(), types.NamespacedName{
Name: name, Name: clusterName,
Namespace: testNamespace, Namespace: testNamespace,
}, &sipCR)).To(Succeed()) }, &sipCR)).To(Succeed())
@ -154,8 +154,8 @@ var _ = Describe("SIPCluster controller", func() {
} }
// Create SIP cluster // Create SIP cluster
name := "subcluster-test4" clusterName := "subcluster-test4"
sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(name, testNamespace, 3, 4) sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(clusterName, testNamespace, 3, 4)
Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed())
Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed())
@ -179,7 +179,7 @@ var _ = Describe("SIPCluster controller", func() {
// Validate SIP CR ready condition has been updated // Validate SIP CR ready condition has been updated
var sipCR airshipv1.SIPCluster var sipCR airshipv1.SIPCluster
Expect(k8sClient.Get(context.Background(), types.NamespacedName{ Expect(k8sClient.Get(context.Background(), types.NamespacedName{
Name: name, Name: clusterName,
Namespace: testNamespace, Namespace: testNamespace,
}, &sipCR)).To(Succeed()) }, &sipCR)).To(Succeed())
@ -217,8 +217,8 @@ var _ = Describe("SIPCluster controller", func() {
Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed())
// Create SIP cluster // Create SIP cluster
name := "subcluster-test5" clusterName := "subcluster-test5"
sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(name, testNamespace, 1, 2) sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(clusterName, testNamespace, 1, 2)
Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed())
Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed())
@ -242,7 +242,7 @@ var _ = Describe("SIPCluster controller", func() {
// Validate SIP CR ready condition has been updated // Validate SIP CR ready condition has been updated
var sipCR airshipv1.SIPCluster var sipCR airshipv1.SIPCluster
Expect(k8sClient.Get(context.Background(), types.NamespacedName{ Expect(k8sClient.Get(context.Background(), types.NamespacedName{
Name: name, Name: clusterName,
Namespace: testNamespace, Namespace: testNamespace,
}, &sipCR)).To(Succeed()) }, &sipCR)).To(Succeed())
@ -279,8 +279,8 @@ var _ = Describe("SIPCluster controller", func() {
Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed())
// Create SIP cluster // Create SIP cluster
name := "subcluster-test6" clusterName := "subcluster-test6"
sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(name, testNamespace, 2, 1) sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(clusterName, testNamespace, 2, 1)
Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), nodeSSHPrivateKeys)).Should(Succeed())
Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), sipCluster)).Should(Succeed())
@ -304,7 +304,7 @@ var _ = Describe("SIPCluster controller", func() {
// Validate SIP CR ready condition has been updated // Validate SIP CR ready condition has been updated
var sipCR airshipv1.SIPCluster var sipCR airshipv1.SIPCluster
Expect(k8sClient.Get(context.Background(), types.NamespacedName{ Expect(k8sClient.Get(context.Background(), types.NamespacedName{
Name: name, Name: clusterName,
Namespace: testNamespace, Namespace: testNamespace,
}, &sipCR)).To(Succeed()) }, &sipCR)).To(Succeed())
@ -340,8 +340,8 @@ var _ = Describe("SIPCluster controller", func() {
Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed())
// Create SIP cluster // Create SIP cluster
name := "subcluster-test3" clusterName := "subcluster-test3"
sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(name, testNamespace, 1, 2) sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(clusterName, testNamespace, 1, 2)
controlPlaneSpec := sipCluster.Spec.Nodes[airshipv1.RoleControlPlane] controlPlaneSpec := sipCluster.Spec.Nodes[airshipv1.RoleControlPlane]
controlPlaneSpec.Scheduling = airshipv1.RackAntiAffinity controlPlaneSpec.Scheduling = airshipv1.RackAntiAffinity
@ -374,7 +374,7 @@ var _ = Describe("SIPCluster controller", func() {
// Validate SIP CR ready condition has been updated // Validate SIP CR ready condition has been updated
var sipCR airshipv1.SIPCluster var sipCR airshipv1.SIPCluster
Expect(k8sClient.Get(context.Background(), types.NamespacedName{ Expect(k8sClient.Get(context.Background(), types.NamespacedName{
Name: name, Name: clusterName,
Namespace: testNamespace, Namespace: testNamespace,
}, &sipCR)).To(Succeed()) }, &sipCR)).To(Succeed())
@ -407,8 +407,8 @@ var _ = Describe("SIPCluster controller", func() {
Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed()) Expect(k8sClient.Create(context.Background(), networkData)).Should(Succeed())
// Create SIP cluster // Create SIP cluster
name := "subcluster-test3" clusterName := "subcluster-test3"
sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(name, testNamespace, 2, 1) sipCluster, nodeSSHPrivateKeys := testutil.CreateSIPCluster(clusterName, testNamespace, 2, 1)
controlPlaneSpec := sipCluster.Spec.Nodes[airshipv1.RoleControlPlane] controlPlaneSpec := sipCluster.Spec.Nodes[airshipv1.RoleControlPlane]
controlPlaneSpec.Scheduling = airshipv1.RackAntiAffinity controlPlaneSpec.Scheduling = airshipv1.RackAntiAffinity
@ -441,7 +441,7 @@ var _ = Describe("SIPCluster controller", func() {
// Validate SIP CR ready condition has been updated // Validate SIP CR ready condition has been updated
var sipCR airshipv1.SIPCluster var sipCR airshipv1.SIPCluster
Expect(k8sClient.Get(context.Background(), types.NamespacedName{ Expect(k8sClient.Get(context.Background(), types.NamespacedName{
Name: name, Name: clusterName,
Namespace: testNamespace, Namespace: testNamespace,
}, &sipCR)).To(Succeed()) }, &sipCR)).To(Succeed())