Skip to content

Commit

Permalink
Use Service Account to fetch event (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard87 authored Dec 16, 2024
1 parent 727d16c commit df8dd9a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion api/environments/environment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type EnvironmentHandlerOptions func(*EnvironmentHandler)
func WithAccounts(accounts models.Accounts) EnvironmentHandlerOptions {
return func(eh *EnvironmentHandler) {
eh.deployHandler = deployments.Init(accounts)
eh.eventHandler = events.Init(accounts.UserAccount.Client, accounts.UserAccount.RadixClient)
eh.eventHandler = events.Init(accounts)
eh.accounts = accounts
}
}
Expand Down
22 changes: 10 additions & 12 deletions api/events/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import (
environmentModels "github.com/equinor/radix-api/api/environments/models"
eventModels "github.com/equinor/radix-api/api/events/models"
"github.com/equinor/radix-api/api/kubequery"
"github.com/equinor/radix-api/models"
"github.com/equinor/radix-common/utils/slice"
radixv1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
"github.com/equinor/radix-operator/pkg/apis/utils"
radixclient "github.com/equinor/radix-operator/pkg/client/clientset/versioned"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sTypes "k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
)

const (
Expand All @@ -41,13 +40,12 @@ type EventHandler interface {
type NamespaceFunc func() string

type eventHandler struct {
kubeClient kubernetes.Interface
radixClient radixclient.Interface
accounts models.Accounts
}

// Init creates a new EventHandler
func Init(kubeClient kubernetes.Interface, radixClient radixclient.Interface) EventHandler {
return &eventHandler{kubeClient: kubeClient, radixClient: radixClient}
func Init(accounts models.Accounts) EventHandler {
return &eventHandler{accounts: accounts}
}

// GetEnvironmentEvents return events for a namespace defined by a namespace
Expand Down Expand Up @@ -88,12 +86,12 @@ func (eh *eventHandler) GetPodEvents(ctx context.Context, appName, envName, comp
}

func (eh *eventHandler) getRadixApplicationAndValidateEnvironment(ctx context.Context, appName string, envName string) (*radixv1.RadixApplication, error) {
radixApplication, err := kubequery.GetRadixApplication(ctx, eh.radixClient, appName)
radixApplication, err := kubequery.GetRadixApplication(ctx, eh.accounts.UserAccount.RadixClient, appName)
if err != nil {
return nil, err
}

_, err = kubequery.GetRadixEnvironment(ctx, eh.radixClient, appName, envName)
_, err = kubequery.GetRadixEnvironment(ctx, eh.accounts.ServiceAccount.RadixClient, appName, envName)
if err != nil {
if errors.IsNotFound(err) {
return nil, environmentModels.NonExistingEnvironment(err, appName, envName)
Expand All @@ -111,7 +109,7 @@ func (eh *eventHandler) existsRadixDeployComponent(ctx context.Context, appName,
}
return false, err
}
radixDeployments, err := kubequery.GetRadixDeploymentsForEnvironments(ctx, eh.radixClient, appName, []string{envName}, 1)
radixDeployments, err := kubequery.GetRadixDeploymentsForEnvironments(ctx, eh.accounts.UserAccount.RadixClient, appName, []string{envName}, 1)
if err != nil {
return false, err
}
Expand All @@ -124,7 +122,7 @@ func (eh *eventHandler) existsRadixDeployComponent(ctx context.Context, appName,

func (eh *eventHandler) getEvents(ctx context.Context, appName, envName, componentName, podName string) ([]*eventModels.Event, error) {
namespace := utils.GetEnvironmentNamespace(appName, envName)
k8sEvents, err := eh.kubeClient.CoreV1().Events(namespace).List(ctx, metav1.ListOptions{})
k8sEvents, err := eh.accounts.UserAccount.Client.CoreV1().Events(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -153,7 +151,7 @@ func (eh *eventHandler) getEvents(ctx context.Context, appName, envName, compone
}

func (eh *eventHandler) getEnvironmentComponentsPodMap(ctx context.Context, appName string, envName string) (map[k8sTypes.UID]*corev1.Pod, error) {
componentPods, err := kubequery.GetPodsForEnvironmentComponents(ctx, eh.kubeClient, appName, envName)
componentPods, err := kubequery.GetPodsForEnvironmentComponents(ctx, eh.accounts.UserAccount.Client, appName, envName)
if err != nil {
return nil, err
}
Expand All @@ -165,7 +163,7 @@ func (eh *eventHandler) getEnvironmentComponentsPodMap(ctx context.Context, appN
}

func (eh *eventHandler) getEnvironmentComponentsIngressMap(ctx context.Context, appName string, envName string) (map[string]*networkingv1.Ingress, error) {
ingresses, err := kubequery.GetIngressesForEnvironments(ctx, eh.kubeClient, appName, []string{envName}, 1)
ingresses, err := kubequery.GetIngressesForEnvironments(ctx, eh.accounts.UserAccount.Client, appName, []string{envName}, 1)
if err != nil {
return nil, err
}
Expand Down
42 changes: 34 additions & 8 deletions api/events/event_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

eventModels "github.com/equinor/radix-api/api/events/models"
"github.com/equinor/radix-api/models"
"github.com/equinor/radix-common/utils/pointers"
"github.com/equinor/radix-common/utils/slice"
operatorutils "github.com/equinor/radix-operator/pkg/apis/utils"
Expand Down Expand Up @@ -90,19 +91,25 @@ func setupTest() (*kubefake.Clientset, *radixfake.Clientset) {

func Test_EventHandler_Init(t *testing.T) {
kubeClient, radixClient := setupTest()
eh := Init(kubeClient, radixClient).(*eventHandler)
accounts := models.Accounts{
UserAccount: models.Account{Client: kubeClient, RadixClient: radixClient},
ServiceAccount: models.Account{Client: kubeClient, RadixClient: radixClient}}
eh := Init(accounts).(*eventHandler)
assert.NotNil(t, eh)
assert.Equal(t, kubeClient, eh.kubeClient)
assert.Equal(t, kubeClient, eh.accounts.UserAccount.Client)
}

func Test_EventHandler_NoEventsWhenThereIsNoRadixApplication(t *testing.T) {
appName, envName := "app1", "env1"
envNamespace := operatorutils.GetEnvironmentNamespace(appName, envName)
kubeClient, radixClient := setupTest()
accounts := models.Accounts{
UserAccount: models.Account{Client: kubeClient, RadixClient: radixClient},
ServiceAccount: models.Account{Client: kubeClient, RadixClient: radixClient}}

createKubernetesEvent(t, kubeClient, envNamespace, ev1, k8sEventTypeNormal, podServer1, k8sKindPod, uid1)

eventHandler := Init(kubeClient, radixClient)
eventHandler := Init(accounts)
events, err := eventHandler.GetEnvironmentEvents(context.Background(), appName, envName)
assert.NotNil(t, err)
assert.Len(t, events, 0)
Expand All @@ -112,13 +119,16 @@ func Test_EventHandler_NoEventsWhenThereIsNoRadixEnvironment(t *testing.T) {
appName, envName := "app1", "env1"
envNamespace := operatorutils.GetEnvironmentNamespace(appName, envName)
kubeClient, radixClient := setupTest()
accounts := models.Accounts{
UserAccount: models.Account{Client: kubeClient, RadixClient: radixClient},
ServiceAccount: models.Account{Client: kubeClient, RadixClient: radixClient}}

createRadixApp(t, kubeClient, radixClient, appName, envName)
err := radixClient.RadixV1().RadixEnvironments().Delete(context.Background(), fmt.Sprintf("%s-%s", appName, envName), metav1.DeleteOptions{})
require.NoError(t, err)
createKubernetesEvent(t, kubeClient, envNamespace, ev1, k8sEventTypeNormal, podServer1, k8sKindPod, uid1)

eventHandler := Init(kubeClient, radixClient)
eventHandler := Init(accounts)
events, err := eventHandler.GetEnvironmentEvents(context.Background(), appName, envName)
assert.NotNil(t, err)
assert.Len(t, events, 0)
Expand All @@ -130,23 +140,31 @@ func Test_EventHandler_GetEvents_PodState(t *testing.T) {

t.Run("ObjectState is nil for normal event type", func(t *testing.T) {
kubeClient, radixClient := setupTest()
accounts := models.Accounts{
UserAccount: models.Account{Client: kubeClient, RadixClient: radixClient},
ServiceAccount: models.Account{Client: kubeClient, RadixClient: radixClient}}

createRadixApp(t, kubeClient, radixClient, appName, envName)
_, err := createKubernetesPod(kubeClient, podServer1, appName, envName, true, true, 0, uid1)
createKubernetesEvent(t, kubeClient, envNamespace, ev1, k8sEventTypeNormal, podServer1, k8sKindPod, uid1)
require.NoError(t, err)
eventHandler := Init(kubeClient, radixClient)
eventHandler := Init(accounts)
events, _ := eventHandler.GetEnvironmentEvents(context.Background(), appName, envName)
assert.Len(t, events, 1)
assert.Nil(t, events[0].InvolvedObjectState)
})

t.Run("ObjectState has Pod state for warning event type", func(t *testing.T) {
kubeClient, radixClient := setupTest()
accounts := models.Accounts{
UserAccount: models.Account{Client: kubeClient, RadixClient: radixClient},
ServiceAccount: models.Account{Client: kubeClient, RadixClient: radixClient}}

createRadixApp(t, kubeClient, radixClient, appName, envName)
_, err := createKubernetesPod(kubeClient, podServer1, appName, envName, true, false, 0, uid1)
createKubernetesEvent(t, kubeClient, envNamespace, ev1, k8sEventTypeWarning, podServer1, k8sKindPod, uid1)
require.NoError(t, err)
eventHandler := Init(kubeClient, radixClient)
eventHandler := Init(accounts)
events, _ := eventHandler.GetEnvironmentEvents(context.Background(), appName, envName)
assert.Len(t, events, 1)
assert.NotNil(t, events[0].InvolvedObjectState)
Expand All @@ -155,9 +173,13 @@ func Test_EventHandler_GetEvents_PodState(t *testing.T) {

t.Run("ObjectState is nil for warning event type when pod not exist", func(t *testing.T) {
kubeClient, radixClient := setupTest()
accounts := models.Accounts{
UserAccount: models.Account{Client: kubeClient, RadixClient: radixClient},
ServiceAccount: models.Account{Client: kubeClient, RadixClient: radixClient}}

createRadixApp(t, kubeClient, radixClient, appName, envName)
createKubernetesEvent(t, kubeClient, envNamespace, ev1, k8sEventTypeNormal, podServer1, k8sKindPod, uid1)
eventHandler := Init(kubeClient, radixClient)
eventHandler := Init(accounts)
events, _ := eventHandler.GetEnvironmentEvents(context.Background(), appName, envName)
assert.Len(t, events, 1)
assert.Nil(t, events[0].InvolvedObjectState)
Expand Down Expand Up @@ -483,6 +505,10 @@ func assertEvents(t *testing.T, expectedEvents []eventModels.Event, actualEvents

func setupTestEnvForHandler(t *testing.T, ts scenario) (EventHandler, *radixfake.Clientset) {
kubeClient, radixClient := setupTest()
accounts := models.Accounts{
UserAccount: models.Account{Client: kubeClient, RadixClient: radixClient},
ServiceAccount: models.Account{Client: kubeClient, RadixClient: radixClient}}

createRadixApplications(t, ts, kubeClient, radixClient)
for _, evProps := range ts.existingEventProps {
createKubernetesEvent(t, kubeClient, operatorutils.GetEnvironmentNamespace(evProps.appName, evProps.envName), evProps.name, evProps.eventType, evProps.objectName, evProps.objectKind, evProps.objectUid)
Expand All @@ -500,7 +526,7 @@ func setupTestEnvForHandler(t *testing.T, ts scenario) (EventHandler, *radixfake
for _, ingressRuleProp := range ts.existingIngressRuleProps {
createIngress(t, kubeClient, ingressRuleProp)
}
eventHandler := Init(kubeClient, radixClient)
eventHandler := Init(accounts)
return eventHandler, radixClient
}

Expand Down

0 comments on commit df8dd9a

Please sign in to comment.