Last active
March 6, 2024 16:21
-
-
Save pjmagee/a32b8eaa76b9108db976b31441ca7d30 to your computer and use it in GitHub Desktop.
Automation Test k8s
This file contains hidden or 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
| apiVersion: apps/v1 | |
| kind: Deployment | |
| metadata: | |
| name: your-deployment-name | |
| namespace: your-namespace | |
| spec: | |
| replicas: 1 | |
| selector: | |
| matchLabels: | |
| app: your-app | |
| template: | |
| metadata: | |
| labels: | |
| app: your-app | |
| spec: | |
| serviceAccountName: tst-agent | |
| containers: | |
| - name: your-container-name | |
| image: your-container-image | |
| # Other container specs |
This file contains hidden or 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
| apiVersion: rbac.authorization.k8s.io/v1 | |
| kind: Role | |
| metadata: | |
| namespace: tst | |
| name: trigger-job | |
| rules: | |
| - apiGroups: ["batch", ""] # "" indicates the core API group | |
| resources: ["cronjobs"] | |
| verbs: ["get", "list", "watch"] # Permissions to read CronJobs | |
| - apiGroups: ["batch"] | |
| resources: ["jobs"] | |
| verbs: ["create", "list", "delete", "get", "watch"] # Permissions to manage Jobs | |
| --- | |
| apiVersion: rbac.authorization.k8s.io/v1 | |
| kind: RoleBinding | |
| metadata: | |
| name: job-manage-rolebinding | |
| namespace: tst | |
| subjects: | |
| - kind: ServiceAccount | |
| name: tst-agent | |
| namespace: tst | |
| roleRef: | |
| kind: Role | |
| name: trigger-job | |
| apiGroup: rbac.authorization.k8s.io | |
This file contains hidden or 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
| async Task Main() | |
| { | |
| var config = KubernetesClientConfiguration.InClusterConfig(); | |
| using(var client = new Kubernetes(config)) | |
| { | |
| var name = "enfapi-data-cleaner-cronjob"; | |
| var nameSpace = "enfapi-tst"; | |
| var cronJob = await client.ReadNamespacedCronJobAsync(name, nameSpace); | |
| /* | |
| In live (which automation test is not deployed to, this is not suspended, and runs on a schedule) | |
| In PreLive such as UAT, we also likely have it not suspended | |
| In test, we want our automation to be able to insert tst data and then trigger the job | |
| */ | |
| if (cronJob.Spec.Suspend == true) | |
| { | |
| var job = new V1Job | |
| { | |
| ApiVersion = "batch/v1", | |
| Kind = "Job", | |
| Metadata = new V1ObjectMeta | |
| { | |
| Name = $"{name}-{DateTime.UtcNow:yyyyMMddHHmmss}", | |
| Labels = cronJob.Spec.JobTemplate.Metadata.Labels, | |
| Annotations = cronJob.Spec.JobTemplate.Metadata.Annotations, | |
| }, | |
| Spec = cronJob.Spec.JobTemplate.Spec | |
| }; | |
| var createdJob = await client.CreateNamespacedJobAsync(job, nameSpace); | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment