Skip to content

Instantly share code, notes, and snippets.

@timmyers
Created May 29, 2020 21:05
Show Gist options
  • Save timmyers/4d2fed53a358d4c98557a5886ae2afbb to your computer and use it in GitHub Desktop.
Save timmyers/4d2fed53a358d4c98557a5886ae2afbb to your computer and use it in GitHub Desktop.
Wait for a resource in pulumi
// Make sure x is a true output so pulumi doesn't try to run this function during preview.
const ingressIP = pulumi.all([x]).apply(async ([x]) => {
const kc = new k8sClient.KubeConfig();
const provider = (this.getProvider('kubernetes::') as any)
const kubeConfig = provider.kubeconfig as pulumi.Output<string>;
const ip = kubeConfig.apply(async (config) => {
kc.loadFromString(config);
const k8sApi = kc.makeApiClient(k8sClient.CoreV1Api);
while (true) {
try {
pulumi.log.info('Waiting for load balancer IP...', this);
const res = await k8sApi.readNamespacedService('istio-ingressgateway', namespace)
return res.body.status.loadBalancer.ingress[0].ip;
} catch (err) {
await new Promise(r => setTimeout(r, 2000));
}
}
});
return ip;
});
@hcharley
Copy link

hcharley commented May 3, 2022

I'm developing a script to grab the status of a CustomResource based on this:

type ManagedCertificateStatusType = 'Provisioning' | 'Ready' | 'Failed';

type ManagedCertificateDomainStatus = {
  domain: string;
  status: ManagedCertificateStatusType;
};

type ManagedCertificateStatus = {
  certificateName: string;
  certificateStatus: ManagedCertificateStatusType;
  domainStatus: ManagedCertificateDomainStatus[];
};
  public setupKubernetesManagedCert() {
    this.addResource(
      'k8-cert',
      new KubernetesCustomResource(
        this.getSlugFor('cert'),
        this.kubernetesManagedCertArgs,
        {
          parent: this,
        }
      )
    );
  }

  public get kubernetesManagedCertKind() {
    return 'ManagedCertificate' as const;
  }

  public get kubernetesManagedCertApiWithVersion() {
    return `networking.gke.io/${this.kubernetesManagedCertApiVersion}` as const;
  }

  public get kubernetesManagedCertApiVersion() {
    return 'v1beta2' as const;
  }

  public get kubernetesManagedCertResourceGroup() {
    return 'networking.gke.io' as const;
  }

  public async getKubernetesManagedCertStatus(): Promise<
    ManagedCertificateStatus | undefined
  > {
    if (!this.myEnvironment.kubernetesManagedCertName) {
      return undefined;
    }

    const kubeConfig = new KubeConfig();

    kubeConfig.loadFromDefault();

    const kubeApi = kubeConfig.makeApiClient(CustomObjectsApi);

    try {
      const resources = await kubeApi.getNamespacedCustomObject(
        this.kubernetesManagedCertResourceGroup,
        this.kubernetesManagedCertApiVersion,
        this.getKubernetesNamespaceOrThrow(),
        'managedcertificates',
        this.myEnvironment.kubernetesManagedCertName
      );

      return (resources.body as any)?.status as ManagedCertificateStatus;
    } catch (cause) {
      this.logger.error(`Failed to get managed cert status: ${cause}`);
      this.logger.error(cause);
      return undefined;
    }
  }

  public get kubernetesManagedCertArgs(): CustomResourceArgs {
    return {
      kind: this.kubernetesManagedCertKind,
      apiVersion: this.kubernetesManagedCertApiWithVersion,
      metadata: {
        namespace: this.myEnvironment.getKubernetesNamespaceOrThrow(),
      },
      spec: {
        domains: this.myEnvironment.serviceHosts,
      },
    };
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment