Skip to content

Commit

Permalink
Merge branch 'master' of github.com:porter-dev/porter into stacks-v2-…
Browse files Browse the repository at this point in the history
…job-history
  • Loading branch information
Feroze Mohideen committed Sep 20, 2023
2 parents a7580c5 + 73742e9 commit c13af46
Show file tree
Hide file tree
Showing 38 changed files with 517 additions and 213 deletions.
2 changes: 2 additions & 0 deletions api/client/porter_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ func (c *Client) CreateOrUpdateAppEnvironment(
deploymentTargetID string,
variables map[string]string,
secrets map[string]string,
Base64AppProto string,
) (*porter_app.UpdateAppEnvironmentResponse, error) {
resp := &porter_app.UpdateAppEnvironmentResponse{}

Expand All @@ -450,6 +451,7 @@ func (c *Client) CreateOrUpdateAppEnvironment(
Variables: variables,
Secrets: secrets,
HardUpdate: false,
Base64AppProto: Base64AppProto,
}

err := c.postRequest(
Expand Down
22 changes: 16 additions & 6 deletions api/server/handlers/cluster/cluster_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/porter-dev/porter/api/server/shared/config"
"github.com/porter-dev/porter/api/types"
"github.com/porter-dev/porter/internal/models"
"github.com/porter-dev/porter/internal/telemetry"
)

type ClusterStatusHandler struct {
Expand Down Expand Up @@ -40,22 +41,25 @@ type ClusterStatusResponse struct {
}

func (c *ClusterStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
ctx, span := telemetry.NewSpan(r.Context(), "serve-cluster-status")
defer span.End()

cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
req := connect.NewRequest(&porterv1.ClusterStatusRequest{
ProjectId: int64(cluster.ProjectID),
ClusterId: int64(cluster.ID),
})
status, err := c.Config().ClusterControlPlaneClient.ClusterStatus(ctx, req)
if err != nil {
e := fmt.Errorf("unable to retrieve status for cluster: %w", err)
c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
err := fmt.Errorf("unable to retrieve status for cluster: %w", err)
err = telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
if status.Msg == nil {
e := fmt.Errorf("unable to parse status for cluster: %w", err)
c.HandleAPIError(w, r, apierrors.NewErrInternal(e))
err := fmt.Errorf("unable to parse status for cluster: %w", err)
err = telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
statusResp := status.Msg
Expand All @@ -68,6 +72,12 @@ func (c *ClusterStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
IsControlPlaneReady: statusResp.ControlPlaneStatus,
}

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "cluster-phase", Value: statusResp.Phase},
telemetry.AttributeKV{Key: "cluster-infra-status", Value: statusResp.InfrastructureStatus},
telemetry.AttributeKV{Key: "cluster-control-plane-status", Value: statusResp.ControlPlaneStatus},
)

c.WriteResult(w, r, resp)
w.WriteHeader(http.StatusOK)
}
7 changes: 1 addition & 6 deletions api/server/handlers/environment_groups/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,10 @@ func (c *UpdateEnvironmentGroupHandler) ServeHTTP(w http.ResponseWriter, r *http
return
}

secrets := make(map[string][]byte)
for k, v := range request.SecretVariables {
secrets[k] = []byte(v)
}

envGroup := environment_groups.EnvironmentGroup{
Name: request.Name,
Variables: request.Variables,
SecretVariables: secrets,
SecretVariables: request.SecretVariables,
CreatedAtUTC: time.Now().UTC(),
}

Expand Down
2 changes: 1 addition & 1 deletion api/server/handlers/environment_groups/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (c *ListEnvironmentGroupsHandler) ServeHTTP(w http.ResponseWriter, r *http.
return
}

allEnvGroupVersions, err := environmentgroups.ListEnvironmentGroups(ctx, agent, environmentgroups.WithNamespace(environmentgroups.Namespace_EnvironmentGroups))
allEnvGroupVersions, err := environmentgroups.ListEnvironmentGroups(ctx, agent, environmentgroups.WithNamespace(environmentgroups.Namespace_EnvironmentGroups), environmentgroups.WithoutDefaultAppEnvironmentGroups())
if err != nil {
err = telemetry.Error(ctx, span, err, "unable to list all environment groups")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
Expand Down
2 changes: 2 additions & 0 deletions api/server/handlers/gitinstallation/get_buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ func initBuilderInfo() map[string]*buildpacks.BuilderInfo {
builders[buildpacks.PaketoBuilder] = &buildpacks.BuilderInfo{
Name: "Paketo",
Builders: []string{
"paketobuildpacks/builder-jammy-full:latest",
"paketobuildpacks/builder:full",
},
}
builders[buildpacks.HerokuBuilder] = &buildpacks.BuilderInfo{
Name: "Heroku",
Builders: []string{
"heroku/builder:22",
"heroku/buildpacks:20",
"heroku/buildpacks:18",
},
Expand Down
41 changes: 31 additions & 10 deletions api/server/handlers/porter_app/create_secret_and_open_pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/porter-dev/porter/internal/auth/token"
"github.com/porter-dev/porter/internal/integrations/ci/actions"
"github.com/porter-dev/porter/internal/models"
"github.com/porter-dev/porter/internal/telemetry"
)

type OpenStackPRHandler struct {
Expand All @@ -34,9 +35,12 @@ func NewOpenStackPRHandler(
}

func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
user, _ := r.Context().Value(types.UserScope).(*models.User)
project, _ := r.Context().Value(types.ProjectScope).(*models.Project)
cluster, _ := r.Context().Value(types.ClusterScope).(*models.Cluster)
ctx, span := telemetry.NewSpan(r.Context(), "serve-open-stack-pr")
defer span.End()

user, _ := ctx.Value(types.UserScope).(*models.User)
project, _ := ctx.Value(types.ProjectScope).(*models.Project)
cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
if reqErr != nil {
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(reqErr, http.StatusBadRequest))
Expand All @@ -45,11 +49,14 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

request := &types.CreateSecretAndOpenGHPRRequest{}
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
}

client, err := getGithubClient(c.Config(), request.GithubAppInstallationID)
if err != nil {
err := telemetry.Error(ctx, span, err, "error creating github client")
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
Expand All @@ -59,12 +66,16 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// generate porter jwt token
jwt, err := token.GetTokenForAPI(user.ID, project.ID)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error getting token for API: %w", err)))
err = fmt.Errorf("error getting token for API: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
encoded, err := jwt.EncodeToken(c.Config().TokenConf)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error encoding API token: %w", err)))
err = fmt.Errorf("error encoding API token: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}

Expand All @@ -78,7 +89,9 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
request.GithubRepoName,
)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error generating secret: %w", err)))
err = fmt.Errorf("error generating secret: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
}
Expand Down Expand Up @@ -113,12 +126,16 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if unwrappedErr != nil {
if errors.Is(unwrappedErr, actions.ErrProtectedBranch) {
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusConflict))
return
} else if errors.Is(unwrappedErr, actions.ErrCreatePRForProtectedBranch) {
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusPreconditionFailed))
return
}
} else {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error setting up application in the github "+
"repo: %w", err)))
err = fmt.Errorf("error setting up application in the github "+
"repo: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
}
Expand All @@ -133,15 +150,19 @@ func (c *OpenStackPRHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// update DB with the PR url
porterApp, err := c.Repo().PorterApp().ReadPorterAppByName(cluster.ID, appName)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("unable to get porter app db: %w", err)))
err = fmt.Errorf("unable to get porter app db: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}

porterApp.PullRequestURL = pr.GetHTMLURL()

_, err = c.Repo().PorterApp().UpdatePorterApp(porterApp)
if err != nil {
c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("unable to write pr url to porter app db: %w", err)))
err = fmt.Errorf("unable to write pr url to porter app db: %w", err)
err := telemetry.Error(ctx, span, err, err.Error())
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion api/server/handlers/porter_app/get_app_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *GetAppEnvHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
DeploymentTargetRepository: c.Repo().DeploymentTarget(),
}

envGroups, err := porter_app.AppEnvironmentFromProto(ctx, envFromProtoInp, porter_app.WithEnvGroupFilter(request.EnvGroups), porter_app.WithSecrets())
envGroups, err := porter_app.AppEnvironmentFromProto(ctx, envFromProtoInp, porter_app.WithEnvGroupFilter(request.EnvGroups), porter_app.WithSecrets(), porter_app.WithoutDefaultAppEnvGroups())
if err != nil {
err := telemetry.Error(ctx, span, err, "error getting app environment from revision")
c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
Expand Down
9 changes: 5 additions & 4 deletions api/server/handlers/porter_app/get_build_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ func (c *GetBuildEnvHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

envFromProtoInp := porter_app.AppEnvironmentFromProtoInput{
ProjectID: project.ID,
ClusterID: int(cluster.ID),
App: appProto,
K8SAgent: agent,
ProjectID: project.ID,
ClusterID: int(cluster.ID),
App: appProto,
K8SAgent: agent,
DeploymentTargetRepository: c.Repo().DeploymentTarget(),
}
envGroups, err := porter_app.AppEnvironmentFromProto(ctx, envFromProtoInp)
if err != nil {
Expand Down
Loading

0 comments on commit c13af46

Please sign in to comment.