Skip to content

Commit

Permalink
Merge pull request #9 from 6zacode-toolbox/add_compose_down
Browse files Browse the repository at this point in the history
feat: add compose down behavior
  • Loading branch information
6za authored Feb 15, 2023
2 parents 4ecb1af + 3243895 commit fd3030e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 48 deletions.
5 changes: 2 additions & 3 deletions operator/api/v1/dockercomposerunner_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ type ComposeStatus struct {
ConfigFiles string `json:"configFiles,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// DockerComposeRunner is the Schema for the dockercomposerunners API
type DockerComposeRunner struct {
metav1.TypeMeta `json:",inline"`
Expand Down
32 changes: 6 additions & 26 deletions operator/controllers/agent_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ func CreateDockerHostCronJob(crd *v1.DockerHost) (*v1batch.CronJob, error) {
return cronjob, nil
}

func CreateDockerComposeRunnerJob(crd *v1.DockerComposeRunner, action string) (*v1batch.Job, error) {
func CreateDockerComposeRunnerJob(crd *v1.CrdDefinition, action string) (*v1batch.Job, error) {
podSpec, _ := CreateDockerComposeRunnerPodSpec(crd.Name, action)
jobMinimal := InstantiateMinimalDockerComposeRunnerJob(crd.Name, NamespaceJobs)
jobMinimal.Labels = GetLabels(crd.GetCrdDefinition())
jobMinimal.Labels = GetLabels(crd)
TTL := int32(30)
job := &v1batch.Job{
ObjectMeta: jobMinimal.ObjectMeta,
Spec: v1batch.JobSpec{
TTLSecondsAfterFinished: &TTL,
Template: apiV1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: jobMinimal.Labels,
Expand Down Expand Up @@ -313,10 +315,12 @@ func checkCronJob(job *v1batch.CronJob) {
func CreateDockerComposeRunnerConfigMap(crd *v1.DockerComposeRunner) *apiV1.ConfigMap {
configMapName := GenerateComposeRunnerConfigMapName(crd.Name)
crdConfig := crd.GetCrdDefinition()
labels := GetLabels(crdConfig)
configMap := &apiV1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: NamespaceJobs,
Labels: labels,
},
Data: map[string]string{
"DOCKER_CERT_PATH": "/certs",
Expand Down Expand Up @@ -361,27 +365,3 @@ func RandStringRunes(n int) string {
func init() {
rand.Seed(time.Now().UnixNano())
}

/*
containers:
- name: docker-agent
image: 6zar/docker-agent:latest
imagePullPolicy: Always
env:
- name: DOCKER_CERT_PATH
value: "/certs"
- name: DOCKER_HOST
value: "tcp://192.168.2.162:2376"
- name: DOCKER_TLS_VERIFY
value: "1"
command: ['/home/app/docker-agent', 'agent', '--crd-api-version', 'tool.6zacode-toolbox.github.io/v1', '--crd-namespace', 'default', '--crd-instance', 'dockerhost-sample', '--crd-resource', 'dockerhosts']
volumeMounts:
- mountPath: "/certs"
name: docker-certs
readOnly: true
serviceAccountName: docker-agent-sa
volumes:
- name: docker-certs
secret:
secretName: docker-secret
*/
71 changes: 52 additions & 19 deletions operator/controllers/dockercomposerunner_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"google.golang.org/grpc/status"
v1batch "k8s.io/api/batch/v1"
apiV1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/cri-api/pkg/errors"
Expand Down Expand Up @@ -114,7 +113,7 @@ func (r *DockerComposeRunnerReconciler) Reconcile(ctx context.Context, req ctrl.
return reconcile.Result{}, nil
}

desiredJob, err := CreateDockerComposeRunnerJob(instance, "up -d")
desiredJob, err := CreateDockerComposeRunnerJob(instance.GetCrdDefinition(), "up -d")
if err != nil {
log.Log.Error(err, "Event Type(err getting desiredState):"+eventType)
return reconcile.Result{}, err
Expand Down Expand Up @@ -165,58 +164,92 @@ func (r *DockerComposeRunnerReconciler) Reconcile(ctx context.Context, req ctrl.
func (r *DockerComposeRunnerReconciler) RunComposeUpJob(desiredJob *v1batch.Job, instance *v1.DockerComposeRunner) error {
//Create ConfigMap
log.Log.Info("RunComposeUpJob:" + instance.Name)
definition := instance.GetCrdDefinition()
cms := &apiV1.ConfigMap{}
err := r.DeleteAllOf(context.TODO(), cms, client.InNamespace(definition.Namespace), client.MatchingLabels(GetLabels(definition)), client.GracePeriodSeconds(5))
if err != nil {
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", definition.Namespace, definition.Name))
return err
}
configMap := CreateDockerComposeRunnerConfigMap(instance)
err := r.Create(context.TODO(), configMap)
err = r.Create(context.TODO(), configMap)
if err != nil {
log.Log.Error(err, fmt.Sprintf("Error creating configmap %s/%s\n", instance.Namespace, configMap))
return err

}

action := "up -d"
err = r.CleanAndCreateJob(instance.GetCrdDefinition(), action)
if err != nil {
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", instance.Namespace, instance.Name))
return err
}
return nil
}

func (r *DockerComposeRunnerReconciler) CleanAndCreateJob(definition *v1.CrdDefinition, action string) error {
//remove old jobs
jobs := &v1batch.Job{}
err = r.DeleteAllOf(context.TODO(), jobs, client.InNamespace(instance.Namespace), client.MatchingLabels(GetLabels(instance.GetCrdDefinition())), client.GracePeriodSeconds(5))
err := r.DeleteAllOf(context.TODO(), jobs, client.InNamespace(definition.Namespace), client.MatchingLabels(GetLabels(definition)), client.GracePeriodSeconds(5))
if err != nil {
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", instance.Namespace, instance.Name))
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", definition.Namespace, definition.Name))
return err

}
//rmeove old pods

//remove old pods
pods := &apiV1.Pod{}
err = r.DeleteAllOf(context.TODO(), pods, client.InNamespace(instance.Namespace), client.MatchingLabels(GetLabels(instance.GetCrdDefinition())), client.GracePeriodSeconds(5))
err = r.DeleteAllOf(context.TODO(), pods, client.InNamespace(definition.Namespace), client.MatchingLabels(GetLabels(definition)), client.GracePeriodSeconds(5))
if err != nil {
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", instance.Namespace, instance.Name))
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", definition.Namespace, definition.Name))
return err
}

//create new job
job, err := CreateDockerComposeRunnerJob(instance, "up -d")
job, err := CreateDockerComposeRunnerJob(definition, action)
if err != nil {
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", instance.Namespace, job.Name))
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", definition.Namespace, job.Name))
return err

}
err = r.Create(context.TODO(), job)
if err != nil {
log.Log.Error(err, fmt.Sprintf("Error creating job %s/%s\n", instance.Namespace, job.Name))
log.Log.Error(err, fmt.Sprintf("Error creating job %s/%s\n", definition.Namespace, job.Name))
return err

}

return nil
}
func (r *DockerComposeRunnerReconciler) RunComposeDownJob(name string, namespace string) error {
//Delete ConfigMap
configMap := &apiV1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: GenerateComposeRunnerConfigMapName(name),
Namespace: namespace,
},
}
_ = r.Delete(context.TODO(), configMap)
/*
configMap := &apiV1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: GenerateComposeRunnerConfigMapName(name),
Namespace: namespace,
},
}
_ = r.Delete(context.TODO(), configMap)
*/
//Err is irrelevant here, as most of the case the delete is no-ops as the resource should not exist yet.

// !TODO: Add some pod or action to clean the jobs dispached.
// USE TTL: https://kubernetes.io/docs/concepts/workloads/controllers/ttlafterfinished/#caveats

defintion := &v1.CrdDefinition{
Name: name,
Namespace: namespace,
APIVersion: "tool.6zacode-toolbox.github.io/v1",
Resource: "dockercomposerunners",
}
action := "down"
err := r.CleanAndCreateJob(defintion, action)
if err != nil {
log.Log.Error(err, fmt.Sprintf("Error creating job object %s/%s\n", defintion.Namespace, defintion.Name))
return err
}

return nil
}

Expand Down

0 comments on commit fd3030e

Please sign in to comment.