Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

stage0: add --expose flag on app level #3691

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions common/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type App struct {
WorkingDir string // working directory override for image
ReadOnlyRootFS bool // read-only rootfs override.
Mounts []schema.Mount // mounts for this app (superseding any mounts in rktApps.mounts of same MountPoint)
Ports []types.Port // ports for this app to be exposed
MemoryLimit *types.ResourceMemory // memory isolator override
CPULimit *types.ResourceCPU // cpu isolator override
CPUShares *types.LinuxCPUShares // cpu-shares isolator override
Expand Down
33 changes: 33 additions & 0 deletions rkt/cli_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package main

import (
"encoding/json"
"fmt"
"net/url"
"strconv"
Expand Down Expand Up @@ -895,3 +896,35 @@ func (au *appStderr) String() string {
func (au *appStderr) Type() string {
return "appStderr"
}

// appExpose is for --expose flags in the form of: --expose=query,protocol=tcp,port=8080,count=1,socketActivated=true
Copy link
Member

Choose a reason for hiding this comment

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

I guess query here would be clearer as portname.

type appExpose apps.Apps

func (ae *appExpose) Set(s string) error {
port, err := types.PortFromString(s)
if err != nil {
return err
}
if port.Protocol == "" {
port.Protocol = "tcp"
}
app := (*apps.Apps)(ae).Last()
if app == nil {
return fmt.Errorf("--expose must follow an application")
}
app.Ports = append(app.Ports, *port)
return nil
}

func (ae *appExpose) String() string {
app := (*apps.Apps)(ae).Last()
if app == nil {
return ""
}
bs, _ := json.Marshal(app.Ports)
return string(bs)
}

func (ae *appExpose) Type() string {
return "appExpose"
}
2 changes: 1 addition & 1 deletion rkt/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func addAppFlags(cmd *cobra.Command) {
cmd.Flags().Var((*appWorkingDir)(&rktApps), "working-dir", "override the working directory of the preceding image")
cmd.Flags().Var((*appReadOnlyRootFS)(&rktApps), "readonly-rootfs", "if set, the app's rootfs will be mounted read-only")
cmd.Flags().Var((*appMount)(&rktApps), "mount", "mount point binding a volume to a path within an app")
cmd.Flags().Var((*appExpose)(&rktApps), "expose", "expose ports of the application (example: '--expose=http,protocol=tcp,port=8080')")
Copy link
Member

Choose a reason for hiding this comment

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

This new flag should be documented in run.md, and possibly also also to prepare / run-prepared (I didn't check, I think this also adds it to the command so maybe only the doc part is missing).

cmd.Flags().Var((*appUser)(&rktApps), "user", "user override for the preceding image (example: '--user=user')")
cmd.Flags().Var((*appGroup)(&rktApps), "group", "group override for the preceding image (example: '--group=group')")
cmd.Flags().Var((*appSupplementaryGIDs)(&rktApps), "supplementary-gids", "supplementary group IDs override for the preceding image (examples: '--supplementary-gids=1024,2048'")
Expand Down Expand Up @@ -335,7 +336,6 @@ func runRun(cmd *cobra.Command, args []string) (exit int) {
pcfg.EnvFromFile = flagEnvFromFile.Strings()
pcfg.Apps = &rktApps
}

if globalFlags.Debug {
stage0.InitDebug()
}
Expand Down
4 changes: 3 additions & 1 deletion stage0/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ func (ce CrossingEntrypoint) Run() error {
// generateRuntimeApp merges runtime information from the image manifest and from
// runtime configuration overrides, returning a full configuration for a runtime app
func generateRuntimeApp(appRunConfig *apps.App, am *schema.ImageManifest, podMounts []schema.Mount) (schema.RuntimeApp, error) {

ra := schema.RuntimeApp{
App: am.App,
Image: schema.RuntimeImage{
Expand All @@ -122,6 +121,9 @@ func generateRuntimeApp(appRunConfig *apps.App, am *schema.ImageManifest, podMou
Mounts: MergeMounts(podMounts, appRunConfig.Mounts),
ReadOnlyRootFS: appRunConfig.ReadOnlyRootFS,
}
if ra.App != nil && appRunConfig != nil {
ra.App.Ports = MergePorts(ra.App.Ports, appRunConfig.Ports)
}

appName, err := types.NewACName(appRunConfig.Name)
if err != nil {
Expand Down
18 changes: 18 additions & 0 deletions stage0/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ func MergeMounts(mounts []schema.Mount, appMounts []schema.Mount) []schema.Mount
return deduplicateMPs(ml)
}

// MergePorts merges two port lists
func MergePorts(a, b []types.Port) []types.Port {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure how relevant this maybe in this context, but this is not a stable merge (i.e. it doesn't guarantee too keep the relative order). It may poses some issues when trying to answer questions like what is the precedence order between manifest ports and exposed ports.

m := make(map[types.ACName]types.Port)
for _, port := range a {
m[port.Name] = port
}
for _, port := range b {
m[port.Name] = port
}
res := make([]types.Port, len(m))
i := 0
for _, port := range m {
res[i] = port
i++
}
return res
}

// generatePodManifest creates the pod manifest from the command line input.
// It returns the pod manifest as []byte on success.
// This is invoked if no pod manifest is specified at the command line.
Expand Down