Ejemplo n.º 1
0
        public async Task <DeploymentResult> DeployAsync(HealthCheckResource resource)
        {
            _logger.LogInformation("Creating secret for hc resource - namespace {namespace}", resource.Metadata.NamespaceProperty);

            var secret = await _secretHandler.GetOrCreateAsync(resource);

            if (resource.HasBrandingConfigured())
            {
                _logger.LogInformation("Creating configmap for hc resource - namespace {namespace}", resource.Metadata.NamespaceProperty);
                await _configMapHandler.GetOrCreateAsync(resource);
            }

            _logger.LogInformation("Creating deployment for hc resource - namespace {namespace}", resource.Metadata.NamespaceProperty);

            var deployment = await _deploymentHandler.GetOrCreateAsync(resource);

            _logger.LogInformation("Creating service for hc resource - namespace {namespace}", resource.Metadata.NamespaceProperty);

            var service = await _serviceHandler.GetOrCreateAsync(resource);

            return(DeploymentResult.Create(deployment, service, secret));
        }
        public V1Deployment Build(HealthCheckResource resource)
        {
            var metadata = new V1ObjectMeta
            {
                OwnerReferences = new List <V1OwnerReference> {
                    resource.CreateOwnerReference()
                },
                Annotations = new Dictionary <string, string>(),
                Labels      = new Dictionary <string, string>
                {
                    ["app"] = resource.Spec.Name
                },
                Name = $"{resource.Spec.Name}-deploy",
                NamespaceProperty = resource.Metadata.NamespaceProperty
            };

            var uiContainer = new V1Container
            {
                ImagePullPolicy = resource.Spec.ImagePullPolicy ?? Constants.DEFAULT_PULL_POLICY,
                Name            = Constants.POD_NAME,
                Image           = resource.Spec.Image ?? Constants.IMAGE_NAME,
                Ports           = new List <V1ContainerPort>
                {
                    new V1ContainerPort(80)
                },
                Env = new List <V1EnvVar>
                {
                    new V1EnvVar("enable_push_endpoint", "true"),
                    new V1EnvVar("push_endpoint_secret", valueFrom: new V1EnvVarSource(secretKeyRef: new V1SecretKeySelector("key", $"{resource.Spec.Name}-secret"))),
                    new V1EnvVar("Logging__LogLevel__Default", "Debug"),
                    new V1EnvVar("Logging__LogLevel__Microsoft", "Warning"),
                    new V1EnvVar("Logging__LogLevel__System", "Warning"),
                    new V1EnvVar("Logging__LogLevel__HealthChecks", "Information")
                }
            };

            uiContainer.MapCustomUIPaths(resource, _operatorDiagnostics);

            var spec = new V1DeploymentSpec
            {
                Selector = new V1LabelSelector
                {
                    MatchLabels = new Dictionary <string, string>
                    {
                        ["app"] = resource.Spec.Name
                    }
                },
                Replicas = 1,
                Template = new V1PodTemplateSpec
                {
                    Metadata = new V1ObjectMeta
                    {
                        Labels = new Dictionary <string, string>
                        {
                            ["app"] = resource.Spec.Name
                        },
                    },
                    Spec = new V1PodSpec
                    {
                        Containers = new List <V1Container>
                        {
                            uiContainer
                        }
                    }
                }
            };

            foreach (var annotation in resource.Spec.DeploymentAnnotations)
            {
                _logger.LogInformation("Adding annotation {Annotation} to ui deployment with value {AnnotationValue}", annotation.Name, annotation.Value);
                metadata.Annotations.Add(annotation.Name, annotation.Value);
            }

            var specification = spec.Template.Spec;
            var container     = specification.Containers.First();

            for (int i = 0; i < resource.Spec.Webhooks.Count; i++)
            {
                var webhook = resource.Spec.Webhooks[i];
                _logger.LogInformation("Adding webhook configuration for webhook {Webhook}", webhook.Name);

                container.Env.Add(new V1EnvVar($"HealthChecksUI__Webhooks__{i}__Name", webhook.Name));
                container.Env.Add(new V1EnvVar($"HealthChecksUI__Webhooks__{i}__Uri", webhook.Uri));
                container.Env.Add(new V1EnvVar($"HealthChecksUI__Webhooks__{i}__Payload", webhook.Payload));
                container.Env.Add(new V1EnvVar($"HealthChecksUI__Webhooks__{i}__RestoredPayload", webhook.RestoredPayload));
            }

            if (resource.HasBrandingConfigured())
            {
                const string volumeName = "healthchecks-volume";

                if (specification.Volumes == null)
                {
                    specification.Volumes = new List <V1Volume>();
                }
                if (container.VolumeMounts == null)
                {
                    container.VolumeMounts = new List <V1VolumeMount>();
                }

                specification.Volumes.Add(new V1Volume(name: volumeName,
                                                       configMap: new V1ConfigMapVolumeSource(name: $"{resource.Spec.Name}-config")));

                container.Env.Add(new V1EnvVar("ui_stylesheet", $"{Constants.STYLES_PATH}/{Constants.STYLE_SHEET_NAME}"));
                container.VolumeMounts.Add(new V1VolumeMount($"/app/{Constants.STYLES_PATH}", volumeName));
            }

            return(new V1Deployment(metadata: metadata, spec: spec));
        }
Ejemplo n.º 3
0
        public V1Deployment Build(HealthCheckResource resource)
        {
            var metadata = new V1ObjectMeta
            {
                OwnerReferences = new List <V1OwnerReference> {
                    resource.CreateOwnerReference()
                },
                Annotations = new Dictionary <string, string>(),
                Labels      = new Dictionary <string, string>
                {
                    ["app"] = resource.Spec.Name
                },
                Name = $"{resource.Spec.Name}-deploy",
                NamespaceProperty = resource.Metadata.NamespaceProperty
            };

            var spec = new V1DeploymentSpec
            {
                Selector = new V1LabelSelector
                {
                    MatchLabels = new Dictionary <string, string>
                    {
                        ["app"] = resource.Spec.Name
                    }
                },
                Replicas = 1,
                Template = new V1PodTemplateSpec
                {
                    Metadata = new V1ObjectMeta
                    {
                        Labels = new Dictionary <string, string>
                        {
                            ["app"] = resource.Spec.Name
                        },
                    },
                    Spec = new V1PodSpec
                    {
                        Containers = new List <V1Container>
                        {
                            new V1Container
                            {
                                ImagePullPolicy = resource.Spec.ImagePullPolicy ?? Constants.DefaultPullPolicy,
                                Name            = Constants.PodName,
                                Image           = resource.Spec.Image ?? Constants.ImageName,
                                Ports           = new List <V1ContainerPort>
                                {
                                    new V1ContainerPort(80)
                                },
                                Env = new List <V1EnvVar>
                                {
                                    new V1EnvVar("ui_path", resource.Spec.UiPath ?? Constants.DefaultUIPath),
                                    new V1EnvVar("enable_push_endpoint", "true"),
                                    new V1EnvVar("push_endpoint_secret", valueFrom: new V1EnvVarSource(secretKeyRef: new V1SecretKeySelector("key", $"{resource.Spec.Name}-secret"))),
                                    new V1EnvVar("Logging__LogLevel__Default", "Debug"),
                                    new V1EnvVar("Logging__LogLevel__Microsoft", "Warning"),
                                    new V1EnvVar("Logging__LogLevel__System", "Warning"),
                                    new V1EnvVar("Logging__LogLevel__HealthChecks", "Information")
                                }
                            }
                        }
                    }
                }
            };

            foreach (var annotation in resource.Spec.DeploymentAnnotations)
            {
                _logger.LogInformation("Adding annotation {Annotation} to ui deployment with value {AnnotationValue}", annotation.Name, annotation.Value);
                metadata.Annotations.Add(annotation.Name, annotation.Value);
            }

            if (resource.HasBrandingConfigured())
            {
                var specification = spec.Template.Spec;
                var container     = specification.Containers.First();
                var volumeName    = "healthchecks-volume";

                if (specification.Volumes == null)
                {
                    specification.Volumes = new List <V1Volume>();
                }
                if (container.VolumeMounts == null)
                {
                    container.VolumeMounts = new List <V1VolumeMount>();
                }

                specification.Volumes.Add(new V1Volume(name: volumeName,
                                                       configMap: new V1ConfigMapVolumeSource(name: $"{resource.Spec.Name}-config")));

                container.Env.Add(new V1EnvVar("ui_stylesheet", $"{Constants.StylesPath}/{Constants.StyleSheetName}"));
                container.VolumeMounts.Add(new V1VolumeMount($"/app/{Constants.StylesPath}", volumeName));
            }

            return(new V1Deployment(metadata: metadata, spec: spec));
        }