Friday, February 7, 2025

Running Parallel Tasks in Azure DevOps YAML Pipeline

1. Running Parallel Jobs

If you want multiple jobs to run in parallel, define them separately under the jobs section.

jobs: - job: Job1 displayName: 'Job 1' pool: vmImage: 'ubuntu-latest' steps: - script: echo "Running Job 1" - job: Job2 displayName: 'Job 2' pool: vmImage: 'ubuntu-latest' steps: - script: echo "Running Job 2"

2. Running Parallel Steps within a Job

Option 1: Using dependsOn for Parallel Execution in Jobs

jobs: - job: Build displayName: 'Build Job' steps: - script: echo "Building the application" - job: Test displayName: 'Test Job' dependsOn: [] # Runs in parallel with Build steps: - script: echo "Running tests" - job: Deploy displayName: 'Deploy Job' dependsOn: [Build, Test] # Runs only after both are completed steps: - script: echo "Deploying the application"

Option 2: Using template for Parallel Execution

Create a separate YAML template file:

# parallel-template.yml steps: - script: echo "Task 1" - script: echo "Task 2"

Then, reference it in the main pipeline:

jobs: - job: ParallelJob steps: - template: parallel-template.yml

Option 3: Using background: true for Background Tasks

steps: - script: echo "Starting Task 1" displayName: 'Task 1' background: true # Runs in the background - script: echo "Starting Task 2" displayName: 'Task 2' background: true # Runs in the background

Best Approach?

  • Use separate jobs if tasks need different agents or environments.
  • Use parallel steps in a job if they share the same environment.
  • Use background tasks for lightweight independent tasks.

Let me know if you need a specific example for your pipeline! 🚀

Wednesday, February 5, 2025

Read ConfigMap of Pods Namespace in AKS using .Net Core

 To fetch ConfigMaps using the above approach, you can modify the code to use the Kubernetes client API for retrieving ConfigMaps.

🔹 Steps to Fetch ConfigMaps in a Namespace

  1. Modify the code to call ListNamespacedConfigMapAsync.
  2. Iterate through the retrieved ConfigMaps.
  3. Extract and display the required details.

Updated C# Code to Fetch ConfigMaps in the openlens Namespace

using System;
using System.Threading.Tasks;
using k8s;
using k8s.Models;

class Program
{
    static async Task Main(string[] args)
    {
        // Load Kubernetes config
        var config = KubernetesClientConfiguration.BuildDefaultConfig();

        // Create Kubernetes client
        IKubernetes client = new Kubernetes(config);

        // Specify the namespace
        string namespaceName = "openlens"; // Change as needed

        try
        {
            // Get the list of ConfigMaps in the namespace
            var configMapList = await client.CoreV1.ListNamespacedConfigMapAsync(namespaceName);

            Console.WriteLine($"ConfigMaps in namespace '{namespaceName}':");

            foreach (var configMap in configMapList.Items)
            {
                Console.WriteLine($"- Name: {configMap.Metadata.Name}");
                Console.WriteLine("  Data:");

                // Display the key-value pairs inside the ConfigMap
                if (configMap.Data != null)
                {
                    foreach (var kvp in configMap.Data)
                    {
                        Console.WriteLine($"    {kvp.Key}: {kvp.Value}");
                    }
                }
                else
                {
                    Console.WriteLine("    (No data)");
                }

                Console.WriteLine(new string('-', 40));
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error fetching ConfigMaps: {ex.Message}");
        }
    }
}

How This Works

  1. Uses ListNamespacedConfigMapAsync(namespaceName) to get all ConfigMaps in the given namespace.
  2. Iterates through each ConfigMap and prints:
    • Name
    • Key-value pairs (if any)
  3. Handles errors gracefully.

🔹 Steps to Run

  1. Ensure kubectl is configured correctly.
  2. Install the KubernetesClient NuGet package:
    dotnet add package KubernetesClient
    
  3. Run the program:
    dotnet run