-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8443 from CecileRobertMichon/watch-mp-nodes
🐛 Add node watcher to MachinePool controller
- Loading branch information
Showing
7 changed files
with
321 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package index | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"sigs.k8s.io/cluster-api/controllers/noderefutil" | ||
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" | ||
) | ||
|
||
const ( | ||
// MachinePoolNodeNameField is used by the MachinePool Controller to index MachinePools by Node name, and add a watch on Nodes. | ||
MachinePoolNodeNameField = "status.nodeRefs.name" | ||
|
||
// MachinePoolProviderIDField is used to index MachinePools by ProviderID. It's useful to find MachinePools | ||
// in a management cluster from Nodes in a workload cluster. | ||
MachinePoolProviderIDField = "spec.providerIDList" | ||
) | ||
|
||
// ByMachinePoolNode adds the machinepool node name index to the | ||
// managers cache. | ||
func ByMachinePoolNode(ctx context.Context, mgr ctrl.Manager) error { | ||
if err := mgr.GetCache().IndexField(ctx, &expv1.MachinePool{}, | ||
MachinePoolNodeNameField, | ||
MachinePoolByNodeName, | ||
); err != nil { | ||
return errors.Wrap(err, "error setting index field") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// MachinePoolByNodeName contains the logic to index MachinePools by Node name. | ||
func MachinePoolByNodeName(o client.Object) []string { | ||
machinepool, ok := o.(*expv1.MachinePool) | ||
if !ok { | ||
panic(fmt.Sprintf("Expected a MachinePool but got a %T", o)) | ||
} | ||
|
||
if len(machinepool.Status.NodeRefs) == 0 { | ||
return nil | ||
} | ||
|
||
nodeNames := make([]string, 0, len(machinepool.Status.NodeRefs)) | ||
for _, ref := range machinepool.Status.NodeRefs { | ||
nodeNames = append(nodeNames, ref.Name) | ||
} | ||
return nodeNames | ||
} | ||
|
||
// ByMachinePoolProviderID adds the machinepool providerID index to the | ||
// managers cache. | ||
func ByMachinePoolProviderID(ctx context.Context, mgr ctrl.Manager) error { | ||
if err := mgr.GetCache().IndexField(ctx, &expv1.MachinePool{}, | ||
MachinePoolProviderIDField, | ||
machinePoolByProviderID, | ||
); err != nil { | ||
return errors.Wrap(err, "error setting index field") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func machinePoolByProviderID(o client.Object) []string { | ||
machinepool, ok := o.(*expv1.MachinePool) | ||
if !ok { | ||
panic(fmt.Sprintf("Expected a MachinePool but got a %T", o)) | ||
} | ||
|
||
if len(machinepool.Spec.ProviderIDList) == 0 { | ||
return nil | ||
} | ||
|
||
providerIDs := make([]string, 0, len(machinepool.Spec.ProviderIDList)) | ||
for _, id := range machinepool.Spec.ProviderIDList { | ||
providerID, err := noderefutil.NewProviderID(id) | ||
if err != nil { | ||
// Failed to create providerID, skipping. | ||
continue | ||
} | ||
providerIDs = append(providerIDs, providerID.IndexKey()) | ||
} | ||
|
||
return providerIDs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package index | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"sigs.k8s.io/cluster-api/controllers/noderefutil" | ||
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1" | ||
) | ||
|
||
func TestIndexMachinePoolByNodeName(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
object client.Object | ||
expected []string | ||
}{ | ||
{ | ||
name: "when the machinepool has no NodeRef", | ||
object: &expv1.MachinePool{}, | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "when the machinepool has valid NodeRefs", | ||
object: &expv1.MachinePool{ | ||
Status: expv1.MachinePoolStatus{ | ||
NodeRefs: []corev1.ObjectReference{ | ||
{ | ||
Name: "node1", | ||
}, | ||
{ | ||
Name: "node2", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: []string{"node1", "node2"}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
got := MachinePoolByNodeName(tc.object) | ||
g.Expect(got).To(ConsistOf(tc.expected)) | ||
}) | ||
} | ||
} | ||
|
||
func TestIndexMachinePoolByProviderID(t *testing.T) { | ||
g := NewWithT(t) | ||
validProviderID, err := noderefutil.NewProviderID("aws://region/zone/1") | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
otherValidProviderID, err := noderefutil.NewProviderID("aws://region/zone/2") | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
|
||
testCases := []struct { | ||
name string | ||
object client.Object | ||
expected []string | ||
}{ | ||
{ | ||
name: "MachinePool has no providerID", | ||
object: &expv1.MachinePool{}, | ||
expected: nil, | ||
}, | ||
{ | ||
name: "MachinePool has invalid providerID", | ||
object: &expv1.MachinePool{ | ||
Spec: expv1.MachinePoolSpec{ | ||
ProviderIDList: []string{"invalid"}, | ||
}, | ||
}, | ||
expected: []string{}, | ||
}, | ||
{ | ||
name: "MachinePool has valid providerIDs", | ||
object: &expv1.MachinePool{ | ||
Spec: expv1.MachinePoolSpec{ | ||
ProviderIDList: []string{validProviderID.String(), otherValidProviderID.String()}, | ||
}, | ||
}, | ||
expected: []string{validProviderID.IndexKey(), otherValidProviderID.IndexKey()}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
g := NewWithT(t) | ||
got := machinePoolByProviderID(tc.object) | ||
g.Expect(got).To(BeEquivalentTo(tc.expected)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.