示例#1
0
        public async Task <int> OnExecuteAsync(CommandLineApplication app)
        {
            var error = false;
            var crds  = CrdGenerator.GenerateCrds(_resourceTypeService).ToList();
            await app.Out.WriteLineAsync($"Found {crds.Count} CRD's.");

            if (!Force && !Prompt.GetYesNo("Should the uninstall proceed?", false, ConsoleColor.Red))
            {
                return(ExitCodes.Error);
            }

            await app.Out.WriteLineAsync(
                $@"Starting uninstall from the cluster with url ""{_client.ApiClient.BaseUri}"".");

            foreach (var crd in crds)
            {
                await app.Out.WriteLineAsync(
                    $@"Uninstall ""{crd.Spec.Group}/{crd.Spec.Names.Kind}"" from the cluster");

                try
                {
                    try
                    {
                        await _client.Delete(crd);
                    }
                    catch (HttpOperationException e) when(e.Response.StatusCode == HttpStatusCode.NotFound)
                    {
                        await _client.Delete((V1beta1CustomResourceDefinition)crd);
                    }
                }
                catch (HttpOperationException e)
                {
                    await app.Out.WriteLineAsync(
                        $@"There was a http (api) error while uninstalling ""{crd.Spec.Group}/{crd.Spec.Names.Kind}"".");

                    await app.Out.WriteLineAsync(e.Message);

                    await app.Out.WriteLineAsync(e.Response.Content);

                    error = true;
                }
                catch (Exception e)
                {
                    await app.Out.WriteLineAsync(
                        $@"There was an error while uninstalling ""{crd.Spec.Group}/{crd.Spec.Names.Kind}"".");

                    await app.Out.WriteLineAsync(e.Message);

                    error = true;
                }
            }

            return(error ? ExitCodes.Error : ExitCodes.Success);
        }
        /// <summary>
        /// If this instance is the leader, delete the V1Lease object.
        /// This way, a new leader can take place, and if all instances
        /// are shut down, the last instance will kill the lease object
        /// and therefore suppress orphans.
        /// </summary>
        /// <returns>A Task.</returns>
        internal async Task ClearLeaseIfLeader()
        {
            if (_namespace.Length == 0)
            {
                _logger.LogTrace("Fetching namespace for leader election.");
                _namespace = await _client.GetCurrentNamespace();
            }

            _logger.LogTrace(@"Fetch V1Lease object for operator ""{operator}"".", _settings.Name);
            var lease = await _client.Get <V1Lease>(_leaseName, _namespace);

            if (lease == null || lease.Spec.HolderIdentity != _hostname)
            {
                return;
            }

            _logger.LogInformation(
                @"Shutting down instance ""{hostname}"". Deleting the lease for operator ""{operator}"".",
                _hostname,
                _settings.Name);
            await _client.Delete(lease);
        }
示例#3
0
        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,
                    },
                },