-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
136 lines (115 loc) · 5.02 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Rest;
using k8s.Operators.Logging;
using Newtonsoft.Json;
namespace k8s.Operators.Samples.Basic
{
class Program
{
static async Task<int> Main(string[] args)
{
IOperator basicOperator = null;
// Setup logging
using var loggerFactory = SetupLogging(args);
var logger = loggerFactory.CreateLogger<Program>();
try
{
logger.LogDebug($"Environment variables: {JsonConvert.SerializeObject(Environment.GetEnvironmentVariables())}");
// Setup termination handlers
SetupSignalHandlers();
// Setup the Kubernetes client
using var client = SetupClient(args);
// Setup the operator
var configuration = GetOperatorConfiguration();
basicOperator = new Operator(configuration, client, loggerFactory);
basicOperator.AddControllerOfType<MyResourceController>();
// Start the operator
return await basicOperator.StartAsync();
}
catch (Exception exception)
{
logger.LogError(exception, "Operator error");
return 1;
}
IKubernetes SetupClient(string[] args)
{
// Load the Kubernetes configuration
KubernetesClientConfiguration config = null;
if (KubernetesClientConfiguration.IsInCluster())
{
logger.LogDebug("Loading cluster configuration");
config = KubernetesClientConfiguration.InClusterConfig();
}
else
{
logger.LogDebug("Loading local configuration");
config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
}
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug($"Client configuration: {JsonConvert.SerializeObject(config)}");
}
return new Kubernetes(config);
}
ILoggerFactory SetupLogging(string[] args)
{
if (!System.Enum.TryParse<LogLevel>(Environment.GetEnvironmentVariable("LOG_LEVEL"), true, out LogLevel logLevel))
{
logLevel = LogLevel.Debug;
}
var loggerFactory = LoggerFactory.Create(builder => builder
.AddConsole(options => options.Format=ConsoleLoggerFormat.Systemd)
.SetMinimumLevel(logLevel)
);
// Enable Kubernetes client logging if level = DEBUG
ServiceClientTracing.IsEnabled = logLevel <= LogLevel.Debug;
ServiceClientTracing.AddTracingInterceptor(new ConsoleTracingInterceptor());
return loggerFactory;
}
void SetupSignalHandlers()
{
// SIGTERM: signal the operator to shut down gracefully
AppDomain.CurrentDomain.ProcessExit += (s, e) =>
{
logger.LogDebug("Received SIGTERM");
basicOperator?.Stop();
};
// SIGINT: try to shut down gracefully on the first attempt
Console.CancelKeyPress += (s, e) =>
{
bool isFirstSignal = !basicOperator.IsDisposing;
logger.LogDebug($"Received SIGINT, first signal: {isFirstSignal}");
if (isFirstSignal)
{
e.Cancel = true;
Environment.Exit(0);
}
};
}
}
private static OperatorConfiguration GetOperatorConfiguration()
{
var configuration = new OperatorConfiguration();
var retryPolicy = new RetryPolicy();
if (int.TryParse(Environment.GetEnvironmentVariable("RETRY_MAX_ATTEMPTS"), out int maxAttempts))
{
retryPolicy.MaxAttempts = maxAttempts;
}
if (int.TryParse(Environment.GetEnvironmentVariable("RETRY_INITIAL_DELAY"), out int initialDelay))
{
retryPolicy.InitialDelay = initialDelay;
}
if (int.TryParse(Environment.GetEnvironmentVariable("RETRY_DELAY_MULTIPLIER"), out int delayMultiplier))
{
retryPolicy.DelayMultiplier = delayMultiplier;
}
configuration.RetryPolicy = retryPolicy;
configuration.WatchNamespace = Environment.GetEnvironmentVariable("WATCH_NAMESPACE");
return configuration;
}
}
}