-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: EXPERIMENTAL add kapp support on furyctl
- Loading branch information
Showing
7 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters