Skip to content

Commit

Permalink
Merge pull request #17 from raskyld/fix/issue-15
Browse files Browse the repository at this point in the history
fix(genyaml): #15 ko.local command
  • Loading branch information
raskyld committed Oct 22, 2023
2 parents e8ea206 + a6c3603 commit d427a43
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
9 changes: 8 additions & 1 deletion cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ The code generated for your main package will be written in zz_generated.tektask
}

func NewGenerateManifest(ctx *Context) *cobra.Command {
var stepCommand string

genYaml := &cobra.Command{
Use: "manifest output-dir",
Short: "Generate your YAML manifests and write them in the given output-dir",
Expand All @@ -126,7 +128,10 @@ tektasker gen -i ./pkg/... manifest ./manifests/
`,
Args: ResolveManifestArgs(ctx),
RunE: func(cmd *cobra.Command, args []string) error {
var gen genall.Generator = genyaml.TaskYamlGenerator{ctx.Logger}
var gen genall.Generator = genyaml.TaskYamlGenerator{
Logger: ctx.Logger,
StepCommand: stepCommand,
}
gens := genall.Generators{&gen}

runtime, err := gens.ForRoots(ctx.Generate.Input)
Expand All @@ -150,6 +155,8 @@ tektasker gen -i ./pkg/... manifest ./manifests/
},
}

genYaml.Flags().StringVar(&stepCommand, "command", "ko-app/{{.KoAppName}}", "What is the entrypoint of the container image of the task")

return genYaml
}

Expand Down
66 changes: 64 additions & 2 deletions internal/genyaml/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package genyaml

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -25,17 +26,37 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"log/slog"
"path"
"path/filepath"
"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/loader"
"sigs.k8s.io/controller-tools/pkg/markers"
"strconv"
"strings"
"text/template"
)

const KubernetesVersionLabel = "app.kubernetes.io/version"

type TaskYamlGenerator struct {
Logger *slog.Logger

// StepCommand is used to fix the command field of the step (i.e. the entrypoint of your container)
StepCommand string
}

// stepCommandArgs is passed when templating the value of StepCommand
type stepCommandArgs struct {
// PkgName is the name of the package being parsed
PkgName string

// ImportPath of the package
ImportPath string

// TODO(raskyld): Fragile code! We should use a custom type capable of replicating the behavior of various ko version
// KoAppName is replicating the way ko builder (0.15) determinate the entrypoint of the built container
// See issue #15 for details
KoAppName string
}

func (TaskYamlGenerator) RegisterMarkers(into *markers.Registry) error {
Expand Down Expand Up @@ -176,11 +197,23 @@ func (g TaskYamlGenerator) buildSteps(task unstructured.Unstructured, pkg *loade
"image": "ko://" + pkg.PkgPath,
}

envs := make([]interface{}, 0)

// NB(raskyld): this code is dirty asf, we should be able to clean it when
// migrating from ad-hoc generation to intermediate representation (IR)

command, err := g.parseFlagCommand(pkg)
if err != nil {
return err
}

cmds := strings.Split(command, " ")
icmds := make([]interface{}, len(cmds))
for i, cmd := range cmds {
icmds[i] = cmd
}

mainStep["command"] = icmds

envs := make([]interface{}, 0)
for _, param := range params {
if param, ok := param.(map[string]interface{}); ok {
paramName := param["name"].(string)
Expand Down Expand Up @@ -220,6 +253,35 @@ func (g TaskYamlGenerator) buildSteps(task unstructured.Unstructured, pkg *loade
return unstructured.SetNestedSlice(task.Object, []interface{}{mainStep}, "spec", "steps")
}

// parseFlagCommand parses the flag `command` passed by the user
func (g TaskYamlGenerator) parseFlagCommand(pkg *loader.Package) (string, error) {
tpl := &template.Template{}
tpl, err := tpl.Parse(g.StepCommand)
if err != nil {
return "", err
}

// TODO(raskyld): migrate this to an interface `KoEntrypointResolver` following the Strategy Pattern.
// Each resolver should be registered with a ko version range
koAppName := path.Base(pkg.PkgPath)
if koAppName == "." || koAppName == string(filepath.Separator) {
koAppName = "ko-app"
}

args := stepCommandArgs{
PkgName: pkg.Name,
ImportPath: pkg.PkgPath,
KoAppName: koAppName,
}

var command bytes.Buffer
err = tpl.Execute(&command, args)
if err != nil {
return "", err
}
return command.String(), nil
}

func (g TaskYamlGenerator) buildWorkspaces(task unstructured.Unstructured, pkgMarkers markers.MarkerValues) error {
if workspaces, ok := pkgMarkers[ttmarkers.MarkerWorkspace]; ok {
workspacesYaml := make([]interface{}, 0, len(workspaces))
Expand Down

0 comments on commit d427a43

Please sign in to comment.