diff --git a/gitops/VERSION b/gitops/VERSION index 3032942..f4e9b20 100644 --- a/gitops/VERSION +++ b/gitops/VERSION @@ -1 +1 @@ -v0.1.0-alpha.7 +v0.1.0-alpha.8 diff --git a/gitops/example/application.yaml b/gitops/example/application.yaml index 537dfa1..7ab2c59 100644 --- a/gitops/example/application.yaml +++ b/gitops/example/application.yaml @@ -3,12 +3,10 @@ kind: Application apiVersion: thongdepzai.cloud/v1alpha1 metadata: name: example-application - labels: - thongdepzai.cloud/app: example-application spec: repository: https://github.com/minhthong582000/k8s-controller-pattern.git - revision: feat/kube-util-impl - path: k8s-controller-pattern/gitops/utils/kube/testdata + revision: main + path: gitops/utils/kube/testdata # --- # kind: Application # apiVersion: thongdepzai.cloud/v1alpha1 diff --git a/gitops/go.mod b/gitops/go.mod index 3340688..6cbe4c6 100644 --- a/gitops/go.mod +++ b/gitops/go.mod @@ -7,6 +7,7 @@ require ( github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.9.0 + go.uber.org/mock v0.4.0 k8s.io/api v0.30.1 k8s.io/apimachinery v0.30.1 k8s.io/client-go v0.30.1 diff --git a/gitops/go.sum b/gitops/go.sum index c04426f..4de69f7 100644 --- a/gitops/go.sum +++ b/gitops/go.sum @@ -139,6 +139,8 @@ github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU= +go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= diff --git a/gitops/internal/controller/controller.go b/gitops/internal/controller/controller.go index e635a1a..7f5d918 100644 --- a/gitops/internal/controller/controller.go +++ b/gitops/internal/controller/controller.go @@ -217,39 +217,38 @@ func (c *Controller) createResources(ctx context.Context, app *v1alpha1.Applicat } log.Debugf("Checked out revision %s", app.Spec.Revision) - // // 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) - // } + // 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(currentResources, generatedResources) + if err != nil { + return fmt.Errorf("error diffing resources: %s", err) + } + if diff { + // Apply manifests + err = c.k8sUtil.ApplyResource(path.Join(repoPath, app.Spec.Path)) + if err != nil { + return fmt.Errorf("error applying resources: %s", err) + } + } else { + log.WithField("application", app.Name).Info("No changes in resources") + } err = c.updateAppStatus( ctx, @@ -279,7 +278,12 @@ func (c *Controller) deleteResources(app *v1alpha1.Application) error { repoPath := path.Join(os.TempDir(), app.Name, strings.Replace(app.Spec.Repository, "/", "_", -1)) log.WithField("application", app.Name).Info("Deleting resources") - err := c.gitUtil.CleanUp(repoPath) + err := c.k8sUtil.DeleteResource(path.Join(repoPath, app.Spec.Path)) + if err != nil { + return fmt.Errorf("error deleting resources: %s", err) + } + + err = c.gitUtil.CleanUp(repoPath) if err != nil { return fmt.Errorf("error cleaning up repository: %s", err) } diff --git a/gitops/internal/controller/controller_test.go b/gitops/internal/controller/controller_test.go index 07aa0cc..81f65ed 100644 --- a/gitops/internal/controller/controller_test.go +++ b/gitops/internal/controller/controller_test.go @@ -3,6 +3,7 @@ package controller import ( "bytes" "context" + "fmt" "os" "path" "strings" @@ -13,8 +14,11 @@ 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/kube" + gitMock "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/git/mock" + k8sUtil "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/kube" + k8sUtilMock "github.com/minhthong582000/k8s-controller-pattern/gitops/utils/kube/mock" "github.com/stretchr/testify/assert" + "go.uber.org/mock/gomock" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" k8syaml "k8s.io/apimachinery/pkg/util/yaml" @@ -31,11 +35,9 @@ func newFakeApp(appString string) *v1alpha1.Application { return &app } -func newFakeController(apps ...runtime.Object) *Controller { +func newFakeController(gitClient git.GitClient, k8sUtil k8sUtil.K8s, apps ...runtime.Object) *Controller { kubeClientSet := fake.NewSimpleClientset() appClientSet := appclientset.NewSimpleClientset(apps...) - gitClient := git.NewGitClient("") - k8sUtil := k8sutil.NewK8s(nil, nil) appInformerFactory := appinformers.NewSharedInformerFactory(appClientSet, time.Second*30) return NewController( @@ -47,16 +49,20 @@ func newFakeController(apps ...runtime.Object) *Controller { ) } -var ( - createResourcesTestCases = []struct { +func Test_CreateResources(t *testing.T) { + ctrl := gomock.NewController(t) + + createResourcesTestCases := []struct { name string app string + mockGitClient git.GitClient + mockk8sUtil k8sUtil.K8s expectedOut string expectedStatus v1alpha1.HealthStatusCode expectedErr string }{ { - name: "Normal application 1", + name: "Should create resources successfully if the repository is valid", app: ` kind: Application apiVersion: thongdepzai.cloud/v1alpha1 @@ -67,24 +73,51 @@ spec: revision: main path: k8s-controller-pattern/gitops `, + mockGitClient: func() git.GitClient { + mock := gitMock.NewMockGitClient(ctrl) + mock.EXPECT().CloneOrFetch(gomock.Any(), gomock.Any()).Return(nil) + mock.EXPECT().Checkout(gomock.Any(), gomock.Any()).Return("randomsha", nil) + return mock + }(), + mockk8sUtil: func() k8sUtil.K8s { + mock := k8sUtilMock.NewMockK8s(ctrl) + mock.EXPECT().GenerateManifests(gomock.Any()).Return(nil, nil) + mock.EXPECT().GetResourceWithLabel(gomock.Any()).Return(nil, nil) + mock.EXPECT().DiffResources(gomock.Any(), gomock.Any()).Return(true, nil) + mock.EXPECT().ApplyResource(gomock.Any()).Return(nil) + return mock + }(), expectedStatus: v1alpha1.HealthStatusCode(v1alpha1.HealthStatusHealthy), }, { - name: "Normal application 2", + name: "Should create resources successfully even if there is no diff between the old and new resources", app: ` kind: Application apiVersion: thongdepzai.cloud/v1alpha1 metadata: - name: test-example-application-two + name: test-example-application-one spec: repository: https://github.com/minhthong582000/k8s-controller-pattern.git revision: main path: k8s-controller-pattern/gitops `, + mockGitClient: func() git.GitClient { + mock := gitMock.NewMockGitClient(ctrl) + mock.EXPECT().CloneOrFetch(gomock.Any(), gomock.Any()).Return(nil) + mock.EXPECT().Checkout(gomock.Any(), gomock.Any()).Return("randomsha", nil) + return mock + }(), + mockk8sUtil: func() k8sUtil.K8s { + mock := k8sUtilMock.NewMockK8s(ctrl) + mock.EXPECT().GenerateManifests(gomock.Any()).Return(nil, nil) + mock.EXPECT().GetResourceWithLabel(gomock.Any()).Return(nil, nil) + mock.EXPECT().DiffResources(gomock.Any(), gomock.Any()).Return(false, nil) + return mock + }(), expectedStatus: v1alpha1.HealthStatusCode(v1alpha1.HealthStatusHealthy), }, { - name: "Application with invalid repository", + name: "Should return error if the application has invalid repository", app: ` kind: Application apiVersion: thongdepzai.cloud/v1alpha1 @@ -95,35 +128,28 @@ spec: revision: main path: k8s-controller-pattern/gitops `, + mockGitClient: func() git.GitClient { + mock := gitMock.NewMockGitClient(ctrl) + mock.EXPECT().CloneOrFetch(gomock.Any(), gomock.Any()).Return( + fmt.Errorf("failed to clone repository: authentication required"), + ) + return mock + }(), expectedStatus: v1alpha1.HealthStatusCode(v1alpha1.HealthStatusProgressing), expectedErr: "error cloning repository: failed to clone repository: authentication required", }, } -) -func Test_CreateResources(t *testing.T) { for _, tt := range createResourcesTestCases { t.Run(tt.name, func(t *testing.T) { ctx := context.Background() app := newFakeApp(tt.app) - controller := newFakeController(app) + controller := newFakeController(tt.mockGitClient, tt.mockk8sUtil, app) err := controller.createResources(ctx, app) if err != nil { assert.Equal(t, tt.expectedErr, err.Error()) - } else { - repoPath := path.Join(os.TempDir(), app.Name, strings.Replace(app.Spec.Repository, "/", "_", -1)) - - // Check if git repository is cloned - assert.DirExists(t, repoPath) - - // TODO: check if the revision is checked out - - // Delete the git repository - err = os.RemoveAll(repoPath) - assert.NoError(t, err) - assert.NoDirExists(t, repoPath) } // Check the status of the application @@ -137,16 +163,20 @@ func Test_CreateResources(t *testing.T) { } } -var ( - deleteResourcesTestCases = []struct { +func Test_DeleteResources(t *testing.T) { + ctrl := gomock.NewController(t) + + testCases := []struct { name string app string + mockGitClient git.GitClient + mockk8sUtil k8sUtil.K8s expectedOut string expectedStatus string expectedErr string }{ { - name: "Normal application", + name: "Should delete resources successfully if the application is valid", app: ` kind: Application apiVersion: thongdepzai.cloud/v1alpha1 @@ -157,9 +187,19 @@ spec: revision: main path: k8s-controller-pattern/gitops `, + mockGitClient: func() git.GitClient { + mock := gitMock.NewMockGitClient(ctrl) + mock.EXPECT().CleanUp(gomock.Any()).Return(nil) + return mock + }(), + mockk8sUtil: func() k8sUtil.K8s { + mock := k8sUtilMock.NewMockK8s(ctrl) + mock.EXPECT().DeleteResource(gomock.Any()).Return(nil) + return mock + }(), }, { - name: "Application with invalid repository", + name: "Should delete resources successfully even if the application has invalid repository", app: ` kind: Application apiVersion: thongdepzai.cloud/v1alpha1 @@ -170,15 +210,23 @@ spec: revision: main path: k8s-controller-pattern/gitops `, + mockGitClient: func() git.GitClient { + mock := gitMock.NewMockGitClient(ctrl) + mock.EXPECT().CleanUp(gomock.Any()).Return(nil) + return mock + }(), + mockk8sUtil: func() k8sUtil.K8s { + mock := k8sUtilMock.NewMockK8s(ctrl) + mock.EXPECT().DeleteResource(gomock.Any()).Return(nil) + return mock + }(), }, } -) -func Test_DeleteResources(t *testing.T) { - for _, tt := range deleteResourcesTestCases { + for _, tt := range testCases { t.Run(tt.name, func(t *testing.T) { app := newFakeApp(tt.app) - controller := newFakeController(app) + controller := newFakeController(tt.mockGitClient, tt.mockk8sUtil, app) // Create a fake git repository repoPath := path.Join(os.TempDir(), app.Name, strings.Replace(app.Spec.Repository, "/", "_", -1)) @@ -191,9 +239,6 @@ func Test_DeleteResources(t *testing.T) { if err != nil { assert.Equal(t, tt.expectedErr, err.Error()) } - - // Check if the git repository is deleted - assert.NoDirExists(t, repoPath) }) } } diff --git a/gitops/utils/git/git.go b/gitops/utils/git/git.go index de6ebc2..9d89972 100644 --- a/gitops/utils/git/git.go +++ b/gitops/utils/git/git.go @@ -19,7 +19,7 @@ type gitClient struct { token string } -func NewGitClient(token string) GitClient { +func NewGitClient(token string) *gitClient { return &gitClient{ token: token, } diff --git a/gitops/utils/git/mock/mock.go b/gitops/utils/git/mock/mock.go new file mode 100644 index 0000000..bc0420a --- /dev/null +++ b/gitops/utils/git/mock/mock.go @@ -0,0 +1,7 @@ +package mock + +import ( + _ "go.uber.org/mock/mockgen/model" +) + +//go:generate mockgen -destination=mock_kube.go -package=mock github.com/minhthong582000/k8s-controller-pattern/gitops/utils/git GitClient diff --git a/gitops/utils/git/mock/mock_kube.go b/gitops/utils/git/mock/mock_kube.go new file mode 100644 index 0000000..ae5778d --- /dev/null +++ b/gitops/utils/git/mock/mock_kube.go @@ -0,0 +1,82 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/minhthong582000/k8s-controller-pattern/gitops/utils/git (interfaces: GitClient) +// +// Generated by this command: +// +// mockgen -destination=mock_kube.go -package=mock github.com/minhthong582000/k8s-controller-pattern/gitops/utils/git GitClient +// + +// Package mock is a generated GoMock package. +package mock + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" +) + +// MockGitClient is a mock of GitClient interface. +type MockGitClient struct { + ctrl *gomock.Controller + recorder *MockGitClientMockRecorder +} + +// MockGitClientMockRecorder is the mock recorder for MockGitClient. +type MockGitClientMockRecorder struct { + mock *MockGitClient +} + +// NewMockGitClient creates a new mock instance. +func NewMockGitClient(ctrl *gomock.Controller) *MockGitClient { + mock := &MockGitClient{ctrl: ctrl} + mock.recorder = &MockGitClientMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockGitClient) EXPECT() *MockGitClientMockRecorder { + return m.recorder +} + +// Checkout mocks base method. +func (m *MockGitClient) Checkout(arg0, arg1 string) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Checkout", arg0, arg1) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Checkout indicates an expected call of Checkout. +func (mr *MockGitClientMockRecorder) Checkout(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Checkout", reflect.TypeOf((*MockGitClient)(nil).Checkout), arg0, arg1) +} + +// CleanUp mocks base method. +func (m *MockGitClient) CleanUp(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CleanUp", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// CleanUp indicates an expected call of CleanUp. +func (mr *MockGitClientMockRecorder) CleanUp(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CleanUp", reflect.TypeOf((*MockGitClient)(nil).CleanUp), arg0) +} + +// CloneOrFetch mocks base method. +func (m *MockGitClient) CloneOrFetch(arg0, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CloneOrFetch", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// CloneOrFetch indicates an expected call of CloneOrFetch. +func (mr *MockGitClientMockRecorder) CloneOrFetch(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CloneOrFetch", reflect.TypeOf((*MockGitClient)(nil).CloneOrFetch), arg0, arg1) +} diff --git a/gitops/utils/kube/kube.go b/gitops/utils/kube/kube.go index 9e961e9..c5e51e2 100644 --- a/gitops/utils/kube/kube.go +++ b/gitops/utils/kube/kube.go @@ -33,7 +33,7 @@ type k8s struct { dynClientSet dynamic.Interface } -func NewK8s(discoveryClient discovery.DiscoveryInterface, dynClientSet dynamic.Interface) K8s { +func NewK8s(discoveryClient discovery.DiscoveryInterface, dynClientSet dynamic.Interface) *k8s { return &k8s{ discoveryClient: discoveryClient, dynClientSet: dynClientSet, @@ -103,7 +103,6 @@ func (k *k8s) GetResourceWithLabel(label map[string]string) ([]*unstructured.Uns if err != nil { return nil, err } - log.Debugf("Found %d API resources", len(serverResources)) wg.Add(len(serverResources)) for _, group := range serverResources { @@ -111,8 +110,6 @@ func (k *k8s) GetResourceWithLabel(label map[string]string) ([]*unstructured.Uns defer wg.Done() for _, resource := range group.APIResources { - log.Debugf("Listing resource %s", resource.Name) - // Skip subresources like pod/logs, pod/status if containsSlash(resource.Name) { continue @@ -128,21 +125,10 @@ func (k *k8s) GetResourceWithLabel(label map[string]string) ([]*unstructured.Uns } var list *unstructured.UnstructuredList - - // List cluster-scoped resources - if !resource.Namespaced { - list, err = k.dynClientSet.Resource(gvr).List(context.TODO(), listOption) - if err != nil { - log.Errorf("Error listing resource %s: %s", gvr.String(), err) - return - } - } else { - // List namespace-scoped resources - list, err = k.dynClientSet.Resource(gvr).List(context.TODO(), listOption) - if err != nil { - log.Errorf("Error listing resource %s: %s", gvr.String(), err) - return - } + list, err = k.dynClientSet.Resource(gvr).List(context.TODO(), listOption) + if err != nil { + log.Warningf("Error listing resource %s: %s", gvr.String(), err) + continue } // Append the resources to the list @@ -154,7 +140,6 @@ func (k *k8s) GetResourceWithLabel(label map[string]string) ([]*unstructured.Uns } }(group) } - wg.Wait() return objs, apiError @@ -163,11 +148,6 @@ func (k *k8s) GetResourceWithLabel(label map[string]string) ([]*unstructured.Uns func (k *k8s) DiffResources(current []*unstructured.Unstructured, new []*unstructured.Unstructured) (bool, error) { isChanged := false - if len(new) == 0 { - log.Debugf("Resources are empty, deleting all resources") - return true, nil - } - // Should use a cache to store current resources hashTable := make(map[string]*unstructured.Unstructured) for _, c := range current { @@ -175,7 +155,8 @@ func (k *k8s) DiffResources(current []*unstructured.Unstructured, new []*unstruc } for _, n := range new { - c, ok := hashTable[n.GetKind()+n.GetName()] + key := n.GetKind() + n.GetName() + c, ok := hashTable[key] if !ok { log.Debugf("Found new resource %s with name %s", n.GetKind(), n.GetName()) isChanged = true @@ -186,6 +167,15 @@ func (k *k8s) DiffResources(current []*unstructured.Unstructured, new []*unstruc log.Debugf("Resource %s with name %s has changed", n.GetKind(), n.GetName()) isChanged = true } + delete(hashTable, key) + } + + // Check if there are resources that need to be deleted + if len(hashTable) > 0 { + for _, c := range hashTable { + log.Debugf("Resource %s with name %s should be deleted", c.GetKind(), c.GetName()) + } + isChanged = true } return isChanged, nil diff --git a/gitops/utils/kube/kube_test.go b/gitops/utils/kube/kube_test.go index 47ffd28..260f4dd 100644 --- a/gitops/utils/kube/kube_test.go +++ b/gitops/utils/kube/kube_test.go @@ -189,6 +189,30 @@ func Test_DiffResources(t *testing.T) { new: []*unstructured.Unstructured{}, expectedResult: true, }, + { + name: "Should return true when resources exist in new but not in the current", + current: []*unstructured.Unstructured{}, + new: []*unstructured.Unstructured{ + { + Object: map[string]interface{}{ + "kind": "Pod", + "apiVersion": "v1", + "metadata": map[string]interface{}{ + "name": "nginx", + }, + "spec": map[string]interface{}{ + "containers": []map[string]interface{}{ + { + "name": "nginx", + "image": "nginx:1.26", + }, + }, + }, + }, + }, + }, + expectedResult: true, + }, } for _, tt := range testCases { diff --git a/gitops/utils/kube/mock/mock.go b/gitops/utils/kube/mock/mock.go new file mode 100644 index 0000000..801552c --- /dev/null +++ b/gitops/utils/kube/mock/mock.go @@ -0,0 +1,7 @@ +package mock + +import ( + _ "go.uber.org/mock/mockgen/model" +) + +//go:generate mockgen -destination=mock_kube.go -package=mock github.com/minhthong582000/k8s-controller-pattern/gitops/utils/kube K8s diff --git a/gitops/utils/kube/mock/mock_kube.go b/gitops/utils/kube/mock/mock_kube.go new file mode 100644 index 0000000..74e2a11 --- /dev/null +++ b/gitops/utils/kube/mock/mock_kube.go @@ -0,0 +1,127 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/minhthong582000/k8s-controller-pattern/gitops/utils/kube (interfaces: K8s) +// +// Generated by this command: +// +// mockgen -destination=mock_kube.go -package=mock github.com/minhthong582000/k8s-controller-pattern/gitops/utils/kube K8s +// + +// Package mock is a generated GoMock package. +package mock + +import ( + reflect "reflect" + + gomock "go.uber.org/mock/gomock" + unstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +// MockK8s is a mock of K8s interface. +type MockK8s struct { + ctrl *gomock.Controller + recorder *MockK8sMockRecorder +} + +// MockK8sMockRecorder is the mock recorder for MockK8s. +type MockK8sMockRecorder struct { + mock *MockK8s +} + +// NewMockK8s creates a new mock instance. +func NewMockK8s(ctrl *gomock.Controller) *MockK8s { + mock := &MockK8s{ctrl: ctrl} + mock.recorder = &MockK8sMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockK8s) EXPECT() *MockK8sMockRecorder { + return m.recorder +} + +// ApplyResource mocks base method. +func (m *MockK8s) ApplyResource(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ApplyResource", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// ApplyResource indicates an expected call of ApplyResource. +func (mr *MockK8sMockRecorder) ApplyResource(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ApplyResource", reflect.TypeOf((*MockK8s)(nil).ApplyResource), arg0) +} + +// DeleteResource mocks base method. +func (m *MockK8s) DeleteResource(arg0 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteResource", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteResource indicates an expected call of DeleteResource. +func (mr *MockK8sMockRecorder) DeleteResource(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteResource", reflect.TypeOf((*MockK8s)(nil).DeleteResource), arg0) +} + +// DiffResources mocks base method. +func (m *MockK8s) DiffResources(arg0, arg1 []*unstructured.Unstructured) (bool, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DiffResources", arg0, arg1) + ret0, _ := ret[0].(bool) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DiffResources indicates an expected call of DiffResources. +func (mr *MockK8sMockRecorder) DiffResources(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DiffResources", reflect.TypeOf((*MockK8s)(nil).DiffResources), arg0, arg1) +} + +// GenerateManifests mocks base method. +func (m *MockK8s) GenerateManifests(arg0 string) ([]*unstructured.Unstructured, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GenerateManifests", arg0) + ret0, _ := ret[0].([]*unstructured.Unstructured) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GenerateManifests indicates an expected call of GenerateManifests. +func (mr *MockK8sMockRecorder) GenerateManifests(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GenerateManifests", reflect.TypeOf((*MockK8s)(nil).GenerateManifests), arg0) +} + +// GetResourceWithLabel mocks base method. +func (m *MockK8s) GetResourceWithLabel(arg0 map[string]string) ([]*unstructured.Unstructured, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetResourceWithLabel", arg0) + ret0, _ := ret[0].([]*unstructured.Unstructured) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetResourceWithLabel indicates an expected call of GetResourceWithLabel. +func (mr *MockK8sMockRecorder) GetResourceWithLabel(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetResourceWithLabel", reflect.TypeOf((*MockK8s)(nil).GetResourceWithLabel), arg0) +} + +// SetLabelsForResources mocks base method. +func (m *MockK8s) SetLabelsForResources(arg0 []*unstructured.Unstructured, arg1 map[string]string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "SetLabelsForResources", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// SetLabelsForResources indicates an expected call of SetLabelsForResources. +func (mr *MockK8sMockRecorder) SetLabelsForResources(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLabelsForResources", reflect.TypeOf((*MockK8s)(nil).SetLabelsForResources), arg0, arg1) +}