Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.
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
163 changes: 163 additions & 0 deletions apps/console/internal/app/graph/generated/generated.go

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

1 change: 1 addition & 0 deletions apps/console/internal/app/graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ type Mutation {
core_updateApp(envName: String!, app: AppIn!): App @isLoggedInAndVerified @hasAccount
core_deleteApp(envName: String!, appName: String!): Boolean! @isLoggedInAndVerified @hasAccount
core_interceptApp(envName: String!, appname: String!, deviceName: String!, intercept: Boolean!, portMappings: [Github__com___kloudlite___operator___apis___crds___v1__AppInterceptPortMappingsIn!]): Boolean! @isLoggedInAndVerified @hasAccount
core_interceptAppOnLocalCluster(envName: String!, appname: String!, clusterName: String!, ipAddr: String!, intercept: Boolean!, portMappings: [Github__com___kloudlite___operator___apis___crds___v1__AppInterceptPortMappingsIn!]): Boolean! @isLoggedInAndVerified @hasAccount
core_removeDeviceIntercepts(envName: String!, deviceName: String!): Boolean! @isLoggedInAndVerified @hasAccount

core_createExternalApp(envName: String!, externalApp: ExternalAppIn!): ExternalApp @isLoggedInAndVerified @hasAccount
Expand Down
17 changes: 17 additions & 0 deletions apps/console/internal/app/graph/schema.resolvers.go

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

1 change: 1 addition & 0 deletions apps/console/internal/domain/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ type Domain interface {
DeleteApp(ctx ResourceContext, name string) error

InterceptApp(ctx ResourceContext, appName string, deviceName string, intercept bool, portMappings []crdsv1.AppInterceptPortMappings) (bool, error)
InterceptAppOnLocalCluster(ctx ResourceContext, appName string, clusterName string, ipAddr string, intercept bool, portMappings []crdsv1.AppInterceptPortMappings) (bool, error)
RestartApp(ctx ResourceContext, appName string) error
RemoveDeviceIntercepts(ctx ResourceContext, deviceName string) error

Expand Down
26 changes: 26 additions & 0 deletions apps/console/internal/domain/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,32 @@ func (d *domain) InterceptApp(ctx ResourceContext, appName string, deviceName st
return true, nil
}

// InterceptApp implements Domain.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider refactoring the InterceptApp functions into a single, more flexible function with options.

The introduction of InterceptAppOnLocalCluster unnecessarily duplicates logic from InterceptApp. To reduce complexity and improve maintainability, consider refactoring these functions into a single, more flexible function. Here's a suggested approach:

func (d *domain) InterceptApp(ctx ResourceContext, appName string, intercept bool, options InterceptOptions) (bool, error) {
    if err := d.canMutateResourcesInEnvironment(ctx); err != nil {
        return false, errors.NewE(err)
    }

    patch := repos.Document{
        fc.AppSpecInterceptEnabled: intercept,
    }

    if options.ToDevice != "" {
        patch[fc.AppSpecInterceptToDevice] = options.ToDevice
    }
    if options.ToIPAddr != "" {
        patch[fc.AppSpecInterceptToIPAddr] = options.ToIPAddr
    }
    if options.PortMappings != nil {
        patch[fc.AppSpecInterceptPortMappings] = options.PortMappings
    }

    uApp, err := d.appRepo.Patch(ctx, ctx.DBFilters().Add(fields.MetadataName, appName), patch)
    if err != nil {
        return false, errors.NewE(err)
    }
    if err := d.applyApp(ctx, uApp); err != nil {
        return false, errors.NewE(err)
    }
    return true, nil
}

type InterceptOptions struct {
    ToDevice     string
    ToIPAddr     string
    PortMappings []crdsv1.AppInterceptPortMappings
}

This refactoring combines both functions into a single, more flexible InterceptApp function. It uses an InterceptOptions struct to handle different intercept scenarios, including local cluster specifics. This approach reduces code duplication while maintaining the ability to handle different intercept types. You can then use this function for both regular and local cluster intercepts:

// For regular intercept
options := InterceptOptions{ToDevice: deviceName}
success, err := d.InterceptApp(ctx, appName, true, options)

// For local cluster intercept
options := InterceptOptions{ToDevice: clusterName, ToIPAddr: serviceIP, PortMappings: portMappings}
success, err := d.InterceptApp(ctx, appName, true, options)

This refactoring simplifies the codebase, reduces duplication, and improves maintainability while preserving all functionality and allowing for future extensions.

func (d *domain) InterceptAppOnLocalCluster(ctx ResourceContext, appName string, clusterName string, ipAddr string, intercept bool, portMappings []crdsv1.AppInterceptPortMappings) (bool, error) {
if err := d.canMutateResourcesInEnvironment(ctx); err != nil {
return false, errors.NewE(err)
}

patch := repos.Document{
fc.AppSpecInterceptEnabled: intercept,
fc.AppSpecInterceptToDevice: clusterName,
fc.AppSpecInterceptToIPAddr: ipAddr,
}

if portMappings != nil {
patch[fc.AppSpecInterceptPortMappings] = portMappings
}

uApp, err := d.appRepo.Patch(ctx, ctx.DBFilters().Add(fields.MetadataName, appName), patch)
if err != nil {
return false, errors.NewE(err)
}
if err := d.applyApp(ctx, uApp); err != nil {
return false, errors.NewE(err)
}
return true, nil
}

func (d *domain) RestartApp(ctx ResourceContext, appName string) error {
if err := d.canMutateResourcesInEnvironment(ctx); err != nil {
return errors.NewE(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
AppSpecInterceptEnabled = "spec.intercept.enabled"
AppSpecInterceptPortMappings = "spec.intercept.portMappings"
AppSpecInterceptToDevice = "spec.intercept.toDevice"
AppSpecInterceptToIPAddr = "spec.intercept.toIPAddr"
AppSpecNodeSelector = "spec.nodeSelector"
AppSpecRegion = "spec.region"
AppSpecReplicas = "spec.replicas"
Expand Down Expand Up @@ -122,6 +123,7 @@ const (
ExternalAppSpecInterceptEnabled = "spec.intercept.enabled"
ExternalAppSpecInterceptPortMappings = "spec.intercept.portMappings"
ExternalAppSpecInterceptToDevice = "spec.intercept.toDevice"
ExternalAppSpecInterceptToIPAddr = "spec.intercept.toIPAddr"
ExternalAppSpecRecord = "spec.record"
ExternalAppSpecRecordType = "spec.recordType"
)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/google/go-github/v43 v43.0.0
github.com/google/go-github/v45 v45.2.0
github.com/gorilla/websocket v1.5.0
github.com/kloudlite/operator v0.0.0-20240924122932-030f95a15613
github.com/kloudlite/operator v0.0.0-20240927061728-c8cb9559e4c3
github.com/matoous/go-nanoid/v2 v2.0.0
github.com/pkg/errors v0.9.1
github.com/sendgrid/sendgrid-go v3.11.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kloudlite/container-registry-authorizer v0.0.0-20231021122509-161dc30fde55 h1:YnZh3TL6AG4EfoInx1/L5zcPHd2QxgLKseJB1KtHjdQ=
github.com/kloudlite/container-registry-authorizer v0.0.0-20231021122509-161dc30fde55/go.mod h1:GZj3wZmIw/qCciclRhgQTgmGiqe8wxoVzMXQjbOfnbc=
github.com/kloudlite/operator v0.0.0-20240924122932-030f95a15613 h1:2vmIyEscCnsRpZ0zaXov1jglR+UcRnCwa4va7DbwcYc=
github.com/kloudlite/operator v0.0.0-20240924122932-030f95a15613/go.mod h1:VkreINDW43qeTsDv9gfGH5M9c5OG/jPGYOGxj8otsGY=
github.com/kloudlite/operator v0.0.0-20240927061728-c8cb9559e4c3 h1:7FaI5EE5oxaf10qVhunIrh2p9cf2U1e7AzarfzDuKtQ=
github.com/kloudlite/operator v0.0.0-20240927061728-c8cb9559e4c3/go.mod h1:VkreINDW43qeTsDv9gfGH5M9c5OG/jPGYOGxj8otsGY=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand Down