diff --git a/compose/compose.go b/compose/compose.go index 203b3b8a2..d7a40d781 100644 --- a/compose/compose.go +++ b/compose/compose.go @@ -1,32 +1,20 @@ package compose import ( + "log" + + chartloader "helm.sh/helm/v3/pkg/chart/loader" + "github.com/compose-spec/compose-go/loader" "github.com/compose-spec/compose-go/types" - "github.com/docker/cli/cli/command" - "github.com/docker/cli/cli/config/configfile" - registry "github.com/docker/cli/cli/registry/client" - "github.com/docker/cli/cli/streams" - "github.com/docker/docker/client" "github.com/docker/helm-prototype/pkg/compose/internal/convert" "github.com/docker/helm-prototype/pkg/compose/internal/helm" utils "github.com/docker/helm-prototype/pkg/compose/internal/utils" + "helm.sh/helm/v3/pkg/action" + env "helm.sh/helm/v3/pkg/cli" + k "helm.sh/helm/v3/pkg/kube" ) -var ( - Client client.APIClient - RegistryClient registry.RegistryClient - ConfigFile *configfile.ConfigFile - Stdout *streams.Out -) - -func WithDockerCli(cli command.Cli) { - Client = cli.Client() - RegistryClient = cli.RegistryClient(false) - ConfigFile = cli.ConfigFile() - Stdout = cli.Out() -} - // Orchestrator is "kubernetes" or "swarm" type Orchestrator string @@ -65,6 +53,10 @@ func NewProject(config types.ConfigDetails, name string) (*Project, error) { // projectFromOptions load a compose project based on command line options func ProjectFromOptions(options *ProjectOptions) (*Project, error) { + if options.Name == "" { + options.Name = "docker-compose" + } + workingDir, configs, err := utils.GetConfigs( options.Name, options.ConfigPaths, @@ -80,7 +72,7 @@ func ProjectFromOptions(options *ProjectOptions) (*Project, error) { }, options.Name) } -func (p *Project) GenerateCharts(path string) error { +func (p *Project) GenerateChart(path string) error { objects, err := convert.MapToKubernetesObjects(p.Config, p.Name) if err != nil { return err @@ -91,6 +83,54 @@ func (p *Project) GenerateCharts(path string) error { } return nil } -func (p *Project) InstallCommand(options *ProjectOptions) error { - return nil + +func (p *Project) InstallChart(n, path string) error { + + if path == "" { + err := p.GenerateChart(path) + if err != nil { + return err + } + } + + settings := env.New() + actionConfig := new(action.Configuration) + println(".......... here ............") + if err := actionConfig.Init(settings.RESTClientGetter(), settings.Namespace(), "memory", nil); err != nil { + log.Fatal(err) + } + println(settings.EnvVars()) + client := action.NewInstall(actionConfig) + println("Original chart version:", client.Version) + client.Version = ">0.0.0-0" + + name, chart, err := client.NameAndChart([]string{n, path}) + if err != nil { + return nil + } + + client.ReleaseName = name + client.Namespace = settings.Namespace() + cp, err := client.ChartPathOptions.LocateChart(chart, settings) + if err != nil { + return err + } + println("CHART PATH: ", cp) + + chartRequested, err := chartloader.Load(cp) + if err != nil { + return err + } + kclient := k.New(settings.RESTClientGetter()) + println(kclient.Namespace) + if err = actionConfig.KubeClient.IsReachable(); err != nil { + println("Kube API is not reachable") + return err + } + println("....Running.....") + println("Chart description: ", chartRequested.Metadata.Description) + release, err := client.Run(chartRequested, map[string]interface{}{}) + + println(release.Name) + return err } diff --git a/compose/internal/helm/action.go b/compose/internal/helm/action.go deleted file mode 100644 index e500abba8..000000000 --- a/compose/internal/helm/action.go +++ /dev/null @@ -1,34 +0,0 @@ -package helm - -import ( - "os" - "sync" - - "k8s.io/cli-runtime/pkg/genericclioptions" -) - -type KubeConfig struct { - namespace string - config genericclioptions.RESTClientGetter - configOnce sync.Once - - // KubeConfig is the path to the kubeconfig file - KubeConfig string - // KubeContext is the name of the kubeconfig context. - KubeContext string - // Bearer KubeToken used for authentication - KubeToken string - // Kubernetes API Server Endpoint for authentication - KubeAPIServer string -} - -func New() *KubeConfig { - - env := KubeConfig{ - namespace: "", - KubeContext: os.Getenv("COMPOSE_KUBECONTEXT"), - KubeToken: os.Getenv("COMPOSE_KUBETOKEN"), - KubeAPIServer: os.Getenv("COMPOSE_KUBEAPISERVER"), - } - return &env -} diff --git a/compose/internal/helm/output.go b/compose/internal/helm/output.go index edf7cdb1e..6c37e2685 100644 --- a/compose/internal/helm/output.go +++ b/compose/internal/helm/output.go @@ -37,7 +37,7 @@ home: Name string } var chartData bytes.Buffer - _ = t.Execute(&chartData, ChartDetails{project}) + _ = t.Execute(&chartData, ChartDetails{Name: project}) if err := out.Write("Chart.yaml", chartData.Bytes()); err != nil { return err