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

Switch Dashboard to use new gRPC client throughout. #6044

Merged
merged 20 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Extend ResourceRef for custom resource ref used by operators to avoid
type error.

Signed-off-by: Michael Nelson <minelson@vmware.com>
  • Loading branch information
absoludity committed Mar 9, 2023
commit 64acc1e800ad4e107782966848627f783e4aaeb0
19 changes: 4 additions & 15 deletions dashboard/src/components/OperatorInstance/OperatorInstance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,11 @@ function parseResource(
if (crd.resources) {
crd.resources?.forEach(r => {
switch (r.kind) {
// So there is confusion here because, for the operator functionality,
// we have extra info on the resource ref (eg. filter for
// ownerreference), which we don't use for the normal, non-operator
// functionality. Yet, we're using the same components (ResourceTabs
// etc.) which expect the normal resource type. Hence
// IAppViewResourceRefs is defined with the normal type, but fromCRD
// returns the marked-up type.
// TODO: What if the internal ResourceRef extended the normal one? Could
// we then pass it happily? Or other option is to check whether the filter object
// owner reference is actually used (doesn't appear to be used)?
// CustomAppView appears to use `namespaced` in tests (but doesn't appear to need to)
// Similarly, Kube.test.ts appears to use `namespaced` unnecessarily.
// What about initial resources... where else is `namespaced` actually used?
// The test "updates the state with the CRD resources" fails if using normal
// resourcerefs here, because of the missing filter. But where is this filter used?
// TODO(minelson): Audit code to see if we can switch to use the normal ResourceRef
// here instead, then remove the shared/ResourceRef with the extra fields.
// https://github.com/vmware-tanzu/kubeapps/issues/6062
case "Deployment":
// eslint-disable-next-line @typescript-eslint/TS2322
result.deployments.push(fromCRD(r, kind, cluster, namespace, ownerRef));
break;
case "StatefulSet":
Expand Down
23 changes: 9 additions & 14 deletions dashboard/src/shared/ResourceRef.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,36 @@

import ResourceRef, { fromCRD } from "./ResourceRef";
import { IClusterServiceVersionCRDResource } from "./types";
import { ResourceRef as APIResourceRef } from "gen/kubeappsapis/core/packages/v1alpha1/packages_pb";

const clusterName = "cluster-name";

describe("ResourceRef", () => {
describe("constructor", () => {
it("returns a ResourceRef with the correct details", () => {
const apiRef = {
const ref = new ResourceRef(clusterName, "deployments", true, "releaseNamespace", {
apiVersion: "apps/v1",
kind: "Deployment",
name: "foo",
namespace: "bar",
} as APIResourceRef;

const ref = new ResourceRef(apiRef, clusterName, "deployments", true, "releaseNamespace");
});
expect(ref).toBeInstanceOf(ResourceRef);
expect(ref).toEqual({
cluster: clusterName,
apiVersion: apiRef.apiVersion,
kind: apiRef.kind,
name: apiRef.name,
apiVersion: "apps/v1",
kind: "Deployment",
name: "foo",
namespace: "bar",
namespaced: true,
plural: "deployments",
});
});

it("sets a default namespace if not in the resource", () => {
const r = {
const ref = new ResourceRef(clusterName, "deployments", true, "default", {
apiVersion: "apps/v1",
kind: "Deployment",
name: "foo",
} as APIResourceRef;

const ref = new ResourceRef(r, clusterName, "deployments", true, "default");
});
expect(ref.namespace).toBe("default");
});

Expand All @@ -62,7 +57,7 @@ describe("ResourceRef", () => {
expect(res).toMatchObject({
apiVersion: "apps/v1",
kind: "Deployment",
name: undefined,
name: "",
namespace: "default",
filter: { metadata: { ownerReferences: [ownerRef] } },
});
Expand All @@ -88,7 +83,7 @@ describe("ResourceRef", () => {
expect(res).toMatchObject({
apiVersion: "rbac.authorization.k8s.io/v1",
kind: "ClusterRole",
name: undefined,
name: "",
namespace: "",
filter: { metadata: { ownerReferences: [ownerRef] } },
});
Expand Down
31 changes: 9 additions & 22 deletions dashboard/src/shared/ResourceRef.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2019-2022 the Kubeapps contributors.
// SPDX-License-Identifier: Apache-2.0

import type { PartialMessage } from "@bufbuild/protobuf";
import { IClusterServiceVersionCRDResource, IKind } from "./types";
import { ResourceRef as APIResourceRef } from "gen/kubeappsapis/core/packages/v1alpha1/packages_pb";

Expand All @@ -11,11 +12,7 @@ export function fromCRD(
namespace: string,
ownerReference: any,
) {
const apiResourceRef = {
apiVersion: kind.apiVersion,
kind: r.kind,
} as APIResourceRef;
const ref = new ResourceRef(apiResourceRef, cluster, kind.plural, kind.namespaced, namespace);
const ref = new ResourceRef(cluster, kind.plural, kind.namespaced, namespace, { apiVersion: kind.apiVersion, kind: r.kind });
ref.filter = {
metadata: { ownerReferences: [ownerReference] },
};
Expand All @@ -29,32 +26,22 @@ export const keyForResourceRef = (r: APIResourceRef) =>

// ResourceRef defines a reference to a namespaced Kubernetes API Object and
// provides helpers to retrieve the resource URL
class ResourceRef {
class ResourceRef extends APIResourceRef {
public cluster: string;
public apiVersion: string;
public kind: string;
public plural: string;
public namespaced: boolean;
public name: string;
public namespace: string;
public filter: any;

// Creates a new ResourceRef instance from an existing IResource. Provide
// defaultNamespace to set if the IResource doesn't specify a namespace.
constructor(
apiRef: APIResourceRef,
cluster: string,
plural: string,
namespaced: boolean,
releaseNamespace: string,
) {
//constructor(data?: PartialMessage<ResourceRef>) {
constructor(cluster: string, plural: string, namespaced: boolean, releaseNamespace: string, data?: PartialMessage<APIResourceRef>) {
data = data || {};
data.namespace = namespaced ? data.namespace || releaseNamespace || "" : "";
super(data);
this.namespaced = namespaced;
this.cluster = cluster;
this.plural = plural;
this.apiVersion = apiRef.apiVersion;
this.kind = apiRef.kind;
this.name = apiRef.name;
this.namespace = namespaced ? apiRef.namespace || releaseNamespace || "" : "";
this.namespaced = namespaced;
return this;
}
}
Expand Down