@ -19,7 +19,9 @@ package main
import (
import (
"context"
"context"
"fmt"
"fmt"
"github.com/fluxcd/pkg/untar"
"io/ioutil"
"io/ioutil"
"net/http"
"os"
"os"
"path"
"path"
"path/filepath"
"path/filepath"
@ -37,10 +39,10 @@ var installCmd = &cobra.Command{
Long : ` The install command deploys the toolkit components in the specified namespace .
Long : ` The install command deploys the toolkit components in the specified namespace .
If a previous version is installed , then an in - place upgrade will be performed . ` ,
If a previous version is installed , then an in - place upgrade will be performed . ` ,
Example : ` # Install the latest version in the gitops - systems namespace
Example : ` # Install the latest version in the gitops - systems namespace
tk install -- version = master -- namespace = gitops - systems
tk install -- version = latest -- namespace = gitops - systems
# Dry - run install for a specific version and a series of components
# Dry - run install for a specific version and a series of components
tk install -- dry - run -- version = 0.0 .1 -- components = "source-controller,kustomize-controller"
tk install -- dry - run -- version = v0 .0 .7 -- components = "source-controller,kustomize-controller"
# Dry - run install with manifests preview
# Dry - run install with manifests preview
tk install -- dry - run -- verbose
tk install -- dry - run -- verbose
@ -65,7 +67,7 @@ func init() {
installCmd . Flags ( ) . BoolVarP ( & installDryRun , "dry-run" , "" , false ,
installCmd . Flags ( ) . BoolVarP ( & installDryRun , "dry-run" , "" , false ,
"only print the object that would be applied" )
"only print the object that would be applied" )
installCmd . Flags ( ) . StringVarP ( & installVersion , "version" , "v" , defaultVersion ,
installCmd . Flags ( ) . StringVarP ( & installVersion , "version" , "v" , defaultVersion ,
"toolkit tag or branch ")
"toolkit version ")
installCmd . Flags ( ) . StringSliceVar ( & installComponents , "components" , defaultComponents ,
installCmd . Flags ( ) . StringSliceVar ( & installComponents , "components" , defaultComponents ,
"list of components, accepts comma-separated values" )
"list of components, accepts comma-separated values" )
installCmd . Flags ( ) . StringVarP ( & installManifestsPath , "manifests" , "" , "" ,
installCmd . Flags ( ) . StringVarP ( & installManifestsPath , "manifests" , "" , "" ,
@ -189,10 +191,10 @@ transformers:
- labels . yaml
- labels . yaml
resources :
resources :
- namespace . yaml
- namespace . yaml
- policies . yaml
- roles
- roles
- github . com / fluxcd / toolkit / manifests / policies ? ref = { { $ version } }
{ { - range . Components } }
{ { - range . Components } }
- github . com / fluxcd / toolkit / manifests / bases / { { . } } ? ref = { { $ version } }
- { { . } } . yaml
{ { - end } }
{ { - end } }
`
`
@ -200,10 +202,44 @@ var kustomizationRolesTmpl = `---
apiVersion : kustomize . config . k8s . io / v1beta1
apiVersion : kustomize . config . k8s . io / v1beta1
kind : Kustomization
kind : Kustomization
resources :
resources :
- github. com / fluxcd / toolkit / manifests / rbac ? ref = { { . Version } }
- rbac. yaml
nameSuffix : - { { . Namespace } }
nameSuffix : - { { . Namespace } }
`
`
func downloadManifests ( version string , tmpDir string ) error {
ghURL := "https://github.com/fluxcd/toolkit/releases/latest/download/manifests.tar.gz"
if strings . HasPrefix ( version , "v" ) {
ghURL = fmt . Sprintf ( "https://github.com/fluxcd/toolkit/releases/download/%s/manifests.tar.gz" , version )
}
ctx , cancel := context . WithTimeout ( context . Background ( ) , timeout )
defer cancel ( )
req , err := http . NewRequest ( "GET" , ghURL , nil )
if err != nil {
return fmt . Errorf ( "failed to create HTTP request for %s, error: %w" , ghURL , err )
}
// download
resp , err := http . DefaultClient . Do ( req . WithContext ( ctx ) )
if err != nil {
return fmt . Errorf ( "failed to download artifact from %s, error: %w" , ghURL , err )
}
defer resp . Body . Close ( )
// check response
if resp . StatusCode != http . StatusOK {
return fmt . Errorf ( "faild to download artifact from %s, status: %s" , ghURL , resp . Status )
}
// extract
if _ , err = untar . Untar ( resp . Body , tmpDir ) ; err != nil {
return fmt . Errorf ( "faild to untar manifests from %s, error: %w" , ghURL , err )
}
return nil
}
func genInstallManifests ( version string , namespace string , components [ ] string , tmpDir string ) error {
func genInstallManifests ( version string , namespace string , components [ ] string , tmpDir string ) error {
model := struct {
model := struct {
Version string
Version string
@ -215,6 +251,10 @@ func genInstallManifests(version string, namespace string, components []string,
Components : components ,
Components : components ,
}
}
if err := downloadManifests ( version , tmpDir ) ; err != nil {
return err
}
if err := utils . execTemplate ( model , namespaceTmpl , path . Join ( tmpDir , "namespace.yaml" ) ) ; err != nil {
if err := utils . execTemplate ( model , namespaceTmpl , path . Join ( tmpDir , "namespace.yaml" ) ) ; err != nil {
return fmt . Errorf ( "generate namespace failed: %w" , err )
return fmt . Errorf ( "generate namespace failed: %w" , err )
}
}
@ -235,6 +275,10 @@ func genInstallManifests(version string, namespace string, components []string,
return fmt . Errorf ( "generate roles failed: %w" , err )
return fmt . Errorf ( "generate roles failed: %w" , err )
}
}
if err := utils . copyFile ( filepath . Join ( tmpDir , "rbac.yaml" ) , filepath . Join ( tmpDir , "roles/rbac.yaml" ) ) ; err != nil {
return fmt . Errorf ( "generate rbac failed: %w" , err )
}
return nil
return nil
}
}