Skip to content

Commit

Permalink
Switch dashboard's createDockerRegistrySecret to use resources API.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Nelson <minelson@vmware.com>
  • Loading branch information
absoludity committed Jan 4, 2022
1 parent fdb9861 commit fe69999
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 47 deletions.
28 changes: 22 additions & 6 deletions dashboard/src/actions/repos.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1214,17 +1214,33 @@ describe("fetchImagePullSecrets", () => {

describe("createDockerRegistrySecret", () => {
it("creates a docker registry", async () => {
const secret = {
type: "kubernetes.io/dockerconfigjson",
};
Secret.createPullSecret = jest.fn().mockReturnValue(secret);
Secret.createPullSecret = jest.fn();
const expectedActions = [
{
type: getType(repoActions.createImagePullSecret),
payload: secret,
payload: "secret-name",
},
];
await store.dispatch(repoActions.createDockerRegistrySecret("", "", "", "", "", ""));

await store.dispatch(
repoActions.createDockerRegistrySecret(
"secret-name",
"user",
"password",
"email",
"server",
"namespace",
),
);
expect(Secret.createPullSecret).toHaveBeenCalledWith(
"default",
"secret-name",
"user",
"password",
"email",
"server",
"namespace",
);
expect(store.getActions()).toEqual(expectedActions);
});

Expand Down
14 changes: 3 additions & 11 deletions dashboard/src/actions/repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const receiveImagePullSecrets = createAction("RECEIVE_IMAGE_PULL_SECRETS"
});

export const createImagePullSecret = createAction("CREATE_IMAGE_PULL_SECRET", resolve => {
return (secret: ISecret) => resolve(secret);
return (secretName: string) => resolve(secretName);
});

const allActions = [
Expand Down Expand Up @@ -433,16 +433,8 @@ export function createDockerRegistrySecret(
clusters: { currentCluster },
} = getState();
try {
const secret = await Secret.createPullSecret(
currentCluster,
name,
user,
password,
email,
server,
namespace,
);
dispatch(createImagePullSecret(secret));
await Secret.createPullSecret(currentCluster, name, user, password, email, server, namespace);
dispatch(createImagePullSecret(name));
return true;
} catch (e: any) {
dispatch(errorRepos(e, "fetch"));
Expand Down
62 changes: 42 additions & 20 deletions dashboard/src/shared/Secret.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { axiosWithAuth } from "./AxiosInstance";
import Secret from "./Secret";
import {
CreateSecretRequest,
CreateSecretResponse,
SecretType,
} from "gen/kubeappsapis/plugins/resources/v1alpha1/resources";
import { KubeappsGrpcClient } from "./KubeappsGrpcClient";

it("gets a secret", async () => {
axiosWithAuth.get = jest.fn().mockReturnValue({ data: "ok" });
Expand All @@ -17,28 +23,44 @@ it("lists secrets", async () => {
);
});

it("creates a pull secret", async () => {
axiosWithAuth.post = jest.fn().mockReturnValue({ data: "ok" });
const name = "repo-1";
const user = "foo";
const password = "pass";
const email = "foo@bar.com";
const server = "docker.io";
const namespace = "default";
expect(
await Secret.createPullSecret("default", name, user, password, email, server, namespace),
).toBe("ok");
expect(axiosWithAuth.post).toHaveBeenCalledWith(
"api/clusters/default/api/v1/namespaces/default/secrets",
{
apiVersion: "v1",
describe("createSecret", () => {
// Create a real client, but we'll stub out the function we're interested in.
const client = new KubeappsGrpcClient().getResourcesServiceClientImpl();
let mockClientCreateSecret: jest.MockedFunction<typeof client.CreateSecret>;
beforeEach(() => {
mockClientCreateSecret = jest
.fn()
.mockImplementation(() => Promise.resolve({} as CreateSecretResponse));

jest.spyOn(client, "CreateSecret").mockImplementation(mockClientCreateSecret);
jest.spyOn(Secret, "resourcesClient").mockImplementation(() => client);
});
afterEach(() => {
jest.restoreAllMocks();
});

it("creates a pull secret", async () => {
const cluster = "default";
const name = "repo-1";
const user = "foo";
const password = "pass";
const email = "foo@bar.com";
const server = "docker.io";
const namespace = "default";

await Secret.createPullSecret("default", name, user, password, email, server, namespace);

expect(mockClientCreateSecret).toHaveBeenCalledWith({
context: {
cluster,
namespace,
},
name,
stringData: {
".dockerconfigjson":
'{"auths":{"docker.io":{"username":"foo","password":"pass","email":"foo@bar.com","auth":"Zm9vOnBhc3M="}}}',
},
kind: "Secret",
metadata: { name: "repo-1" },
type: "kubernetes.io/dockerconfigjson",
},
);
type: SecretType.SECRET_TYPE_DOCKER_CONFIG_JSON,
} as CreateSecretRequest);
});
});
24 changes: 14 additions & 10 deletions dashboard/src/shared/Secret.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { axiosWithAuth } from "./AxiosInstance";
import { IK8sList, ISecret } from "./types";
import * as url from "./url";
import { KubeappsGrpcClient } from "./KubeappsGrpcClient";
import {
CreateSecretRequest,
SecretType,
} from "gen/kubeappsapis/plugins/resources/v1alpha1/resources";

export default class Secret {
public static resourcesClient = () => new KubeappsGrpcClient().getResourcesServiceClientImpl();
public static async get(cluster: string, namespace: string, name: string) {
const u = url.api.k8s.secret(cluster, namespace, name);
const { data } = await axiosWithAuth.get<ISecret>(u);
Expand All @@ -24,7 +30,6 @@ export default class Secret {
server: string,
namespace: string,
) {
const u = url.api.k8s.secrets(cluster, namespace);
const dockercfg = {
auths: {
[server]: {
Expand All @@ -35,17 +40,16 @@ export default class Secret {
},
},
};
const { data } = await axiosWithAuth.post<ISecret>(u, {
apiVersion: "v1",
await this.resourcesClient().CreateSecret({
context: {
cluster,
namespace,
},
name,
type: SecretType.SECRET_TYPE_DOCKER_CONFIG_JSON,
stringData: {
".dockerconfigjson": JSON.stringify(dockercfg),
},
kind: "Secret",
metadata: {
name,
},
type: "kubernetes.io/dockerconfigjson",
});
return data;
} as CreateSecretRequest);
}
}

0 comments on commit fe69999

Please sign in to comment.