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

🐞 Save host secret first #896

Merged
merged 1 commit into from
Feb 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface OnSaveHostParams {
}

/**
* Saves host data, including associated secrets, for a given set of host pairs.
* Saves hosts data, including associated secrets, for a given set of host+secret pairs.
* If a host already exists for a host pair, the host is updated with the new properties.
* Otherwise, a new host and secret are created.
*
Expand All @@ -44,13 +44,14 @@ export const onSaveHost = async ({
const hostNetwork = hostPair.inventory.networkAdapters.find(
({ name }) => name === network.name,
);
const encodedIpAddress = Base64.encode(hostNetwork.ipAddress);

if (!hostNetwork) {
throw new Error(`can't find network ${network.name} on host ${hostPair.host.metadata.name}`);
}

await processHostPair(
const encodedIpAddress = Base64.encode(hostNetwork.ipAddress);

await processHostSecretPair(
provider,
hostPair,
hostNetwork.ipAddress,
Expand All @@ -74,7 +75,7 @@ export const onSaveHost = async ({
* @param {string} encodedProvider - The Base64 encoded provider.
* @param {string} encodedIpAddress - The Base64 encoded IP address.
*/
async function processHostPair(
async function processHostSecretPair(
provider,
hostPair,
ipAddress,
Expand All @@ -86,13 +87,39 @@ async function processHostPair(
const { host, inventory }: { host: V1beta1Host; inventory: VSphereHost } = hostPair;

if (host?.metadata?.name) {
// Host already set, update network in the host and secret
const { name: secretName, namespace: secretNamespace } = host.spec.secret;

const secretData = await getSecret(secretName, secretNamespace);

await patchHost(host, ipAddress);
await patchSecret(secretData, encodedIpAddress, encodedUser, encodedPassword);
} else {
// Create a new host and secret pair

// Create a Secret
const secretData = {
kind: 'Secret',
apiVersion: 'v1',
metadata: {
generateName: `${provider.metadata.name}-${inventory.id}-`,
namespace: provider.metadata.namespace,
labels: {
createdForResourceType: 'hosts',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahadas is this the correct labels we need to set in the secret ?

createdForResource: inventory.id,
},
},
data: {
ip: encodedIpAddress,
password: encodedPassword,
provider: encodedProvider,
user: encodedUser,
},
type: 'Opaque',
};
const createdSecret = await createSecret(secretData);

// Create Host
const newHostData = {
apiVersion: 'forklift.konveyor.io/v1beta1',
kind: 'Host',
Expand All @@ -116,45 +143,20 @@ async function processHostPair(
name: provider.metadata.name,
namespace: provider.metadata.namespace,
},
secret: {},
},
};
const createdHost = await createHost(newHostData);

const secretData = {
kind: 'Secret',
apiVersion: 'v1',
metadata: {
generateName: `${provider.metadata.name}-${inventory.id}-`,
namespace: provider.metadata.namespace,
labels: {
createdForResourceType: 'hosts',
createdForResource: inventory.id,
secret: {
name: createdSecret.metadata.name,
namespace: createdSecret.metadata.namespace,
},
ownerReferences: [
{
apiVersion: 'forklift.konveyor.io/v1beta1',
kind: 'Host',
name: createdHost.metadata.name,
uid: createdHost.metadata.uid,
},
],
},
data: {
ip: encodedIpAddress,
password: encodedPassword,
provider: encodedProvider,
user: encodedUser,
},
type: 'Opaque',
};
const createdSecret = await createSecret(secretData);
const createdHost = await createHost(newHostData);

const secretRef = {
name: createdSecret.metadata.name,
namespace: createdSecret.metadata.namespace,
// Patch Secret owner Ref
const ownerRef = {
name: createdHost.metadata.name,
uid: createdHost.metadata.uid,
};
await patchHostSecret(createdHost, secretRef);
await patchHostSecret(createdSecret, ownerRef);
}
}

Expand Down Expand Up @@ -222,18 +224,22 @@ async function createHost(newHostData: V1beta1Host) {
return createdHost;
}

async function patchHostSecret(host: V1beta1Host, secretRef: { name: string; namespace: string }) {
async function patchHostSecret(secret: V1Secret, ownerRef: { name: string; uid: string }) {
await k8sPatch({
model: HostModel,
resource: host,
model: SecretModel,
resource: secret,
data: [
{
op: 'replace',
path: '/spec/secret',
value: {
name: secretRef.name,
namespace: secretRef.namespace,
},
path: '/metadata/ownerReferences',
value: [
{
apiVersion: 'forklift.konveyor.io/v1beta1',
kind: 'Host',
name: ownerRef.name,
uid: ownerRef.uid,
},
],
},
],
});
Expand Down
Loading