Skip to content

Commit 4d9c909

Browse files
authored
Merge pull request #2673 from kubernetes-sigs/refine-KataCCLabel
cleanup: refine kataNode label matching
2 parents a25789f + 47075dc commit 4d9c909

File tree

2 files changed

+38
-92
lines changed

2 files changed

+38
-92
lines changed

pkg/azurefile/azurefile.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ const (
162162
premium = "premium"
163163
selectRandomMatchingAccountField = "selectrandommatchingaccount"
164164
accountQuotaField = "accountquota"
165+
defaultKataCCLabel = "kubernetes.azure.com/kata-cc-isolation"
165166

166167
accountNotProvisioned = "StorageAccountIsNotProvisioned"
167168
// this is a workaround fix for 429 throttling issue, will update cloud provider for better fix later
@@ -1342,32 +1343,30 @@ func (d *Driver) getFileShareClientForSub(subscriptionID string) (fileshareclien
13421343
return d.cloud.ComputeClientFactory.GetFileShareClientForSub(subscriptionID)
13431344
}
13441345

1345-
func getNodeInfoFromLabels(ctx context.Context, nodeID string, kubeClient clientset.Interface) (string, string, string, error) {
1346+
func isKataNode(ctx context.Context, nodeID string, kubeClient clientset.Interface) bool {
1347+
if nodeID == "" {
1348+
return false
1349+
}
1350+
13461351
if kubeClient == nil || kubeClient.CoreV1() == nil {
1347-
return "", "", "", fmt.Errorf("kubeClient is nil")
1352+
klog.Warningf("kubeClient is nil, cannot check if node(%s) is a kata node", nodeID)
1353+
return false
13481354
}
13491355

13501356
node, err := kubeClient.CoreV1().Nodes().Get(ctx, nodeID, metav1.GetOptions{})
13511357
if err != nil {
1352-
return "", "", "", fmt.Errorf("get node(%s) failed with %v", nodeID, err)
1353-
}
1354-
1355-
if len(node.Labels) == 0 {
1356-
return "", "", "", fmt.Errorf("node(%s) label is empty", nodeID)
1358+
klog.Warningf("failed to get node(%s): %v", nodeID, err)
1359+
return false
13571360
}
1358-
return node.Labels["kubernetes.azure.com/kata-cc-isolation"], node.Labels["kubernetes.azure.com/kata-mshv-vm-isolation"], node.Labels["katacontainers.io/kata-runtime"], nil
1359-
}
13601361

1361-
func isKataNode(ctx context.Context, nodeID string, kubeClient clientset.Interface) bool {
1362-
if nodeID == "" {
1362+
if node == nil || len(node.Labels) == 0 {
13631363
return false
13641364
}
1365-
kataCCIsolationLabel, kataVMIsolationLabel, kataRuntimeLabel, err := getNodeInfoFromLabels(ctx, nodeID, kubeClient)
13661365

1367-
if err != nil {
1368-
klog.Warningf("failed to get node info from labels: %v", err)
1366+
// Check for the kata isolation labels
1367+
if _, ok := node.Labels[defaultKataCCLabel]; !ok {
13691368
return false
13701369
}
1371-
klog.V(4).Infof("node(%s) labels: kataVMIsolationLabel(%s), kataRuntimeLabel(%s)", nodeID, kataVMIsolationLabel, kataRuntimeLabel)
1372-
return strings.EqualFold(kataCCIsolationLabel, "true")
1370+
klog.V(4).Infof("node(%s) is a kata node with labels: %v", nodeID, node.Labels)
1371+
return true
13731372
}

pkg/azurefile/azurefile_test.go

Lines changed: 23 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,90 +1635,26 @@ func TestGetFileShareClientForSub(t *testing.T) {
16351635
}
16361636
}
16371637

1638-
func TestGetNodeInfoFromLabels(t *testing.T) {
1638+
func TestIsKataNode(t *testing.T) {
16391639
testCases := []struct {
1640-
name string
1641-
nodeName string
1642-
labels map[string]string
1643-
setupClient bool
1644-
expectedVals [3]string
1645-
expectedErr error
1640+
name string
1641+
nodeName string
1642+
labels map[string]string
1643+
setupClient bool
1644+
expected bool
16461645
}{
16471646
{
16481647
name: "Error when kubeClient is nil",
16491648
nodeName: "test-node",
16501649
setupClient: false,
1651-
expectedErr: fmt.Errorf("kubeClient is nil"),
1652-
},
1653-
{
1654-
name: "Error when node does not exist",
1655-
nodeName: "nonexistent-node",
1656-
setupClient: true,
1657-
expectedErr: fmt.Errorf("get node(nonexistent-node) failed with nodes \"nonexistent-node\" not found"),
1650+
expected: false,
16581651
},
16591652
{
1660-
name: "Error when node has no labels",
1661-
nodeName: "test-node",
1662-
setupClient: true,
1663-
labels: map[string]string{}, // Node exists but has no labels
1664-
expectedErr: fmt.Errorf("node(test-node) label is empty"),
1653+
name: "nodeName is empty",
1654+
nodeName: "",
1655+
setupClient: false,
1656+
expected: false,
16651657
},
1666-
{
1667-
name: "Success with kata labels",
1668-
nodeName: "test-node",
1669-
setupClient: true,
1670-
labels: map[string]string{
1671-
"kubernetes.azure.com/kata-cc-isolation": "true",
1672-
"kubernetes.azure.com/kata-mshv-vm-isolation": "true",
1673-
"katacontainers.io/kata-runtime": "false",
1674-
},
1675-
expectedVals: [3]string{"true", "true", "false"},
1676-
expectedErr: nil,
1677-
},
1678-
}
1679-
1680-
for _, tc := range testCases {
1681-
t.Run(tc.name, func(t *testing.T) {
1682-
ctx := context.TODO()
1683-
var clientset kubernetes.Interface
1684-
1685-
if tc.setupClient {
1686-
clientset = fake.NewSimpleClientset()
1687-
}
1688-
1689-
if tc.labels != nil && tc.setupClient {
1690-
node := &v1api.Node{
1691-
ObjectMeta: metav1.ObjectMeta{
1692-
Name: tc.nodeName,
1693-
Labels: tc.labels,
1694-
},
1695-
}
1696-
_, err := clientset.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{})
1697-
assert.NoError(t, err)
1698-
}
1699-
1700-
kataCCIsolation, kataVMIsolation, kataRuntime, err := getNodeInfoFromLabels(ctx, tc.nodeName, clientset)
1701-
1702-
if tc.expectedErr != nil {
1703-
assert.EqualError(t, err, tc.expectedErr.Error())
1704-
} else {
1705-
assert.NoError(t, err)
1706-
assert.Equal(t, tc.expectedVals[0], kataCCIsolation)
1707-
assert.Equal(t, tc.expectedVals[1], kataVMIsolation)
1708-
assert.Equal(t, tc.expectedVals[2], kataRuntime)
1709-
}
1710-
})
1711-
}
1712-
}
1713-
1714-
func TestIsKataNode(t *testing.T) {
1715-
testCases := []struct {
1716-
name string
1717-
nodeName string
1718-
labels map[string]string
1719-
setupClient bool
1720-
expected bool
1721-
}{
17221658
{
17231659
name: "Node does not exist",
17241660
nodeName: "",
@@ -1739,7 +1675,18 @@ func TestIsKataNode(t *testing.T) {
17391675
nodeName: "test-node",
17401676
setupClient: true,
17411677
labels: map[string]string{
1742-
"kubernetes.azure.com/kata-cc-isolation": "true",
1678+
defaultKataCCLabel: "",
1679+
},
1680+
expected: true,
1681+
},
1682+
{
1683+
name: "Node has kata labels",
1684+
nodeName: "test-node",
1685+
setupClient: true,
1686+
labels: map[string]string{
1687+
defaultKataCCLabel: "test",
1688+
"kubernetes.azure.com/kata-mshv-vm-isolation": "true",
1689+
"katacontainers.io/kata-runtime": "true",
17431690
},
17441691
expected: true,
17451692
},

0 commit comments

Comments
 (0)