Skip to content

Commit

Permalink
support for generateName (redhat-developer#549)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jan 10, 2023
1 parent b00defe commit 066a07a
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import io.fabric8.kubernetes.client.Watcher
import io.fabric8.kubernetes.client.WatcherException
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation
import io.fabric8.kubernetes.client.dsl.Resource
import io.fabric8.kubernetes.client.dsl.VisitFromServerGetWatchDeleteRecreateWaitApplicable
import io.fabric8.kubernetes.client.dsl.base.ResourceDefinitionContext
import io.fabric8.kubernetes.client.utils.ApiVersionUtil
import io.fabric8.kubernetes.client.utils.Serialization
Expand All @@ -52,9 +53,7 @@ class NonCachingSingleResourceOperator(
*/
fun get(resource: HasMetadata): HasMetadata? {
return if (hasName(resource)) {
val genericKubernetesResource = toGenericKubernetesResource(resource)
val op = createOperation(resource)
op.withName(genericKubernetesResource.metadata.name)
createOperation(resource)
.fromServer()
.get()
} else {
Expand All @@ -78,13 +77,14 @@ class NonCachingSingleResourceOperator(
* @return the resource that was created
*/
fun replace(resource: HasMetadata): HasMetadata? {
val genericKubernetesResource = toGenericKubernetesResource(resource)
val op = createOperation(resource)

return if (hasName(genericKubernetesResource)) {
op.createOrReplace(genericKubernetesResource)
} else if (hasGenerateName(genericKubernetesResource)) {
op.create(genericKubernetesResource)
return if (hasName(resource)) {
createOperation(resource)
.createOrReplace()
} else if (hasGenerateName(resource)) {
val genericKubernetesResource = toGenericKubernetesResource(resource)
val created = createGenericResourceOperation(genericKubernetesResource)
.create(genericKubernetesResource)
toTypedResource(created)
} else {
throw ResourceException("Could not replace ${resource.kind ?: "resource"}: has neither name nor generateName.")
}
Expand All @@ -103,13 +103,28 @@ class NonCachingSingleResourceOperator(
val genericKubernetesResource = toGenericKubernetesResource(resource)
val op = createOperation(genericKubernetesResource)
return if (hasName(genericKubernetesResource)) {
op.watch(GenericKubernetesResourceWatcherAdapter(watcher))
op.watch(watcher)
} else {
null
}
}

private fun createOperation(resource: HasMetadata): NonNamespaceOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>> {
private fun createOperation(resource: HasMetadata): VisitFromServerGetWatchDeleteRecreateWaitApplicable<HasMetadata> {
val context = createResourceDefinitionContext(resource)
val inNamespace = resourceOrClientNamespace(resource, client)
return if (context.isNamespaceScoped
&& true == inNamespace?.isNotEmpty()
) {
client.get()
.resource(resource)
.inNamespace(inNamespace)
} else {
client.get()
.resource(resource)
}
}

private fun createGenericResourceOperation(resource: HasMetadata): NonNamespaceOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resource<GenericKubernetesResource>> {
val context = createResourceDefinitionContext(resource)
val inNamespace = resourceOrClientNamespace(resource, client)
return if (context.isNamespaceScoped
Expand Down Expand Up @@ -157,8 +172,24 @@ class NonCachingSingleResourceOperator(
return if (resource is GenericKubernetesResource) {
resource
} else {
val yaml = Serialization.asYaml(resource)
Serialization.unmarshal(yaml, GenericKubernetesResource::class.java)
val json = Serialization.asJson(resource)
Serialization.unmarshal(json, GenericKubernetesResource::class.java)
}
}

/**
* Returns a resource in the requested type for a given [GenericKubernetesResource].
* The given resource is returned as if it is a subclass of [HasMetadata].
*
* @param resource a [GenericKubernetesResource] to convert
* @return a subclass of [HasMetadata]
*/
private fun <T: HasMetadata> toTypedResource(resource: GenericKubernetesResource?): T? {
return if (resource == null) {
null
} else {
val json = Serialization.asJson(resource)
Serialization.unmarshal(json)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.kubernetes.model.resource

import io.fabric8.kubernetes.api.model.HasMetadata

class ResourceIdentifier(val name: String?, val generateName: String?, val namespace: String, private val resourceKind: ResourceKind<*>) {
constructor(resource: HasMetadata) : this(
resource.metadata.name,
resource.metadata.generateName,
resource.metadata.namespace,
ResourceKind.create(resource)
)
val kind: String
get() {
return resourceKind.kind
}
val version: String
get() {
return resourceKind.version
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ResourceIdentifier

if (name != other.name) return false
if (generateName != other.generateName) return false
if (namespace != other.namespace) return false
if (kind != other.kind) return false
if (version != other.version) return false

return true
}

override fun hashCode(): Int {
var result = name.hashCode()
result = 31 * result + namespace.hashCode()
result = 31 * result + kind.hashCode()
result = 31 * result + version.hashCode()
return result
}

}

0 comments on commit 066a07a

Please sign in to comment.