Skip to content

Commit

Permalink
Merge pull request #434 from claudiolor/master
Browse files Browse the repository at this point in the history
  • Loading branch information
kingmakerbot authored May 6, 2021
2 parents 88ac0c0 + f372a1a commit fecc75e
Show file tree
Hide file tree
Showing 24 changed files with 1,504 additions and 38 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ jobs:
# SSH bastion
- ssh-bastion

# img-export for persistent VM instances snapshot
- img-exporter

# Laboratory environments
- novnc
- tigervnc
Expand Down Expand Up @@ -156,6 +159,10 @@ jobs:
- component: ssh-bastion
context: ./operators/build/ssh-bastion

# img-exporter image for InstanceSnapshot
- component: img-exporter
context: ./operators/build/img-exporter

steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
41 changes: 41 additions & 0 deletions deploy/crownlabs/templates/clusterroles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,47 @@ rules:
- patch
- delete
- deletecollection

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: crownlabs-view-instance-snapshots
labels:
{{- include "crownlabs.labels" . | nindent 4 }}
rules:
- apiGroups:
- crownlabs.polito.it
resources:
- instancesnapshots
- instancesnapshots/status
verbs:
- get
- list
- watch

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: crownlabs-manage-instance-snapshots
labels:
{{- include "crownlabs.labels" . | nindent 4 }}
rules:
- apiGroups:
- crownlabs.polito.it
resources:
- instancesnapshots
- instancesnapshots/status
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
- deletecollection

---
apiVersion: rbac.authorization.k8s.io/v1
Expand Down
7 changes: 7 additions & 0 deletions deploy/crownlabs/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ instance-operator:
novncImage: crownlabs/novnc
filebrowserImage: filebrowser/filebrowser
filebrowserImageTag: latest
containerVmSnapshots:
kanikoImage: gcr.io/kaniko-project/executor
exportImage: "crownlabs/img-exporter"
exportImageTag: ""
privateContainerRegistry:
url: registry.crownlabs.example.com
secretName: registry-credentials

tenant-operator:
replicaCount: 1
Expand Down
3 changes: 2 additions & 1 deletion operators/api/v1alpha2/instance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type InstanceSpec struct {
Tenant GenericRef `json:"tenant.crownlabs.polito.it/TenantRef"`

// +kubebuilder:default=true
// +kubebuilder:validation:Optional

// Whether the current instance is running or not. This field is meaningful
// only in case the Instance refers to persistent environments, and it allows
Expand All @@ -39,7 +40,7 @@ type InstanceSpec struct {
// attaching it to the same disk used previously. The flag, on the other hand,
// is silently ignored in case of non-persistent environments, as the state
// cannot be preserved among reboots.
Running bool `json:"running,omitempty"`
Running bool `json:"running"`
}

// InstanceStatus reflects the most recently observed status of the Instance.
Expand Down
94 changes: 94 additions & 0 deletions operators/api/v1alpha2/instancesnapshot_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
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 v1alpha2

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// SnapshotStatus is an enumeration representing the current state of the InstanceSnapshot.
type SnapshotStatus string

const (
// Pending -> The snapshot resource has been observed and the
// process is waiting to be started.
Pending SnapshotStatus = "Pending"
// Processing -> The process of creation of the snapshot started.
Processing SnapshotStatus = "Processing"
// Completed -> The snapshot of the instance has been created.
Completed SnapshotStatus = "Completed"
// Failed -> The process of creation of the snapshot failed.
Failed SnapshotStatus = "Failed"
)

// InstanceSnapshotSpec defines the desired state of InstanceSnapshot.
type InstanceSnapshotSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Instance is the reference to the persistent VM instance to be snapshotted.
// The instance should not be running, otherwise it won't be possible to
// steal the volume and extract its content.
Instance GenericRef `json:"instanceRef"`

// Environment represents the reference to the environment to be snapshotted, in case more are
// associated with the same Instance. If not specified, the first available environment is considered.
Environment GenericRef `json:"environmentRef,omitempty"`

// +kubebuilder:validation:MinLength=1

// ImageName is the name of the image to pushed in the docker registry.
ImageName string `json:"imageName"`
}

// InstanceSnapshotStatus defines the observed state of InstanceSnapshot.
type InstanceSnapshotStatus struct {
// Phase represents the current state of the Instance Snapshot.
Phase SnapshotStatus `json:"phase"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:shortName="isnap"
// +kubebuilder:printcolumn:name="Phase",type=string,JSONPath=`.status.phase`
// +kubebuilder:printcolumn:name="ImageName",type=string,JSONPath=`.spec.imageName`
// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`

// InstanceSnapshot is the Schema for the instancesnapshots API.
type InstanceSnapshot struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec InstanceSnapshotSpec `json:"spec,omitempty"`
Status InstanceSnapshotStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// InstanceSnapshotList contains a list of InstanceSnapshot.
type InstanceSnapshotList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []InstanceSnapshot `json:"items"`
}

func init() {
SchemeBuilder.Register(&InstanceSnapshot{}, &InstanceSnapshotList{})
}
91 changes: 91 additions & 0 deletions operators/api/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions operators/build/img-exporter/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM alpine:3.13.4

# Install the qemu-img useful to convert the image
RUN apk add --update --no-cache qemu-img

# Copy the entrypoint script
COPY exporter.sh /

# Run the entrypoint which converts the image and creates the dockerfile
CMD ["/exporter.sh"]
63 changes: 63 additions & 0 deletions operators/build/img-exporter/exporter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/sh
IMG_DIR=/data
OUT_DIR=/img-tmp
IMG_NAME=disk.img
OUT_IMAGE=vm-snapshot.qcow2
PROG_NAME=$0

usage(){
echo "Usage: $PROG_NAME [-options]"
echo " -d, --img-dir Specify the working directory [DEFAULT=$IMG_DIR]"
echo " -o, --out-dir Specify the output directory [DEFAULT=$OUT_DIR]"
echo " -n, --img-name Specify the name of the image [DEFAULT=$OUT_IMAGE]"
exit 1
}

parse_args(){
while [ "${1:-}" != "" ]; do
case "$1" in
"-d" | "--img-dir")
shift
IMG_DIR=$1
;;
"-o" | "--out-dir")
shift
OUT_DIR=$1
;;
"-n" | "--img-name")
shift
IMG_NAME=$1
;;
*)
usage
;;
esac
shift
done
}

export_img(){
echo "Converting the image..."

# Check if output directory exists, if not create it
# and try with the conversion of the image.
mkdir -p "$OUT_DIR"
qemu-img convert -c -f raw -O qcow2 "${IMG_DIR}/${IMG_NAME}" "${OUT_DIR}/${OUT_IMAGE}"

echo "Creating Dockerfile..."
# Create the Dockerfile.
cat <<EOF > "${OUT_DIR}/Dockerfile"
FROM scratch
ADD ${OUT_IMAGE} /disk/
EOF
}

parse_args "$@"

if export_img;
then
echo "${IMG_DIR}/${IMG_NAME} successully converted"
else
echo "Conversion unsuccessfully completed"
exit 1
fi
Loading

0 comments on commit fecc75e

Please sign in to comment.