Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect groupVersion when dynamically loading resource-type metadata from cluster API #139

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
36 changes: 23 additions & 13 deletions src/KubeClient/ApiMetadata/KubeApiMetadataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public sealed class KubeApiMetadataCache
/// </summary>
public static readonly IReadOnlyCollection<string> ApiGroupPrefixes = new string[] { "api", "apis" };

/// <summary>
/// The character used as a separator between a resource type's groupVersion and kind.
/// </summary>
const char ResourceTypeSeparator = ':';

/// <summary>
/// An object used to synchronise access to cache state.
/// </summary>
Expand Down Expand Up @@ -466,7 +471,7 @@ async Task<List<KubeApiMetadata>> LoadGroupApis(IKubeApiClient kubeClient, strin
}

apiMetadata.Add(
new KubeApiMetadata(kind, groupVersion.Version ?? groupVersion.GroupVersion, singularName, shortNames, isPreferredVersion, apiPaths)
new KubeApiMetadata(kind, groupVersion.GroupVersion ?? groupVersion.Version, singularName, shortNames, isPreferredVersion, apiPaths)
);
}

Expand Down Expand Up @@ -495,20 +500,25 @@ public string[] GetCacheKeys()
/// </returns>
public (string kind, string apiVersion)[] GetKnownResourceKinds()
{
List<(string kind, string apiVersion)> knownResourceKinds = new List<(string kind, string apiVersion)>();

lock (_stateLock)
{
return _metadata.Keys
.Where(
key => key.IndexOf('/') != -1
)
.Select(
key => key.Split('/')
)
.Select(
keyParts => (kind: keyParts[0], apiVersion: keyParts[1])
)
.ToArray();
foreach (string cacheKey in _metadata.Keys)
{
string[] keyComponents = cacheKey.Split(new char[] { ResourceTypeSeparator }, count: 2);

// Ignore aliases for resource types from preferred versions (i.e. ones without a groupVersion).
if (keyComponents.Length != 2)
continue;

knownResourceKinds.Add(
(kind: keyComponents[1], apiVersion: keyComponents[0])
);
}
}

return knownResourceKinds.ToArray();
}

/// <summary>
Expand Down Expand Up @@ -579,7 +589,7 @@ static string CreateCacheKey(string kind, string apiVersion)
if (String.IsNullOrWhiteSpace(apiVersion))
throw new ArgumentException("Argument cannot be null, empty, or entirely composed of whitespace: 'apiVersion'.", nameof(apiVersion));

return $"{apiVersion}/{kind}";
return $"{apiVersion}{ResourceTypeSeparator}{kind}";
}
}
}