Exemple #1
0
        public async Task <int> OnExecuteAsync(CommandLineApplication app)
        {
            var fileWriter = new FileWriter(app.Out);

            if (OutputPath != null &&
                _hasWebhooks &&
                (!File.Exists(Path.Join(OutputPath, "ca.pem")) || !File.Exists(Path.Join(OutputPath, "ca-key.pem"))))
            {
                using var certManager = new CertificateGenerator(app.Out);
                await certManager.CreateCaCertificateAsync(OutputPath);
            }

            fileWriter.Add(
                $"kustomization.{Format.ToString().ToLower()}",
                _serializer.Serialize(
                    new KustomizationConfig
            {
                Resources = new List <string>
                {
                    $"deployment.{Format.ToString().ToLower()}",
                },
                CommonLabels = new Dictionary <string, string>
                {
                    { "operator-element", "operator-instance" },
                },
                ConfigMapGenerator = _hasWebhooks
                            ? new List <KustomizationConfigMapGenerator>
                {
                    new()
                    {
                        Name  = "webhook-ca",
                        Files = new List <string>
                        {
                            "ca.pem",
                            "ca-key.pem",
                        },
                    },
                    new()
                    {
                        Name     = "webhook-config",
                        Literals = new List <string>
                        {
                            "KESTREL__ENDPOINTS__HTTP__URL=http://0.0.0.0:80",
                            "KESTREL__ENDPOINTS__HTTPS__URL=https://0.0.0.0:443",
                            "KESTREL__ENDPOINTS__HTTPS__CERTIFICATE__PATH=/certs/server.pem",
                            "KESTREL__ENDPOINTS__HTTPS__CERTIFICATE__KEYPATH=/certs/server-key.pem",
                        },
                    },
                }
        public async Task <int> OnExecuteAsync(CommandLineApplication app)
        {
            var @namespace = await _client.GetCurrentNamespace();

            using var certManager = new CertificateGenerator(app.Out);

#if DEBUG
            CertificatesPath   = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            CaCertificatesPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            await certManager.CreateCaCertificateAsync(CaCertificatesPath);
#endif

            Directory.CreateDirectory(CertificatesPath);
            File.Copy(Path.Join(CaCertificatesPath, "ca.pem"), Path.Join(Path.Join(CertificatesPath, "ca.pem")));
            await certManager.CreateServerCertificateAsync(
                CertificatesPath,
                _settings.Name,
                @namespace,
                Path.Join(CaCertificatesPath, "ca.pem"),
                Path.Join(CaCertificatesPath, "ca-key.pem"));

            var deployment = (await _client.List <V1Deployment>(
                                  @namespace,
                                  new EqualsSelector("operator-deployment", _settings.Name))).FirstOrDefault();
            if (deployment != null)
            {
                deployment.Kind       = V1Deployment.KubeKind;
                deployment.ApiVersion = $"{V1Deployment.KubeGroup}/{V1Deployment.KubeApiVersion}";
            }

            await app.Out.WriteLineAsync("Create service.");

            await _client.Delete <V1Service>(_settings.Name, @namespace);

            await _client.Create(
                new V1Service(
                    V1Service.KubeApiVersion,
                    V1Service.KubeKind,
                    new V1ObjectMeta(
                        name: _settings.Name,
                        namespaceProperty: @namespace,
                        ownerReferences: deployment != null
                            ? new List <V1OwnerReference>
            {
                deployment.MakeOwnerReference(),
            }
                            : null,
                        labels: new Dictionary <string, string>
            {
                { "operator", _settings.Name },
                { "usage", "webhook-service" },
            }),
                    new V1ServiceSpec
            {
                Ports = new List <V1ServicePort>
                {
                    new()
                    {
                        Name = "https",
                        TargetPort = "https",
                        Port = 443,
                    },
                },