Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment bugfixes #29

Merged
merged 11 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ GOBIN=$(shell go env GOBIN)
endif

# Check if oc or kubectl are installed and determine which of the two to use
ifeq ($(K8S_CLI),)
ifeq (,$(shell which kubectl))
ifeq (,$(shell which oc))
$(error oc or kubectl is required to proceed)
Expand All @@ -39,6 +40,7 @@ endif
else
K8S_CLI := kubectl
endif
endif


# Setting SHELL to bash allows bash commands to be executed by recipes.
Expand Down
6 changes: 6 additions & 0 deletions config/crd/bases/registry.devfile.io_devfileregistries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ spec:
description: The registry name (can be any string) that is used
as identifier for devfile telemetry.
type: string
registryViewerWriteKey:
description: Specify a telemetry write key for the registry viewer
component to allow data to be sent to a client's own Segment
analytics source. If the write key is specified then telemetry
for the registry viewer component will be enabled
type: string
type: object
tls:
description: DevfileRegistrySpecTLS defines the desired state for
Expand Down
6 changes: 3 additions & 3 deletions pkg/registry/configmap.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020-2022 Red Hat, Inc.
Copyright 2020-2023 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -52,8 +52,8 @@ http:

viewerEnvfile := fmt.Sprintf(`
ANALYTICS_WRITE_KEY=%s
DEVFILE_REGISTRIES=[{\"name\":\"Community\",\"url\":\"http://localhost:8080\",\"fqdn\":\"http://%s.%s\"}]`,
cr.Spec.Telemetry.RegistryViewerWriteKey, IngressName(cr.Name), cr.Spec.K8s.IngressDomain)
DEVFILE_REGISTRIES=[{"name":"Community","url":"http://localhost:8080","fqdn":"%s"}]`,
cr.Spec.Telemetry.RegistryViewerWriteKey, cr.Status.URL)

configMapData["registry-config.yml"] = registryConfig
configMapData[".env.registry-viewer"] = viewerEnvfile
Expand Down
3 changes: 2 additions & 1 deletion pkg/registry/defaults.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020-2022 Red Hat, Inc.
Copyright 2020-2023 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,6 +44,7 @@ const (
DevfileIndexMetricsPort = 7071
OCIMetricsPortName = "oci-registry-metrics"
OCIMetricsPort = 5001
OCIServerPort = 5000
RegistryViewerPort = 3000
)

Expand Down
116 changes: 88 additions & 28 deletions pkg/registry/deployment.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020-2022 Red Hat, Inc.
Copyright 2020-2023 Red Hat, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,6 +30,11 @@ import (

func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Scheme, labels map[string]string) *appsv1.Deployment {
replicas := int32(1)
allowPrivilegeEscalation := false
runAsNonRoot := true
runAsUser := int64(1001)
runAsGroup := int64(2001)
fsGroup := int64(3001)

dep := &appsv1.Deployment{
ObjectMeta: generateObjectMeta(cr.Name, cr.Namespace, labels),
Expand All @@ -48,6 +53,16 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc
Image: cr.Spec.DevfileIndexImage,
ImagePullPolicy: corev1.PullAlways,
Name: "devfile-registry",
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
RunAsNonRoot: &runAsNonRoot,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
SeccompProfile: &corev1.SeccompProfile{
Type: "RuntimeDefault",
},
},
Ports: []corev1.ContainerPort{{
ContainerPort: DevfileIndexPort,
}},
Expand All @@ -68,6 +83,9 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc
Port: intstr.FromInt(DevfileIndexPort),
},
},
InitialDelaySeconds: 15,
PeriodSeconds: 10,
TimeoutSeconds: 3,
},
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{
Expand All @@ -76,6 +94,9 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc
Port: intstr.FromInt(DevfileIndexPort),
},
},
InitialDelaySeconds: 15,
PeriodSeconds: 10,
TimeoutSeconds: 3,
},
Env: []corev1.EnvVar{
{
Expand All @@ -91,6 +112,16 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc
{
Image: GetOCIRegistryImage(cr),
Name: "oci-registry",
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
RunAsNonRoot: &runAsNonRoot,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
SeccompProfile: &corev1.SeccompProfile{
Type: "RuntimeDefault",
},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("100m"),
Expand All @@ -101,6 +132,28 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc
corev1.ResourceMemory: resource.MustParse("256Mi"),
},
},
LivenessProbe: &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/v2",
Port: intstr.FromInt(OCIServerPort),
},
},
InitialDelaySeconds: 30,
PeriodSeconds: 10,
TimeoutSeconds: 3,
},
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/v2",
Port: intstr.FromInt(OCIServerPort),
},
},
InitialDelaySeconds: 3,
PeriodSeconds: 10,
TimeoutSeconds: 3,
},
VolumeMounts: []corev1.VolumeMount{
{
Name: DevfileRegistryVolumeName,
Expand Down Expand Up @@ -143,10 +196,31 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc

// Set Registry Viewer if headless is false, else run headless mode
if !IsHeadlessEnabled(cr) {
dep.Spec.Template.Spec.Containers[0].StartupProbe = &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/viewer",
Port: intstr.FromInt(RegistryViewerPort),
},
},
InitialDelaySeconds: 30,
PeriodSeconds: 10,
TimeoutSeconds: 3,
}
dep.Spec.Template.Spec.Containers = append(dep.Spec.Template.Spec.Containers, corev1.Container{
Image: GetRegistryViewerImage(cr),
ImagePullPolicy: corev1.PullAlways,
Name: "registry-viewer",
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
RunAsNonRoot: &runAsNonRoot,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
SeccompProfile: &corev1.SeccompProfile{
Type: "RuntimeDefault",
},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("250m"),
Expand All @@ -157,35 +231,11 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc
corev1.ResourceMemory: resource.MustParse("256Mi"),
},
},
LivenessProbe: &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/viewer",
Port: intstr.FromInt(RegistryViewerPort),
},
},
},
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/viewer",
Port: intstr.FromInt(RegistryViewerPort),
},
},
},
StartupProbe: &corev1.Probe{
Handler: corev1.Handler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/viewer",
Port: intstr.FromInt(RegistryViewerPort),
},
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "viewer-env-file",
MountPath: "/app/apps/registry-viewer/.env.local",
SubPath: ".env.local",
MountPath: "/app/.env.production",
SubPath: ".env.production",
},
},
})
Expand All @@ -199,7 +249,7 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc
Items: []corev1.KeyToPath{
{
Key: ".env.registry-viewer",
Path: ".env.local",
Path: ".env.production",
},
},
},
Expand All @@ -213,6 +263,16 @@ func GenerateDeployment(cr *registryv1alpha1.DevfileRegistry, scheme *runtime.Sc
})
}

// Enables podspec security context if storage is enabled
if cr.Spec.Storage.Enabled == nil || *cr.Spec.Storage.Enabled {
dep.Spec.Template.Spec.SecurityContext = &corev1.PodSecurityContext{
RunAsNonRoot: &runAsNonRoot,
RunAsUser: &runAsUser,
RunAsGroup: &runAsGroup,
FSGroup: &fsGroup,
}
}

// Set DevfileRegistry instance as the owner and controller
_ = ctrl.SetControllerReference(cr, dep, scheme)
return dep
Expand Down