Skip to content

Commit

Permalink
Added method giving radix component deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
satr committed Jul 13, 2023
1 parent 4651401 commit 16e275c
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 40 deletions.
36 changes: 33 additions & 3 deletions api/deployments/deployment_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/equinor/radix-operator/pkg/apis/kube"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
operatorUtils "github.com/equinor/radix-operator/pkg/apis/utils"
radixLabels "github.com/equinor/radix-operator/pkg/apis/utils/labels"
radixlabels "github.com/equinor/radix-operator/pkg/apis/utils/labels"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -28,7 +29,8 @@ type DeployHandler interface {
GetComponentsForDeploymentName(ctx context.Context, appName, deploymentID string) ([]*deploymentModels.Component, error)
GetComponentsForDeployment(ctx context.Context, appName string, deployment *deploymentModels.DeploymentSummary) ([]*deploymentModels.Component, error)
GetLatestDeploymentForApplicationEnvironment(ctx context.Context, appName, environment string) (*deploymentModels.DeploymentSummary, error)
GetDeploymentsForJob(ctx context.Context, appName, jobName string) ([]*deploymentModels.DeploymentSummary, error)
GetDeploymentsForPipelineJob(context.Context, string, string) ([]*deploymentModels.DeploymentSummary, error)
GetDeploymentsForJobComponent(context.Context, string, string, string) ([]*deploymentModels.ComponentDeploymentSummary, error)
}

// DeployHandler Instance variables
Expand Down Expand Up @@ -105,8 +107,8 @@ func (deploy *deployHandler) GetDeploymentsForApplicationEnvironment(ctx context
return deployments, err
}

// GetDeploymentsForJob Lists deployments for job name
func (deploy *deployHandler) GetDeploymentsForJob(ctx context.Context, appName, jobName string) ([]*deploymentModels.DeploymentSummary, error) {
// GetDeploymentsForPipelineJob Lists deployments for pipeline job name
func (deploy *deployHandler) GetDeploymentsForPipelineJob(ctx context.Context, appName, jobName string) ([]*deploymentModels.DeploymentSummary, error) {
environments, err := deploy.getEnvironmentNames(ctx, appName)
if err != nil {
return nil, err
Expand All @@ -115,6 +117,34 @@ func (deploy *deployHandler) GetDeploymentsForJob(ctx context.Context, appName,
return deploy.getDeployments(ctx, appName, environments, jobName, false)
}

// GetDeploymentsForJobComponent Lists deployments for application job component name
func (deploy *deployHandler) GetDeploymentsForJobComponent(ctx context.Context, appName, environment, componentName string) ([]*deploymentModels.ComponentDeploymentSummary, error) {
ns := operatorUtils.GetEnvironmentNamespace(appName, environment)
radixDeploymentList, err := deploy.accounts.UserAccount.RadixClient.RadixV1().RadixDeployments(ns).List(ctx, metav1.ListOptions{LabelSelector: radixLabels.Merge(
radixLabels.ForApplicationName(appName), radixLabels.ForEnvironmentName(environment)).String()})
if err != nil {
return nil, err
}
rds := sortRdsByActiveFromDesc(radixDeploymentList.Items)

var deploymentSummaries []*deploymentModels.ComponentDeploymentSummary
for _, rd := range rds {
builder := deploymentModels.NewComponentDeploymentSummaryBuilder().WithRadixDeployment(&rd)
for _, jobComponent := range rd.Spec.Jobs {
if jobComponent.Name != componentName {
continue
}
summary, err := builder.WithRadixDeployComponent(&jobComponent).Build()
if err != nil {
return nil, err
}
deploymentSummaries = append(deploymentSummaries, summary)
break
}
}
return deploymentSummaries, nil
}

// GetDeploymentWithName Handler for GetDeploymentWithName
func (deploy *deployHandler) GetDeploymentWithName(ctx context.Context, appName, deploymentName string) (*deploymentModels.Deployment, error) {
// Need to list all deployments to find active to of deployment
Expand Down
27 changes: 21 additions & 6 deletions api/deployments/mock/deployment_handler_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions api/deployments/models/component_deployment_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package models

import (
"fmt"
radixutils "github.com/equinor/radix-common/utils"
"github.com/equinor/radix-operator/pkg/apis/kube"
v1 "github.com/equinor/radix-operator/pkg/apis/radix/v1"
)

// ComponentDeploymentSummaryBuilder Builds DTOs
type ComponentDeploymentSummaryBuilder interface {
WithRadixDeployment(*v1.RadixDeployment) ComponentDeploymentSummaryBuilder
WithRadixDeployComponent(component v1.RadixCommonDeployComponent) ComponentDeploymentSummaryBuilder
Build() (*ComponentDeploymentSummary, error)
}

type componentDeploymentSummaryBuilder struct {
component v1.RadixCommonDeployComponent
radixDeployment *v1.RadixDeployment
}

// NewComponentDeploymentSummaryBuilder Constructor for application ComponentDeploymentSummaryBuilder
func NewComponentDeploymentSummaryBuilder() ComponentDeploymentSummaryBuilder {
return &componentDeploymentSummaryBuilder{}
}

// WithRadixDeployComponent With RadixDeployComponent
func (b *componentDeploymentSummaryBuilder) WithRadixDeployComponent(component v1.RadixCommonDeployComponent) ComponentDeploymentSummaryBuilder {
b.component = component
return b
}

func (b *componentDeploymentSummaryBuilder) WithRadixDeployment(rd *v1.RadixDeployment) ComponentDeploymentSummaryBuilder {
b.radixDeployment = rd
return b
}

func (b *componentDeploymentSummaryBuilder) Build() (*ComponentDeploymentSummary, error) {
if b.component == nil || b.radixDeployment == nil {
return nil, fmt.Errorf("component or RadixDeployment are empty")
}
return &ComponentDeploymentSummary{
Name: b.radixDeployment.GetName(),
ComponentName: b.component.GetName(),
ActiveFrom: radixutils.FormatTimestamp(b.radixDeployment.Status.ActiveFrom.Time),
ActiveTo: radixutils.FormatTimestamp(b.radixDeployment.Status.ActiveTo.Time),
Condition: string(b.radixDeployment.Status.Condition),
GitCommitHash: b.radixDeployment.GetLabels()[kube.RadixCommitLabel],
}, nil
}
38 changes: 38 additions & 0 deletions api/deployments/models/deployment_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,41 @@ type DeploymentSummary struct {
// example: "v1.22.1 v1.22.3"
GitTags string `json:"gitTags,omitempty"`
}

// ComponentDeploymentSummary describe a component deployment
// swagger:model ComponentDeploymentSummary
type ComponentDeploymentSummary struct {
// Name the unique name of the Radix application deployment
//
// required: true
// example: radix-canary-golang-tzbqi
Name string `json:"name"`

// Name of the component
//
// required: true
ComponentName string `json:"componentName"`

// ActiveFrom Timestamp when the deployment starts (or created)
//
// required: true
// example: 2006-01-02T15:04:05Z
ActiveFrom string `json:"activeFrom"`

// ActiveTo Timestamp when the deployment ends
//
// required: false
// example: 2006-01-02T15:04:05Z
ActiveTo string `json:"activeTo,omitempty"`

// GitCommitHash the hash of the git commit from which radixconfig.yaml was parsed
//
// required: false
// example: 4faca8595c5283a9d0f17a623b9255a0d9866a2e
GitCommitHash string `json:"gitCommitHash,omitempty"`

// Condition the condition of the component
//
// required: false
Condition string `json:"condition,omitempty"`
}
62 changes: 62 additions & 0 deletions api/environments/environment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func (c *environmentController) GetRoutes() models.Routes {
Method: "GET",
HandlerFunc: c.GetOAuthAuxiliaryResourcePodLog,
},
models.Route{
Path: rootPath + "/environments/{envName}/jobcomponents/{jobComponentName}/deployments",
Method: "GET",
HandlerFunc: c.GetJobComponentRadixDeployments,
},
models.Route{
Path: rootPath + "/environments/{envName}/jobcomponents/{jobComponentName}/jobs",
Method: "GET",
Expand Down Expand Up @@ -1212,6 +1217,63 @@ func (c *environmentController) GetScheduledJobLog(accounts models.Accounts, w h
}
}

// GetJobComponentRadixDeployments Get list of RadixDeployments for the job component
func (c *environmentController) GetJobComponentRadixDeployments(accounts models.Accounts, w http.ResponseWriter, r *http.Request) {
// swagger:operation GET /applications/{appName}/environments/{envName}/jobcomponents/{jobComponentName}/deployments job GetJobComponentRadixDeployments
// ---
// summary: Get list of RadixDeployments for the job component
// parameters:
// - name: appName
// in: path
// description: Name of application
// type: string
// required: true
// - name: envName
// in: path
// description: Name of environment
// type: string
// required: true
// - name: jobComponentName
// in: path
// description: Name of job-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 test group (Required if Impersonate-User is set)
// type: array
// items:
// type: string
// required: false
// responses:
// "200":
// description: "Radix component deployments"
// schema:
// type: array
// items:
// "$ref": "#/definitions/ComponentDeploymentSummary"
// "404":
// description: "Not found"
appName := mux.Vars(r)["appName"]
envName := mux.Vars(r)["envName"]
jobComponentName := mux.Vars(r)["jobComponentName"]

eh := c.environmentHandlerFactory(accounts)
jobComponentDeployments, err := eh.deployHandler.GetDeploymentsForJobComponent(r.Context(), appName, envName, jobComponentName)

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

radixhttp.JSONResponse(w, r, jobComponentDeployments)
}

// GetJobs Get list of scheduled jobs
func (c *environmentController) GetJobs(accounts models.Accounts, w http.ResponseWriter, r *http.Request) {
// swagger:operation GET /applications/{appName}/environments/{envName}/jobcomponents/{jobComponentName}/jobs job getJobs
Expand Down
6 changes: 3 additions & 3 deletions api/environments/environment_controller_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
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/tlsvalidator/mock"
tlsvalidatormock "github.com/equinor/radix-api/api/utils/tlsvalidator/mock"
"github.com/equinor/radix-common/utils"
"github.com/equinor/radix-common/utils/pointers"
operatordefaults "github.com/equinor/radix-operator/pkg/apis/defaults"
Expand Down Expand Up @@ -823,7 +823,7 @@ func Test_ExternalDnsAliasSecretTestSuite(t *testing.T) {

type externalDnsAliasSecretTestSuite struct {
suite.Suite
tlsValidator *mock.MockTLSSecretValidator
tlsValidator *tlsvalidatormock.MockInterface
commonTestUtils *commontest.Utils
envvironmentTestUtils *controllertest.Utils
kubeClient kubernetes.Interface
Expand Down Expand Up @@ -867,7 +867,7 @@ func (s *externalDnsAliasSecretTestSuite) buildCertificate(certCN, issuerCN stri

func (s *externalDnsAliasSecretTestSuite) SetupTest() {
ctrl := gomock.NewController(s.T())
s.tlsValidator = mock.NewMockTLSSecretValidator(ctrl)
s.tlsValidator = tlsvalidatormock.NewMockInterface(ctrl)
s.commonTestUtils, s.envvironmentTestUtils, _, s.kubeClient, s.radixClient, _, s.secretProviderClient = setupTest([]EnvironmentHandlerOptions{WithTLSSecretValidator(s.tlsValidator)})

s.appName, s.componentName, s.environmentName, s.alias = "any-app", "backend", "dev", "cdn.myalias.com"
Expand Down
2 changes: 1 addition & 1 deletion api/environments/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (eh EnvironmentHandler) getRadixDeployment(ctx context.Context, appName string, envName string) (*deploymentModels.DeploymentSummary, *v1.RadixDeployment, error) {
func (eh EnvironmentHandler) getRadixDeployment(ctx context.Context, appName, envName string) (*deploymentModels.DeploymentSummary, *v1.RadixDeployment, error) {
envNs := operatorutils.GetEnvironmentNamespace(appName, envName)
deploymentSummary, err := eh.deployHandler.GetLatestDeploymentForApplicationEnvironment(ctx, appName, envName)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions api/jobs/job_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (jh JobHandler) GetApplicationJob(ctx context.Context, appName, jobName str
return nil, err
}

jobDeployments, err := jh.deploy.GetDeploymentsForJob(ctx, appName, jobName)
jobDeployments, err := jh.deploy.GetDeploymentsForPipelineJob(ctx, appName, jobName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func getPipelineRunTaskModelByTaskSpec(pipelineRun *v1beta1.PipelineRun, taskRun
}
}
logEmbeddedCommandIndex := strings.Index(pipelineTaskModel.StatusMessage, "for logs run")
if logEmbeddedCommandIndex >= 0 { //Avoid to publish kubectl command, provided by Tekton component after "for logs run" prefix for failed task step
if logEmbeddedCommandIndex >= 0 { // Avoid to publish kubectl command, provided by Tekton component after "for logs run" prefix for failed task step
pipelineTaskModel.StatusMessage = pipelineTaskModel.StatusMessage[0:logEmbeddedCommandIndex]
}
return &pipelineTaskModel
Expand Down
10 changes: 5 additions & 5 deletions api/jobs/job_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ func (s *JobHandlerTestSuite) Test_GetApplicationJob() {
s.Nil(actualJob)
})

s.Run("deployHandle.GetDeploymentsForJob return error", func() {
s.Run("deployHandle.GetDeploymentsForPipelineJob return error", func() {
ctrl := gomock.NewController(s.T())
defer ctrl.Finish()

dh := deployMock.NewMockDeployHandler(ctrl)
dh.EXPECT().GetDeploymentsForJob(context.Background(), appName, jobName).Return(nil, assert.AnError).Times(1)
dh.EXPECT().GetDeploymentsForPipelineJob(context.Background(), appName, jobName).Return(nil, assert.AnError).Times(1)
h := Init(s.accounts, dh)

actualJob, actualErr := h.GetApplicationJob(context.Background(), appName, jobName)
Expand All @@ -138,7 +138,7 @@ func (s *JobHandlerTestSuite) Test_GetApplicationJob() {

deployList := []*deploymentModels.DeploymentSummary{&deploySummary}
dh := deployMock.NewMockDeployHandler(ctrl)
dh.EXPECT().GetDeploymentsForJob(context.Background(), appName, jobName).Return(deployList, nil).Times(1)
dh.EXPECT().GetDeploymentsForPipelineJob(context.Background(), appName, jobName).Return(deployList, nil).Times(1)
h := Init(s.accounts, dh)

actualJob, actualErr := h.GetApplicationJob(context.Background(), appName, jobName)
Expand Down Expand Up @@ -181,7 +181,7 @@ func (s *JobHandlerTestSuite) Test_GetApplicationJob_Created() {
defer ctrl.Finish()

dh := deployMock.NewMockDeployHandler(ctrl)
dh.EXPECT().GetDeploymentsForJob(context.Background(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
dh.EXPECT().GetDeploymentsForPipelineJob(context.Background(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
h := Init(s.accounts, dh)
rj := v1.RadixJob{ObjectMeta: metav1.ObjectMeta{Name: scenario.jobName, Namespace: utils.GetAppNamespace(appName), CreationTimestamp: scenario.creationTimestamp}}
if scenario.jobStatusCreated != emptyTime {
Expand Down Expand Up @@ -212,7 +212,7 @@ func (s *JobHandlerTestSuite) Test_GetApplicationJob_Status() {
defer ctrl.Finish()

dh := deployMock.NewMockDeployHandler(ctrl)
dh.EXPECT().GetDeploymentsForJob(context.Background(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
dh.EXPECT().GetDeploymentsForPipelineJob(context.Background(), gomock.Any(), gomock.Any()).Return(nil, nil).Times(1)
h := Init(s.accounts, dh)
rj := v1.RadixJob{
ObjectMeta: metav1.ObjectMeta{Name: scenario.jobName, Namespace: utils.GetAppNamespace(appName)},
Expand Down
Loading

0 comments on commit 16e275c

Please sign in to comment.