Skip to content

Commit

Permalink
feat: implement kube utils (#6)
Browse files Browse the repository at this point in the history
Signed-off-by: minhthong582000 <[email protected]>
  • Loading branch information
minhthong582000 authored Jun 3, 2024
1 parent 8a714e6 commit a1ce209
Show file tree
Hide file tree
Showing 15 changed files with 654 additions and 100 deletions.
2 changes: 1 addition & 1 deletion gitops/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.0-alpha.6
v0.1.0-alpha.7
21 changes: 16 additions & 5 deletions gitops/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
appinformers "github.com/minhthong582000/k8s-controller-pattern/gitops/pkg/informers/externalversions"
"github.com/minhthong582000/k8s-controller-pattern/gitops/pkg/signals"
"github.com/minhthong582000/k8s-controller-pattern/gitops/utils/git"
k8sutil "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/k8s"
k8sutil "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/kube"
logutil "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/log"
"github.com/spf13/cobra"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -50,11 +51,8 @@ var runCmd = &cobra.Command{
return err
}
}

config.Timeout = 120 * time.Second

gitClient := git.NewGitClient("")
k8sutil := k8sutil.NewK8s()
appClientSet, err := appclient.NewForConfig(config)
if err != nil {
return err
Expand All @@ -63,13 +61,26 @@ var runCmd = &cobra.Command{
if err != nil {
return err
}

// Set up the git client
gitUtil := git.NewGitClient("")
dynClientSet, err := dynamic.NewForConfig(config)
if err != nil {
return err
}

// Set up k8s utility
discoveryClient := clientSet.Discovery()
k8sutil := k8sutil.NewK8s(discoveryClient, dynClientSet)

// Set up the controller
appInformerFactory := appinformers.NewSharedInformerFactory(appClientSet, time.Second*30)
stopCh := signals.SetupSignalHandler()
ctrl := controller.NewController(
clientSet,
appClientSet,
appInformerFactory.Thongdepzai().V1alpha1().Applications(),
gitClient,
gitUtil,
k8sutil,
)
appInformerFactory.Start(stopCh)
Expand Down
26 changes: 13 additions & 13 deletions gitops/example/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ metadata:
thongdepzai.cloud/app: example-application
spec:
repository: https://github.com/minhthong582000/k8s-controller-pattern.git
revision: main
path: k8s-controller-pattern/gitops
---
kind: Application
apiVersion: thongdepzai.cloud/v1alpha1
metadata:
name: example-application-two
labels:
thongdepzai.cloud/app: example-application-two
spec:
repository: https://github.com/minhthong582000/k8s-controller-pattern.git
revision: main
path: k8s-controller-pattern/gitops
revision: feat/kube-util-impl
path: k8s-controller-pattern/gitops/utils/kube/testdata
# ---
# kind: Application
# apiVersion: thongdepzai.cloud/v1alpha1
# metadata:
# name: example-application-two
# labels:
# thongdepzai.cloud/app: example-application-two
# spec:
# repository: https://github.com/minhthong582000/k8s-controller-pattern.git
# revision: main
# path: k8s-controller-pattern/gitops
65 changes: 35 additions & 30 deletions gitops/internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
appinformers "github.com/minhthong582000/k8s-controller-pattern/gitops/pkg/informers/externalversions/application/v1alpha1"
applisters "github.com/minhthong582000/k8s-controller-pattern/gitops/pkg/listers/application/v1alpha1"
"github.com/minhthong582000/k8s-controller-pattern/gitops/utils/git"
k8sutil "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/k8s"
k8sutil "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/kube"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
Expand Down Expand Up @@ -190,7 +190,7 @@ func (c *Controller) processNextItem() bool {

func (c *Controller) createResources(ctx context.Context, app *v1alpha1.Application) error {
repoPath := path.Join(os.TempDir(), app.Name, strings.Replace(app.Spec.Repository, "/", "_", -1))

log.Println(repoPath)
err := c.updateAppStatus(
ctx,
app,
Expand All @@ -217,34 +217,39 @@ func (c *Controller) createResources(ctx context.Context, app *v1alpha1.Applicat
}
log.Debugf("Checked out revision %s", app.Spec.Revision)

// Generate manifests
oldResources, err := c.k8sUtil.GenerateManifests(path.Join(repoPath, app.Spec.Path))
if err != nil {
return fmt.Errorf("error generating manifests: %s", err)
}

// Get current resources
label := fmt.Sprintf("%s=%s", common.LabelKeyAppInstance, app.Name)
newResources, err := c.k8sUtil.GetResourceWithLabel(label)
if err != nil {
return fmt.Errorf("error getting resources with label: %s, %s", label, err)
}

// Calculate diff
diff, err := c.k8sUtil.DiffResources(oldResources, newResources)
if err != nil {
return fmt.Errorf("error diffing resources: %s", err)
}
if !diff {
log.Info("No changes in resources, skipping")
return nil
}

// Apply manifests
err = c.k8sUtil.ApplyResource(path.Join(repoPath, app.Spec.Path))
if err != nil {
return fmt.Errorf("error applying resources: %s", err)
}
// // Generate manifests
// log.Infof("Generating manifests for application %s", app.Name)
// generatedResources, err := c.k8sUtil.GenerateManifests(path.Join(repoPath, app.Spec.Path))
// if err != nil {
// return fmt.Errorf("error generating manifests: %s", err)
// }

// // Get current resources
// log.Infof("Getting resources for application %s", app.Name)
// label := map[string]string{
// common.LabelKeyAppInstance: app.Name,
// }
// currentResources, err := c.k8sUtil.GetResourceWithLabel(label)
// if err != nil {
// return fmt.Errorf("error getting resources with label: %s, %s", label, err)
// }

// // Calculate diff
// log.Infof("Diffing resources for application %s", app.Name)
// diff, err := c.k8sUtil.DiffResources(generatedResources, currentResources)
// if err != nil {
// return fmt.Errorf("error diffing resources: %s", err)
// }
// if !diff {
// log.Info("No changes in resources, skipping")
// return nil
// }

// // Apply manifests
// err = c.k8sUtil.ApplyResource(path.Join(repoPath, app.Spec.Path))
// if err != nil {
// return fmt.Errorf("error applying resources: %s", err)
// }

err = c.updateAppStatus(
ctx,
Expand Down
26 changes: 20 additions & 6 deletions gitops/internal/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
appclientset "github.com/minhthong582000/k8s-controller-pattern/gitops/pkg/clientset/versioned/fake"
appinformers "github.com/minhthong582000/k8s-controller-pattern/gitops/pkg/informers/externalversions"
"github.com/minhthong582000/k8s-controller-pattern/gitops/utils/git"
k8sutil "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/k8s"
k8sutil "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/kube"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -35,7 +35,7 @@ func newFakeController(apps ...runtime.Object) *Controller {
kubeClientSet := fake.NewSimpleClientset()
appClientSet := appclientset.NewSimpleClientset(apps...)
gitClient := git.NewGitClient("")
k8sUtil := k8sutil.NewK8s()
k8sUtil := k8sutil.NewK8s(nil, nil)
appInformerFactory := appinformers.NewSharedInformerFactory(appClientSet, time.Second*30)

return NewController(
Expand All @@ -56,12 +56,26 @@ var (
expectedErr string
}{
{
name: "Normal application",
name: "Normal application 1",
app: `
kind: Application
apiVersion: thongdepzai.cloud/v1alpha1
metadata:
name: example-application
name: test-example-application-one
spec:
repository: https://github.com/minhthong582000/k8s-controller-pattern.git
revision: main
path: k8s-controller-pattern/gitops
`,
expectedStatus: v1alpha1.HealthStatusCode(v1alpha1.HealthStatusHealthy),
},
{
name: "Normal application 2",
app: `
kind: Application
apiVersion: thongdepzai.cloud/v1alpha1
metadata:
name: test-example-application-two
spec:
repository: https://github.com/minhthong582000/k8s-controller-pattern.git
revision: main
Expand Down Expand Up @@ -137,7 +151,7 @@ var (
kind: Application
apiVersion: thongdepzai.cloud/v1alpha1
metadata:
name: another-example-application
name: test-another-example-application-one
spec:
repository: https://github.com/minhthong582000/k8s-controller-pattern.git
revision: main
Expand All @@ -150,7 +164,7 @@ spec:
kind: Application
apiVersion: thongdepzai.cloud/v1alpha1
metadata:
name: another-example-application
name: test-another-example-application-two
spec:
repository: https://github.com/kubernetes/kubernetes-but-not-exist.git
revision: main
Expand Down
4 changes: 1 addition & 3 deletions gitops/utils/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ func (g *gitClient) CloneOrFetch(url, path string) error {
if err != nil {
return fmt.Errorf("failed to clone repository: %w", err)
}

return nil
}

// Fetch the latest changes if it's already cloned
// Fetch the latest changes
r, err := git.PlainOpen(path)
if err != nil {
return fmt.Errorf("failed to open repository: %w", err)
Expand Down
7 changes: 4 additions & 3 deletions gitops/utils/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ func TestGitClient_CloneOrFetch_CleanUp(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
path := path.Join(os.TempDir(), strings.Replace(tt.url, "/", "_", -1))

g := &gitClient{
token: tt.gitClient.token,
}
g := NewGitClient(tt.gitClient.token)
err := g.CloneOrFetch(tt.url, path)
if err != nil {
assert.Equal(t, tt.expectedErr, err.Error())
Expand All @@ -55,11 +53,14 @@ func TestGitClient_CloneOrFetch_CleanUp(t *testing.T) {
err = g.CloneOrFetch(tt.url, path)
if err != nil {
assert.Equal(t, tt.expectedErr, err.Error())
return
}
assert.DirExists(t, path)

// Clean up
err = g.CleanUp(path)
assert.NoError(t, err)
assert.NoDirExists(t, path)
})
}
}
38 changes: 0 additions & 38 deletions gitops/utils/k8s/k8s.go

This file was deleted.

1 change: 0 additions & 1 deletion gitops/utils/k8s/k8s_test.go

This file was deleted.

Loading

0 comments on commit a1ce209

Please sign in to comment.