diff --git a/cmd/app.go b/cmd/app.go index f14d722..b91a0b1 100644 --- a/cmd/app.go +++ b/cmd/app.go @@ -32,8 +32,31 @@ var statusCmd = &cobra.Command{ fmt.Println("Error parsing configuration file.") return } + ready, err := util.DeploymentStatus(yamlFile) if err != nil { + yamlFile, yamlErr := util.LoadDynamicYamlFromFile(config.Spec.Params) + if yamlErr != nil { + fmt.Printf("Error reading file '%v' %v", config.Spec.Params, yamlErr.Error()) + return + } + + flatMap := yamlFile.FlattenToKeyValue(util.AppendDotFlatMapKeyFormatter) + provider, providerErr := util.GetYamlStringValue(flatMap, "application.provider") + if providerErr != nil { + fmt.Printf("Unable to read application.provider from params.yaml %v", providerErr.Error()) + return + } + if provider == nil { + fmt.Printf("application.provider is not set in params.yaml") + return + } + + if *provider == "microk8s" { + fmt.Printf("Unable to connect to cluster. Make sure you are running with \nKUBECONFIG=./kubeconfig opctl app status\nError: %v", err.Error()) + return + } + fmt.Println(err.Error()) return } diff --git a/cmd/apply.go b/cmd/apply.go index 3773789..8283052 100644 --- a/cmd/apply.go +++ b/cmd/apply.go @@ -75,17 +75,40 @@ var applyCmd = &cobra.Command{ resApp := "" errResApp := "" + resApp, errResApp, err = applyKubernetesFile(applicationKubernetesYamlFilePath) + if err != nil { + yamlFile, yamlErr := util.LoadDynamicYamlFromFile(config.Spec.Params) + if yamlErr != nil { + fmt.Printf("Error reading file '%v' %v", config.Spec.Params, yamlErr.Error()) + return + } + + flatMap := yamlFile.FlattenToKeyValue(util.AppendDotFlatMapKeyFormatter) + provider, providerErr := util.GetYamlStringValue(flatMap, "application.provider") + if providerErr != nil { + fmt.Printf("Unable to read application.provider from params.yaml %v", providerErr.Error()) + return + } + if provider == nil { + fmt.Printf("application.provider is not set in params.yaml") + return + } + + if *provider == "microk8s" { + fmt.Printf("Unable to connect to cluster. Make sure you are running with \nKUBECONFIG=./kubeconfig opctl apply\nError: %v", err.Error()) + return + } + + fmt.Printf("\nFailed: %v", err.Error()) + return + } log.Printf("res: %v", resApp) if errResApp != "" { log.Printf("err: %v", errResApp) } - if err != nil { - fmt.Printf("\nFailed: %v", err.Error()) - return - } //Once applied, verify the application is running before moving on with the rest //of the yaml. applicationRunning := false diff --git a/cmd/auth.go b/cmd/auth.go index 71544fe..9c3fbbc 100644 --- a/cmd/auth.go +++ b/cmd/auth.go @@ -4,6 +4,7 @@ import ( "crypto/md5" "encoding/hex" "fmt" + opConfig "github.com/onepanelio/cli/config" "github.com/onepanelio/cli/util" "github.com/spf13/cobra" @@ -29,12 +30,45 @@ var tokenCmd = &cobra.Command{ Long: "Get a token for a given provider. Google Cloud Platform is different from minikube, for example.", Example: "auth token", Run: func(cmd *cobra.Command, args []string) { - config := util.NewConfig() + config, err := util.NewConfig() + if err != nil { + fmt.Printf("Error getting kubernetes configuration: %v", err.Error()) + return + } + if ServiceAccountName == "" { ServiceAccountName = "admin" } token, username, err := util.GetBearerToken(config, "", ServiceAccountName) if err != nil { + configFilePath := "config.yaml" + opConfig, opErr := opConfig.FromFile(configFilePath) + if opErr != nil { + fmt.Printf("Unable to read configuration file: %v", err.Error()) + return + } + yamlFile, yamlErr := util.LoadDynamicYamlFromFile(opConfig.Spec.Params) + if yamlErr != nil { + fmt.Printf("Error reading file '%v' %v", opConfig.Spec.Params, yamlErr.Error()) + return + } + + flatMap := yamlFile.FlattenToKeyValue(util.AppendDotFlatMapKeyFormatter) + provider, providerErr := util.GetYamlStringValue(flatMap, "application.provider") + if providerErr != nil { + fmt.Printf("Unable to read application.provider from params.yaml %v", providerErr.Error()) + return + } + if provider == nil { + fmt.Printf("application.provider is not set in params.yaml") + return + } + + if *provider == "microk8s" { + fmt.Printf("Make sure you are running with \nKUBECONFIG=./kubeconfig opctl auth token\nError: %v", err.Error()) + return + } + fmt.Printf("Error encountered for user %s: %s\n", username, err.Error()) } diff --git a/util/dynamic_yaml.go b/util/dynamic_yaml.go index 1500b1c..2d0346d 100644 --- a/util/dynamic_yaml.go +++ b/util/dynamic_yaml.go @@ -745,3 +745,22 @@ func NodeValueToActual(node *yaml.Node) (interface{}, error) { return value, nil } + +// GetYamlStringValue will attempt to get the key from the input mapping and return it as a lowercase string +// If the key does not exist, nil is returned, with no error +// If the value exists, but is not a string, an error is returned +func GetYamlStringValue(mapping map[string]interface{}, key string) (*string, error) { + value, ok := mapping[key] + if !ok { + return nil, nil + } + + valueString, okString := value.(string) + if !okString { + return nil, fmt.Errorf("value is not a string") + } + + result := strings.ToLower(valueString) + + return &result, nil +} \ No newline at end of file diff --git a/util/kubeconfig.go b/util/kubeconfig.go index 59a2f82..57c40d3 100644 --- a/util/kubeconfig.go +++ b/util/kubeconfig.go @@ -16,12 +16,9 @@ import ( type Config = restclient.Config -func NewConfig() (config *Config) { - config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( +func NewConfig() (config *Config, err error) { + config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig( clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).ClientConfig() - if err != nil { - panic(err) - } return } diff --git a/util/kubectl.go b/util/kubectl.go index 5a43847..7b24d21 100644 --- a/util/kubectl.go +++ b/util/kubectl.go @@ -256,6 +256,7 @@ func GetClusterIp(url string) { hostsPath = "C:\\Windows\\System32\\Drivers\\etc\\hosts" } + dnsRecordMessage = "local" fmt.Printf("\nIn your %v file, add %v and point it to %v\n", hostsPath, stdout, fqdn) } else { dnsRecordMessage = "an A"