Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show events on components and replicas #698

Merged
merged 19 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions api/applications/applications_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -77,7 +76,6 @@ func setupTestWithFactory(t *testing.T, handlerFactory ApplicationHandlerFactory
commonTestUtils := commontest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient)
err := commonTestUtils.CreateClusterPrerequisites(clusterName, egressIps, subscriptionId)
require.NoError(t, err)
_ = os.Setenv(defaults.ActiveClusternameEnvironmentVariable, clusterName)
prometheusHandlerMock := createPrometheusHandlerMock(t, radixclient, nil)

// controllerTestUtils is used for issuing HTTP request and processing responses
Expand Down Expand Up @@ -974,6 +972,7 @@ func TestGetApplication_WithEnvironments(t *testing.T) {
WithEnvironmentName(anyOrphanedEnvironment))
orphanedRe.Status.Reconciled = metav1.Now()
orphanedRe.Status.Orphaned = true
orphanedRe.Status.OrphanedTimestamp = pointers.Ptr(metav1.Now())
_, err = radix.RadixV1().RadixEnvironments().Update(context.Background(), orphanedRe, metav1.UpdateOptions{})
require.NoError(t, err)

Expand Down Expand Up @@ -1684,7 +1683,6 @@ func TestHandleTriggerPipeline_Promote_JobHasCorrectParameters(t *testing.T) {
const (
appName = "an-app"
commitId = "475f241c-478b-49da-adfb-3c336aaab8d2"
deploymentName = "a-deployment"
fromEnvironment = "origin"
toEnvironment = "target"
)
Expand Down
4 changes: 0 additions & 4 deletions api/buildstatus/build_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package buildstatus
import (
"errors"
"io"
"os"
"testing"
"time"

Expand All @@ -15,7 +14,6 @@ import (
certclientfake "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned/fake"
controllertest "github.com/equinor/radix-api/api/test"
"github.com/equinor/radix-api/api/test/mock"
"github.com/equinor/radix-operator/pkg/apis/defaults"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
commontest "github.com/equinor/radix-operator/pkg/apis/test"
builders "github.com/equinor/radix-operator/pkg/apis/utils"
Expand Down Expand Up @@ -43,8 +41,6 @@ func setupTest(t *testing.T) (*commontest.Utils, *kubefake.Clientset, *radixfake
commonTestUtils := commontest.NewTestUtils(kubeclient, radixclient, kedaClient, secretproviderclient)
err := commonTestUtils.CreateClusterPrerequisites(clusterName, egressIps, subscriptionId)
require.NoError(t, err)
_ = os.Setenv(defaults.ActiveClusternameEnvironmentVariable, clusterName)

return &commonTestUtils, kubeclient, radixclient, kedaClient, secretproviderclient, certClient
}

Expand Down
134 changes: 133 additions & 1 deletion api/environments/environment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ func (c *environmentController) GetRoutes() models.Routes {
Method: "GET",
HandlerFunc: c.GetEnvironmentEvents,
},
models.Route{
Path: rootPath + "/environments/{envName}/events/components/{componentName}",
Method: "GET",
HandlerFunc: c.GetComponentEvents,
},
models.Route{
Path: rootPath + "/environments/{envName}/events/components/{componentName}/replicas/{podName}",
Method: "GET",
HandlerFunc: c.GetPodEvents,
},
models.Route{
Path: rootPath + "/environments/{envName}/components/{componentName}/stop",
Method: "POST",
Expand Down Expand Up @@ -522,15 +532,137 @@ func (c *environmentController) GetEnvironmentEvents(accounts models.Accounts, w
envName := mux.Vars(r)["envName"]

environmentHandler := c.environmentHandlerFactory(accounts)
events, err := environmentHandler.GetEnvironmentEvents(r.Context(), appName, envName)
events, err := environmentHandler.eventHandler.GetEnvironmentEvents(r.Context(), appName, envName)

if err != nil {
c.ErrorResponse(w, r, err)
return
}

c.JSONResponse(w, r, events)

}

// GetComponentEvents Get events for an application environment component
func (c *environmentController) GetComponentEvents(accounts models.Accounts, w http.ResponseWriter, r *http.Request) {
// swagger:operation GET /applications/{appName}/environments/{envName}/events/components/{componentName} environment getComponentEvents
// ---
// summary: Lists events for an application environment
// parameters:
// - name: appName
// in: path
// description: name of Radix application
// type: string
// required: true
// - name: envName
// in: path
// description: name of environment
// type: string
// required: true
// - name: componentName
// in: path
// description: Name of component
// type: string
// required: true
// - name: Impersonate-User
// in: header
// description: Works only with custom setup of cluster. Allow impersonation of test users (Required if Impersonate-Group is set)
// type: string
// required: false
// - name: Impersonate-Group
// in: header
// description: Works only with custom setup of cluster. Allow impersonation of a comma-seperated list of test groups (Required if Impersonate-User is set)
// type: string
// required: false
// responses:
// "200":
// description: "Successful get environment events"
// schema:
// type: "array"
// items:
// "$ref": "#/definitions/Event"
// "401":
// description: "Unauthorized"
// "404":
// description: "Not found"

appName := mux.Vars(r)["appName"]
envName := mux.Vars(r)["envName"]
componentName := mux.Vars(r)["componentName"]

environmentHandler := c.environmentHandlerFactory(accounts)
events, err := environmentHandler.eventHandler.GetComponentEvents(r.Context(), appName, envName, componentName)

if err != nil {
c.ErrorResponse(w, r, err)
return
}

c.JSONResponse(w, r, events)
}

// GetPodEvents Get events for an application environment component
func (c *environmentController) GetPodEvents(accounts models.Accounts, w http.ResponseWriter, r *http.Request) {
// swagger:operation GET /applications/{appName}/environments/{envName}/events/components/{componentName}/replicas/{podName} environment getReplicaEvents
// ---
// summary: Lists events for an application environment
// parameters:
// - name: appName
// in: path
// description: name of Radix application
// type: string
// required: true
// - name: envName
// in: path
// description: name of environment
// type: string
// required: true
// - name: componentName
// in: path
// description: Name of component
// type: string
// required: true
// - name: podName
// in: path
// description: Name of pod
// type: string
// required: true
// - name: Impersonate-User
// in: header
// description: Works only with custom setup of cluster. Allow impersonation of test users (Required if Impersonate-Group is set)
// type: string
// required: false
// - name: Impersonate-Group
// in: header
// description: Works only with custom setup of cluster. Allow impersonation of a comma-seperated list of test groups (Required if Impersonate-User is set)
// type: string
// required: false
// responses:
// "200":
// description: "Successful get environment events"
// schema:
// type: "array"
// items:
// "$ref": "#/definitions/Event"
// "401":
// description: "Unauthorized"
// "404":
// description: "Not found"

appName := mux.Vars(r)["appName"]
envName := mux.Vars(r)["envName"]
componentName := mux.Vars(r)["componentName"]
podName := mux.Vars(r)["podName"]

environmentHandler := c.environmentHandlerFactory(accounts)
events, err := environmentHandler.eventHandler.GetPodEvents(r.Context(), appName, envName, componentName, podName)

if err != nil {
c.ErrorResponse(w, r, err)
return
}

c.JSONResponse(w, r, events)
}

// StopComponent Stops job
Expand Down
41 changes: 2 additions & 39 deletions api/environments/environment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ import (
"testing"
"time"

certclient "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned"
certclientfake "github.com/cert-manager/cert-manager/pkg/client/clientset/versioned/fake"
deploymentModels "github.com/equinor/radix-api/api/deployments/models"
environmentModels "github.com/equinor/radix-api/api/environments/models"
event "github.com/equinor/radix-api/api/events"
eventMock "github.com/equinor/radix-api/api/events/mock"
eventModels "github.com/equinor/radix-api/api/events/models"
"github.com/equinor/radix-api/api/secrets"
secretModels "github.com/equinor/radix-api/api/secrets/models"
"github.com/equinor/radix-api/api/secrets/suffix"
controllertest "github.com/equinor/radix-api/api/test"
"github.com/equinor/radix-api/api/utils"
authnmock "github.com/equinor/radix-api/api/utils/token/mock"
"github.com/equinor/radix-api/models"
radixhttp "github.com/equinor/radix-common/net/http"
radixutils "github.com/equinor/radix-common/utils"
"github.com/equinor/radix-common/utils/numbers"
Expand All @@ -47,7 +43,6 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
kubefake "k8s.io/client-go/kubernetes/fake"
testing2 "k8s.io/client-go/testing"
secretsstorevclient "sigs.k8s.io/secrets-store-csi-driver/pkg/client/clientset/versioned"
Expand Down Expand Up @@ -328,7 +323,8 @@ func TestDeleteEnvironment_OneOrphanedEnvironment_OnlyOrphanedCanBeDeleted(t *te
NewEnvironmentBuilder().
WithAppLabel().
WithAppName(anyAppName).
WithEnvironmentName(anyOrphanedEnvironment))
WithEnvironmentName(anyOrphanedEnvironment).
WithOrphaned(true))
require.NoError(t, err)

// Test
Expand Down Expand Up @@ -987,27 +983,6 @@ func TestCreateSecret(t *testing.T) {
assert.Equal(t, http.StatusOK, response.Code)
}

func Test_GetEnvironmentEvents_Handler(t *testing.T) {
commonTestUtils, _, _, kubeclient, radixclient, kedaClient, _, secretproviderclient, certClient := setupTest(t, nil)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
eventHandler := eventMock.NewMockEventHandler(ctrl)
handler := initHandler(kubeclient, radixclient, kedaClient, secretproviderclient, certClient, WithEventHandler(eventHandler))
raBuilder := operatorutils.ARadixApplication().WithAppName(anyAppName).WithEnvironment(anyEnvironment, "master")

_, err := commonTestUtils.ApplyApplication(raBuilder)
require.NoError(t, err)
nsFunc := event.RadixEnvironmentNamespace(raBuilder.BuildRA(), anyEnvironment)
eventHandler.EXPECT().
GetEvents(context.Background(), controllertest.EqualsNamespaceFunc(nsFunc)).
Return([]*eventModels.Event{{}, {}}, nil).
Times(1)

events, err := handler.GetEnvironmentEvents(context.Background(), anyAppName, anyEnvironment)
assert.Nil(t, err)
assert.Len(t, events, 2)
}

func TestRestartAuxiliaryResource(t *testing.T) {
auxType := "oauth"
called := 0
Expand Down Expand Up @@ -2702,18 +2677,6 @@ func Test_DeleteBatch(t *testing.T) {
}
}

func initHandler(client kubernetes.Interface,
radixclient radixclient.Interface,
kedaClient kedav2.Interface,
secretproviderclient secretsstorevclient.Interface,
certClient certclient.Interface,
handlerConfig ...EnvironmentHandlerOptions) EnvironmentHandler {
accounts := models.NewAccounts(client, radixclient, kedaClient, secretproviderclient, nil, certClient, client, radixclient, kedaClient, secretproviderclient, nil, certClient)
options := []EnvironmentHandlerOptions{WithAccounts(accounts)}
options = append(options, handlerConfig...)
return Init(options...)
}

type ComponentCreatorStruct struct {
name string
number int
Expand Down
29 changes: 2 additions & 27 deletions api/environments/environment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
deploymentModels "github.com/equinor/radix-api/api/deployments/models"
environmentModels "github.com/equinor/radix-api/api/environments/models"
"github.com/equinor/radix-api/api/events"
eventModels "github.com/equinor/radix-api/api/events/models"
"github.com/equinor/radix-api/api/kubequery"
apimodels "github.com/equinor/radix-api/api/models"
"github.com/equinor/radix-api/api/pods"
Expand Down Expand Up @@ -39,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)
eh.eventHandler = events.Init(accounts.UserAccount.Client, accounts.UserAccount.RadixClient)
eh.accounts = accounts
}
}
Expand Down Expand Up @@ -239,7 +238,7 @@ func (eh EnvironmentHandler) DeleteEnvironment(ctx context.Context, appName, env
return err
}

if !re.Status.Orphaned {
if !re.Status.Orphaned && re.Status.OrphanedTimestamp == nil {
// Must be removed from radix config first
return environmentModels.CannotDeleteNonOrphanedEnvironment(appName, envName)
}
Expand All @@ -254,30 +253,6 @@ func (eh EnvironmentHandler) DeleteEnvironment(ctx context.Context, appName, env
return nil
}

// GetEnvironmentEvents Handler for GetEnvironmentEvents
func (eh EnvironmentHandler) GetEnvironmentEvents(ctx context.Context, appName, envName string) ([]*eventModels.Event, error) {
radixApplication, err := kubequery.GetRadixApplication(ctx, eh.accounts.UserAccount.RadixClient, appName)
if err != nil {
return nil, err
}

_, err = kubequery.GetRadixEnvironment(ctx, eh.accounts.ServiceAccount.RadixClient, appName, envName)
// _, err = eh.getConfigurationStatus(ctx, envName, radixApplication)
if err != nil {
if errors.IsNotFound(err) {
return nil, environmentModels.NonExistingEnvironment(err, appName, envName)
}
return nil, err
}

environmentEvents, err := eh.eventHandler.GetEvents(ctx, events.RadixEnvironmentNamespace(radixApplication, envName))
if err != nil {
return nil, err
}

return environmentEvents, nil
}

// getNotOrphanedEnvNames returns a slice of non-unique-names of not-orphaned environments
func (eh EnvironmentHandler) getNotOrphanedEnvNames(ctx context.Context, appName string) ([]string, error) {
reList, err := kubequery.GetRadixEnvironments(ctx, eh.accounts.ServiceAccount.RadixClient, appName)
Expand Down
Loading
Loading