Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GitHandler {
config.scm.scmManager.internal = false
config.scm.scmManager.urlForJenkins = config.scm.scmManager.url
} else {
log.debug("Setting configs for internal SCM-Manager")
log.debug('Setting configs for internal SCM-Manager')

config.scm.scmManager.internal = true
config.scm.scmManager.urlForJenkins = "http://scmm.${config.application.namePrefix}${config.scm.scmManager.namespace}.svc.cluster.local/scm"
Expand Down Expand Up @@ -73,19 +73,19 @@ class GitHandler {
return tenant
}

throw new IllegalStateException("No SCM provider found.")
throw new IllegalStateException('No SCM provider found.')
}

private GitProvider createTenantScmProvider() {
switch (config.scm.scmProviderType) {
case ScmProviderType.GITLAB:
return new GitlabProvider(config, config.scm.gitlab)

case ScmProviderType.SCM_MANAGER:
return new ScmManagerProvider(config,
config.scm.scmManager,
k8sClient,
networkingUtils)
networkingUtils,
config.application.namePrefix ?: '')

default:
throw new IllegalArgumentException("Unsupported SCM provider found in TenantSCM: ${config.scm.scmProviderType}")
Expand All @@ -96,36 +96,32 @@ class GitHandler {
switch (config.multiTenant.scmProviderType) {
case ScmProviderType.GITLAB:
return new GitlabProvider(config, config.multiTenant.gitlab)

case ScmProviderType.SCM_MANAGER:
return new ScmManagerProvider(config,
config.multiTenant.scmManager,
k8sClient,
networkingUtils)
networkingUtils,
centralScmManagerServicePrefix())

default:
throw new IllegalArgumentException("Unsupported SCM-Central provider: ${config.multiTenant.scmProviderType}")
}
}

private void setupExternalRepositoriesIfPossible() {
final String namePrefix = (config.application.namePrefix ?: "").trim()
final String namePrefix = (config.application.namePrefix ?: '').trim()
final boolean repositorySetupBlockedByInternalScmBootstrap = isRepositorySetupBlockedByInternalScmBootstrap()

log.info(
"Evaluating repository setup: centralConfigured={}, tenantConfigured={}, namePrefix='{}', repositorySetupBlockedByInternalScmBootstrap={}",
log.info("Evaluating repository setup: centralConfigured={}, tenantConfigured={}, namePrefix='{}', repositorySetupBlockedByInternalScmBootstrap={}",
central != null,
tenant != null,
namePrefix,
repositorySetupBlockedByInternalScmBootstrap
)
repositorySetupBlockedByInternalScmBootstrap)

if (repositorySetupBlockedByInternalScmBootstrap) {
log.info(
"Skipping repository setup because the configured internal SCM-Manager is not deployed yet. " +
"Repository setup can continue immediately when an external SCM-Manager is configured. namePrefix='{}'",
namePrefix
)
log.info('Skipping repository setup because the configured internal SCM-Manager is not deployed yet. ' +
"Repository setup can continue immediately when an external SCM-Manager is configured. namePrefix='{}'",
namePrefix)
return
}

Expand All @@ -138,13 +134,14 @@ class GitHandler {
setupRepos(tenant, namePrefix)
}
}

private boolean isRepositorySetupBlockedByInternalScmBootstrap() {
config.scm.scmProviderType == ScmProviderType.SCM_MANAGER && config.scm.scmManager?.internal
return config.scm.scmProviderType == ScmProviderType.SCM_MANAGER && config.scm.scmManager?.internal
}

static void setupRepos(GitProvider gitProvider, String namePrefix = "") {
gitProvider.createRepository(withOrgPrefix(namePrefix, "argocd/cluster-resources"),
"GitOps repo for basic cluster-resources")
static void setupRepos(GitProvider gitProvider, String namePrefix = '') {
gitProvider.createRepository(withOrgPrefix(namePrefix, 'argocd/cluster-resources'),
'GitOps repo for basic cluster-resources')
}

static String withOrgPrefix(String prefix, String repoPath) {
Expand All @@ -154,4 +151,16 @@ class GitHandler {

return prefix + repoPath
}


private String centralScmManagerServicePrefix() {
def namespace = (config.multiTenant.scmManager.namespace ?: '').strip()
def baseNamespace = 'scm-manager'

if (namespace == baseNamespace || !namespace.endsWith(baseNamespace)) {
return ''
}

return namespace.substring(0, namespace.length() - baseNamespace.length())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,6 @@ interface GitProvider {

URI prometheusMetricsEndpoint()

/**
* Deletes the given repository on the provider, if supported.
* Note: This capability is not used by the current destruction flow,
* which talks directly to provider-specific clients (e.g. ScmManagerApiClient).*/
void deleteRepository(String namespace, String repository, boolean prefixNamespace)

/**
* Deletes a user account on the provider, if supported.
* Note: Not used by the current destruction flow; kept as an optional capability
* on the GitProvider abstraction */
void deleteUser(String name)

/**
* Sets the default branch of a repository, if supported by the provider;
* kept as an optional capability on the GitProvider abstraction */
void setDefaultBranch(String repoTarget, String branch)

String getUrl()

String getProtocol()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,6 @@ class GitlabProvider implements GitProvider {
return null
}

/**
* No-op by design. GitLab repository deletion is not managed through this abstraction.
* Kept for interface compatibility only.*/
@Override
void deleteRepository(String namespace, String repository, boolean prefixNamespace) {
// intentionally left blank
}

/**
* No-op by design. User deletion is not supported or handled through this provider.
* Kept for interface compatibility only.*/
@Override
void deleteUser(String name) {
// intentionally left blank
}

/**
* No-op by design. Default branch management is not implemented via this abstraction.
* Kept for interface compatibility only.*/
@Override
void setDefaultBranch(String repoTarget, String branch) {
// intentionally left blank
}

private Group parentGroup() {
String raw = gitlabConfig?.parentGroupId?.trim()
if (!raw) throw new IllegalArgumentException("--gitlab-group-id is required")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ class ScmManagerProvider implements GitProvider {
Config config

ScmManagerProvider(Config config,
ScmManagerConfig scmmConfig,
K8sClient k8sClient,
NetworkingUtils networkingUtils) {
ScmManagerConfig scmmConfig,
K8sClient k8sClient,
NetworkingUtils networkingUtils,
String servicePrefix = '') {
this.scmmConfig = scmmConfig
this.config = config
this.k8sClient = k8sClient
Expand All @@ -39,7 +40,8 @@ class ScmManagerProvider implements GitProvider {
this.urls = new ScmManagerUrlResolver(this.config,
this.scmmConfig,
this.k8sClient,
this.networkingUtils)
this.networkingUtils,
servicePrefix)
}

ScmManagerApiClient getApiClient() {
Expand All @@ -56,7 +58,7 @@ class ScmManagerProvider implements GitProvider {
boolean createRepository(String repoTarget, String description, boolean initialize = true) {
def repoNamespace = repoTarget.split('/', 2)[0]
def repoName = repoTarget.split('/', 2)[1]
def repo = new Repository(repoNamespace, repoName, description ?: "")
def repo = new Repository(repoNamespace, repoName, description ?: '')

Response<Void> response = getApiClient().repositoryApi().create(repo, initialize).execute()
return handle201or409(response, "Repository ${repoNamespace}/${repoName}")
Expand Down Expand Up @@ -125,21 +127,6 @@ class ScmManagerProvider implements GitProvider {
return urls.prometheusEndpoint()
}

@Override
void deleteRepository(String namespace, String repository, boolean prefixNamespace) {
// intentionally left blank
}

@Override
void deleteUser(String name) {
// intentionally left blank
}

@Override
void setDefaultBranch(String repoTarget, String branch) {
// intentionally left blank
}

private static Permission.Role mapToScmManager(AccessRole role) {
switch (role) {
case AccessRole.READ:
Expand All @@ -158,32 +145,17 @@ class ScmManagerProvider implements GitProvider {
}
}

private static boolean handle201or409(Response<?> response, String what) {
int code = response.code()

if (code == 409) {
log.debug("${what} already exists - ignoring HTTP 409")
return false
private static boolean handle201or409(Response<Void> response, String resourceName) {
if (response.code() == 201) {
log.debug("${resourceName} created successfully")
return true
}

if (code != 201) {
throw new RuntimeException("Could not create ${what}. HTTP Details: ${response.code()} ${response.message()}: ${response.errorBody()?.string()}")
if (response.code() == 409) {
log.debug("${resourceName} already exists")
return false
}

return true
}

/**
* Test-only constructor.*/
ScmManagerProvider(Config config,
ScmManagerConfig scmmConfig,
ScmManagerUrlResolver urls,
ScmManagerApiClient apiClient) {
this.scmmConfig = Objects.requireNonNull(scmmConfig, "scmmConfig must not be null")
this.config = Objects.requireNonNull(config, "config must not be null")
this.urls = Objects.requireNonNull(urls, "urls must not be null")
this.apiClient = apiClient ?: new ScmManagerApiClient(urls.clientApiBase().toString(),
scmmConfig.credentials,
config.application.insecure)
throw new RuntimeException("Failed to create ${resourceName}. HTTP Status: ${response.code()} - ${response.message()}")
}
}
Loading