-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
7 changed files
with
147 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmdutil | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error" | ||
"github.com/operator-framework/operator-sdk/pkg/generator" | ||
|
||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
const configYaml = "./config/config.yaml" | ||
|
||
// MustInProjectRoot checks if the current dir is the project root. | ||
func MustInProjectRoot() { | ||
// if the current directory has the "./config/config.yaml" file, then it is safe to say | ||
// we are at the project root. | ||
_, err := os.Stat(configYaml) | ||
if err != nil && os.IsNotExist(err) { | ||
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("must in project root dir: %v", err)) | ||
} | ||
} | ||
|
||
// GetConfig gets the values from ./config/config.yaml and parses them into a Config struct. | ||
func GetConfig() *generator.Config { | ||
c := &generator.Config{} | ||
fp, err := ioutil.ReadFile(configYaml) | ||
if err != nil { | ||
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to read config file %v: (%v)", configYaml, err)) | ||
} | ||
if err = yaml.Unmarshal(fp, c); err != nil { | ||
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to unmarshal config file %v: (%v)", configYaml, err)) | ||
} | ||
return c | ||
} |
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,19 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/up" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewUpCmd() *cobra.Command { | ||
upCmd := &cobra.Command{ | ||
Use: "up", | ||
Short: "Launches the operator", | ||
Long: `The up command has subcommands that can launch the operator in various ways. | ||
`, | ||
} | ||
|
||
upCmd.AddCommand(up.NewLocalCmd()) | ||
return upCmd | ||
} |
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,78 @@ | ||
package up | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"os/user" | ||
"path/filepath" | ||
|
||
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd/cmdutil" | ||
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error" | ||
"github.com/operator-framework/operator-sdk/pkg/util/k8sutil" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewLocalCmd() *cobra.Command { | ||
upLocalCmd := &cobra.Command{ | ||
Use: "local", | ||
Short: "Launches the operator locally", | ||
Long: `The operator-sdk up local command launches the operator on the local machine | ||
by building the operator binary with the ability to access a | ||
kubernetes cluster using a kubeconfig file. | ||
`, | ||
Run: upLocalFunc, | ||
} | ||
|
||
upLocalCmd.Flags().StringVar(&kubeConfig, "kubeconfig", "", "The file path to kubernetes configuration file; defaults to $HOME/.kube/config") | ||
|
||
return upLocalCmd | ||
} | ||
|
||
var ( | ||
kubeConfig string | ||
) | ||
|
||
const ( | ||
gocmd = "go" | ||
run = "run" | ||
cmd = "cmd" | ||
main = "main.go" | ||
defaultConfigPath = ".kube/config" | ||
) | ||
|
||
func upLocalFunc(cmd *cobra.Command, args []string) { | ||
mustKubeConfig() | ||
cmdutil.MustInProjectRoot() | ||
c := cmdutil.GetConfig() | ||
upLocal(c.ProjectName) | ||
} | ||
|
||
// mustKubeConfig checks if the kubeconfig file exists. | ||
func mustKubeConfig() { | ||
// if kubeConfig is not specified, search for the default kubeconfig file under the $HOME/.kube/config. | ||
if len(kubeConfig) == 0 { | ||
usr, err := user.Current() | ||
if err != nil { | ||
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to determine user's home dir: %v", err)) | ||
} | ||
kubeConfig = filepath.Join(usr.HomeDir, defaultConfigPath) | ||
} | ||
|
||
_, err := os.Stat(kubeConfig) | ||
if err != nil && os.IsNotExist(err) { | ||
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to find the kubeconfig file (%v): %v", kubeConfig, err)) | ||
} | ||
} | ||
|
||
func upLocal(projectName string) { | ||
dc := exec.Command(gocmd, run, filepath.Join(cmd, projectName, main)) | ||
dc.Stdout = os.Stdout | ||
dc.Stderr = os.Stderr | ||
dc.Env = append(os.Environ(), fmt.Sprintf("%v=%v", k8sutil.KubeConfigEnvVar, kubeConfig)) | ||
err := dc.Run() | ||
if err != nil { | ||
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to run operator locally: %v", err)) | ||
} | ||
} |
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,7 @@ | ||
package k8sutil | ||
|
||
const ( | ||
// KubeConfigEnvVar defines the env variable KUBERNETES_CONFIG which | ||
// contains the kubeconfig file path. | ||
KubeConfigEnvVar = "KUBERNETES_CONFIG" | ||
) |