55 "errors"
66 "fmt"
77 "net/url"
8+ "strings"
89 "time"
910
1011 synv1alpha1 "github.com/projectsyn/lieutenant-operator/api/v1alpha1"
@@ -23,15 +24,15 @@ const (
2324 SecretHostKeysName = "hostKeys"
2425 // SecretEndpointName is the name of the secret entry containing the api endpoint
2526 SecretEndpointName = "endpoint"
27+ // SecretSSHEndpointName is the name of the secret entry containing the ssh endpoint (optional)
28+ SecretSSHEndpointName = "sshEndpoint"
2629 // DeletionMagicString defines when a file should be deleted from the repository
27- //TODO it will be replaced with something better in the future TODO
30+ // TODO it will be replaced with something better in the future TODO
2831 DeletionMagicString = "{delete}"
2932)
3033
31- var (
32- // implementations holds each a copy of the registered Git implementation
33- implementations []Implementation
34- )
34+ // implementations holds each a copy of the registered Git implementation
35+ var implementations []Implementation
3536
3637// Register adds a type to the list of supported Git implementations.
3738func Register (i Implementation ) {
@@ -40,7 +41,6 @@ func Register(i Implementation) {
4041
4142// NewRepo returns a Repo object that can handle the specific URL
4243func NewRepo (opts RepoOptions ) (Repo , error ) {
43-
4444 for _ , imp := range implementations {
4545 if exists , err := imp .IsType (opts .URL ); exists {
4646 newImp , err := imp .New (opts )
@@ -66,6 +66,7 @@ type RepoOptions struct {
6666 DeployKeys map [string ]synv1alpha1.DeployKey
6767 Logger logr.Logger
6868 URL * url.URL
69+ SSHHost string
6970 Path string
7071 RepoName string
7172 DisplayName string
@@ -212,11 +213,19 @@ func GetGitClient(ctx context.Context, instance *synv1alpha1.GitRepoTemplate, na
212213 }
213214
214215 repoURL , err := url .Parse (string (secret .Data [SecretEndpointName ]) + "/" + instance .Path + "/" + instance .RepoName )
215-
216216 if err != nil {
217217 return nil , "" , err
218218 }
219219
220+ sshHost := ""
221+ if raw , ok := secret .Data [SecretSSHEndpointName ]; ok {
222+ parsed , err := parseSSHEndpoint (string (raw ))
223+ if err != nil {
224+ return nil , "" , fmt .Errorf ("invalid ssh endpoint in secret %s: %w" , secret .GetName (), err )
225+ }
226+ sshHost = parsed
227+ }
228+
220229 repoOptions := RepoOptions {
221230 Credentials : Credentials {
222231 Token : string (secret .Data [SecretTokenName ]),
@@ -227,6 +236,7 @@ func GetGitClient(ctx context.Context, instance *synv1alpha1.GitRepoTemplate, na
227236 RepoName : instance .RepoName ,
228237 DisplayName : instance .DisplayName ,
229238 URL : repoURL ,
239+ SSHHost : sshHost ,
230240 TemplateFiles : instance .TemplateFiles ,
231241 DeletionPolicy : instance .DeletionPolicy ,
232242 }
@@ -239,5 +249,39 @@ func GetGitClient(ctx context.Context, instance *synv1alpha1.GitRepoTemplate, na
239249 err = repo .Connect ()
240250
241251 return repo , hostKeysString , err
252+ }
253+
254+ func parseSSHEndpoint (raw string ) (string , error ) {
255+ trimmed := strings .TrimSpace (raw )
256+ if trimmed == "" {
257+ return "" , nil
258+ }
259+
260+ parsed , err := url .Parse (trimmed )
261+ if err != nil {
262+ return "" , err
263+ }
264+
265+ if parsed .Host != "" {
266+ return parsed .Host , nil
267+ }
268+
269+ host := strings .TrimSpace (parsed .Path )
270+ if host == "" {
271+ return "" , fmt .Errorf ("ssh endpoint has no host" )
272+ }
273+
274+ if at := strings .LastIndex (host , "@" ); at >= 0 {
275+ host = host [at + 1 :]
276+ }
277+
278+ if slash := strings .Index (host , "/" ); slash >= 0 {
279+ host = host [:slash ]
280+ }
281+
282+ if host == "" {
283+ return "" , fmt .Errorf ("ssh endpoint has no host" )
284+ }
242285
286+ return host , nil
243287}
0 commit comments