Skip to content

Commit 2549df5

Browse files
authored
Merge pull request #99 from onepanelio/feat/microk8s.command
feat: detect microk8s provider in apply/app status
2 parents 233463d + 8ff71d6 commit 2549df5

6 files changed

Lines changed: 107 additions & 10 deletions

File tree

cmd/app.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,31 @@ var statusCmd = &cobra.Command{
3232
fmt.Println("Error parsing configuration file.")
3333
return
3434
}
35+
3536
ready, err := util.DeploymentStatus(yamlFile)
3637
if err != nil {
38+
yamlFile, yamlErr := util.LoadDynamicYamlFromFile(config.Spec.Params)
39+
if yamlErr != nil {
40+
fmt.Printf("Error reading file '%v' %v", config.Spec.Params, yamlErr.Error())
41+
return
42+
}
43+
44+
flatMap := yamlFile.FlattenToKeyValue(util.AppendDotFlatMapKeyFormatter)
45+
provider, providerErr := util.GetYamlStringValue(flatMap, "application.provider")
46+
if providerErr != nil {
47+
fmt.Printf("Unable to read application.provider from params.yaml %v", providerErr.Error())
48+
return
49+
}
50+
if provider == nil {
51+
fmt.Printf("application.provider is not set in params.yaml")
52+
return
53+
}
54+
55+
if *provider == "microk8s" {
56+
fmt.Printf("Unable to connect to cluster. Make sure you are running with \nKUBECONFIG=./kubeconfig opctl app status\nError: %v", err.Error())
57+
return
58+
}
59+
3760
fmt.Println(err.Error())
3861
return
3962
}

cmd/apply.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,40 @@ var applyCmd = &cobra.Command{
7575
resApp := ""
7676
errResApp := ""
7777

78+
7879
resApp, errResApp, err = applyKubernetesFile(applicationKubernetesYamlFilePath)
80+
if err != nil {
81+
yamlFile, yamlErr := util.LoadDynamicYamlFromFile(config.Spec.Params)
82+
if yamlErr != nil {
83+
fmt.Printf("Error reading file '%v' %v", config.Spec.Params, yamlErr.Error())
84+
return
85+
}
86+
87+
flatMap := yamlFile.FlattenToKeyValue(util.AppendDotFlatMapKeyFormatter)
88+
provider, providerErr := util.GetYamlStringValue(flatMap, "application.provider")
89+
if providerErr != nil {
90+
fmt.Printf("Unable to read application.provider from params.yaml %v", providerErr.Error())
91+
return
92+
}
93+
if provider == nil {
94+
fmt.Printf("application.provider is not set in params.yaml")
95+
return
96+
}
97+
98+
if *provider == "microk8s" {
99+
fmt.Printf("Unable to connect to cluster. Make sure you are running with \nKUBECONFIG=./kubeconfig opctl apply\nError: %v", err.Error())
100+
return
101+
}
102+
103+
fmt.Printf("\nFailed: %v", err.Error())
104+
return
105+
}
79106

80107
log.Printf("res: %v", resApp)
81108
if errResApp != "" {
82109
log.Printf("err: %v", errResApp)
83110
}
84111

85-
if err != nil {
86-
fmt.Printf("\nFailed: %v", err.Error())
87-
return
88-
}
89112
//Once applied, verify the application is running before moving on with the rest
90113
//of the yaml.
91114
applicationRunning := false

cmd/auth.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/md5"
55
"encoding/hex"
66
"fmt"
7+
opConfig "github.com/onepanelio/cli/config"
78

89
"github.com/onepanelio/cli/util"
910
"github.com/spf13/cobra"
@@ -29,12 +30,45 @@ var tokenCmd = &cobra.Command{
2930
Long: "Get a token for a given provider. Google Cloud Platform is different from minikube, for example.",
3031
Example: "auth token",
3132
Run: func(cmd *cobra.Command, args []string) {
32-
config := util.NewConfig()
33+
config, err := util.NewConfig()
34+
if err != nil {
35+
fmt.Printf("Error getting kubernetes configuration: %v", err.Error())
36+
return
37+
}
38+
3339
if ServiceAccountName == "" {
3440
ServiceAccountName = "admin"
3541
}
3642
token, username, err := util.GetBearerToken(config, "", ServiceAccountName)
3743
if err != nil {
44+
configFilePath := "config.yaml"
45+
opConfig, opErr := opConfig.FromFile(configFilePath)
46+
if opErr != nil {
47+
fmt.Printf("Unable to read configuration file: %v", err.Error())
48+
return
49+
}
50+
yamlFile, yamlErr := util.LoadDynamicYamlFromFile(opConfig.Spec.Params)
51+
if yamlErr != nil {
52+
fmt.Printf("Error reading file '%v' %v", opConfig.Spec.Params, yamlErr.Error())
53+
return
54+
}
55+
56+
flatMap := yamlFile.FlattenToKeyValue(util.AppendDotFlatMapKeyFormatter)
57+
provider, providerErr := util.GetYamlStringValue(flatMap, "application.provider")
58+
if providerErr != nil {
59+
fmt.Printf("Unable to read application.provider from params.yaml %v", providerErr.Error())
60+
return
61+
}
62+
if provider == nil {
63+
fmt.Printf("application.provider is not set in params.yaml")
64+
return
65+
}
66+
67+
if *provider == "microk8s" {
68+
fmt.Printf("Make sure you are running with \nKUBECONFIG=./kubeconfig opctl auth token\nError: %v", err.Error())
69+
return
70+
}
71+
3872
fmt.Printf("Error encountered for user %s: %s\n", username, err.Error())
3973
}
4074

util/dynamic_yaml.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,22 @@ func NodeValueToActual(node *yaml.Node) (interface{}, error) {
745745

746746
return value, nil
747747
}
748+
749+
// GetYamlStringValue will attempt to get the key from the input mapping and return it as a lowercase string
750+
// If the key does not exist, nil is returned, with no error
751+
// If the value exists, but is not a string, an error is returned
752+
func GetYamlStringValue(mapping map[string]interface{}, key string) (*string, error) {
753+
value, ok := mapping[key]
754+
if !ok {
755+
return nil, nil
756+
}
757+
758+
valueString, okString := value.(string)
759+
if !okString {
760+
return nil, fmt.Errorf("value is not a string")
761+
}
762+
763+
result := strings.ToLower(valueString)
764+
765+
return &result, nil
766+
}

util/kubeconfig.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@ import (
1616

1717
type Config = restclient.Config
1818

19-
func NewConfig() (config *Config) {
20-
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
19+
func NewConfig() (config *Config, err error) {
20+
config, err = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
2121
clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).ClientConfig()
22-
if err != nil {
23-
panic(err)
24-
}
2522

2623
return
2724
}

util/kubectl.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ func GetClusterIp(url string) {
256256
hostsPath = "C:\\Windows\\System32\\Drivers\\etc\\hosts"
257257
}
258258

259+
dnsRecordMessage = "local"
259260
fmt.Printf("\nIn your %v file, add %v and point it to %v\n", hostsPath, stdout, fqdn)
260261
} else {
261262
dnsRecordMessage = "an A"

0 commit comments

Comments
 (0)