Skip to content

Commit

Permalink
Improve error handling and general cleanup (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
tekenstam committed Aug 21, 2019
1 parent b5e8fc9 commit d48bb32
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
@@ -1,5 +1,8 @@
language: go

services:
- docker

go:
- 1.12.x

Expand Down
7 changes: 4 additions & 3 deletions api/v1alpha1/podcheckpoint_types.go
Expand Up @@ -70,20 +70,21 @@ type PodCheckpointStatus struct {
Failed int32 `json:"failed,omitempty" protobuf:"varint,6,opt,name=failed"`
}

// PodCheckpointConditionType is used to define valid conditions.
type PodCheckpointConditionType string

// These are valid conditions of a PodSaveState.
// These are valid conditions of a PodCheckpoint.
const (
// JobComplete means the job has completed its execution.
PodCheckpointComplete PodCheckpointConditionType = "Complete"
// JobFailed means the job has failed its execution.
PodCheckpointFailed PodCheckpointConditionType = "Failed"
)

// PodSaveStateCondition describes current state of a PodSaveState.
// PodCheckpointCondition describes current state of a PodCheckpoint.
type PodCheckpointCondition struct {
// Type of job condition, Complete or Failed.
Type PodCheckpointConditionType `json:"type" protobuf:"bytes,1,opt,name=type,casttype=PodSaveStateConditionType"`
Type PodCheckpointConditionType `json:"type" protobuf:"bytes,1,opt,name=type,casttype=PodCheckpointConditionType"`
// Status of the condition, one of True, False, Unknown.
Status corev1.ConditionStatus `json:"status" protobuf:"bytes,2,opt,name=status,casttype=k8s.io/api/core/v1.ConditionStatus"`
// Last time the condition was checked.
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/podcheckpoint_types_test.go
Expand Up @@ -41,7 +41,7 @@ var _ = Describe("PodCheckpoint", func() {
// Add any teardown steps that needs to be executed after each test
})

// Add Tests for OpenAPI validation (or additonal CRD features) specified in
// Add Tests for OpenAPI validation (or additional CRD features) specified in
// your API definition.
// Avoid adding tests for vanilla CRUD operations because they would
// test Kubernetes API server, which isn't the goal here.
Expand Down
12 changes: 7 additions & 5 deletions controllers/podcheckpoint_controller.go
Expand Up @@ -66,6 +66,7 @@ func ignoreNotFound(err error) error {
// +kubebuilder:rbac:groups=forensics.orkaproj.io,resources=podcheckpoints,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=forensics.orkaproj.io,resources=podcheckpoints/status,verbs=get;update;patch

// Reconcile is the main entry point for comparing current state of custom resource with desired state and converge to the desired state
func (r *PodCheckpointReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("podcheckpoint", req.NamespacedName)
Expand Down Expand Up @@ -93,7 +94,7 @@ func (r *PodCheckpointReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
"instance.Status", podCheckpoint.Status,
"instance.Spec", podCheckpoint.Spec,
)
// Set the PodSaveState status based on the status of the owned Job
// Set the PodCheckpoint status based on the status of the owned Job
status := &forensicsv1alpha1.PodCheckpointStatus{
StartTime: &metav1.Time{Time: time.Now()},
CompletionTime: &metav1.Time{Time: time.Now()},
Expand Down Expand Up @@ -133,12 +134,12 @@ func (r *PodCheckpointReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
tok := strings.SplitAfter(containerStatus.ContainerID, "://")

if len(tok) != 2 || tok[0] != "docker://" {
// Return error if invalid containerId is provided.
err = utils.CommandError{ID: 1, Result: fmt.Sprintf("Unexpected ContainerID (%s)", containerStatus.ContainerID)}
break
} else {
id := tok[1]
s = append(s, id)
return ctrl.Result{}, err
}
id := tok[1]
s = append(s, id)
}

job := &batchv1.Job{
Expand Down Expand Up @@ -245,6 +246,7 @@ func (r *PodCheckpointReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
return ctrl.Result{}, nil
}

// SetupWithManager will configure the controller manager
func (r *PodCheckpointReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&forensicsv1alpha1.PodCheckpoint{}).
Expand Down
4 changes: 2 additions & 2 deletions kube-forensics-worker.sh
Expand Up @@ -2,7 +2,7 @@

# set -x

echo "Welcome to pod-savestate-worker!"
echo "Welcome to kube-forensics-worker!"

export timestamp=$(date +"%s")
short_container_id=${CONTAINER_ID:0:12}
Expand All @@ -19,7 +19,7 @@ echo Short Container ID: $short_container_id
echo Subpath: $SUBPATH
echo Destination Bucket: $DEST_BUCKET

local_dest_dir=/savestate/${SUBPATH}/${timestamp}
local_dest_dir=/forensics/${SUBPATH}/${timestamp}
mkdir -p ${local_dest_dir}

kubectl describe pod ${POD_NAME} -n ${NAMESPACE} > ${local_dest_dir}/${POD_NAME}.txt
Expand Down
2 changes: 1 addition & 1 deletion utils/commanderror.go
Expand Up @@ -2,7 +2,7 @@ package utils

import "fmt"

// This struct used to report errors.
// CommandError is used to report errors.
type CommandError struct {
ID int
Result string
Expand Down
6 changes: 4 additions & 2 deletions utils/kubernetesapi.go
Expand Up @@ -10,12 +10,13 @@ import (
"k8s.io/client-go/kubernetes"
)

// K8s client can be overridden for unit testing
// KubernetesAPI allows the K8s client to be overridden for unit testing
type KubernetesAPI struct {
Suffix string
Client kubernetes.Interface
}

// GetContainerIdsForPod is a helper function to get a list of all the container Ids in a Pod
func (k KubernetesAPI) GetContainerIdsForPod(podName string, ns string) ([]string, string, error) {
var options metav1.GetOptions
pod, err := k.Client.CoreV1().Pods(ns).Get(podName, options)
Expand Down Expand Up @@ -47,7 +48,8 @@ func (k KubernetesAPI) GetContainerIdsForPod(podName string, ns string) ([]strin
return s, pod.Spec.NodeName, err
}

func (k KubernetesAPI) CreateJobFromJson(jobAsJson string, ns string) error {
// CreateJobFromJSON is a helper function to unmarshal json into a Job object
func (k KubernetesAPI) CreateJobFromJSON(jobAsJson string, ns string) error {

var p batchv1.Job
err := json.Unmarshal([]byte(jobAsJson), &p)
Expand Down

0 comments on commit d48bb32

Please sign in to comment.