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 @@ -14,7 +14,6 @@ package com.redhat.devtools.gateway.auth.tls
import com.redhat.devtools.gateway.kubeconfig.BlockStyleFilePersister
import com.redhat.devtools.gateway.kubeconfig.KubeConfigNamedCluster
import com.redhat.devtools.gateway.kubeconfig.KubeConfigUtils
import com.redhat.devtools.gateway.kubeconfig.KubeConfigUtils.path
import com.redhat.devtools.gateway.openshift.Utils
import java.security.cert.X509Certificate

Expand All @@ -33,14 +32,14 @@ object KubeConfigTlsWriter {
)

// Find the kubeconfig that actually contains this cluster
val config = allConfigs.firstOrNull { kubeConfig ->
kubeConfig.clusters?.any { entry ->
val configFile = allConfigs.firstOrNull { configFile ->
configFile.config.clusters?.any { entry ->
val map = entry as? Map<*, *> ?: return@any false
map["name"] == namedCluster.name
} == true
} ?: return

val clusterEntry = config.clusters
val clusterEntry = configFile.config.clusters
?.firstOrNull { entry ->
val map = entry as? Map<*, *> ?: return@firstOrNull false
map["name"] == namedCluster.name
Expand All @@ -59,14 +58,14 @@ object KubeConfigTlsWriter {
removeInsecureSkipTlsVerify(clusterEntry)

// Persist
val file = config.path?.toFile() ?: return
val file = configFile.path.toFile()
val persister = BlockStyleFilePersister(file)
persister.save(
config.contexts,
config.clusters,
config.users,
config.preferences,
config.currentContext
configFile.config.contexts,
configFile.config.clusters,
configFile.config.users,
configFile.config.preferences,
configFile.config.currentContext
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class FileWatcher(
while (isActive) {
val key = watchService.poll(100, TimeUnit.MILLISECONDS)
if (key == null) {
retryUnregisteredParents()
@Suppress("ConvertLongToDuration")
delay(100)
continue
Expand Down Expand Up @@ -78,11 +79,9 @@ class FileWatcher(
* @return this instance for chaining.
*/
fun addFile(path: Path): FileWatcher {
val parentDir = path.parent
if (parentDir != null
&& !monitoredFiles.contains(path)) {
registerDirectory(parentDir)
monitoredFiles.add(path)
val parentDir = path.parent ?: return this
registerDirectory(parentDir)
if (monitoredFiles.add(path)) {
invokeOnFileChanged(path)
}
return this
Expand Down Expand Up @@ -113,10 +112,33 @@ class FileWatcher(
}
}

/**
* Re-registers parent directories of monitored files that were missing when [addFile]
* was called. Once such a directory exists, it is registered for watching and the
* [onFileChanged] callback is invoked for each monitored file in it, so listeners are
* notified without the caller having to re-add the files.
*/
private fun retryUnregisteredParents() {
monitoredFiles
.mapNotNull { it.parent }
.distinct()
.filter { it !in registeredDirectories.values }
.filter { Files.isDirectory(it) }
.forEach { directory ->
registerDirectory(directory)
monitoredFiles
.filter { it.parent == directory }
.forEach { file -> invokeOnFileChanged(file) }
}
}

private fun registerDirectory(directory: Path) {
if (registeredDirectories.values.any { it == directory }) {
return
}
if (!Files.isDirectory(directory)) {
return
}
val watchKey = directory.register(
watchService,
StandardWatchEventKinds.ENTRY_CREATE,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2026 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package com.redhat.devtools.gateway.kubeconfig

import io.kubernetes.client.util.KubeConfig
import java.nio.file.Path

class KubeConfigFile(
val config: KubeConfig,
val path: Path
)

Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ class KubeConfigMonitor(
}

private fun startWatchingNew(newPaths: Set<Path>) {
(newPaths - fileWatcher.getMonitoredFiles()).forEach { path ->
newPaths.forEach { path ->
val alreadyMonitored = fileWatcher.getMonitoredFiles().contains(path)
fileWatcher.addFile(path)
logger.info("Started monitoring kubeconfig file: $path")
if (!alreadyMonitored) {
logger.info("Started monitoring kubeconfig file: $path")
}
}
}

Expand Down
Loading
Loading