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

chore: add a warning comment to GetClientByKind() #1137

Merged
merged 1 commit into from
Oct 17, 2024
Merged
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
35 changes: 29 additions & 6 deletions kubernetes/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,23 @@ func NewKubeClient(client kubernetes.Interface, config *rest.Config) *DynamicCli
return &DynamicClient{config: config, client: client}
}

func (c *DynamicClient) FetchResources(ctx context.Context, resources ...unstructured.Unstructured) ([]unstructured.Unstructured, error) {
func (c *DynamicClient) FetchResources(
ctx context.Context,
resources ...unstructured.Unstructured,
) ([]unstructured.Unstructured, error) {
if len(resources) == 0 {
return nil, nil
}

eg, ctx := errgroup.WithContext(ctx)
var items = make(chan unstructured.Unstructured, len(resources))
items := make(chan unstructured.Unstructured, len(resources))
for i := range resources {
resource := resources[i]
client, err := c.GetClientByGroupVersionKind(resource.GroupVersionKind().Group, resource.GroupVersionKind().Version, resource.GetKind())
client, err := c.GetClientByGroupVersionKind(
resource.GroupVersionKind().Group,
resource.GroupVersionKind().Version,
resource.GetKind(),
)
if err != nil {
return nil, err
}
Expand All @@ -69,7 +76,9 @@ func (c *DynamicClient) FetchResources(ctx context.Context, resources ...unstruc
return output, nil
}

func (c *DynamicClient) GetClientByGroupVersionKind(group, version, kind string) (dynamic.NamespaceableResourceInterface, error) {
func (c *DynamicClient) GetClientByGroupVersionKind(
group, version, kind string,
) (dynamic.NamespaceableResourceInterface, error) {
dynamicClient, err := c.GetDynamicClient()
if err != nil {
return nil, err
Expand All @@ -94,6 +103,11 @@ func (c *DynamicClient) GetClientByGroupVersionKind(group, version, kind string)
return dynamicClient.Resource(mapping.Resource), nil
}

// WARN: "Kind" is not specific enough.
// A cluster can have various resources with the same Kind.
// example: helmchrats.helm.cattle.io & helmcharts.source.toolkit.fluxcd.io both have HelmChart as the kind.
//
// Use GetClientByGroupVersionKind instead.
func (c *DynamicClient) GetClientByKind(kind string) (dynamic.NamespaceableResourceInterface, error) {
dynamicClient, err := c.GetDynamicClient()
if err != nil {
Expand Down Expand Up @@ -136,15 +150,24 @@ func (c *DynamicClient) GetRestMapper() (meta.RESTMapper, error) {
host = strings.ReplaceAll(host, "-", "_")
host = strings.ReplaceAll(host, ":", "_")
cacheDir := os.ExpandEnv("$HOME/.kube/cache/discovery/" + host)
cache, err := disk.NewCachedDiscoveryClientForConfig(c.config, cacheDir, "", properties.Duration(10*time.Minute, "kubernetes.cache.timeout"))
cache, err := disk.NewCachedDiscoveryClientForConfig(
c.config,
cacheDir,
"",
properties.Duration(10*time.Minute, "kubernetes.cache.timeout"),
)
if err != nil {
return nil, err
}
c.restMapper = restmapper.NewDeferredDiscoveryRESTMapper(cache)
return c.restMapper, err
}

func (c *DynamicClient) ExecutePodf(ctx context.Context, namespace, pod, container string, command ...string) (string, string, error) {
func (c *DynamicClient) ExecutePodf(
ctx context.Context,
namespace, pod, container string,
command ...string,
) (string, string, error) {
const tty = false
req := c.client.CoreV1().RESTClient().Post().
Resource("pods").
Expand Down
Loading