@ -20,6 +20,7 @@ import (
"context"
"context"
"fmt"
"fmt"
"io/ioutil"
"io/ioutil"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
@ -41,28 +42,36 @@ var createHelmReleaseCmd = &cobra.Command{
Aliases : [ ] string { "hr" } ,
Aliases : [ ] string { "hr" } ,
Short : "Create or update a HelmRelease resource" ,
Short : "Create or update a HelmRelease resource" ,
Long : "The helmrelease create command generates a HelmRelease resource for a given HelmRepository source." ,
Long : "The helmrelease create command generates a HelmRelease resource for a given HelmRepository source." ,
Example : ` # Create a HelmRelease from a source
Example : ` # Create a HelmRelease from a HelmRepository source
gotk create hr podinfo \
gotk create hr podinfo \
-- interval = 10 m \
-- interval = 10 m \
-- release - name = podinfo \
-- release - name = podinfo \
-- target - namespace = default \
-- target - namespace = default \
-- source = podinfo \
-- source = HelmRepository/ podinfo \
-- chart - name = podinfo \
-- chart = podinfo \
-- chart - version = ">4.0.0"
-- chart - version = ">4.0.0"
# Create a HelmRelease from a GitRepository source
gotk create hr podinfo \
-- interval = 10 m \
-- release - name = podinfo \
-- target - namespace = default \
-- source = GitRepository / podinfo \
-- chart = . / charts / podinfo
# Create a HelmRelease with values for a local YAML file
# Create a HelmRelease with values for a local YAML file
gotk create hr podinfo \
gotk create hr podinfo \
-- target - namespace = default \
-- target - namespace = default \
-- source = podinfo \
-- source = HelmRepository/ podinfo \
-- chart - name = podinfo \
-- chart = podinfo \
-- chart - version = 4.0 .5 \
-- chart - version = 4.0 .5 \
-- values = . / my - values . yaml
-- values = . / my - values . yaml
# Create a HelmRelease definition on disk without applying it on the cluster
# Create a HelmRelease definition on disk without applying it on the cluster
gotk create hr podinfo \
gotk create hr podinfo \
-- target - namespace = default \
-- target - namespace = default \
-- source = podinfo \
-- source = HelmRepository/ podinfo \
-- chart - name = podinfo \
-- chart = podinfo \
-- chart - version = 4.0 .5 \
-- chart - version = 4.0 .5 \
-- values = . / values . yaml \
-- values = . / values . yaml \
-- export > podinfo - release . yaml
-- export > podinfo - release . yaml
@ -74,7 +83,7 @@ var (
hrName string
hrName string
hrSource string
hrSource string
hrDependsOn [ ] string
hrDependsOn [ ] string
hrChart Name string
hrChart string
hrChartVersion string
hrChartVersion string
hrTargetNamespace string
hrTargetNamespace string
hrValuesFile string
hrValuesFile string
@ -82,9 +91,9 @@ var (
func init ( ) {
func init ( ) {
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrName , "release-name" , "" , "name used for the Helm release, defaults to a composition of '<target-namespace>-<hr-name>'" )
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrName , "release-name" , "" , "name used for the Helm release, defaults to a composition of '<target-namespace>-<hr-name>'" )
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrSource , "source" , "" , " HelmRepository name ")
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrSource , "source" , "" , " source that contains the chart (<kind>/<name>) ")
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrChart Name , "chart -name ", "" , "Helm chart name ")
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrChart , "chart ", "" , "Helm chart name or path ")
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrChartVersion , "chart-version" , "" , "Helm chart version, accepts semver range ")
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrChartVersion , "chart-version" , "" , "Helm chart version, accepts semver range (ignored for charts from GitRepository sources) ")
createHelmReleaseCmd . Flags ( ) . StringArrayVar ( & hrDependsOn , "depends-on" , nil , "HelmReleases that must be ready before this release can be installed" )
createHelmReleaseCmd . Flags ( ) . StringArrayVar ( & hrDependsOn , "depends-on" , nil , "HelmReleases that must be ready before this release can be installed" )
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrTargetNamespace , "target-namespace" , "" , "namespace to install this release, defaults to the HelmRelease namespace" )
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrTargetNamespace , "target-namespace" , "" , "namespace to install this release, defaults to the HelmRelease namespace" )
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrValuesFile , "values" , "" , "local path to the values.yaml file" )
createHelmReleaseCmd . Flags ( ) . StringVar ( & hrValuesFile , "values" , "" , "local path to the values.yaml file" )
@ -100,11 +109,16 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
if hrSource == "" {
if hrSource == "" {
return fmt . Errorf ( "source is required" )
return fmt . Errorf ( "source is required" )
}
}
if hrChartName == "" {
hrSourceElements := strings . Split ( hrSource , "/" )
return fmt . Errorf ( "chart name is required" )
if len ( hrSourceElements ) != 2 {
return fmt . Errorf ( "source must be in format <kind>/<name>" )
}
hrSourceKind , hrSourceName := hrSourceElements [ 0 ] , hrSourceElements [ 1 ]
if hrSourceKind != sourcev1 . HelmRepositoryKind && hrSourceKind != sourcev1 . GitRepositoryKind {
return fmt . Errorf ( "source kind must be one of: %s" , [ ] string { sourcev1 . HelmRepositoryKind , sourcev1 . GitRepositoryKind } )
}
}
if hrChartVersion == "" {
if hrChart == "" {
return fmt . Errorf ( "chart version is required" )
return fmt . Errorf ( "chart name or path is required")
}
}
ctx , cancel := context . WithTimeout ( context . Background ( ) , timeout )
ctx , cancel := context . WithTimeout ( context . Background ( ) , timeout )
@ -133,11 +147,11 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
TargetNamespace : hrTargetNamespace ,
TargetNamespace : hrTargetNamespace ,
Chart : helmv2 . HelmChartTemplate {
Chart : helmv2 . HelmChartTemplate {
Spec : helmv2 . HelmChartTemplateSpec {
Spec : helmv2 . HelmChartTemplateSpec {
Chart : hrChart Name ,
Chart : hrChart ,
Version : hrChartVersion ,
Version : hrChartVersion ,
SourceRef : helmv2 . CrossNamespaceObjectReference {
SourceRef : helmv2 . CrossNamespaceObjectReference {
Kind : sourcev1. HelmRepository Kind,
Kind : hrSource Kind,
Name : hrSource ,
Name : hrSource Name ,
} ,
} ,
} ,
} ,
} ,
} ,