issue 8857: support third party tolerations

Signed-off-by: Lyndon-Li <lyonghui@vmware.com>
This commit is contained in:
Lyndon-Li
2025-07-07 17:32:54 +08:00
parent c505021752
commit a752b54614
12 changed files with 263 additions and 15 deletions

View File

@@ -53,6 +53,7 @@ var (
ErrDaemonSetNotFound = errors.New("daemonset not found")
ErrNodeAgentLabelNotFound = errors.New("node-agent label not found")
ErrNodeAgentAnnotationNotFound = errors.New("node-agent annotation not found")
ErrNodeAgentTolerationNotFound = errors.New("node-agent toleration not found")
)
type LoadConcurrency struct {
@@ -256,6 +257,26 @@ func GetAnnotationValue(ctx context.Context, kubeClient kubernetes.Interface, na
return val, nil
}
func GetToleration(ctx context.Context, kubeClient kubernetes.Interface, namespace string, key string, osType string) (*corev1api.Toleration, error) {
dsName := daemonSet
if osType == kube.NodeOSWindows {
dsName = daemonsetWindows
}
ds, err := kubeClient.AppsV1().DaemonSets(namespace).Get(ctx, dsName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrapf(err, "error getting %s daemonset", dsName)
}
for i, t := range ds.Spec.Template.Spec.Tolerations {
if t.Key == key {
return &ds.Spec.Template.Spec.Tolerations[i], nil
}
}
return nil, ErrNodeAgentTolerationNotFound
}
func GetHostPodPath(ctx context.Context, kubeClient kubernetes.Interface, namespace string, osType string) (string, error) {
dsName := daemonSet
if osType == kube.NodeOSWindows {

View File

@@ -592,6 +592,116 @@ func TestGetAnnotationValue(t *testing.T) {
}
}
func TestGetToleration(t *testing.T) {
daemonSet := &appsv1api.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "node-agent",
},
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
},
}
daemonSetWithOtherToleration := &appsv1api.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "node-agent",
},
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
},
Spec: appsv1api.DaemonSetSpec{
Template: corev1api.PodTemplateSpec{
Spec: corev1api.PodSpec{
Tolerations: []corev1api.Toleration{
{
Key: "other-toleration-key",
},
},
},
},
},
}
daemonSetWithToleration := &appsv1api.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "node-agent",
},
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
},
Spec: appsv1api.DaemonSetSpec{
Template: corev1api.PodTemplateSpec{
Spec: corev1api.PodSpec{
Tolerations: []corev1api.Toleration{
{
Key: "fake-toleration",
Value: "true",
},
},
},
},
},
}
tests := []struct {
name string
kubeClientObj []runtime.Object
namespace string
expectedValue corev1api.Toleration
expectErr string
}{
// {
// name: "ds get error",
// namespace: "fake-ns",
// expectErr: "error getting node-agent daemonset: daemonsets.apps \"node-agent\" not found",
// },
{
name: "no toleration",
namespace: "fake-ns",
kubeClientObj: []runtime.Object{
daemonSet,
},
expectErr: ErrNodeAgentTolerationNotFound.Error(),
},
{
name: "no expecting toleration",
namespace: "fake-ns",
kubeClientObj: []runtime.Object{
daemonSetWithOtherToleration,
},
expectErr: ErrNodeAgentTolerationNotFound.Error(),
},
{
name: "expecting toleration",
namespace: "fake-ns",
kubeClientObj: []runtime.Object{
daemonSetWithToleration,
},
expectedValue: corev1api.Toleration{
Key: "fake-toleration",
Value: "true",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fakeKubeClient := fake.NewSimpleClientset(test.kubeClientObj...)
value, err := GetToleration(context.TODO(), fakeKubeClient, test.namespace, "fake-toleration", kube.NodeOSLinux)
if test.expectErr == "" {
require.NoError(t, err)
assert.Equal(t, test.expectedValue, *value)
} else {
assert.EqualError(t, err, test.expectErr)
}
})
}
}
func TestGetHostPodPath(t *testing.T) {
daemonSet := &appsv1api.DaemonSet{
ObjectMeta: metav1.ObjectMeta{