I have yaml file
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: ct-cron
spec:
schedule: "*/1 * * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
template:
metadata:
labels:
app: your-periodic-batch-job
aadpodidbinding: managedaks
spec:
containers:
- name: redmine-cron
image: orm.azurecr.io/py-app:v3
imagePullPolicy: IfNotPresent
- name: redmine-cron1
image: orm.azurecr.io/py-app:v5
imagePullPolicy: IfNotPresent
volumeMounts:
- name: store01-inline1
mountPath: "/mnt/secrets-store"
readOnly: true
restartPolicy: OnFailure
volumes:
- name: store01-inline1
csi:
driver: secrets-store.csi.k8s.io
readOnly: true
volumeAttributes:
secretProviderClass: "azure-kvname-podid"
I stored this yaml as a string in the database.I want to deploy this YAML using c# code. But I couldn't find any option to deploy the yaml file using c# code.Is it possible?
It's possible with the nuget package Kube.netesClient
it includes a yaml parser, sample
var typeMap = new Dictionary<String, Type>();
typeMap.Add("v1/Pod", typeof(V1Pod));
typeMap.Add("v1/Service", typeof(V1Service));
typeMap.Add("apps/v1beta1/Deployment", typeof(Appsv1beta1Deployment));
var objects = await Yaml.LoadAllFromFileAsync(args[0], typeMap);
Those objects can be applied with the Kube.netes class' Create methods eg Namespace
var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
IKubernetes client = new Kubernetes(k8SClientConfig);
var ns = new V1Namespace { Metadata = new V1ObjectMeta { Name = "test" } };
var result = client.CreateNamespace(ns);
You can use the Kube.netesClient c# sdk as others have pointed. Here is an example to create a namespace. Cronjob is in BatchV1Api class, so while deserializing your yaml, use that.
using k8s;
using k8s.Models;
public class Program
{
public static async Task Main(string[] args)
{
KubernetesClientConfiguration config = KubernetesClientConfiguration.BuildDefaultConfig();
IKubernetes client = new Kubernetes(config);
var label = "label name";
var nsContent = $@"apiVersion: v1
kind: Namespace
metadata:
name: test
labels:
label1 : {label}";
var objs = KubernetesYaml.LoadAllFromString(nsContent);
var ns = (V1Namespace)objs[0];
await client.CoreV1.CreateNamespaceAsync(ns);
// Or you can use the Deserialize method as follows
var nsContent2 = $@"apiVersion: v1
kind: Namespace
metadata:
name: test2
labels:
label1 : {label}";
var ns2 = KubernetesYaml.Deserialize<V1Namespace>(nsContent2);
await client.CoreV1.CreateNamespaceAsync(ns2);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.