chore: define common aliases for k8s packages (#8672)
Some checks failed
Run the E2E test on kind / build (push) Failing after 6m48s
Run the E2E test on kind / setup-test-matrix (push) Successful in 3s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / Build (push) Failing after 35s
Close stale issues and PRs / stale (push) Successful in 8s
Trivy Nightly Scan / Trivy nightly scan (velero, main) (push) Failing after 1m11s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-aws, main) (push) Failing after 47s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-gcp, main) (push) Failing after 49s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-microsoft-azure, main) (push) Failing after 43s

* lchore: define common alias for k8s packages

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

* Update .golangci.yaml

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

* Update .golangci.yaml

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

* Update .golangci.yaml

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

---------

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
This commit is contained in:
Matthieu MOREL
2025-04-22 12:14:47 +02:00
committed by GitHub
parent f0fde6e1d4
commit c6a420bd3a
139 changed files with 1968 additions and 1954 deletions

View File

@@ -26,7 +26,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
corev1api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
@@ -70,8 +70,8 @@ type microServiceBRWatcher struct {
thisPod string
thisContainer string
associatedObject string
eventCh chan *v1.Event
podCh chan *v1.Pod
eventCh chan *corev1api.Event
podCh chan *corev1api.Pod
startedFromEvent bool
terminatedFromEvent bool
wgWatcher sync.WaitGroup
@@ -95,8 +95,8 @@ func newMicroServiceBRWatcher(client client.Client, kubeClient kubernetes.Interf
thisPod: podName,
thisContainer: containerName,
associatedObject: associatedObject,
eventCh: make(chan *v1.Event, 10),
podCh: make(chan *v1.Pod, 2),
eventCh: make(chan *corev1api.Event, 10),
podCh: make(chan *corev1api.Pod, 2),
wgWatcher: sync.WaitGroup{},
log: log,
}
@@ -105,12 +105,12 @@ func newMicroServiceBRWatcher(client client.Client, kubeClient kubernetes.Interf
}
func (ms *microServiceBRWatcher) Init(ctx context.Context, param any) error {
eventInformer, err := ms.mgr.GetCache().GetInformer(ctx, &v1.Event{})
eventInformer, err := ms.mgr.GetCache().GetInformer(ctx, &corev1api.Event{})
if err != nil {
return errors.Wrap(err, "error getting event informer")
}
podInformer, err := ms.mgr.GetCache().GetInformer(ctx, &v1.Pod{})
podInformer, err := ms.mgr.GetCache().GetInformer(ctx, &corev1api.Pod{})
if err != nil {
return errors.Wrap(err, "error getting pod informer")
}
@@ -118,7 +118,7 @@ func (ms *microServiceBRWatcher) Init(ctx context.Context, param any) error {
eventHandler, err := eventInformer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj any) {
evt := obj.(*v1.Event)
evt := obj.(*corev1api.Event)
if evt.InvolvedObject.Namespace != ms.namespace || evt.InvolvedObject.Name != ms.associatedObject {
return
}
@@ -126,7 +126,7 @@ func (ms *microServiceBRWatcher) Init(ctx context.Context, param any) error {
ms.eventCh <- evt
},
UpdateFunc: func(_, obj any) {
evt := obj.(*v1.Event)
evt := obj.(*corev1api.Event)
if evt.InvolvedObject.Namespace != ms.namespace || evt.InvolvedObject.Name != ms.associatedObject {
return
}
@@ -142,12 +142,12 @@ func (ms *microServiceBRWatcher) Init(ctx context.Context, param any) error {
podHandler, err := podInformer.AddEventHandler(
cache.ResourceEventHandlerFuncs{
UpdateFunc: func(_, obj any) {
pod := obj.(*v1.Pod)
pod := obj.(*corev1api.Pod)
if pod.Namespace != ms.namespace || pod.Name != ms.thisPod {
return
}
if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
if pod.Status.Phase == corev1api.PodSucceeded || pod.Status.Phase == corev1api.PodFailed {
ms.podCh <- pod
}
},
@@ -230,7 +230,7 @@ func (ms *microServiceBRWatcher) StartRestore(snapshotID string, target AccessPo
}
func (ms *microServiceBRWatcher) reEnsureThisPod(ctx context.Context) error {
thisPod := &v1.Pod{}
thisPod := &corev1api.Pod{}
if err := ms.client.Get(ctx, types.NamespacedName{
Namespace: ms.namespace,
Name: ms.thisPod,
@@ -238,7 +238,7 @@ func (ms *microServiceBRWatcher) reEnsureThisPod(ctx context.Context) error {
return errors.Wrapf(err, "error getting this pod %s", ms.thisPod)
}
if thisPod.Status.Phase == v1.PodSucceeded || thisPod.Status.Phase == v1.PodFailed {
if thisPod.Status.Phase == corev1api.PodSucceeded || thisPod.Status.Phase == corev1api.PodFailed {
ms.podCh <- thisPod
ms.log.WithField("this pod", ms.thisPod).Infof("This pod comes to terminital status %s before watch start", thisPod.Status.Phase)
}
@@ -264,7 +264,7 @@ func (ms *microServiceBRWatcher) startWatch() {
ms.wgWatcher.Done()
}()
var lastPod *v1.Pod
var lastPod *corev1api.Pod
watchLoop:
for {
@@ -319,7 +319,7 @@ func (ms *microServiceBRWatcher) startWatch() {
logger.Info("Calling callback on data path pod termination")
if lastPod.Status.Phase == v1.PodSucceeded {
if lastPod.Status.Phase == corev1api.PodSucceeded {
result := funcGetResultFromMessage(ms.taskType, terminateMessage, ms.log)
ms.callbacks.OnProgress(ms.ctx, ms.namespace, ms.taskName, getCompletionProgressFromResult(ms.taskType, result))
ms.callbacks.OnCompleted(ms.ctx, ms.namespace, ms.taskName, result)
@@ -335,7 +335,7 @@ func (ms *microServiceBRWatcher) startWatch() {
}()
}
func (ms *microServiceBRWatcher) onEvent(evt *v1.Event) {
func (ms *microServiceBRWatcher) onEvent(evt *corev1api.Event) {
switch evt.Reason {
case EventReasonStarted:
ms.startedFromEvent = true

View File

@@ -29,7 +29,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
corev1api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
kubeclientfake "k8s.io/client-go/kubernetes/fake"
@@ -61,7 +61,7 @@ func TestReEnsureThisPod(t *testing.T) {
namespace: "velero",
thisPod: "fake-pod-1",
kubeClientObj: []runtime.Object{
builder.ForPod("velero", "fake-pod-1").Phase(v1.PodRunning).Result(),
builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodRunning).Result(),
},
},
{
@@ -69,7 +69,7 @@ func TestReEnsureThisPod(t *testing.T) {
namespace: "velero",
thisPod: "fake-pod-1",
kubeClientObj: []runtime.Object{
builder.ForPod("velero", "fake-pod-1").Phase(v1.PodSucceeded).Result(),
builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodSucceeded).Result(),
},
expectChan: true,
},
@@ -78,7 +78,7 @@ func TestReEnsureThisPod(t *testing.T) {
namespace: "velero",
thisPod: "fake-pod-1",
kubeClientObj: []runtime.Object{
builder.ForPod("velero", "fake-pod-1").Phase(v1.PodFailed).Result(),
builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodFailed).Result(),
},
expectChan: true,
},
@@ -87,7 +87,7 @@ func TestReEnsureThisPod(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
scheme := runtime.NewScheme()
v1.AddToScheme(scheme)
corev1api.AddToScheme(scheme)
fakeClientBuilder := fake.NewClientBuilder()
fakeClientBuilder = fakeClientBuilder.WithScheme(scheme)
@@ -97,7 +97,7 @@ func TestReEnsureThisPod(t *testing.T) {
namespace: test.namespace,
thisPod: test.thisPod,
client: fakeClient,
podCh: make(chan *v1.Pod, 2),
podCh: make(chan *corev1api.Pod, 2),
log: velerotest.NewLogger(),
}
@@ -124,7 +124,7 @@ type startWatchFake struct {
progress int
}
func (sw *startWatchFake) getPodContainerTerminateMessage(pod *v1.Pod, container string) string {
func (sw *startWatchFake) getPodContainerTerminateMessage(pod *corev1api.Pod, container string) string {
return sw.terminationMessage
}
@@ -153,7 +153,7 @@ func (sw *startWatchFake) OnProgress(ctx context.Context, namespace string, task
}
type insertEvent struct {
event *v1.Event
event *corev1api.Event
after time.Duration
delay time.Duration
}
@@ -166,7 +166,7 @@ func TestStartWatch(t *testing.T) {
thisContainer string
terminationMessage string
redirectLogErr error
insertPod *v1.Pod
insertPod *corev1api.Pod
insertEventsBefore []insertEvent
insertEventsAfter []insertEvent
ctxCancel bool
@@ -187,16 +187,16 @@ func TestStartWatch(t *testing.T) {
name: "completed with rantional sequence",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodSucceeded).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodSucceeded).Result(),
insertEventsBefore: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
},
{
event: &v1.Event{Reason: EventReasonCompleted},
event: &corev1api.Event{Reason: EventReasonCompleted},
},
{
event: &v1.Event{Reason: EventReasonStopped},
event: &corev1api.Event{Reason: EventReasonStopped},
delay: time.Second,
},
},
@@ -209,16 +209,16 @@ func TestStartWatch(t *testing.T) {
name: "completed",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodSucceeded).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodSucceeded).Result(),
insertEventsBefore: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
},
{
event: &v1.Event{Reason: EventReasonCompleted},
event: &corev1api.Event{Reason: EventReasonCompleted},
},
{
event: &v1.Event{Reason: EventReasonStopped},
event: &corev1api.Event{Reason: EventReasonStopped},
},
},
expectStartEvent: true,
@@ -230,16 +230,16 @@ func TestStartWatch(t *testing.T) {
name: "completed with redirect error",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodSucceeded).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodSucceeded).Result(),
insertEventsBefore: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
},
{
event: &v1.Event{Reason: EventReasonCompleted},
event: &corev1api.Event{Reason: EventReasonCompleted},
},
{
event: &v1.Event{Reason: EventReasonStopped},
event: &corev1api.Event{Reason: EventReasonStopped},
},
},
redirectLogErr: errors.New("fake-error"),
@@ -252,15 +252,15 @@ func TestStartWatch(t *testing.T) {
name: "complete but terminated event not received in time",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodSucceeded).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodSucceeded).Result(),
insertEventsBefore: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
},
},
insertEventsAfter: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
after: time.Second * 6,
},
},
@@ -272,18 +272,18 @@ func TestStartWatch(t *testing.T) {
name: "complete but terminated event not received immediately",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodSucceeded).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodSucceeded).Result(),
insertEventsBefore: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
},
},
insertEventsAfter: []insertEvent{
{
event: &v1.Event{Reason: EventReasonCompleted},
event: &corev1api.Event{Reason: EventReasonCompleted},
},
{
event: &v1.Event{Reason: EventReasonStopped},
event: &corev1api.Event{Reason: EventReasonStopped},
delay: time.Second,
},
},
@@ -296,22 +296,22 @@ func TestStartWatch(t *testing.T) {
name: "completed with progress",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodSucceeded).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodSucceeded).Result(),
insertEventsBefore: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
},
{
event: &v1.Event{Reason: EventReasonProgress, Message: "fake-progress-1"},
event: &corev1api.Event{Reason: EventReasonProgress, Message: "fake-progress-1"},
},
{
event: &v1.Event{Reason: EventReasonProgress, Message: "fake-progress-2"},
event: &corev1api.Event{Reason: EventReasonProgress, Message: "fake-progress-2"},
},
{
event: &v1.Event{Reason: EventReasonCompleted},
event: &corev1api.Event{Reason: EventReasonCompleted},
},
{
event: &v1.Event{Reason: EventReasonStopped},
event: &corev1api.Event{Reason: EventReasonStopped},
delay: time.Second,
},
},
@@ -324,16 +324,16 @@ func TestStartWatch(t *testing.T) {
name: "failed",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodFailed).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodFailed).Result(),
insertEventsBefore: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
},
{
event: &v1.Event{Reason: EventReasonCancelled},
event: &corev1api.Event{Reason: EventReasonCancelled},
},
{
event: &v1.Event{Reason: EventReasonStopped},
event: &corev1api.Event{Reason: EventReasonStopped},
},
},
terminationMessage: "fake-termination-message-1",
@@ -345,7 +345,7 @@ func TestStartWatch(t *testing.T) {
name: "pod crash",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodFailed).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodFailed).Result(),
terminationMessage: "fake-termination-message-2",
expectFail: true,
},
@@ -353,16 +353,16 @@ func TestStartWatch(t *testing.T) {
name: "canceled",
thisPod: "fak-pod-1",
thisContainer: "fake-container-1",
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(v1.PodFailed).Result(),
insertPod: builder.ForPod("velero", "fake-pod-1").Phase(corev1api.PodFailed).Result(),
insertEventsBefore: []insertEvent{
{
event: &v1.Event{Reason: EventReasonStarted},
event: &corev1api.Event{Reason: EventReasonStarted},
},
{
event: &v1.Event{Reason: EventReasonCancelled},
event: &corev1api.Event{Reason: EventReasonCancelled},
},
{
event: &v1.Event{Reason: EventReasonStopped},
event: &corev1api.Event{Reason: EventReasonStopped},
},
},
terminationMessage: fmt.Sprintf("Failed to init data path service for DataUpload %s: %v", "fake-du-name", errors.New(ErrCancelled)),
@@ -390,8 +390,8 @@ func TestStartWatch(t *testing.T) {
namespace: test.namespace,
thisPod: test.thisPod,
thisContainer: test.thisContainer,
podCh: make(chan *v1.Pod, 2),
eventCh: make(chan *v1.Event, 10),
podCh: make(chan *corev1api.Pod, 2),
eventCh: make(chan *corev1api.Event, 10),
log: velerotest.NewLogger(),
callbacks: Callbacks{
OnCompleted: sw.OnCompleted,