Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #937 from caos/spring-cleaning
Browse files Browse the repository at this point in the history
Spring Cleaning
  • Loading branch information
Elio Bischof authored Jan 14, 2022
2 parents 353c136 + c363ef0 commit 011d127
Show file tree
Hide file tree
Showing 60 changed files with 763 additions and 467 deletions.
2 changes: 1 addition & 1 deletion cmd/gen-crds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
flag.Parse()

if kubeconfig != "" {
k8sClient, err := kubernetes.NewK8sClientWithPath(mntr.Monitor{}, kubeconfig)
k8sClient, err := kubernetes.NewK8sClientPathBeforeInCluster(mntr.Monitor{}, kubeconfig)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/networking-debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {

if gitopsmode {

k8sClient, err := cli.Client(monitor, orbConfig, gitClient, kubeconfig, gitopsmode, true)
k8sClient, err := cli.Init(monitor, orbConfig, gitClient, kubeconfig, gitopsmode, true, false)
if err != nil {
monitor.Error(err)
os.Exit(1)
Expand Down
43 changes: 13 additions & 30 deletions cmd/orbctl/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ package main
import (
"errors"

"github.com/caos/orbos/mntr"

"github.com/caos/orbos/pkg/git"

orbcfg "github.com/caos/orbos/pkg/orb"
"github.com/spf13/cobra"

boomapi "github.com/caos/orbos/internal/operator/boom/api"
"github.com/caos/orbos/internal/operator/orbiter"
orbadapter "github.com/caos/orbos/internal/operator/orbiter/kinds/orb"
"github.com/caos/orbos/mntr"
"github.com/caos/orbos/pkg/git"
"github.com/caos/orbos/pkg/kubernetes/cli"
"github.com/caos/orbos/pkg/labels"
"github.com/spf13/cobra"
)

func APICommand(getRv GetRootValues) *cobra.Command {
Expand All @@ -27,41 +25,26 @@ func APICommand(getRv GetRootValues) *cobra.Command {

cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {

rv, err := getRv("api", "", nil)
if err != nil {
return err
}
rv := getRv("api", "", nil)
defer rv.ErrFunc(err)

if !rv.Gitops {
return mntr.ToUserError(errors.New("api command is only supported with the --gitops flag"))
}

monitor := rv.Monitor
orbConfig := rv.OrbConfig
gitClient := rv.GitClient

if err := orbcfg.IsComplete(orbConfig); err != nil {
return err
}

if err := gitClient.Configure(orbConfig.URL, []byte(orbConfig.Repokey)); err != nil {
return err
}

if err := gitClient.Clone(); err != nil {
if _, err := cli.Init(rv.Monitor, rv.OrbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, true, true); err != nil {
return err
}

var desireds []git.GitDesiredState
if gitClient.Exists(git.OrbiterFile) {
_, _, _, migrate, desired, _, _, err := orbiter.Adapt(gitClient, monitor, make(chan struct{}), orbadapter.AdaptFunc(
if rv.GitClient.Exists(git.OrbiterFile) {
_, _, _, migrate, desired, _, _, err := orbiter.Adapt(rv.GitClient, monitor, make(chan struct{}), orbadapter.AdaptFunc(
labels.NoopOperator("ORBOS"),
orbConfig,
rv.OrbConfig,
gitCommit,
true,
false,
gitClient,
rv.GitClient,
))
if err != nil {
return err
Expand All @@ -74,9 +57,9 @@ func APICommand(getRv GetRootValues) *cobra.Command {
})
}
}
if gitClient.Exists(git.BoomFile) {
if rv.GitClient.Exists(git.BoomFile) {

desired, err := gitClient.ReadTree(git.BoomFile)
desired, err := rv.GitClient.ReadTree(git.BoomFile)
if err != nil {
return err
}
Expand All @@ -94,7 +77,7 @@ func APICommand(getRv GetRootValues) *cobra.Command {
}
}
if len(desireds) > 0 {
return gitClient.PushGitDesiredStates(monitor, "migrate apis", desireds)
return rv.GitClient.PushGitDesiredStates(monitor, "migrate apis", desireds)
}
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/orbctl/cmds/takeoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmds

import (
"context"
"fmt"
"strings"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -65,21 +66,21 @@ func Takeoff(
return nil
}

k8sClient, err := cli.Client(
k8sClient, err := cli.Init(
monitor,
orbConfig,
gitClient,
kubeconfig,
gitOps,
false,
false,
)
if err != nil {
return err
}

if err := kubernetes.EnsureCaosSystemNamespace(monitor, k8sClient); err != nil {
monitor.Info("failed to apply common resources into k8s-cluster")
return err
return fmt.Errorf("failed to apply common resources into k8s-cluster: %w", err)
}

if gitOps {
Expand All @@ -90,8 +91,7 @@ func Takeoff(
}

if err := kubernetes.EnsureOrbconfigSecret(monitor, k8sClient, orbConfigBytes); err != nil {
monitor.Info("failed to apply configuration resources into k8s-cluster")
return err
return fmt.Errorf("failed to apply configuration resources into k8s-cluster: %w", err)
}
}

Expand Down
30 changes: 16 additions & 14 deletions cmd/orbctl/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import (
"fmt"

"github.com/caos/orbos/mntr"

"github.com/caos/orbos/pkg/cfg"

"github.com/caos/orbos/pkg/kubernetes/cli"

"github.com/caos/orbos/pkg/git"

"github.com/caos/orbos/pkg/kubernetes/cli"
"github.com/caos/orbos/pkg/orb"
"github.com/spf13/cobra"
)
Expand All @@ -37,7 +33,7 @@ func ConfigCommand(getRv GetRootValues) *cobra.Command {

cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {

rv, _ := getRv("configure", "", map[string]interface{}{"masterkey": newMasterKey != "", "newRepoURL": newRepoURL})
rv := getRv("configure", "", map[string]interface{}{"masterkey": newMasterKey != "", "newRepoURL": newRepoURL})
defer rv.ErrFunc(err)

if !rv.Gitops {
Expand All @@ -58,9 +54,13 @@ func ConfigCommand(getRv GetRootValues) *cobra.Command {
return err
}

k8sClient, err := cli.Client(rv.Monitor, rv.OrbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, false)
var uninitialized bool
k8sClient, err := cli.Init(rv.Monitor, rv.OrbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, false, false)
if err != nil {
// ignore
if !errors.Is(err, cli.ErrNotInitialized) {
return err
}
uninitialized = true
err = nil
}

Expand All @@ -72,12 +72,14 @@ func ConfigCommand(getRv GetRootValues) *cobra.Command {
}
}

if err := cfg.ApplyOrbconfigSecret(
rv.OrbConfig,
k8sClient,
rv.Monitor,
); err != nil {
return err
if !uninitialized {
if err := cfg.ApplyOrbconfigSecret(
rv.OrbConfig,
k8sClient,
rv.Monitor,
); err != nil {
return err
}
}

return cfg.ConfigureOperators(
Expand Down
16 changes: 6 additions & 10 deletions cmd/orbctl/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os/exec"
"strings"

"github.com/caos/orbos/pkg/kubernetes/cli"

"github.com/caos/orbos/mntr"

"github.com/spf13/cobra"
Expand All @@ -27,29 +29,23 @@ func EditCommand(getRv GetRootValues) *cobra.Command {
Example: `orbctl file edit desired.yml`,
RunE: func(cmd *cobra.Command, args []string) (err error) {

rv, err := getRv("edit", "", map[string]interface{}{"file": args[0]})
if err != nil {
return err
}
rv := getRv("edit", "", map[string]interface{}{"file": args[0]})
defer rv.ErrFunc(err)

orbConfig := rv.OrbConfig
gitClient := rv.GitClient

if !rv.Gitops {
return mntr.ToUserError(errors.New("edit command is only supported with the --gitops flag"))
}

if err := initRepo(orbConfig, gitClient); err != nil {
if _, err := cli.Init(rv.Monitor, rv.OrbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, true, true); err != nil {
return err
}

edited, err := captureInputFromEditor(GetPreferredEditorFromEnvironment, bytes.NewReader(gitClient.Read(args[0])))
edited, err := captureInputFromEditor(GetPreferredEditorFromEnvironment, bytes.NewReader(rv.GitClient.Read(args[0])))
if err != nil {
panic(err)
}

return gitClient.UpdateRemote("File written by orbctl", func() []git.File {
return rv.GitClient.UpdateRemote("File written by orbctl", func() []git.File {
return []git.File{{
Path: args[0],
Content: edited,
Expand Down
11 changes: 2 additions & 9 deletions cmd/orbctl/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,14 @@ func ExecCommand(getRv GetRootValues) *cobra.Command {
machineID = args[0]
}

rv, err := getRv("exec", "", map[string]interface{}{"machine": machineID, "command": command != ""})
if err != nil {
return err
}
rv := getRv("exec", "", map[string]interface{}{"machine": machineID, "command": command != ""})
defer rv.ErrFunc(err)

monitor := rv.Monitor
orbConfig := rv.OrbConfig
gitClient := rv.GitClient

if !rv.Gitops {
return mntr.ToUserError(errors.New("exec command is only supported with the --gitops flag and a committed orbiter.yml"))
}

return machines(monitor, gitClient, orbConfig, func(machineIDs []string, machines map[string]infra.Machine, _ *tree.Tree) error {
return machines(monitor, rv.GitClient, rv.OrbConfig, func(machineIDs []string, machines map[string]infra.Machine, _ *tree.Tree) error {

if machineID == "" {
if err := survey.AskOne(&survey.Select{
Expand Down
11 changes: 2 additions & 9 deletions cmd/orbctl/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,14 @@ func ListCommand(getRv GetRootValues) *cobra.Command {

cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {

rv, err := getRv("list", "", map[string]interface{}{"column": column, "context": context})
if err != nil {
return err
}
rv := getRv("list", "", map[string]interface{}{"column": column, "context": context})
defer rv.ErrFunc(err)

monitor := rv.Monitor
orbConfig := rv.OrbConfig
gitClient := rv.GitClient

if !rv.Gitops {
return mntr.ToUserError(errors.New("list command is only supported with the --gitops flag and a committed orbiter.yml"))
}

return machines(monitor, gitClient, orbConfig, func(machineIDs []string, machines map[string]infra.Machine, _ *tree.Tree) error {
return machines(monitor, rv.GitClient, rv.OrbConfig, func(machineIDs []string, machines map[string]infra.Machine, _ *tree.Tree) error {

printer := tableprinter.New(os.Stdout)
printer.BorderTop, printer.BorderBottom = true, true
Expand Down
4 changes: 3 additions & 1 deletion cmd/orbctl/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"

"github.com/caos/orbos/pkg/kubernetes/cli"

orbcfg "github.com/caos/orbos/pkg/orb"

"github.com/caos/orbos/pkg/labels"
Expand All @@ -16,7 +18,7 @@ import (

func machines(monitor mntr.Monitor, gitClient *git.Client, orbConfig *orbcfg.Orb, do func(machineIDs []string, machines map[string]infra.Machine, desired *tree.Tree) error) error {

if err := initRepo(orbConfig, gitClient); err != nil {
if _, err := cli.Init(monitor, orbConfig, gitClient, "", true, true, true); err != nil {
return err
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/orbctl/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Patching a node property non-interactively: orbctl file path orbiter.yml cluster
flags.BoolVar(&stdin, "stdin", false, "Read content value by stdin")
flags.BoolVar(&exact, "exact", false, "Write the content exactly at the path given without further prompting")

cmd.RunE = func(cmd *cobra.Command, args []string) error {
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {

var path []string
if len(args) > 1 {
Expand All @@ -47,10 +47,7 @@ Patching a node property non-interactively: orbctl file path orbiter.yml cluster

filePath := args[0]

rv, err := getRv("patch", "", map[string]interface{}{"value": value, "filePath": filePath, "valuePath": file, "stdin": stdin, "exact": exact})
if err != nil {
return err
}
rv := getRv("patch", "", map[string]interface{}{"value": value, "filePath": filePath, "valuePath": file, "stdin": stdin, "exact": exact})
defer rv.ErrFunc(err)

if !rv.Gitops {
Expand Down
7 changes: 2 additions & 5 deletions cmd/orbctl/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ func PrintCommand(getRv GetRootValues) *cobra.Command {
Short: "Print the files contents to stdout",
Args: cobra.ExactArgs(1),
Example: `orbctl file print orbiter.yml`,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) (err error) {

file := args[0]

rv, err := getRv("print", "", map[string]interface{}{"file": file})
if err != nil {
return err
}
rv := getRv("print", "", map[string]interface{}{"file": file})
defer rv.ErrFunc(err)

if !rv.Gitops {
Expand Down
Loading

0 comments on commit 011d127

Please sign in to comment.