This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
501 additions
and
142 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/spf13/cobra" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
|
||
"github.com/substratusai/substratus/internal/cli/utils" | ||
"github.com/substratusai/substratus/internal/tui" | ||
) | ||
|
||
func applyCommand() *cobra.Command { | ||
var flags struct { | ||
namespace string | ||
filename string | ||
kubeconfig string | ||
} | ||
|
||
run := func(cmd *cobra.Command, args []string) error { | ||
defer tui.LogFile.Close() | ||
|
||
if flags.filename == "" { | ||
return fmt.Errorf("Flag -f (--filename) required") | ||
} | ||
|
||
kubeconfigNamespace, restConfig, err := utils.BuildConfigFromFlags("", flags.kubeconfig) | ||
if err != nil { | ||
return fmt.Errorf("rest config: %w", err) | ||
} | ||
|
||
clientset, err := kubernetes.NewForConfig(restConfig) | ||
if err != nil { | ||
return fmt.Errorf("clientset: %w", err) | ||
} | ||
|
||
client, err := NewClient(clientset, restConfig) | ||
if err != nil { | ||
return fmt.Errorf("client: %w", err) | ||
} | ||
|
||
// Initialize our program | ||
tui.P = tea.NewProgram((&tui.ApplyModel{ | ||
Ctx: cmd.Context(), | ||
Filename: flags.filename, | ||
Namespace: tui.Namespace{ | ||
Contextual: kubeconfigNamespace, | ||
Specified: flags.namespace, | ||
}, | ||
Client: client, | ||
K8s: clientset, | ||
}).New()) | ||
if _, err := tui.P.Run(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "apply", | ||
Aliases: []string{"ap"}, | ||
Short: "Apply Substratus (or any Kubernetes) objects", | ||
Example: ` # Scan *.yaml files looking for manifests to apply. | ||
sub apply ./dir/ | ||
# Apply a single manifest file. | ||
sub apply -f manifests.yaml | ||
# Apply a remote manifest. | ||
sub apply -f https://some/manifest.yaml`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if err := run(cmd, args); err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
defaultKubeconfig := os.Getenv("KUBECONFIG") | ||
if defaultKubeconfig == "" { | ||
defaultKubeconfig = clientcmd.RecommendedHomeFile | ||
} | ||
|
||
cmd.Flags().StringVarP(&flags.kubeconfig, "kubeconfig", "", defaultKubeconfig, "") | ||
cmd.Flags().StringVarP(&flags.namespace, "namespace", "n", "", "Namespace of Notebook") | ||
cmd.Flags().StringVarP(&flags.filename, "filename", "f", "", "Manifest file") | ||
|
||
return cmd | ||
} |
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
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
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
Oops, something went wrong.