Skip to content

Commit

Permalink
Merge pull request #99 from onepanelio/feat/microk8s.command
Browse files Browse the repository at this point in the history
feat: detect microk8s provider in apply/app status
  • Loading branch information
Vafilor authored Apr 28, 2021
2 parents 233463d + 8ff71d6 commit 2549df5
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 10 deletions.
23 changes: 23 additions & 0 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 27 additions & 4 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 35 additions & 1 deletion cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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())
}

Expand Down
19 changes: 19 additions & 0 deletions util/dynamic_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 2 additions & 5 deletions util/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions util/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2549df5

Please sign in to comment.