Skip to content

Commit fe3024a

Browse files
author
Mohd Uzair
authored
Merge pull request #588 from Alero-Awani/meshkit-minify
Add Minify and Flatten for In-cluster deployment with Minikube
2 parents 7b4737d + e7cfa2d commit fe3024a

File tree

4 files changed

+81
-24
lines changed

4 files changed

+81
-24
lines changed

utils/error.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ var (
4646
ErrCopyFileCode = "replace_me"
4747
ErrCloseFileCode = "replace_me"
4848
ErrCompressToTarGZCode = "meshkit-11248"
49+
50+
ErrConvertToByteCode = "meshkit-11187"
51+
4952
ErrOpenFileCode = "replace_me"
53+
5054
)
5155
var (
5256
ErrExtractType = errors.New(
@@ -151,6 +155,10 @@ func ErrCreateDir(err error, filepath string) error {
151155
return errors.New(ErrCreateDirCode, errors.Alert, []string{fmt.Sprintf("error creating directory at %s", filepath)}, []string{err.Error()}, []string{"invalid path provided", "insufficient permissions"}, []string{"provide a valid path", "retry by using an absolute path", "check for sufficient permissions for the user"})
152156
}
153157

158+
func ErrConvertToByte(err error) error {
159+
return errors.New(ErrConvertToByteCode, errors.Alert, []string{("error converting data to []byte")}, []string{err.Error()}, []string{"Unsupported data types", "invalid configuration data", "failed serialization of data"}, []string{"check for any custom types in the data that might not be serializable", "Verify that the data type being passed is valid for conversion to []byte"})
160+
}
161+
154162
func ErrGettingLatestReleaseTag(err error) error {
155163
return errors.New(
156164
ErrGettingLatestReleaseTagCode,

utils/kubernetes/apply-helm-chart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func generateAction(actionConfig *action.Configuration, cfg ApplyHelmChartConfig
454454
case UNINSTALL:
455455
return func(c *chart.Chart) error {
456456
act := action.NewUninstall(actionConfig)
457-
457+
458458
act.DryRun = cfg.DryRun
459459
if _, err := act.Run(cfg.ReleaseName); err != nil {
460460
return ErrApplyHelmChart(err)

utils/kubernetes/client.go

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,58 +14,90 @@ import (
1414
func DetectKubeConfig(configfile []byte) (config *rest.Config, err error) {
1515
if len(configfile) > 0 {
1616
var cfgFile []byte
17-
cfgFile, err = processConfig(configfile)
17+
18+
_, cfgFile, err = ProcessConfig(configfile, "")
1819
if err != nil {
1920
return nil, err
2021
}
2122

2223
if config, err = clientcmd.RESTConfigFromKubeConfig(cfgFile); err == nil {
23-
return config, err
24+
return config, ErrRestConfigFromKubeConfig(err)
2425
}
2526
}
2627

2728
// If deployed within the cluster
2829
if config, err = rest.InClusterConfig(); err == nil {
29-
return config, err
30+
return config, ErrRestConfigFromKubeConfig(err)
3031
}
3132

3233
// Look for kubeconfig from the path mentioned in $KUBECONFIG
3334
kubeconfig := os.Getenv("KUBECONFIG")
3435
if kubeconfig != "" {
35-
if config, err = clientcmd.BuildConfigFromFlags("", kubeconfig); err == nil {
36-
return config, err
36+
_, cfgFile, err := ProcessConfig(kubeconfig, "")
37+
if err != nil {
38+
return nil, err
39+
}
40+
if config, err = clientcmd.RESTConfigFromKubeConfig(cfgFile); err == nil {
41+
return config, ErrRestConfigFromKubeConfig(err)
3742
}
3843
}
3944

4045
// Look for kubeconfig at the default path
4146
path := filepath.Join(utils.GetHome(), ".kube", "config")
42-
if config, err = clientcmd.BuildConfigFromFlags("", path); err == nil {
43-
return config, err
47+
_, cfgFile, err := ProcessConfig(path, "")
48+
if err != nil {
49+
return nil, err
50+
}
51+
if config, err = clientcmd.RESTConfigFromKubeConfig(cfgFile); err == nil {
52+
return config, ErrRestConfigFromKubeConfig(err)
4453
}
4554

4655
return
4756
}
4857

49-
func processConfig(configFile []byte) ([]byte, error) {
50-
cfg, err := clientcmd.Load(configFile)
58+
// ProcessConfig handles loading, validating, and optionally saving or returning a kubeconfig
59+
func ProcessConfig(kubeConfig interface{}, outputPath string) (*clientcmdapi.Config, []byte, error) {
60+
var config *clientcmdapi.Config
61+
var err error
62+
63+
// Load the Kubeconfig
64+
switch v := kubeConfig.(type) {
65+
case string:
66+
config, err = clientcmd.LoadFromFile(v)
67+
case []byte:
68+
config, err = clientcmd.Load(v)
69+
default:
70+
return nil, nil, ErrLoadConfig(err)
71+
}
5172
if err != nil {
52-
return nil, ErrLoadConfig(err)
73+
return nil, nil, ErrLoadConfig(err)
5374
}
5475

55-
err = clientcmdapi.MinifyConfig(cfg)
56-
if err != nil {
57-
return nil, ErrValidateConfig(err)
76+
// Validate and Process the Config
77+
if err := clientcmdapi.MinifyConfig(config); err != nil {
78+
return nil, nil, ErrValidateConfig(err)
5879
}
5980

60-
err = clientcmdapi.FlattenConfig(cfg)
61-
if err != nil {
62-
return nil, ErrValidateConfig(err)
81+
if err := clientcmdapi.FlattenConfig(config); err != nil {
82+
return nil, nil, ErrValidateConfig(err)
83+
}
84+
85+
if err := clientcmd.Validate(*config); err != nil {
86+
return nil, nil, ErrValidateConfig(err)
6387
}
6488

65-
err = clientcmd.Validate(*cfg)
89+
// Convert the config to []byte
90+
configBytes, err := clientcmd.Write(*config)
6691
if err != nil {
67-
return nil, ErrValidateConfig(err)
92+
return nil, nil, utils.ErrConvertToByte(err)
93+
}
94+
95+
//Save the Processed config to a file
96+
if outputPath != "" {
97+
if err := clientcmd.WriteToFile(*config, outputPath); err != nil {
98+
return nil, nil, utils.ErrWriteFile(err, outputPath)
99+
}
68100
}
69101

70-
return clientcmd.Write(*cfg)
102+
return config, configBytes, nil
71103
}

utils/kubernetes/error.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
ErrEntryWithChartVersionNotExistsCode = "meshkit-11204"
3636
ErrEndpointNotFound = errors.New(ErrEndpointNotFoundCode, errors.Alert, []string{"Unable to discover an endpoint"}, []string{}, []string{}, []string{})
3737
ErrInvalidAPIServer = errors.New(ErrInvalidAPIServerCode, errors.Alert, []string{"Invalid API Server URL"}, []string{}, []string{}, []string{})
38+
ErrRestConfigFromKubeConfigCode = "meshkit-11205"
3839
)
3940

4041
func ErrApplyManifest(err error) error {
@@ -51,22 +52,22 @@ func ErrApplyHelmChart(err error) error {
5152
return errors.New(ErrApplyHelmChartCode, errors.Alert, []string{"Error applying helm chart"}, []string{err.Error()}, []string{"Chart could be invalid"}, []string{"Make sure to apply valid chart"})
5253
}
5354

54-
// ErrApplyHelmChart is the error which occurs in the process of applying helm chart
55+
// ErrNewKubeClient is the error which occurs when creating a new Kubernetes clientset
5556
func ErrNewKubeClient(err error) error {
5657
return errors.New(ErrNewKubeClientCode, errors.Alert, []string{"Error creating kubernetes clientset"}, []string{err.Error()}, []string{"Kubernetes config is not accessible to meshery or not valid"}, []string{"Upload your kubernetes config via the settings dashboard. If uploaded, wait for a minute for it to get initialized"})
5758
}
5859

59-
// ErrApplyHelmChart is the error which occurs in the process of applying helm chart
60+
// ErrNewDynClient is the error which occurs when creating a new dynamic client
6061
func ErrNewDynClient(err error) error {
6162
return errors.New(ErrNewDynClientCode, errors.Alert, []string{"Error creating dynamic client"}, []string{err.Error()}, []string{"Kubernetes config is not accessible to meshery or not valid"}, []string{"Upload your kubernetes config via the settings dashboard. If uploaded, wait for a minute for it to get initialized"})
6263
}
6364

64-
// ErrApplyHelmChart is the error which occurs in the process of applying helm chart
65+
// ErrNewDiscovery is the error which occurs when creating a new discovery client
6566
func ErrNewDiscovery(err error) error {
6667
return errors.New(ErrNewDiscoveryCode, errors.Alert, []string{"Error creating discovery client"}, []string{err.Error()}, []string{"Discovery resource is invalid or doesnt exist"}, []string{"Makes sure the you input valid resource for discovery"})
6768
}
6869

69-
// ErrApplyHelmChart is the error which occurs in the process of applying helm chart
70+
// ErrNewInformer is the error which occurs when creating a new informer
7071
func ErrNewInformer(err error) error {
7172
return errors.New(ErrNewInformerCode, errors.Alert, []string{"Error creating informer client"}, []string{err.Error()}, []string{"Informer is invalid or doesnt exist"}, []string{"Makes sure the you input valid resource for the informer"})
7273
}
@@ -100,3 +101,19 @@ func ErrEntryWithChartVersionNotExists(entry, appVersion string) error {
100101
func ErrHelmRepositoryNotFound(repo string, err error) error {
101102
return errors.New(ErrHelmRepositoryNotFoundCode, errors.Alert, []string{"Helm repo not found"}, []string{fmt.Sprintf("either the repo %s does not exists or is corrupt: %v", repo, err)}, []string{}, []string{})
102103
}
104+
105+
// ErrRestConfigFromKubeConfig returns an error when failing to create a REST config from a kubeconfig file.
106+
func ErrRestConfigFromKubeConfig(err error) error {
107+
return errors.New(ErrRestConfigFromKubeConfigCode,
108+
errors.Alert,
109+
[]string{"Failed to create REST config from kubeconfig."},
110+
[]string{fmt.Sprintf("Error occured while creating REST config from kubeconfig: %s", err.Error())},
111+
[]string{
112+
"The provided kubeconfig data might be invalid or corrupted.",
113+
"The kubeconfig might be incomplete or missing required fields."},
114+
[]string{
115+
"Verify that the kubeconfig data is valid.",
116+
"Ensure the kubeconfig contains all necessary cluster, user, and context information.",
117+
"Check if the kubeconfig data was properly read and passed to the function."},
118+
)
119+
}

0 commit comments

Comments
 (0)