Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 21, 2025

Overview

This PR adds Windows service registration support to azure-cns, enabling it to self-register as a Windows service for automatic startup and recovery. This addresses the requirements from the Azure Batch team who need azure-cns to automatically restart on crashes and start automatically when VMs reboot.

Changes

Core Implementation

  • Windows Service Handler (cns/service/service_windows.go): Implements Windows service functionality using golang.org/x/sys/windows/svc package

    • Service installation with automatic startup configuration
    • Service uninstallation with graceful shutdown
    • Windows Service Control Manager integration
    • Event log integration for service lifecycle events
    • Auto-recovery configuration (5-second restart delays, 3 attempts, 24-hour reset)
  • Platform Stub (cns/service/service_other.go): Non-Windows platforms return appropriate errors when service operations are attempted

  • Service Integration (cns/service/main.go):

    • Added --service command-line flag with install, uninstall, and run actions
    • Automatic detection when started by Windows Service Manager
    • Graceful handling of service stop/shutdown requests
  • Configuration (common/config.go): Added service-related command-line option constants

Testing

  • Unit Tests (cns/service/service_test.go): Tests for service functions on non-Windows platforms
  • All existing tests pass
  • Binary builds successfully on Linux

Documentation

  • CNS README (cns/README.md): General documentation with Windows service overview
  • Windows Service Guide (cns/doc/windows-service.md): Comprehensive guide including:
    • Installation and configuration instructions
    • Service management using PowerShell and Windows tools
    • Troubleshooting section
    • Event log monitoring examples
    • Command reference

Usage

Install and Start Service

# Install as Windows service (requires Administrator privileges)
azure-cns.exe --service install

# Start the service
net start azure-cns

# Verify status
Get-Service azure-cns

View Service Logs

Get-EventLog -LogName Application -Source azure-cns -Newest 20

Uninstall Service

net stop azure-cns
azure-cns.exe --service uninstall

Service Configuration

The service is registered with the following settings:

  • Service Name: azure-cns
  • Display Name: Azure Container Networking Service
  • Start Type: Automatic
  • Service Account: LocalSystem
  • Recovery Actions: Restart after 5 seconds on failure (3 attempts)

Benefits

  1. Automatic Startup: Service starts automatically on system boot
  2. Self-Recovery: Automatic restart when the process crashes
  3. Standard Management: Works with native Windows service tools (services.msc, net.exe, PowerShell)
  4. Event Logging: Lifecycle events logged to Windows Event Log
  5. No External Dependencies: Self-registration without requiring NSSM or other tools

Platform Compatibility

  • Windows: Full service registration and management support
  • Linux: Service operations return appropriate errors (use systemd or other init systems)

Testing Notes

  • ✅ Builds successfully on Linux
  • ✅ All existing tests pass
  • ✅ New unit tests pass
  • ✅ No breaking changes
  • ⏳ Manual Windows testing recommended before deployment

Fixes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>[Azure CNS] Register azure-cns as windows service</issue_title>
<issue_description>Azure Batch team uses Azure-CNS for ADF works. We run azure-cns.exe (only windows) as standalone executable at individual VM.

However, we need register azure-cns.exe as windows service so it will restart when it crashes or start automatically when VM reboot.

We would like the feature for azure-cns to add a new flag which can register itself as windows service.</issue_description>

<agent_instructions>Learn about best practices related to apps registering as a Windows service and how that could be implemented in CNS. If apps typically don't register themselves and that is an orchestrator's responsibility, take no action.
</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@rbtr @xingwu1 I'm not familiar with Windows services. Is it typical for the executable to register _itself_ as a Windows service? That seems like something that an orchestrator would manage. If this is the standard, feel free to open a PR to add this support.

Fixes #4046


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits October 21, 2025 16:56
- Add command-line flags for service management (--service install/uninstall/run)
- Implement Windows service handler using golang.org/x/sys/windows/svc
- Add automatic service detection when started by Windows Service Manager
- Configure service to auto-restart on failure with 5-second delays
- Provide event log integration for service lifecycle events
- Add stub implementation for non-Windows platforms
- Update command-line help to show new service options

Co-authored-by: rbtr <2940321+rbtr@users.noreply.github.com>
- Add comprehensive Windows service documentation (cns/doc/windows-service.md)
- Create CNS README with Windows service information
- Include installation, configuration, and troubleshooting guides
- Add command-line reference and examples

Co-authored-by: rbtr <2940321+rbtr@users.noreply.github.com>
- Add unit tests for service functions on non-Windows platforms
- Verify proper error handling when service functions are called on Linux
- Ensure isWindowsService returns false on non-Windows platforms

Co-authored-by: rbtr <2940321+rbtr@users.noreply.github.com>
Copilot AI changed the title [WIP] Add feature to register azure-cns as Windows service Add Windows service registration support to azure-cns Oct 21, 2025
Copilot AI requested a review from rbtr October 21, 2025 17:05
Copilot finished work on behalf of rbtr October 21, 2025 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Azure CNS] Register azure-cns as windows service

2 participants