Skip to content

Commit

Permalink
Merge pull request #36 from kubeslice/rc-0.4.3
Browse files Browse the repository at this point in the history
feat(): support for chart values with custom topology configuration
  • Loading branch information
priyank-upadhyay authored Feb 23, 2023
2 parents ad0a074 + f7f9e58 commit a2f2694
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// "github.com/spf13/cobra/doc"
)

var version = "0.4.2"
var version = "0.4.3"
var rootCmd = &cobra.Command{
Use: "kubeslice-cli",
Version: version,
Expand Down
2 changes: 1 addition & 1 deletion doc/kubeslice-cli_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ kubeslice-cli install [flags]
- worker-registration: Skips the registration of KubeSlice Workers on the Controller
- worker: Skips the installation of KubeSlice Worker
- demo: Skips the installation of additional example applications
- ui: Skips the installtion of enterprise UI components (Kubeslice-Manager)
- ui: Skips the installtion of enterprise UI components (Kubeslice-Manager)
```

### Options inherited from parent commands
Expand Down
4 changes: 3 additions & 1 deletion pkg/internal/bootstrap-configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type HelmChartConfiguration struct {
type HelmChart struct {
ChartName string `yaml:"chart_name"`
Version string `yaml:"version"`
// Values to be passed as --set arguments to helm install
Values map[string]string `yaml:"values"`
}

type KubeSliceConfiguration struct {
Expand All @@ -36,7 +38,7 @@ type ClusterConfiguration struct {
KubeConfigPath string `yaml:"kube_config_path"`
ControllerCluster Cluster `yaml:"controller"`
WorkerClusters []Cluster `yaml:"workers"`
ClusterType string `yaml:"cluster"`
ClusterType string `yaml:"cluster_type"`
}

type Cluster struct {
Expand Down
10 changes: 6 additions & 4 deletions pkg/internal/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func InstallKubeSliceController(ApplicationConfiguration *ConfigurationSpecs) {

cc := ApplicationConfiguration.Configuration.ClusterConfiguration
hc := ApplicationConfiguration.Configuration.HelmChartConfiguration
generateControllerValuesFile(cc.ControllerCluster, ApplicationConfiguration.Configuration.HelmChartConfiguration.ImagePullSecret)
generateControllerValuesFile(cc.ControllerCluster, ApplicationConfiguration.Configuration.HelmChartConfiguration)
util.Printf("%s Generated Helm Values file for Controller Installation %s", util.Tick, controllerValuesFileName)
time.Sleep(200 * time.Millisecond)

Expand All @@ -52,9 +52,11 @@ func UninstallKubeSliceController(ApplicationConfiguration *ConfigurationSpecs)
// util.Printf("%s Waiting for KubeSlice Manager Pods to be removed...", util.Wait)
}

func generateControllerValuesFile(cluster Cluster, imagePullSecret ImagePullSecrets) {

util.DumpFile(fmt.Sprintf(controllerValuesTemplate+generateImagePullSecretsValue(imagePullSecret), cluster.ControlPlaneAddress), kubesliceDirectory+"/"+controllerValuesFileName)
func generateControllerValuesFile(cluster Cluster, hcConfig HelmChartConfiguration) {
err := generateValuesFile(kubesliceDirectory+"/"+controllerValuesFileName, &hcConfig.ControllerChart, fmt.Sprintf(controllerValuesTemplate+generateImagePullSecretsValue(hcConfig.ImagePullSecret), cluster.ControlPlaneAddress))
if err != nil {
log.Fatalf("%s %s", util.Cross, err)
}
}

func installKubeSliceController(cluster Cluster, hc HelmChartConfiguration) {
Expand Down
11 changes: 7 additions & 4 deletions pkg/internal/enterprise.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ kubeslice:
func InstallKubeSliceUI(ApplicationConfiguration *ConfigurationSpecs) {
util.Printf("\nInstalling KubeSlice Manager...")
if ApplicationConfiguration.Configuration.HelmChartConfiguration.UIChart.ChartName == "" {
util.Printf("%s UI Helm Chart not found. Update UI chart configuration in topology file.", util.Cross)
util.Printf("%s Skipping Kubeslice Manager installaition. UI Helm Chart not found in topology file.", util.Warn)
return
}
cc := ApplicationConfiguration.Configuration.ClusterConfiguration
hc := ApplicationConfiguration.Configuration.HelmChartConfiguration
time.Sleep(200 * time.Millisecond)
clusterType := ApplicationConfiguration.Configuration.ClusterConfiguration.ClusterType
generateUIValuesFile(clusterType, cc.ControllerCluster, ApplicationConfiguration.Configuration.HelmChartConfiguration.ImagePullSecret)
generateUIValuesFile(clusterType, cc.ControllerCluster, ApplicationConfiguration.Configuration.HelmChartConfiguration)
installKubeSliceUI(cc.ControllerCluster, hc)
util.Printf("%s Successfully installed helm chart %s/%s", util.Tick, hc.RepoAlias, hc.UIChart.ChartName)
time.Sleep(200 * time.Millisecond)
Expand All @@ -52,14 +52,17 @@ func UninstallKubeSliceUI(ApplicationConfiguration *ConfigurationSpecs) {
}
}

func generateUIValuesFile(clusterType string, cluster Cluster, imagePullSecrets ImagePullSecrets) {
func generateUIValuesFile(clusterType string, cluster Cluster, hcConfig HelmChartConfiguration) {
serviceType := ""
if clusterType == "kind" {
serviceType = "NodePort"
} else {
serviceType = "LoadBalancer"
}
util.DumpFile(fmt.Sprintf(UIValuesTemplate+generateImagePullSecretsValue(imagePullSecrets), serviceType), kubesliceDirectory+"/"+uiValuesFileName)
err := generateValuesFile(kubesliceDirectory+"/"+uiValuesFileName, &hcConfig.UIChart, fmt.Sprintf(UIValuesTemplate+generateImagePullSecretsValue(hcConfig.ImagePullSecret), serviceType))
if err != nil {
log.Fatalf("%s %s", util.Cross, err)
}
}

func installKubeSliceUI(cluster Cluster, hc HelmChartConfiguration) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/get-network-info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func GatherNetworkInformation(ApplicationConfiguration *ConfigurationSpecs) {
util.Printf("\nFetching Network Address for Clusters...")

if ApplicationConfiguration.Configuration.ClusterConfiguration.Profile == "" {
if ApplicationConfiguration.Configuration.ClusterConfiguration.Profile == "" && ApplicationConfiguration.Configuration.ClusterConfiguration.ClusterType != "kind" {
setControlPlaneAddress(&ApplicationConfiguration.Configuration.ClusterConfiguration)
setNodeIP(&ApplicationConfiguration.Configuration.ClusterConfiguration)
} else {
Expand Down
61 changes: 61 additions & 0 deletions pkg/internal/values.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package internal

import (
"fmt"
"io/ioutil"
"strings"

"gopkg.in/yaml.v2"
)

func mergeMaps(dest, src map[interface{}]interface{}) map[interface{}]interface{} {
for k, v := range src {
if d, ok := dest[k]; ok {
switch d.(type) {
case map[interface{}]interface{}:
dest[k] = mergeMaps(d.(map[interface{}]interface{}), v.(map[interface{}]interface{}))
default:
dest[k] = v
}
} else {
dest[k] = v
}
}
return dest
}

func generateValuesFile(filePath string, hc *HelmChart, defaults string) error {
valuesMap := make(map[interface{}]interface{})
for k, v := range hc.Values {
keys := strings.Split(k, ".")
currentMap := valuesMap
for i, key := range keys {
if i == len(keys)-1 {
currentMap[key] = v
} else {
if currentMap[key] == nil {
currentMap[key] = make(map[interface{}]interface{})
}
currentMap = currentMap[key].(map[interface{}]interface{})
}
}
}

defaultsMap := make(map[interface{}]interface{})
if err := yaml.Unmarshal([]byte(defaults), &defaultsMap); err != nil {
return fmt.Errorf("error parsing defaults: %v", err)
}

mergedMap := mergeMaps(valuesMap, defaultsMap)

finalData, err := yaml.Marshal(mergedMap)
if err != nil {
return fmt.Errorf("error encoding final data as YAML: %v", err)
}

if err := ioutil.WriteFile(filePath, finalData, 0644); err != nil {
return fmt.Errorf("error writing values file: %v", err)
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/internal/verify-executables.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func VerifyExecutables(ApplicationConfiguration *ConfigurationSpecs) {
util.Printf("Verifying Executables...")
time.Sleep(200 * time.Millisecond)
if ApplicationConfiguration.Configuration.ClusterConfiguration.Profile != "" {
if ApplicationConfiguration.Configuration.ClusterConfiguration.Profile != "" || ApplicationConfiguration.Configuration.ClusterConfiguration.ClusterType == "kind" {
util.ExecutablePaths = map[string]string{
"kind": "kind",
"kubectl": "kubectl",
Expand Down
13 changes: 7 additions & 6 deletions pkg/internal/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ func InstallKubeSliceWorker(ApplicationConfiguration *ConfigurationSpecs) {
filename := "helm-values-" + cluster.Name + ".yaml"
generateWorkerValuesFile(cluster,
filename,
ApplicationConfiguration.Configuration.HelmChartConfiguration.ImagePullSecret,
ApplicationConfiguration.Configuration.ClusterConfiguration.ControllerCluster,
ApplicationConfiguration.Configuration.KubeSliceConfiguration.ProjectName)
ApplicationConfiguration.Configuration)

util.Printf("%s Generated Helm Values file for Worker Installation %s", util.Tick, filename)
time.Sleep(200 * time.Millisecond)
Expand Down Expand Up @@ -82,10 +80,10 @@ func Retry(backoffLimit int, sleep time.Duration, f func() error) (err error) {
return fmt.Errorf("retry failed after %d attempts (took %d seconds), last error: %s", backoffLimit, int(elapsed.Seconds()), err)
}

func generateWorkerValuesFile(cluster Cluster, valuesFile string, imagePullSecrets ImagePullSecrets, cc Cluster, projectName string) {
func generateWorkerValuesFile(cluster Cluster, valuesFile string, config Configuration) {
var secrets map[string]string
err := Retry(3, 1*time.Second, func() (err error) {
secrets = fetchSecret(cluster.Name, cc, projectName)
secrets = fetchSecret(cluster.Name, config.ClusterConfiguration.ControllerCluster, config.KubeSliceConfiguration.ProjectName)
if secrets["namespace"] == "" || secrets["controllerEndpoint"] == "" || secrets["ca.crt"] == "" || secrets["token"] == "" {
return fmt.Errorf("secret is empty")
}
Expand All @@ -94,7 +92,10 @@ func generateWorkerValuesFile(cluster Cluster, valuesFile string, imagePullSecre
if err != nil {
log.Fatalf("Unable to fetch secrets\n%s", err)
}
util.DumpFile(fmt.Sprintf(workerValuesTemplate+generateImagePullSecretsValue(imagePullSecrets), secrets["namespace"], secrets["controllerEndpoint"], secrets["ca.crt"], secrets["token"], cluster.Name, cluster.NodeIP, cluster.ControlPlaneAddress), kubesliceDirectory+"/"+valuesFile)
err = generateValuesFile(kubesliceDirectory+"/"+valuesFile, &config.HelmChartConfiguration.WorkerChart, fmt.Sprintf(workerValuesTemplate+generateImagePullSecretsValue(config.HelmChartConfiguration.ImagePullSecret), secrets["namespace"], secrets["controllerEndpoint"], secrets["ca.crt"], secrets["token"], cluster.Name, cluster.NodeIP, cluster.ControlPlaneAddress))
if err != nil {
log.Fatalf("%s %s", util.Cross, err)
}
}

func installWorker(cluster Cluster, valuesName string, helmChartConfig HelmChartConfiguration) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/slicectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func basicInstall(skipSteps map[string]string) {
if !skipKind {
internal.CreateKindClusters(ApplicationConfiguration)
}
if !skipCalico {
internal.InstallCalico(&ApplicationConfiguration.Configuration.ClusterConfiguration)
}
}
if (ApplicationConfiguration.Configuration.ClusterConfiguration.Profile != "" || ApplicationConfiguration.Configuration.ClusterConfiguration.ClusterType == "kind") && !skipCalico {
internal.InstallCalico(&ApplicationConfiguration.Configuration.ClusterConfiguration)
}
internal.GatherNetworkInformation(ApplicationConfiguration)
internal.AddHelmCharts(ApplicationConfiguration)
Expand Down
4 changes: 4 additions & 0 deletions samples/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ configuration:
cluster_configuration:
profile: #{the KubeSlice Profile for the demo. Possible values [full-demo, minimal-demo]}
kube_config_path: #{specify the kube config file to use for topology setup; for topology only}
cluster_type: #{optional: specify the type of cluster. Valid values are kind, cloud, data-center}
controller:
name: #{the user defined name of the controller cluster}
context_name: #{the name of the context to use from kubeconfig file; for topology only}
Expand Down Expand Up @@ -39,12 +40,15 @@ configuration:
controller_chart:
chart_name: #{The name of the Controller Chart}
version: #{The version of the chart to use. Leave blank for latest version}
values: #(Values to be passed as --set arguments to helm install)
worker_chart:
chart_name: #{The name of the Worker Chart}
version: #{The version of the chart to use. Leave blank for latest version}
values: #(Values to be passed as --set arguments to helm install)
ui_chart:
chart_name: #{The name of the UI/Enterprise Chart}
version: #{The version of the chart to use. Leave blank for latest version}
values: #(Values to be passed as --set arguments to helm install)
helm_username: #{Helm Username if the repo is private}
helm_password: #{Helm Password if the repo is private}
image_pull_secret: #{The image pull secrets. Optional for OpenSource, required for enterprise}
Expand Down
5 changes: 3 additions & 2 deletions util/print-util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const (
Cross = string(rune(0x274c))
Tick = string(rune(0x2714))
Wait = string(rune(0x267B))
Run = string(rune(0x1F3C3))
Run = string(rune(0x1F3C3))
Warn = string(rune(0x26A0))
)

func Printf(format string, a ...interface{}) {
Expand All @@ -27,4 +28,4 @@ func Fatalf(format string, a ...interface{}) {
fmt.Println(format + "\n")
}
os.Exit(1)
}
}

0 comments on commit a2f2694

Please sign in to comment.