From 3243895abcaae0fcffc471e4b6bd140141a6573b Mon Sep 17 00:00:00 2001 From: 6za <53096417+6za@users.noreply.github.com> Date: Wed, 15 Feb 2023 01:23:44 +0000 Subject: [PATCH] feat: add compose down behavior Signed-off-by: 6za <53096417+6za@users.noreply.github.com> --- operator/api/v1/dockercomposerunner_types.go | 5 +- operator/controllers/agent_utils.go | 32 ++------- .../dockercomposerunner_controller.go | 71 ++++++++++++++----- 3 files changed, 60 insertions(+), 48 deletions(-) diff --git a/operator/api/v1/dockercomposerunner_types.go b/operator/api/v1/dockercomposerunner_types.go index 05d6216..df47592 100644 --- a/operator/api/v1/dockercomposerunner_types.go +++ b/operator/api/v1/dockercomposerunner_types.go @@ -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"` diff --git a/operator/controllers/agent_utils.go b/operator/controllers/agent_utils.go index 3c9e621..6c3f105 100644 --- a/operator/controllers/agent_utils.go +++ b/operator/controllers/agent_utils.go @@ -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, @@ -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", @@ -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 -*/ diff --git a/operator/controllers/dockercomposerunner_controller.go b/operator/controllers/dockercomposerunner_controller.go index bc4af89..415ae6d 100644 --- a/operator/controllers/dockercomposerunner_controller.go +++ b/operator/controllers/dockercomposerunner_controller.go @@ -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" @@ -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 @@ -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 }