Created
August 18, 2020 23:09
-
-
Save lblackstone/d2d1c29507685c0833612988945dedf3 to your computer and use it in GitHub Desktop.
Use streamInvoke to watch k8s Event stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as k8s from "@pulumi/kubernetes"; | |
import { streamInvoke } from "@pulumi/pulumi/runtime"; | |
import * as pulumi from "@pulumi/pulumi"; | |
// Install the sealed secret controller. | |
new k8s.yaml.ConfigFile("sealed-secret-controller", { | |
file: "https://github.com/bitnami-labs/sealed-secrets/releases/download/v0.12.4/controller.yaml", | |
}) | |
// Create a SealedSecret. | |
const sealedSecret = new k8s.apiextensions.CustomResource("sealed-secret", { | |
apiVersion: "bitnami.com/v1alpha1", | |
kind: "SealedSecret", | |
metadata: { name: "mysecret" } , | |
"spec": { | |
"template": { | |
"metadata": { | |
"name": "mysecret", | |
"namespace": "default", | |
"annotations": { | |
"secret": "supersecret" | |
} | |
}, | |
"type": "Opaque" | |
}, | |
"encryptedData": { | |
"password": "AgCAnSQ6fo/dxRqnH+EkW7/AQo2u8Iu2sSJbGrsv7UAWyTlSzR+8zFFghcohfGsUxgHeFsLMoAPZ16XzKV8T9Ig9MLd8So/lzM7glNhnfTzVOunAjq5/xH5UaxWgv7Giw0A4Mg4RjBcsc6tmDScCZ1b1s348R8BWCC37u0Q+2dv3UXK8QWfJg5hJKX8gfX8EEd2ss6uOanU97BjXdti0bNtLD2LTOKleB12o4JMmFljgIhZnn2RIr4MmYMZKSUKJbyuyHLY+VNdSu6fBZWT6tc8buRr5JwqsS6k6VJsK0sFw9b+OJEnmghJ1r9CdzIB3afMcBSDoXDCvuklVaJqQsHRhVePmnaeEXvHcR9GQPpJ2mUJ1jdqWSk7pjonEK5aMxyQXZFHFr6OUzH0NOC3lNOx9cyf9VmD57IW/aXwQfYzUlwmJs0PnosOvprY5pd/L+tIFh9M1EdvGHsSaRI5EL1Ok8IHEFtUsHM77cvexTnARfH0WiMrrXKNYYkwZu0cDZnPaXXSREDAn7btXnray/CHOPfpqNH4E/Ngr1/KZ0KR9GbOhCENkRxdSSp2r05mns76PQJzMXmuAFJD4Q8sBFutaoGXfwtdrwl7NghOmpuwN151rU/fzXrmxucyHb+lmVKnBYTgtSmWXjhF1rxk0nTsCQozGtyMdZRZaES2dbTLyDN2+ym8xJkyDQ+dEP9LqKtJWt0H2deb7/am4yg==", | |
"username": "AgAaFV0nGaB5Dzao3fNfQElJzLtZGPcgT69rUAEujYUNuWknkAwzcTwWcAgn8FYvFhR6MpC5FN7u9FQ6BcT3t68K/Ex/5o/rSqolbdiG4PY5WEYkVniMoUfER27uC8dlTAdr2Qf91F1vE6JjUxkto7IUxWRObUCS61M5kZajhDQyU0yn4RCJoE5/eytlztZ9hIH/jBDrphZRSOvFD09DBjngUQyZ/55EIEBX8AVuizR9u3XSL7fsym+eDwLSfJixsaY+nYloSjbN0txEhSUAlXW8RgaOq7lRCMet4KcrzqXh+5JAQS/x5eTI2oYUZCle6fdImF4O/pCyAgpNGMZrhPwTfIkh5eugCdsjfKuH1fD/hw/vA1hwsiXGlrkPhbPZ0zhsPfHIeTpAmZiYz5gahncjb8/GojTOTVs3iZ9OVrY7teRx78gVtlA9b61KGs9oMmUPN2tCzWjbRVkgFaKy10CXbVESydzwDUVFdppu5L8kbDTJBKujXZA0PP/Omy/KN3ro/cGurAtZ3edrzCvq+DYO0Hhvk/Dx3VHkjou8uR0cAVXeRkKbuH4AdzVDT3kWqhWAwI5Mj5R4as6uHRB5YGUYy+0KcHtL+NjvnnXqR0uqFQUahoHiaFP1CE2u9nUJ2X+03gigw9OMhYzrjpfdBj/PqFlNoHakyy7HLnJ76hfBETLT/XExYEd9xqdm1J44Y+K5jg/gj33ZFg==" | |
} | |
} | |
}) | |
// Watch specified k8s resource stream for updates and invoke a user-provided callback when an update occurs. | |
async function watch<T>(group: string, version: string, kind: string, callback: (type: string, object: any) => Promise<T | undefined>): Promise<T> { | |
const events = await streamInvoke("kubernetes:kubernetes:watch", { | |
group, version, kind, | |
}); | |
try { | |
for await(const {type, object} of events) { | |
const v = await callback(type, object); | |
if (v !== undefined) { | |
return v; | |
} | |
} | |
throw new Error("no result!"); | |
} finally { | |
events.cancel(); | |
} | |
} | |
// Watch for updates to v1/Secrets and return the id of the Secret if it matches the provided name. | |
const secretName = pulumi.all([sealedSecret.id, sealedSecret.metadata]).apply(([id, metadata]) => { | |
return watch("", "v1", "Secret", async (type, object) => { | |
if (type == "ADDED" && object.metadata.name == metadata.name) { | |
return id; | |
} | |
return undefined; | |
}); | |
}) | |
// Get the Secret by name. Normally, this operation would fail if the Secret did not exist, but using | |
// watch allows us to make this call only when the Secret is present. | |
export const secret = k8s.core.v1.Secret.get("mysecret", secretName) |
@lblackstone The streamInvoke function is only Javascript :( ... We develop with the python sdk.
This example is a proof of concept from a hackday project. We're tracking the development of first-class support in pulumi/pulumi-kubernetes#1260
@lblackstone any workarounds on how to make this work inside preview? Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@lblackstone this fails on preview, is it expected?