@@ -26,6 +26,8 @@ import (
2626	"time" 
2727
2828	"github.com/spf13/cobra" 
29+ 	corev1 "k8s.io/api/core/v1" 
30+ 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 
2931
3032	"github.com/fluxcd/pkg/git" 
3133)
@@ -41,14 +43,17 @@ the bootstrap command will perform an upgrade if needed.`,
4143	Example : `  # Create a GitLab API token and export it as an env var 
4244  export GITLAB_TOKEN=<my-token> 
4345
44-   # Run bootstrap for a private repo owned by a GitLab group  
46+   # Run bootstrap for a private repo using HTTPS token authentication   
4547  gotk bootstrap gitlab --owner=<group> --repository=<repo name> 
4648
49+   # Run bootstrap for a private repo using SSH authentication 
50+   gotk bootstrap gitlab --owner=<group> --repository=<repo name> --ssh-hostname=gitlab.com 
51+ 
4752  # Run bootstrap for a repository path 
4853  gotk bootstrap gitlab --owner=<group> --repository=<repo name> --path=dev-cluster 
4954
5055  # Run bootstrap for a public repository on a personal account 
51-   gotk bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal=true   
56+   gotk bootstrap gitlab --owner=<user> --repository=<repo name> --private=false --personal=true 
5257
5358  # Run bootstrap for a private repo hosted on a GitLab server  
5459  gotk bootstrap gitlab --owner=<group> --repository=<repo name> --hostname=<domain> 
@@ -77,7 +82,7 @@ func init() {
7782	bootstrapGitLabCmd .Flags ().BoolVar (& glPrivate , "private" , true , "is private repository" )
7883	bootstrapGitLabCmd .Flags ().DurationVar (& glInterval , "interval" , time .Minute , "sync interval" )
7984	bootstrapGitLabCmd .Flags ().StringVar (& glHostname , "hostname" , git .GitLabDefaultHostname , "GitLab hostname" )
80- 	bootstrapGitLabCmd .Flags ().StringVar (& glSSHHostname , "ssh-hostname" , "" , "GitLab SSH hostname, defaults to hostname if not specified " )
85+ 	bootstrapGitLabCmd .Flags ().StringVar (& glSSHHostname , "ssh-hostname" , "" , "GitLab SSH hostname, when specified a deploy key will be added to the repository " )
8186	bootstrapGitLabCmd .Flags ().StringVar (& glPath , "path" , "" , "repository path, when specified the cluster sync will be scoped to this path" )
8287
8388	bootstrapCmd .AddCommand (bootstrapGitLabCmd )
@@ -172,34 +177,54 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error {
172177		logger .Successf ("install completed" )
173178	}
174179
175- 	// setup SSH deploy key 
176- 	if  shouldCreateDeployKey (ctx , kubeClient , namespace ) {
177- 		logger .Actionf ("configuring deploy key" )
178- 		u , err  :=  url .Parse (repository .GetSSH ())
179- 		if  err  !=  nil  {
180- 			return  fmt .Errorf ("git URL parse failed: %w" , err )
181- 		}
180+ 	repoURL  :=  repository .GetURL ()
182181
183- 		key , err  :=  generateDeployKey (ctx , kubeClient , u , namespace )
184- 		if  err  !=  nil  {
185- 			return  fmt .Errorf ("generating deploy key failed: %w" , err )
182+ 	if  glSSHHostname  !=  ""  {
183+ 		// setup SSH deploy key 
184+ 		repoURL  =  repository .GetSSH ()
185+ 		if  shouldCreateDeployKey (ctx , kubeClient , namespace ) {
186+ 			logger .Actionf ("configuring deploy key" )
187+ 			u , err  :=  url .Parse (repoURL )
188+ 			if  err  !=  nil  {
189+ 				return  fmt .Errorf ("git URL parse failed: %w" , err )
190+ 			}
191+ 
192+ 			key , err  :=  generateDeployKey (ctx , kubeClient , u , namespace )
193+ 			if  err  !=  nil  {
194+ 				return  fmt .Errorf ("generating deploy key failed: %w" , err )
195+ 			}
196+ 
197+ 			keyName  :=  "gotk" 
198+ 			if  glPath  !=  ""  {
199+ 				keyName  =  fmt .Sprintf ("gotk-%s" , glPath )
200+ 			}
201+ 
202+ 			if  changed , err  :=  provider .AddDeployKey (ctx , repository , key , keyName ); err  !=  nil  {
203+ 				return  err 
204+ 			} else  if  changed  {
205+ 				logger .Successf ("deploy key configured" )
206+ 			}
186207		}
187- 
188- 		keyName  :=  "gotk" 
189- 		if  glPath  !=  ""  {
190- 			keyName  =  fmt .Sprintf ("gotk-%s" , glPath )
208+ 	} else  {
209+ 		// setup HTTPS token auth 
210+ 		secret  :=  corev1.Secret {
211+ 			ObjectMeta : metav1.ObjectMeta {
212+ 				Name :      namespace ,
213+ 				Namespace : namespace ,
214+ 			},
215+ 			StringData : map [string ]string {
216+ 				"username" : "git" ,
217+ 				"password" : glToken ,
218+ 			},
191219		}
192- 
193- 		if  changed , err  :=  provider .AddDeployKey (ctx , repository , key , keyName ); err  !=  nil  {
220+ 		if  err  :=  upsertSecret (ctx , kubeClient , secret ); err  !=  nil  {
194221			return  err 
195- 		} else  if  changed  {
196- 			logger .Successf ("deploy key configured" )
197222		}
198223	}
199224
200225	// configure repo synchronization 
201226	logger .Actionf ("generating sync manifests" )
202- 	if  err  :=  generateSyncManifests (repository . GetSSH () , bootstrapBranch , namespace , namespace , glPath , tmpDir , glInterval ); err  !=  nil  {
227+ 	if  err  :=  generateSyncManifests (repoURL , bootstrapBranch , namespace , namespace , glPath , tmpDir , glInterval ); err  !=  nil  {
203228		return  err 
204229	}
205230
0 commit comments