Skip to content

Commit a1b6b61

Browse files
authored
support selecting deployment target for app commands (#4097)
1 parent 2bb636f commit a1b6b61

File tree

8 files changed

+80
-68
lines changed

8 files changed

+80
-68
lines changed

api/client/porter_app.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,8 +634,12 @@ func (c *Client) PorterYamlV2Pods(
634634
ctx context.Context,
635635
projectID, clusterID uint,
636636
porterAppName string,
637-
req *types.PorterYamlV2PodsRequest,
637+
deploymentTargetName string,
638638
) (*types.GetReleaseAllPodsResponse, error) {
639+
req := &porter_app.PodStatusRequest{
640+
DeploymentTargetName: deploymentTargetName,
641+
}
642+
639643
resp := &types.GetReleaseAllPodsResponse{}
640644

641645
err := c.getRequest(
@@ -655,11 +659,11 @@ func (c *Client) PorterYamlV2Pods(
655659
func (c *Client) UpdateImage(
656660
ctx context.Context,
657661
projectID, clusterID uint,
658-
appName, deploymentTargetId, tag string,
662+
appName, deploymentTargetName, tag string,
659663
) (*porter_app.UpdateImageResponse, error) {
660664
req := &porter_app.UpdateImageRequest{
661-
Tag: tag,
662-
DeploymentTargetId: deploymentTargetId,
665+
Tag: tag,
666+
DeploymentTargetName: deploymentTargetName,
663667
}
664668

665669
resp := &porter_app.UpdateImageResponse{}

api/server/handlers/porter_app/pod_status.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ func NewPodStatusHandler(
3737

3838
// PodStatusRequest is the expected format for a request body on GET /apps/pods
3939
type PodStatusRequest struct {
40-
DeploymentTargetID string `schema:"deployment_target_id"`
41-
ServiceName string `schema:"service"`
40+
DeploymentTargetName string `schema:"deployment_target_name"`
41+
DeploymentTargetID string `schema:"deployment_target_id"`
42+
ServiceName string `schema:"service"`
4243
}
4344

4445
func (c *PodStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@@ -62,20 +63,19 @@ func (c *PodStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
6263
cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
6364
project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
6465

65-
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "service-name", Value: request.ServiceName}, telemetry.AttributeKV{Key: "app-name", Value: appName})
66-
67-
if request.DeploymentTargetID == "" {
68-
err := telemetry.Error(ctx, span, nil, "must provide deployment target id")
69-
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
70-
return
71-
}
72-
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})
66+
telemetry.WithAttributes(span,
67+
telemetry.AttributeKV{Key: "service-name", Value: request.ServiceName},
68+
telemetry.AttributeKV{Key: "app-name", Value: appName},
69+
telemetry.AttributeKV{Key: "input-deployment-target-id", Value: request.DeploymentTargetID},
70+
telemetry.AttributeKV{Key: "input-deployment-target-name", Value: request.DeploymentTargetName},
71+
)
7372

7473
deploymentTarget, err := deployment_target.DeploymentTargetDetails(ctx, deployment_target.DeploymentTargetDetailsInput{
75-
ProjectID: int64(project.ID),
76-
ClusterID: int64(cluster.ID),
77-
DeploymentTargetID: request.DeploymentTargetID,
78-
CCPClient: c.Config().ClusterControlPlaneClient,
74+
ProjectID: int64(project.ID),
75+
ClusterID: int64(cluster.ID),
76+
DeploymentTargetID: request.DeploymentTargetID,
77+
DeploymentTargetName: request.DeploymentTargetName,
78+
CCPClient: c.Config().ClusterControlPlaneClient,
7979
})
8080
if err != nil {
8181
err := telemetry.Error(ctx, span, err, "error getting deployment target details")
@@ -84,7 +84,10 @@ func (c *PodStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
8484
}
8585

8686
namespace := deploymentTarget.Namespace
87-
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace", Value: namespace})
87+
telemetry.WithAttributes(span,
88+
telemetry.AttributeKV{Key: "namespace", Value: namespace},
89+
telemetry.AttributeKV{Key: "deployment-target-id", Value: deploymentTarget.ID},
90+
)
8891

8992
agent, err := c.GetAgent(r, cluster, "")
9093
if err != nil {
@@ -97,9 +100,9 @@ func (c *PodStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
97100

98101
var selectors string
99102
if request.ServiceName == "" {
100-
selectors = fmt.Sprintf("porter.run/deployment-target-id=%s,porter.run/app-name=%s", request.DeploymentTargetID, appName)
103+
selectors = fmt.Sprintf("porter.run/deployment-target-id=%s,porter.run/app-name=%s", deploymentTarget.ID, appName)
101104
} else {
102-
selectors = fmt.Sprintf("porter.run/service-name=%s,porter.run/deployment-target-id=%s,porter.run/app-name=%s", request.ServiceName, request.DeploymentTargetID, appName)
105+
selectors = fmt.Sprintf("porter.run/service-name=%s,porter.run/deployment-target-id=%s,porter.run/app-name=%s", deploymentTarget.ID, request.DeploymentTargetID, appName)
103106
}
104107
podsList, err := agent.GetPodsByLabel(selectors, namespace)
105108
if err != nil {

api/server/handlers/porter_app/update_image.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ func NewUpdateImageHandler(
3636

3737
// UpdateImageRequest is the request object for the /apps/{porter_app_name}/update-image endpoint
3838
type UpdateImageRequest struct {
39-
DeploymentTargetId string `json:"deployment_target_id"`
40-
Repository string `json:"repository"`
41-
Tag string `json:"tag"`
39+
DeploymentTargetId string `json:"deployment_target_id"`
40+
DeploymentTargetName string `json:"deployment_target_name"`
41+
Repository string `json:"repository"`
42+
Tag string `json:"tag"`
4243
}
4344

4445
// UpdateImageResponse is the response object for the /apps/{porter_app_name}/update-image endpoint
@@ -74,12 +75,22 @@ func (c *UpdateImageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
7475
return
7576
}
7677

78+
telemetry.WithAttributes(span,
79+
telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetId},
80+
telemetry.AttributeKV{Key: "deployment-target-name", Value: request.DeploymentTargetName},
81+
telemetry.AttributeKV{Key: "repository", Value: request.Repository},
82+
telemetry.AttributeKV{Key: "tag", Value: request.Tag},
83+
)
84+
7785
updateImageReq := connect.NewRequest(&porterv1.UpdateAppImageRequest{
78-
ProjectId: int64(project.ID),
79-
DeploymentTargetId: request.DeploymentTargetId,
80-
RepositoryUrl: request.Repository,
81-
Tag: request.Tag,
82-
AppName: appName,
86+
ProjectId: int64(project.ID),
87+
RepositoryUrl: request.Repository,
88+
Tag: request.Tag,
89+
AppName: appName,
90+
DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
91+
Id: request.DeploymentTargetId,
92+
Name: request.DeploymentTargetName,
93+
},
8394
})
8495
ccpResp, err := c.Config().ClusterControlPlaneClient.UpdateAppImage(ctx, updateImageReq)
8596
if err != nil {

cli/cmd/commands/app.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ var (
4040
appInteractive bool
4141
appContainerName string
4242
appTag string
43+
deploymentTarget string
4344
appCpuMilli int
4445
appMemoryMi int
4546
jobName string
@@ -58,6 +59,14 @@ func registerCommand_App(cliConf config.CLIConfig) *cobra.Command {
5859
Short: "Runs a command for your application.",
5960
}
6061

62+
appCmd.PersistentFlags().StringVarP(
63+
&deploymentTarget,
64+
"target",
65+
"x",
66+
"default",
67+
"the deployment target for the app, default is \"default\"",
68+
)
69+
6170
// appRunCmd represents the "porter app run" subcommand
6271
appRunCmd := &cobra.Command{
6372
Use: "run [application] -- COMMAND [args...]",
@@ -238,7 +247,7 @@ func appRun(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client a
238247
// updated exec args includes launcher command prepended if needed, otherwise it is the same as execArgs
239248
var updatedExecArgs []string
240249
if project.ValidateApplyV2 {
241-
podsSimple, updatedExecArgs, namespace, err = getPodsFromV2PorterYaml(ctx, execArgs, client, cliConfig, args[0])
250+
podsSimple, updatedExecArgs, namespace, err = getPodsFromV2PorterYaml(ctx, execArgs, client, cliConfig, args[0], deploymentTarget)
242251
if err != nil {
243252
return err
244253
}
@@ -553,23 +562,12 @@ func appGetPodsV1PorterYaml(ctx context.Context, cliConfig config.CLIConfig, cli
553562
return res, containerHasLauncherStartCommand, nil
554563
}
555564

556-
func appGetPodsV2PorterYaml(ctx context.Context, cliConfig config.CLIConfig, client api.Client, porterAppName string) ([]appPodSimple, string, bool, error) {
565+
func appGetPodsV2PorterYaml(ctx context.Context, cliConfig config.CLIConfig, client api.Client, porterAppName string, deploymentTargetName string) ([]appPodSimple, string, bool, error) {
557566
pID := cliConfig.Project
558567
cID := cliConfig.Cluster
559568
var containerHasLauncherStartCommand bool
560569

561-
targetResp, err := client.DefaultDeploymentTarget(ctx, pID, cID)
562-
if err != nil {
563-
return nil, "", containerHasLauncherStartCommand, fmt.Errorf("error calling default deployment target endpoint: %w", err)
564-
}
565-
566-
if targetResp.DeploymentTargetID == "" {
567-
return nil, "", containerHasLauncherStartCommand, errors.New("deployment target id is empty")
568-
}
569-
570-
resp, err := client.PorterYamlV2Pods(ctx, pID, cID, porterAppName, &types.PorterYamlV2PodsRequest{
571-
DeploymentTargetID: targetResp.DeploymentTargetID,
572-
})
570+
resp, err := client.PorterYamlV2Pods(ctx, pID, cID, porterAppName, deploymentTargetName)
573571
if err != nil {
574572
return nil, "", containerHasLauncherStartCommand, err
575573
}
@@ -1215,7 +1213,7 @@ func appUpdateTag(ctx context.Context, user *types.GetAuthenticatedUserResponse,
12151213
}
12161214

12171215
if project.ValidateApplyV2 {
1218-
tag, err := v2.UpdateImage(ctx, appTag, client, cliConf.Project, cliConf.Cluster, args[0])
1216+
tag, err := v2.UpdateImage(ctx, appTag, client, cliConf.Project, cliConf.Cluster, args[0], deploymentTarget)
12191217
if err != nil {
12201218
return fmt.Errorf("error updating tag: %w", err)
12211219
}
@@ -1276,8 +1274,8 @@ func getPodsFromV1PorterYaml(ctx context.Context, execArgs []string, client api.
12761274
return podsSimple, execArgs, nil
12771275
}
12781276

1279-
func getPodsFromV2PorterYaml(ctx context.Context, execArgs []string, client api.Client, cliConfig config.CLIConfig, porterAppName string) ([]appPodSimple, []string, string, error) {
1280-
podsSimple, namespace, containerHasLauncherStartCommand, err := appGetPodsV2PorterYaml(ctx, cliConfig, client, porterAppName)
1277+
func getPodsFromV2PorterYaml(ctx context.Context, execArgs []string, client api.Client, cliConfig config.CLIConfig, porterAppName string, deploymentTargetName string) ([]appPodSimple, []string, string, error) {
1278+
podsSimple, namespace, containerHasLauncherStartCommand, err := appGetPodsV2PorterYaml(ctx, cliConfig, client, porterAppName, deploymentTargetName)
12811279
if err != nil {
12821280
return nil, nil, "", fmt.Errorf("could not retrieve list of pods: %w", err)
12831281
}

cli/cmd/v2/update_image.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@ import (
99
)
1010

1111
// UpdateImage updates the image of an application
12-
func UpdateImage(ctx context.Context, tag string, client api.Client, projectId, clusterId uint, appName string) (string, error) {
13-
targetResp, err := client.DefaultDeploymentTarget(ctx, projectId, clusterId)
14-
if err != nil {
15-
return "", fmt.Errorf("error calling default deployment target endpoint: %w", err)
16-
}
17-
18-
if targetResp.DeploymentTargetID == "" {
19-
return "", errors.New("deployment target id is empty")
12+
func UpdateImage(ctx context.Context, tag string, client api.Client, projectId, clusterId uint, appName string, deploymentTargetName string) (string, error) {
13+
if deploymentTargetName == "" {
14+
return "", errors.New("please provide a deployment target")
2015
}
2116

2217
if tag == "" {
2318
tag = "latest"
2419
}
2520

26-
resp, err := client.UpdateImage(ctx, projectId, clusterId, appName, targetResp.DeploymentTargetID, tag)
21+
resp, err := client.UpdateImage(ctx, projectId, clusterId, appName, deploymentTargetName, tag)
2722
if err != nil {
2823
return "", fmt.Errorf("unable to update image: %w", err)
2924
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ require (
8383
github.com/matryer/is v1.4.0
8484
github.com/nats-io/nats.go v1.24.0
8585
github.com/open-policy-agent/opa v0.44.0
86-
github.com/porter-dev/api-contracts v0.2.71
86+
github.com/porter-dev/api-contracts v0.2.72
8787
github.com/riandyrn/otelchi v0.5.1
8888
github.com/santhosh-tekuri/jsonschema/v5 v5.0.1
8989
github.com/stefanmcshane/helm v0.0.0-20221213002717-88a4a2c6e77d

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,8 +1520,8 @@ github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77
15201520
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
15211521
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
15221522
github.com/polyfloyd/go-errorlint v0.0.0-20210722154253-910bb7978349/go.mod h1:wi9BfjxjF/bwiZ701TzmfKu6UKC357IOAtNr0Td0Lvw=
1523-
github.com/porter-dev/api-contracts v0.2.71 h1:A9JRjOzXLx9U2ECCvUORZEhLygExDyB0kafe+TnUsM4=
1524-
github.com/porter-dev/api-contracts v0.2.71/go.mod h1:fX6JmP5QuzxDLvqP3evFOTXjI4dHxsG0+VKNTjImZU8=
1523+
github.com/porter-dev/api-contracts v0.2.72 h1:+yYSMzRBmlum+u0MJBe6WBf+nvWHuf37MQUiNTH/nIY=
1524+
github.com/porter-dev/api-contracts v0.2.72/go.mod h1:fX6JmP5QuzxDLvqP3evFOTXjI4dHxsG0+VKNTjImZU8=
15251525
github.com/porter-dev/switchboard v0.0.3 h1:dBuYkiVLa5Ce7059d6qTe9a1C2XEORFEanhbtV92R+M=
15261526
github.com/porter-dev/switchboard v0.0.3/go.mod h1:xSPzqSFMQ6OSbp42fhCi4AbGbQbsm6nRvOkrblFeXU4=
15271527
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=

internal/deployment_target/get.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ import (
1111

1212
// DeploymentTargetDetailsInput is the input to the DeploymentTargetDetails function
1313
type DeploymentTargetDetailsInput struct {
14-
ProjectID int64
15-
ClusterID int64
16-
DeploymentTargetID string
17-
CCPClient porterv1connect.ClusterControlPlaneServiceClient
14+
ProjectID int64
15+
ClusterID int64
16+
DeploymentTargetID string
17+
DeploymentTargetName string
18+
CCPClient porterv1connect.ClusterControlPlaneServiceClient
1819
}
1920

2021
// DeploymentTarget is a struct representing the unique cluster, namespace pair for a deployment target
@@ -40,16 +41,16 @@ func DeploymentTargetDetails(ctx context.Context, inp DeploymentTargetDetailsInp
4041
if inp.ProjectID == 0 {
4142
return deploymentTarget, telemetry.Error(ctx, span, nil, "project id is empty")
4243
}
43-
if inp.DeploymentTargetID == "" {
44-
return deploymentTarget, telemetry.Error(ctx, span, nil, "deployment target id is empty")
45-
}
4644
if inp.CCPClient == nil {
4745
return deploymentTarget, telemetry.Error(ctx, span, nil, "cluster control plane client is nil")
4846
}
4947

5048
deploymentTargetDetailsReq := connect.NewRequest(&porterv1.DeploymentTargetDetailsRequest{
51-
ProjectId: inp.ProjectID,
52-
DeploymentTargetId: inp.DeploymentTargetID,
49+
ProjectId: inp.ProjectID,
50+
DeploymentTargetIdentifier: &porterv1.DeploymentTargetIdentifier{
51+
Id: inp.DeploymentTargetID,
52+
Name: inp.DeploymentTargetName,
53+
},
5354
})
5455

5556
deploymentTargetDetailsResp, err := inp.CCPClient.DeploymentTargetDetails(ctx, deploymentTargetDetailsReq)
@@ -67,7 +68,7 @@ func DeploymentTargetDetails(ctx context.Context, inp DeploymentTargetDetailsInp
6768
}
6869

6970
deploymentTarget = DeploymentTarget{
70-
ID: inp.DeploymentTargetID,
71+
ID: target.Id,
7172
Name: target.Name,
7273
Namespace: target.Namespace,
7374
ClusterID: target.ClusterId,

0 commit comments

Comments
 (0)