garm-provider-edge-connect/internal/spec/spec.go

113 lines
2.2 KiB
Go
Raw Normal View History

package spec
import (
"fmt"
"net/url"
"strings"
"github.com/cloudbase/garm-provider-common/params"
corev1 "k8s.io/api/core/v1"
)
type GitHubScopeDetails struct {
BaseURL string
Repo string
Org string
Enterprise string
}
func ExtractGitHubScopeDetails(gitRepoURL string) (GitHubScopeDetails, error) {
if gitRepoURL == "" {
return GitHubScopeDetails{}, fmt.Errorf("no gitRepoURL supplied")
}
u, err := url.Parse(gitRepoURL)
if err != nil {
return GitHubScopeDetails{}, fmt.Errorf("invalid URL: %w", err)
}
if u.Scheme == "" || u.Host == "" {
return GitHubScopeDetails{}, fmt.Errorf("invalid URL: %s", gitRepoURL)
}
pathParts := strings.Split(strings.Trim(u.Path, "/"), "/")
scope := GitHubScopeDetails{
BaseURL: u.Scheme + "://" + u.Host,
}
switch {
case len(pathParts) == 1:
scope.Org = pathParts[0]
case len(pathParts) == 2 && pathParts[0] == "enterprises":
scope.Enterprise = pathParts[1]
case len(pathParts) == 2:
scope.Org = pathParts[0]
scope.Repo = pathParts[1]
default:
return GitHubScopeDetails{}, fmt.Errorf("URL does not match the expected patterns")
}
return scope, nil
}
func GetRunnerEnvs(gitHubScope GitHubScopeDetails, bootstrapParams params.BootstrapInstance) []corev1.EnvVar {
return []corev1.EnvVar{
{
2025-09-09 14:42:39 +02:00
Name: "RUNNER_GITEA_INSTANCE",
Value: bootstrapParams.RepoURL,
},
{
Name: "RUNNER_GROUP",
Value: bootstrapParams.GitHubRunnerGroup,
},
{
Name: "RUNNER_NAME",
Value: bootstrapParams.Name,
},
{
Name: "RUNNER_LABELS",
Value: strings.Join(bootstrapParams.Labels, ","),
},
{
Name: "RUNNER_NO_DEFAULT_LABELS",
Value: "true",
},
{
Name: "DISABLE_RUNNER_UPDATE",
Value: "true",
},
{
Name: "RUNNER_WORKDIR",
Value: "/runner/_work/",
},
{
Name: "GITHUB_URL",
Value: gitHubScope.BaseURL,
},
{
Name: "RUNNER_EPHEMERAL",
Value: "true",
},
{
Name: "RUNNER_TOKEN",
Value: "dummy",
},
{
Name: "METADATA_URL",
Value: bootstrapParams.MetadataURL,
},
{
Name: "BEARER_TOKEN",
Value: bootstrapParams.InstanceToken,
},
{
Name: "CALLBACK_URL",
Value: bootstrapParams.CallbackURL,
},
{
Name: "JIT_CONFIG_ENABLED",
Value: fmt.Sprintf("%t", bootstrapParams.JitConfigEnabled),
},
}
}