issue 8754: add third party annotation support

Signed-off-by: Lyndon-Li <lyonghui@vmware.com>
This commit is contained in:
Lyndon-Li
2025-03-10 10:38:26 +08:00
parent d18278aa58
commit b170892e64
8 changed files with 218 additions and 26 deletions
+26 -2
View File
@@ -44,8 +44,9 @@ const (
)
var (
ErrDaemonSetNotFound = errors.New("daemonset not found")
ErrNodeAgentLabelNotFound = errors.New("node-agent label not found")
ErrDaemonSetNotFound = errors.New("daemonset not found")
ErrNodeAgentLabelNotFound = errors.New("node-agent label not found")
ErrNodeAgentAnnotationNotFound = errors.New("node-agent annotation not found")
)
type LoadConcurrency struct {
@@ -225,3 +226,26 @@ func GetLabelValue(ctx context.Context, kubeClient kubernetes.Interface, namespa
return val, nil
}
func GetAnnotationValue(ctx context.Context, kubeClient kubernetes.Interface, namespace string, key string, osType string) (string, error) {
dsName := daemonSet
if osType == kube.NodeOSWindows {
dsName = daemonsetWindows
}
ds, err := kubeClient.AppsV1().DaemonSets(namespace).Get(ctx, dsName, metav1.GetOptions{})
if err != nil {
return "", errors.Wrapf(err, "error getting %s daemonset", dsName)
}
if ds.Spec.Template.Annotations == nil {
return "", ErrNodeAgentAnnotationNotFound
}
val, found := ds.Spec.Template.Annotations[key]
if !found {
return "", ErrNodeAgentAnnotationNotFound
}
return val, nil
}
+129
View File
@@ -461,3 +461,132 @@ func TestGetLabelValue(t *testing.T) {
})
}
}
func TestGetAnnotationValue(t *testing.T) {
daemonSet := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "node-agent",
},
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
},
}
daemonSetWithOtherAnnotation := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "node-agent",
},
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
},
Spec: appsv1.DaemonSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"fake-other-annotation": "fake-value-1",
},
},
},
},
}
daemonSetWithAnnotation := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "node-agent",
},
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
},
Spec: appsv1.DaemonSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"fake-annotation": "fake-value-2",
},
},
},
},
}
daemonSetWithEmptyAnnotation := &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Namespace: "fake-ns",
Name: "node-agent",
},
TypeMeta: metav1.TypeMeta{
Kind: "DaemonSet",
},
Spec: appsv1.DaemonSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"fake-annotation": "",
},
},
},
},
}
tests := []struct {
name string
kubeClientObj []runtime.Object
namespace string
expectedValue string
expectErr string
}{
{
name: "ds get error",
namespace: "fake-ns",
expectErr: "error getting node-agent daemonset: daemonsets.apps \"node-agent\" not found",
},
{
name: "no annotation",
namespace: "fake-ns",
kubeClientObj: []runtime.Object{
daemonSet,
},
expectErr: ErrNodeAgentAnnotationNotFound.Error(),
},
{
name: "no expecting annotation",
namespace: "fake-ns",
kubeClientObj: []runtime.Object{
daemonSetWithOtherAnnotation,
},
expectErr: ErrNodeAgentAnnotationNotFound.Error(),
},
{
name: "expecting annotation",
namespace: "fake-ns",
kubeClientObj: []runtime.Object{
daemonSetWithAnnotation,
},
expectedValue: "fake-value-2",
},
{
name: "expecting empty annotation",
namespace: "fake-ns",
kubeClientObj: []runtime.Object{
daemonSetWithEmptyAnnotation,
},
expectedValue: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fakeKubeClient := fake.NewSimpleClientset(test.kubeClientObj...)
value, err := GetAnnotationValue(context.TODO(), fakeKubeClient, test.namespace, "fake-annotation", kube.NodeOSLinux)
if test.expectErr == "" {
assert.NoError(t, err)
assert.Equal(t, test.expectedValue, value)
} else {
assert.EqualError(t, err, test.expectErr)
}
})
}
}