Skip to content

Commit

Permalink
update work
Browse files Browse the repository at this point in the history
  • Loading branch information
pthomison committed Apr 5, 2024
1 parent 8acb717 commit 5b19dfd
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 147 deletions.
10 changes: 7 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ tasks:
image-registry: echo {{.REGISTRY}}
image-repository: echo {{.REPOSITORY}}

run:
create:
cmds:
- go run .
- go run {{.BUILD_FLAGS}} . create

delete:
cmds:
- go run {{.BUILD_FLAGS}} . delete

test:
env:
Expand All @@ -34,7 +38,7 @@ tasks:

e2e:
cmds:
- go test {{.BUILD_FLAGS}} ./e2e/... -v --count=1 #-tag containers_image_openpgp
- go test {{.BUILD_FLAGS}} ./e2e/... -v --count=1

docker-test-image:
cmds:
Expand Down
142 changes: 142 additions & 0 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package cmd

import (
"context"
"fmt"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/davecgh/go-spew/spew"
"github.com/pthomison/k3auto/internal/containers"
"github.com/pthomison/k3auto/internal/docker"
"github.com/pthomison/k3auto/internal/flux"
"github.com/pthomison/k3auto/internal/k8s"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"

kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
sourcev1beta2 "github.com/fluxcd/source-controller/api/v1beta2"
)

func ensureDeployment(ctx context.Context, k8sC client.Client, name string, namespace string, repository string, image string, tag string) error {
oci := sourcev1beta2.OCIRepository{}
err := k8sC.Get(ctx, client.ObjectKey{
Name: name,
Namespace: namespace,
}, &oci)

if err != nil && !errors.IsNotFound(err) {
spew.Dump(errors.IsNotFound(err), err)
return err
} else if err != nil {
oci = flux.NewOCIRepoObject(name, namespace, repository, image, tag)
err = k8sC.Create(ctx, &oci)
if err != nil {
return err
}
} else {
oci.Spec.Reference.Tag = tag
err = k8sC.Update(ctx, &oci)
if err != nil {
return err
}
}

kustomization := kustomizev1.Kustomization{}
err = k8sC.Get(ctx, client.ObjectKey{
Name: name,
Namespace: namespace,
}, &kustomization)

if err != nil && !errors.IsNotFound(err) {
return err
} else if err != nil {
kustomization = flux.NewOCIKustomizationObject(name, namespace)
err = k8sC.Create(ctx, &kustomization)
if err != nil {
return err
}
}

err = flux.WaitForKustomization(ctx, k8sC, v1.ObjectMeta{
Name: name,
Namespace: namespace,
})
if err != nil {
return err
}

return nil
}

func Deploy(ctx context.Context, name string, directory string, filesystem afero.Fs) error {

k8sC, err := k8s.NewClient()
if err != nil {
return err
}

repository := fmt.Sprintf("%v:5000", "docker-registry.docker-registry.svc.cluster.local")
localRepository := fmt.Sprintf("%v:5000", "127.0.0.1")

image := name
namespace := "kube-system"

logrus.Infof("%v Deployments Injecting", name)

initImageRef := fmt.Sprintf("%v:%v", name, name)

hash, err := docker.BuildImage(ctx, directory, docker.DumpDockerfile, []string{initImageRef}, filesystem)
if err != nil {
return err
}

tag := hash
imageRef := fmt.Sprintf("%v:%v", name, hash)

err = docker.TagImage(ctx, initImageRef, imageRef)
if err != nil {
return err
}

dep := appsv1.Deployment{}
err = k8sC.Get(ctx, client.ObjectKey{
Name: "docker-registry",
Namespace: "docker-registry",
}, &dep)
if err != nil {
return err
}

pods := corev1.PodList{}
var selector client.MatchingLabels = dep.Spec.Selector.MatchLabels
err = k8sC.List(ctx, &pods, selector)
if err != nil {
return err
}

closeChan, err := k8s.PortForward(ctx, pods.Items[0].Name, pods.Items[0].Namespace, 5000)
if err != nil {
return err
}

err = containers.PushImage(ctx, imageRef, localRepository)
if err != nil {
return err
}

close(closeChan)

err = ensureDeployment(ctx, k8sC, name, namespace, repository, image, tag)
if err != nil {
return err
}

logrus.Infof("%v Deployments Injected", name)

return nil
}
15 changes: 15 additions & 0 deletions cmd/forward.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

var ForwardCmd = &cobra.Command{
Use: "forward",
Short: "Forwards Ports To the Environment",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Implementation TBD")
},
}
11 changes: 10 additions & 1 deletion cmd/k3auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
_ "embed"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -25,5 +26,13 @@ func init() {
K3AutoCmd.AddCommand(VersionCmd)
K3AutoCmd.AddCommand(CreateCmd)
K3AutoCmd.AddCommand(DeleteCmd)
// K3AutoCmd.AddCommand(UpdateCmd)
K3AutoCmd.AddCommand(UpdateCmd)
// K3AutoCmd.AddCommand(ForwardCmd)
}

func checkError(err error) {
if err != nil {
logrus.Fatal(err)
panic(err)
}
}
95 changes: 0 additions & 95 deletions cmd/utils.go

This file was deleted.

19 changes: 10 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ require (
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
k8s.io/api v0.28.6
k8s.io/apiextensions-apiserver v0.28.6
k8s.io/apimachinery v0.28.6
k8s.io/client-go v0.28.6
k8s.io/kubectl v0.28.6
sigs.k8s.io/controller-runtime v0.16.3
k8s.io/api v0.29.0
k8s.io/apiextensions-apiserver v0.29.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v0.29.0
k8s.io/kubectl v0.29.0
sigs.k8s.io/controller-runtime v0.17.2
)

require (
Expand Down Expand Up @@ -55,7 +55,7 @@ require (
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fluxcd/pkg/apis/acl v0.1.0 // indirect
github.com/fluxcd/pkg/apis/kustomize v1.3.0 // indirect
Expand All @@ -68,7 +68,7 @@ require (
github.com/go-jose/go-jose/v3 v3.0.2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.2.4 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.21.1 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
Expand Down Expand Up @@ -127,6 +127,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/onsi/ginkgo/v2 v2.15.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
Expand Down Expand Up @@ -200,7 +201,7 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/component-base v0.28.6 // indirect
k8s.io/component-base v0.29.0 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect
Expand Down
Loading

0 comments on commit 5b19dfd

Please sign in to comment.