Skip to content

Commit

Permalink
Add cloud image registry implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
air-31 committed Dec 20, 2024
1 parent 07e4a5a commit c614fbe
Show file tree
Hide file tree
Showing 15 changed files with 933 additions and 39 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/build-matrix.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
"build-args": "COMPONENT=tenant-operator",
"harbor-project": "crownlabs-core"
},
{
"component": "cloudimg-registry",
"context": "./operators",
"dockerfile": "./operators/build/golang-common/Dockerfile",
"build-args": "COMPONENT=cloudimg-registry",
"harbor-project": "crownlabs-core"
},
{
"component": "bastion-operator",
"context": "./operators",
Expand Down
87 changes: 87 additions & 0 deletions operators/cmd/cloudimg-registry/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2020-2024 Politecnico di Torino
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package main contains the entrypoint for the cloud image registry.
package main

import (
"context"
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"time"

"k8s.io/klog/v2"

"github.com/netgroup-polito/CrownLabs/operators/pkg/ciregistry"
)

var (
dataRoot = flag.String("data-root", "/data", "Root data path for the server")
listenerAddr = flag.String("listener-addr", ":8080", "Address for the server to listen on")
readHeaderTimeoutSeconds = flag.Int("read-header-timeout-secs", 2, "Number of seconds allowed to read request headers")
)

func main() {
// Initialize klog
klog.InitFlags(nil)
defer klog.Flush()

// Parse flags
flag.Parse()

// Update ciregistry configuration
ciregistry.DataRoot = *dataRoot

// Start the server
server := initializeServer(*listenerAddr, *readHeaderTimeoutSeconds)

// Graceful shutdown setup
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)

go func() {
klog.Infof("Starting server on %s", server.Addr)
klog.Infof("API documentation available at http://localhost%s/docs", server.Addr)

if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
klog.Fatalf("Server failed: %v", err)
}
}()

<-stop
klog.Info("Shutting down server gracefully...")

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

if err := server.Shutdown(ctx); err != nil {
klog.Fatalf("Server forced to shutdown: %v", err)
}

klog.Info("Server gracefully stopped")
}

func initializeServer(addr string, readTimeoutSeconds int) *http.Server {
handler := ciregistry.NewRouter()
server := &http.Server{
Addr: addr,
Handler: handler,
ReadHeaderTimeout: time.Duration(readTimeoutSeconds) * time.Second,
}

return server
}
23 changes: 23 additions & 0 deletions operators/deploy/cloudimg-registry/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
20 changes: 20 additions & 0 deletions operators/deploy/cloudimg-registry/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: v2
name: cloudimg-registry
description: The CrownLabs Cloud Image Registry

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0

icon: https://crownlabs.polito.it/images/logo.svg
61 changes: 61 additions & 0 deletions operators/deploy/cloudimg-registry/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "cloudimg-registry.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If the release name contains the chart name, it will be used as a full name.
*/}}
{{- define "cloudimg-registry.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
The version of the application to be deployed
*/}}
{{- define "cloudimg-registry.version" -}}
{{- if .Values.global }}
{{- .Values.image.tag | default .Values.global.version | default .Chart.AppVersion }}
{{- else }}
{{- .Values.image.tag | default .Chart.AppVersion }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "cloudimg-registry.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "cloudimg-registry.labels" -}}
helm.sh/chart: {{ include "cloudimg-registry.chart" . }}
{{ include "cloudimg-registry.selectorLabels" . }}
app.kubernetes.io/version: {{ include "cloudimg-registry.version" . | quote }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "cloudimg-registry.selectorLabels" -}}
app.kubernetes.io/name: {{ include "cloudimg-registry.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
51 changes: 51 additions & 0 deletions operators/deploy/cloudimg-registry/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "cloudimg-registry.fullname" . }}
labels:
{{ include "cloudimg-registry.labels" . | nindent 4 }}
{{- with .Values.deploymentAnnotations }}
annotations:
{{- toYaml . | nindent 4 }}
{{- end }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{ include "cloudimg-registry.selectorLabels" . | nindent 6 }}
template:
metadata:
{{- with .Values.podAnnotations }}
annotations:
{{- toYaml . | nindent 8 }}
{{- end }}
labels:
{{- include "cloudimg-registry.selectorLabels" . | nindent 8 }}
spec:
{{- with .Values.imagePullSecrets }}
imagePullSecrets:
{{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ include "cloudimg-registry.version" . }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
args:
- "--data-root={{ .Values.configurations.dataRoot }}"
- "--read-header-timeout-secs={{ .Values.configurations.readHeaderTimeoutSeconds }}"
- "--listener-addr=:8080"
ports:
- name: http
containerPort: 8080
protocol: TCP
resources:
{{- toYaml .Values.resources | nindent 12 }}
volumeMounts:
- name: "{{ include "cloudimg-registry.fullname" . }}-storage"
mountPath: {{ .Values.configurations.dataRoot }}
securityContext:
{{- toYaml .Values.securityContext | nindent 12 }}
volumes:
- name: "{{ include "cloudimg-registry.fullname" . }}-storage"
persistentVolumeClaim:
claimName: "{{ include "cloudimg-registry.fullname" . }}-pvc"
13 changes: 13 additions & 0 deletions operators/deploy/cloudimg-registry/templates/pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: "{{ include "cloudimg-registry.fullname" . }}-pvc"
labels:
{{ include "cloudimg-registry.labels" . | nindent 4 }}
spec:
accessModes:
- {{ .Values.configurations.volume.accessMode }}
resources:
requests:
storage: {{ .Values.configurations.volume.size }}
storageClassName: {{ .Values.configurations.volume.storageClass }}
15 changes: 15 additions & 0 deletions operators/deploy/cloudimg-registry/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "cloudimg-registry.fullname" . }}
labels:
{{ include "cloudimg-registry.labels" . | nindent 4 }}
spec:
type: ClusterIP
selector:
{{ include "cloudimg-registry.selectorLabels" . | nindent 4 }}
ports:
- name: http
port: 80
targetPort: http
protocol: TCP
51 changes: 51 additions & 0 deletions operators/deploy/cloudimg-registry/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Default values for cloudimg registry.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

configurations:
# Comma separated list of whitelisted IPs (can include glob expressions) that can create instances
allowedIPs: ""
targetNamespace: "cloudimg-registry"
dataRoot: "/data"
readHeaderTimeoutSeconds: 2
volume:
size: "100Gi"
accessMode: "ReadWriteMany"
storageClass: "rook-cephfs-primary"

image:
repository: crownlabs/cloudimg-registry
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart version.
tag: ""

imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""

deploymentAnnotations: {}
podAnnotations: {}
ingressAnnotations: {}

podSecurityContext:
fsGroup: 2000

securityContext:
capabilities:
drop:
- ALL
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 2000
runAsGroup: 2000
privileged: false

resources:
limits:
memory: 500Mi
cpu: 1000m
requests:
memory: 500Mi
cpu: 100m
Loading

0 comments on commit c614fbe

Please sign in to comment.