Skip to content

Commit

Permalink
feat: EXPERIMENTAL add kapp support on furyctl
Browse files Browse the repository at this point in the history
  • Loading branch information
nutellinoit committed May 8, 2024
1 parent 2b473da commit 1bccd19
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 3 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.22

toolchain go1.22.0

//replace github.com/sighupio/fury-distribution => ../fury-distribution

require (
github.com/Al-Pragliola/go-version v1.6.2
github.com/Masterminds/sprig/v3 v3.2.3
Expand All @@ -21,7 +23,7 @@ require (
github.com/r3labs/diff/v3 v3.0.1
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
github.com/shirou/gopsutil/v3 v3.24.1
github.com/sighupio/fury-distribution v1.28.0
github.com/sighupio/fury-distribution v1.29.0-kapp-rc.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,8 @@ github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnj
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/sighupio/fury-distribution v1.28.0 h1:L86HzVE1z10KcQF08SzOEcD4OLOZkT3r36Qdrt6B1ls=
github.com/sighupio/fury-distribution v1.28.0/go.mod h1:iYBnl0N/6zGPz3j3L2X6nzFC3nzNQtIkAzmaPfnO0zA=
github.com/sighupio/fury-distribution v1.29.0-kapp-rc.0 h1:iS7nuTvBkLkKZH/L6ossdisjN7eRoXsO8lhd7CSsSn4=
github.com/sighupio/fury-distribution v1.29.0-kapp-rc.0/go.mod h1:iYBnl0N/6zGPz3j3L2X6nzFC3nzNQtIkAzmaPfnO0zA=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
Expand Down
4 changes: 4 additions & 0 deletions internal/cluster/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ type OperationPhase struct {
YqPath string
HelmPath string
HelmfilePath string
KappPath string
TerraformPlanPath string
TerraformLogsPath string
TerraformOutputsPath string
Expand All @@ -168,6 +169,7 @@ func NewOperationPhase(folder string, kfdTools config.KFDTools, binPath string)
yqPath := path.Join(binPath, "yq", kfdTools.Common.Yq.Version, "yq")
helmPath := path.Join(binPath, "helm", kfdTools.Common.Helm.Version, "helm")
helmfilePath := path.Join(binPath, "helmfile", kfdTools.Common.Helmfile.Version, "helmfile")
kappPath := path.Join(binPath, "kapp", kfdTools.Common.Kapp.Version, "kapp")

planPath := path.Join(basePath, "terraform", "plan")
logsPath := path.Join(basePath, "terraform", "logs")
Expand All @@ -187,6 +189,7 @@ func NewOperationPhase(folder string, kfdTools config.KFDTools, binPath string)
YqPath: yqPath,
HelmPath: helmPath,
HelmfilePath: helmfilePath,
KappPath: kappPath,
}
}

Expand Down Expand Up @@ -287,6 +290,7 @@ func (op *OperationPhase) CopyPathsToConfig(cfg *template.Config) {
"terraform": op.TerraformPath,
"vendorPath": path.Join(op.Path, "..", "vendor"),
"yq": op.YqPath,
"kapp": op.KappPath,
}
}

Expand Down
75 changes: 75 additions & 0 deletions internal/dependencies/tools/kapp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright (c) 2017-present SIGHUP s.r.l All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tools

import (
"fmt"
"path/filepath"
"regexp"
"runtime"
"strings"

"github.com/sighupio/furyctl/internal/semver"
"github.com/sighupio/furyctl/internal/tool/kapp"
iox "github.com/sighupio/furyctl/internal/x/io"
)

func NewKapp(runner *kapp.Runner, version string) *Kapp {
return &Kapp{
arch: runtime.GOARCH,
os: runtime.GOOS,
version: version,
checker: &checker{
regex: regexp.MustCompile(fmt.Sprintf("kapp version %s", semver.Regex)),
runner: runner,
splitFn: func(version string) []string {
return strings.Split(version, " ")
},
trimFn: func(tokens []string) string {
return tokens[len(tokens)-1]
},
},
}
}

type Kapp struct {
arch string
checker *checker
os string
version string
}

func (*Kapp) SupportsDownload() bool {
return true
}

func (k *Kapp) SrcPath() string {
return fmt.Sprintf(
"https://github.com/carvel-dev/kapp/releases/download/%s/kapp-%s-%s",
semver.EnsurePrefix(k.version),
k.os,
k.arch,
)
}

func (k *Kapp) Rename(basePath string) error {
oldPath := filepath.Join(basePath, fmt.Sprintf("kapp-%s-%s", k.os, k.arch))
newPath := filepath.Join(basePath, "kapp")

if err := iox.CopyFile(oldPath, newPath); err != nil {
return fmt.Errorf("error while renaming kapp: %w", err)
}

return nil
}


func (k *Kapp) CheckBinVersion() error {
if err := k.checker.version(k.version); err != nil {
return fmt.Errorf("kapp: %w", err)
}

return nil
}
9 changes: 9 additions & 0 deletions internal/dependencies/tools/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/sighupio/furyctl/internal/tool/shell"
"github.com/sighupio/furyctl/internal/tool/terraform"
"github.com/sighupio/furyctl/internal/tool/yq"
"github.com/sighupio/furyctl/internal/tool/kapp"
execx "github.com/sighupio/furyctl/internal/x/exec"
)

Expand Down Expand Up @@ -164,6 +165,14 @@ func (f *Factory) Create(name tool.Name, version string) Tool {

return NewHelmfile(hfr, version)

case tool.Kapp:
ka, ok := t.(*kapp.Runner)
if !ok {
panic(fmt.Sprintf("expected kapp.Runner, got %T", t))
}

return NewKapp(ka, version)

default:
return nil
}
Expand Down
1 change: 1 addition & 0 deletions internal/distribution/iac.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (m *IACBuilder) Build() error {
"terraform": "",
"vendorPath": "",
"yq": "",
"kapp": "",
}

tmplCfg.Data["checks"] = map[any]any{
Expand Down
107 changes: 107 additions & 0 deletions internal/tool/kapp/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (c) 2017-present SIGHUP s.r.l All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package kapp

import (
"fmt"

"github.com/google/uuid"

execx "github.com/sighupio/furyctl/internal/x/exec"
)

type Paths struct {
Kapp string
WorkDir string
}

type Runner struct {
executor execx.Executor
paths Paths
serverSide bool
cmds map[string]*execx.Cmd
}

func NewRunner(executor execx.Executor, paths Paths, serverSide bool) *Runner {
return &Runner{
executor: executor,
paths: paths,
serverSide: serverSide,
cmds: make(map[string]*execx.Cmd),
}
}

func (r *Runner) CmdPath() string {
return r.paths.Kapp
}

func (r *Runner) newCmd(args []string, sensitive bool) (*execx.Cmd, string) {
cmd := execx.NewCmd(r.paths.Kapp, execx.CmdOptions{
Args: args,
Executor: r.executor,
WorkDir: r.paths.WorkDir,
Sensitive: sensitive,
})

id := uuid.NewString()
r.cmds[id] = cmd

return cmd, id
}

func (r *Runner) deleteCmd(id string) {
delete(r.cmds, id)
}

func (r *Runner) Deploy(manifestPath string, params ...string) error {
args := []string{"deploy"}

if r.serverSide {
args = append(args, "--server-side")
}

if len(params) > 0 {
args = append(args, params...)
}

args = append(args, "-a", "kfd")

args = append(args, "-n", "kube-system")

args = append(args, "-f", manifestPath)

cmd, id := r.newCmd(args, false)
defer r.deleteCmd(id)

if _, err := execx.CombinedOutput(cmd); err != nil {
return fmt.Errorf("error applying manifests: %w", err)
}

return nil
}

func (r *Runner) Version() (string, error) {
args := []string{"version"}

cmd, id := r.newCmd(args, false)
defer r.deleteCmd(id)

out, err := execx.CombinedOutput(cmd)
if err != nil {
return "", fmt.Errorf("error getting kapp version: %w", err)
}

return out, nil
}

func (r *Runner) Stop() error {
for _, cmd := range r.cmds {
if err := cmd.Stop(); err != nil {
return fmt.Errorf("error stopping kapp runner: %w", err)
}
}

return nil
}
12 changes: 12 additions & 0 deletions internal/tool/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/sighupio/furyctl/internal/tool/shell"
"github.com/sighupio/furyctl/internal/tool/terraform"
"github.com/sighupio/furyctl/internal/tool/yq"
"github.com/sighupio/furyctl/internal/tool/kapp"
execx "github.com/sighupio/furyctl/internal/x/exec"
)

Expand All @@ -37,6 +38,7 @@ const (
Shell Name = "shell"
Helm Name = "helm"
Helmfile Name = "helmfile"
Kapp Name = "kapp"
)

type Runner interface {
Expand Down Expand Up @@ -139,6 +141,16 @@ func (rf *RunnerFactory) Create(name Name, version, workDir string) Runner {
Helmfile: filepath.Join(rf.paths.Bin, string(name), version, string(name)),
WorkDir: workDir,
})

case Kapp:
return kapp.NewRunner(
rf.executor,
kapp.Paths{
Kapp: filepath.Join(rf.paths.Bin, string(name), version, string(name)),
WorkDir: workDir,
},
false,
)

default:
return nil
Expand Down

0 comments on commit 1bccd19

Please sign in to comment.