Skip to content

Commit

Permalink
drop: standard platform API request (#2717)
Browse files Browse the repository at this point in the history
* drop: standard platform API request
feat: get buildpack specific EVs out of the staging script configs
tweak: quickly reject when already staging, no need to do all the script stuff in that case

* import chart work
  • Loading branch information
andreas-kupries committed Nov 24, 2023
1 parent 1e1f1fb commit a3c16e3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
8 changes: 5 additions & 3 deletions acceptance/helpers/machine/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (m *Machine) MakeGolangApp(appName string, instances int, deployFromCurrent
return m.MakeAppWithDir(appName, instances, deployFromCurrentDir, appDir)
}

func (m *Machine) MakeAppWithDir(appName string, instances int, deployFromCurrentDir bool, appDir string) string {
func (m *Machine) MakeAppWithDir(appName string, instances int, deployFromCurrentDir bool, appDir string, more ...string) string {
By("creating app " + appName)

var pushOutput string
Expand All @@ -87,8 +87,10 @@ func (m *Machine) MakeAppWithDir(appName string, instances int, deployFromCurren
// This means that the command runs with it as the CWD.
pushOutput, err = m.EpinioPush(appDir,
appName,
"--name", appName,
"--instances", strconv.Itoa(instances))
append([]string{
"--name", appName,
"--instances", strconv.Itoa(instances),
}, more...)...)
} else {
// Note: appDir is handed as second argument to the epinio cli.
// This means that the command gets the sources from that directory instead of CWD.
Expand Down
58 changes: 39 additions & 19 deletions internal/api/v1/application/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/go-logr/logr"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/utils/ptr"

Expand Down Expand Up @@ -162,6 +163,15 @@ func Stage(c *gin.Context) apierror.APIErrors {
return apierror.InternalError(err, "failed to get the application resource")
}

// quickly reject conflict with (still) active staging
staging, err := application.IsCurrentlyStaging(ctx, cluster, req.App.Namespace, req.App.Name)
if err != nil {
return apierror.InternalError(err)
}
if staging {
return apierror.NewBadRequestError("staging job for image ID still running")
}

// get builder image from either request, application, or default as final fallback

builderImage, builderErr := getBuilderImage(req, app)
Expand All @@ -186,16 +196,9 @@ func Stage(c *gin.Context) apierror.APIErrors {
log.Info("staging app", "unpack", config.UnpackImage)
log.Info("staging app", "userid", config.UserID)
log.Info("staging app", "groupid", config.GroupID)
log.Info("staging app", "build env", config.Env)
log.Info("staging app", "namespace", namespace, "app", req)

staging, err := application.IsCurrentlyStaging(ctx, cluster, req.App.Namespace, req.App.Name)
if err != nil {
return apierror.InternalError(err)
}
if staging {
return apierror.NewBadRequestError("staging job for image ID still running")
}

s3ConnectionDetails, err := s3manager.GetConnectionDetails(ctx, cluster,
helmchart.Namespace(), helmchart.S3ConnectionDetailsSecretName)
if err != nil {
Expand Down Expand Up @@ -255,6 +258,15 @@ func Stage(c *gin.Context) apierror.APIErrors {
memoryRequest := viper.GetString("staging-resource-memory")
diskRequest := viper.GetString("staging-resource-disk")

// merge buildpack and general EV, i.e. `config.Env`, and `environment`.
// ATTENTION: in case of conflict the general, i.e. user-specified!, EV has priority.
for name, value := range config.Env {
if _, found := environment[name]; found {
continue
}
environment[name] = value
}

params := stageParam{
AppRef: req.App,
BuilderImage: builderImage,
Expand Down Expand Up @@ -520,10 +532,8 @@ func newJobRun(app stageParam) (*batchv1.Job, *corev1.Secret) {
volumes, volumeMounts = mountS3Certs(volumes, volumeMounts)
volumes, volumeMounts = mountRegistryCerts(app, volumes, volumeMounts)

// Create job environment as a copy of the app environment, plus standard variable.
// Create job environment as a copy of the app environment
env := make(map[string][]byte)

env["CNB_PLATFORM_API"] = []byte("0.4")
for _, ev := range app.Environment {
env[ev.Name] = []byte(ev.Value)
}
Expand Down Expand Up @@ -854,14 +864,16 @@ func appendEnvVar(envs []corev1.EnvVar, name, value string) []corev1.EnvVar {
return append(envs, corev1.EnvVar{Name: name, Value: value})
}

// StagingScriptConfig holds all the information for using a (set of) buildpack(s)
type StagingScriptConfig struct {
Name string // config name. Needed to mount the resource in the pod
Builder string // glob pattern for builders supported by this resource
UserID int64 // user id to run the build phase with (`cnb` user)
GroupID int64 // group id to run the build hase with
Base string // optional, name of resource to pull the other parts from
DownloadImage string // image to run the download phase with
UnpackImage string // image to run the unpack phase with
Name string // config name. Needed to mount the resource in the pod
Builder string // glob pattern for builders supported by this resource
UserID int64 // user id to run the build phase with (`cnb` user)
GroupID int64 // group id to run the build hase with
Base string // optional, name of resource to pull the other parts from
DownloadImage string // image to run the download phase with
UnpackImage string // image to run the unpack phase with
Env models.EnvVariableMap // environment settings
}

func DetermineStagingScripts(ctx context.Context,
Expand Down Expand Up @@ -969,6 +981,7 @@ func StagingScriptConfigResolve(ctx context.Context, logger logr.Logger, cluster

// Fill config from the base.
// BEWARE: Keep user/group data of the incoming config.
// BEWARE: Keep environment data of the incoming config.

config.Name = base.Name
config.DownloadImage = base.Data["downloadImage"]
Expand All @@ -984,7 +997,7 @@ func NewStagingScriptConfig(config v1.ConfigMap) (*StagingScriptConfig, error) {
Base: config.Data["base"],
DownloadImage: config.Data["downloadImage"],
UnpackImage: config.Data["unpackImage"],
// user, group id, see below.
// env, user, group id, see below.
}

userID, err := strconv.ParseInt(config.Data["userID"], 10, 64)
Expand All @@ -996,6 +1009,13 @@ func NewStagingScriptConfig(config v1.ConfigMap) (*StagingScriptConfig, error) {
return nil, err
}

envString := config.Data["env"]

err = yaml.Unmarshal([]byte(envString), &stagingScript.Env)
if err != nil {
return nil, err
}

stagingScript.GroupID = groupID
stagingScript.UserID = userID

Expand Down

0 comments on commit a3c16e3

Please sign in to comment.