-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: d-g-town <[email protected]>
- Loading branch information
Showing
7 changed files
with
236 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package porter_app | ||
|
||
import ( | ||
"net/http" | ||
|
||
"connectrpc.com/connect" | ||
porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1" | ||
"github.com/porter-dev/porter/api/server/authz" | ||
"github.com/porter-dev/porter/api/server/handlers" | ||
"github.com/porter-dev/porter/api/server/shared" | ||
"github.com/porter-dev/porter/api/server/shared/apierrors" | ||
"github.com/porter-dev/porter/api/server/shared/config" | ||
"github.com/porter-dev/porter/api/server/shared/requestutils" | ||
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/internal/models" | ||
"github.com/porter-dev/porter/internal/telemetry" | ||
) | ||
|
||
// UpdateImageHandler is the handler for the /apps/{porter_app_name}/update-image endpoint | ||
type UpdateImageHandler struct { | ||
handlers.PorterHandlerReadWriter | ||
authz.KubernetesAgentGetter | ||
} | ||
|
||
// NewUpdateImageHandler handles POST requests to the /apps/{porter_app_name}/update-image endpoint | ||
func NewUpdateImageHandler( | ||
config *config.Config, | ||
decoderValidator shared.RequestDecoderValidator, | ||
writer shared.ResultWriter, | ||
) *UpdateImageHandler { | ||
return &UpdateImageHandler{ | ||
PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), | ||
KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config), | ||
} | ||
} | ||
|
||
// UpdateImageRequest is the request object for the /apps/{porter_app_name}/update-image endpoint | ||
type UpdateImageRequest struct { | ||
DeploymentTargetId string `json:"deployment_target_id"` | ||
Repository string `json:"repository"` | ||
Tag string `json:"tag"` | ||
} | ||
|
||
// UpdateImageResponse is the response object for the /apps/{porter_app_name}/update-image endpoint | ||
type UpdateImageResponse struct { | ||
Repository string `json:"repository"` | ||
Tag string `json:"tag"` | ||
} | ||
|
||
func (c *UpdateImageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx, span := telemetry.NewSpan(r.Context(), "serve-update-image") | ||
defer span.End() | ||
|
||
project, _ := ctx.Value(types.ProjectScope).(*models.Project) | ||
|
||
if !project.GetFeatureFlag(models.ValidateApplyV2, c.Config().LaunchDarklyClient) { | ||
err := telemetry.Error(ctx, span, nil, "project does not have validate apply v2 enabled") | ||
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusForbidden)) | ||
return | ||
} | ||
|
||
appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName) | ||
if reqErr != nil { | ||
err := telemetry.Error(ctx, span, nil, "error parsing app name") | ||
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) | ||
return | ||
} | ||
telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName}) | ||
|
||
request := &UpdateImageRequest{} | ||
if ok := c.DecodeAndValidate(w, r, request); !ok { | ||
err := telemetry.Error(ctx, span, nil, "error decoding request") | ||
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) | ||
return | ||
} | ||
|
||
updateImageReq := connect.NewRequest(&porterv1.UpdateAppImageRequest{ | ||
ProjectId: int64(project.ID), | ||
DeploymentTargetId: request.DeploymentTargetId, | ||
RepositoryUrl: request.Repository, | ||
Tag: request.Tag, | ||
AppName: appName, | ||
}) | ||
ccpResp, err := c.Config().ClusterControlPlaneClient.UpdateAppImage(ctx, updateImageReq) | ||
if err != nil { | ||
err := telemetry.Error(ctx, span, err, "error calling ccp update porter app image") | ||
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError)) | ||
return | ||
} | ||
|
||
res := &UpdateImageResponse{ | ||
Repository: ccpResp.Msg.RepositoryUrl, | ||
Tag: ccpResp.Msg.Tag, | ||
} | ||
|
||
c.WriteResult(w, r, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
"github.com/porter-dev/porter/api/types" | ||
"github.com/porter-dev/porter/cli/cmd/config" | ||
"github.com/porter-dev/porter/cli/cmd/utils" | ||
v2 "github.com/porter-dev/porter/cli/cmd/v2" | ||
"github.com/spf13/cobra" | ||
batchv1 "k8s.io/api/batch/v1" | ||
v1 "k8s.io/api/core/v1" | ||
|
@@ -353,7 +354,7 @@ func appCleanup(ctx context.Context, _ *types.GetAuthenticatedUserResponse, clie | |
} | ||
|
||
for _, podName := range selectedPods { | ||
color.New(color.FgBlue).Printf("Deleting ephemeral pod: %s\n", podName) | ||
_, _ = color.New(color.FgBlue).Printf("Deleting ephemeral pod: %s\n", podName) | ||
|
||
err = config.Clientset.CoreV1().Pods(appNamespace).Delete( | ||
ctx, podName, metav1.DeleteOptions{}, | ||
|
@@ -602,7 +603,7 @@ func appExecuteRunEphemeral(ctx context.Context, config *AppPorterRunSharedConfi | |
// delete the ephemeral pod no matter what | ||
defer appDeletePod(ctx, config, podName, namespace) //nolint:errcheck,gosec // do not want to change logic of CLI. New linter error | ||
|
||
color.New(color.FgYellow).Printf("Waiting for pod %s to be ready...", podName) | ||
_, _ = color.New(color.FgYellow).Printf("Waiting for pod %s to be ready...", podName) | ||
if err = appWaitForPod(ctx, config, newPod); err != nil { | ||
color.New(color.FgRed).Println("failed") | ||
return appHandlePodAttachError(ctx, err, config, namespace, podName, container) | ||
|
@@ -1145,44 +1146,58 @@ func appCreateEphemeralPodFromExisting( | |
} | ||
|
||
func appUpdateTag(ctx context.Context, _ *types.GetAuthenticatedUserResponse, client api.Client, cliConfig config.CLIConfig, _ config.FeatureFlags, args []string) error { | ||
namespace := fmt.Sprintf("porter-stack-%s", args[0]) | ||
if appTag == "" { | ||
appTag = "latest" | ||
} | ||
release, err := client.GetRelease(ctx, cliConfig.Project, cliConfig.Cluster, namespace, args[0]) | ||
project, err := client.GetProject(ctx, cliConfig.Project) | ||
if err != nil { | ||
return fmt.Errorf("Unable to find application %s", args[0]) | ||
} | ||
repository, ok := release.Config["global"].(map[string]interface{})["image"].(map[string]interface{})["repository"].(string) | ||
if !ok || repository == "" { | ||
return fmt.Errorf("Application %s does not have an associated image repository. Unable to update tag", args[0]) | ||
} | ||
imageInfo := types.ImageInfo{ | ||
Repository: repository, | ||
Tag: appTag, | ||
} | ||
createUpdatePorterAppRequest := &types.CreatePorterAppRequest{ | ||
ClusterID: cliConfig.Cluster, | ||
ProjectID: cliConfig.Project, | ||
ImageInfo: imageInfo, | ||
OverrideRelease: false, | ||
return fmt.Errorf("could not retrieve project from Porter API. Please contact [email protected]") | ||
} | ||
|
||
color.New(color.FgGreen).Printf("Updating application %s to build using tag \"%s\"\n", args[0], appTag) | ||
if project.ValidateApplyV2 { | ||
tag, err := v2.UpdateImage(ctx, appTag, client, cliConfig.Project, cliConfig.Cluster, args[0]) | ||
if err != nil { | ||
return fmt.Errorf("error updating tag: %w", err) | ||
} | ||
_, _ = color.New(color.FgGreen).Printf("Successfully updated application %s to use tag \"%s\"\n", args[0], tag) | ||
return nil | ||
} else { | ||
namespace := fmt.Sprintf("porter-stack-%s", args[0]) | ||
if appTag == "" { | ||
appTag = "latest" | ||
} | ||
release, err := client.GetRelease(ctx, cliConfig.Project, cliConfig.Cluster, namespace, args[0]) | ||
if err != nil { | ||
return fmt.Errorf("Unable to find application %s", args[0]) | ||
} | ||
repository, ok := release.Config["global"].(map[string]interface{})["image"].(map[string]interface{})["repository"].(string) | ||
if !ok || repository == "" { | ||
return fmt.Errorf("Application %s does not have an associated image repository. Unable to update tag", args[0]) | ||
} | ||
imageInfo := types.ImageInfo{ | ||
Repository: repository, | ||
Tag: appTag, | ||
} | ||
createUpdatePorterAppRequest := &types.CreatePorterAppRequest{ | ||
ClusterID: cliConfig.Cluster, | ||
ProjectID: cliConfig.Project, | ||
ImageInfo: imageInfo, | ||
OverrideRelease: false, | ||
} | ||
|
||
_, _ = color.New(color.FgGreen).Printf("Updating application %s to build using tag \"%s\"\n", args[0], appTag) | ||
|
||
_, err = client.CreatePorterApp( | ||
ctx, | ||
cliConfig.Project, | ||
cliConfig.Cluster, | ||
args[0], | ||
createUpdatePorterAppRequest, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("Unable to update application %s: %w", args[0], err) | ||
} | ||
_, err = client.CreatePorterApp( | ||
ctx, | ||
cliConfig.Project, | ||
cliConfig.Cluster, | ||
args[0], | ||
createUpdatePorterAppRequest, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("Unable to update application %s: %w", args[0], err) | ||
} | ||
|
||
color.New(color.FgGreen).Printf("Successfully updated application %s to use tag \"%s\"\n", args[0], appTag) | ||
return nil | ||
_, _ = color.New(color.FgGreen).Printf("Successfully updated application %s to use tag \"%s\"\n", args[0], appTag) | ||
return nil | ||
} | ||
} | ||
|
||
func getPodsFromV1PorterYaml(ctx context.Context, execArgs []string, client api.Client, cliConfig config.CLIConfig, porterAppName string, namespace string) ([]appPodSimple, []string, error) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package v2 | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
api "github.com/porter-dev/porter/api/client" | ||
) | ||
|
||
// UpdateImage updates the image of an application | ||
func UpdateImage(ctx context.Context, tag string, client api.Client, projectId, clusterId uint, appName string) (string, error) { | ||
targetResp, err := client.DefaultDeploymentTarget(ctx, projectId, clusterId) | ||
if err != nil { | ||
return "", fmt.Errorf("error calling default deployment target endpoint: %w", err) | ||
} | ||
|
||
if targetResp.DeploymentTargetID == "" { | ||
return "", errors.New("deployment target id is empty") | ||
} | ||
|
||
if tag == "" { | ||
tag = "latest" | ||
} | ||
|
||
resp, err := client.UpdateImage(ctx, projectId, clusterId, appName, targetResp.DeploymentTargetID, tag) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to update image: %w", err) | ||
} | ||
|
||
return resp.Tag, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters