Init commit
This commit is contained in:
7
src/internal/connectors/argoapp.go
Normal file
7
src/internal/connectors/argoapp.go
Normal file
@ -0,0 +1,7 @@
|
||||
package connectors
|
||||
|
||||
// Use ArgoApp as a source
|
||||
type ArgoApp struct {
|
||||
}
|
||||
|
||||
|
9
src/internal/connectors/types.go
Normal file
9
src/internal/connectors/types.go
Normal file
@ -0,0 +1,9 @@
|
||||
package connectors
|
||||
|
||||
const (
|
||||
ARGO_APPLICATION = "argoproj.io/v1alpha1/Application"
|
||||
)
|
||||
|
||||
func GetSupportedConnectors() []string {
|
||||
return []string{ARGO_APPLICATION}
|
||||
}
|
131
src/internal/controller/appsource_controller.go
Normal file
131
src/internal/controller/appsource_controller.go
Normal file
@ -0,0 +1,131 @@
|
||||
/*
|
||||
Copyright 2024.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"gopkg.in/src-d/go-git.v4"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
k8sbadhouseplantsnetv1alpha1 "github.com/allanger/gitops-diff-operator/api/v1alpha1"
|
||||
"github.com/allanger/gitops-diff-operator/internal/connectors"
|
||||
applicationv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
|
||||
diffv1alpha1 "github.com/allanger/gitops-diff-operator/api/v1alpha1"
|
||||
)
|
||||
|
||||
// AppSourceReconciler reconciles a AppSource object
|
||||
type AppSourceReconciler struct {
|
||||
client.Client
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
|
||||
// +kubebuilder:rbac:groups=k8s.badhouseplants.net,resources=appsources,verbs=get;list;watch;create;update;patch;delete
|
||||
// +kubebuilder:rbac:groups=k8s.badhouseplants.net,resources=appsources/status,verbs=get;update;patch
|
||||
// +kubebuilder:rbac:groups=k8s.badhouseplants.net,resources=appsources/finalizers,verbs=update
|
||||
func (r *AppSourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
log := log.FromContext(ctx)
|
||||
|
||||
log.Info("dmmu")
|
||||
|
||||
// TODO: It should not be hardcoded
|
||||
reconcilePeriod := 30 * time.Second
|
||||
reconcileResult := reconcile.Result{RequeueAfter: reconcilePeriod}
|
||||
|
||||
source := &diffv1alpha1.AppSource{}
|
||||
err := r.Client.Get(ctx, types.NamespacedName{
|
||||
Namespace: req.Namespace,
|
||||
Name: req.Name,
|
||||
}, source)
|
||||
|
||||
if err != nil {
|
||||
if k8serrors.IsNotFound(err) {
|
||||
return reconcileResult, nil
|
||||
}
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
object := fmt.Sprintf("%s/%s", source.Spec.Resource.Api, source.Spec.Resource.Kind)
|
||||
if !slices.Contains(connectors.GetSupportedConnectors(), object) {
|
||||
err := fmt.Errorf("object is not supported by the operator: %s", object)
|
||||
return reconcile.Result{
|
||||
Requeue: false,
|
||||
}, err
|
||||
}
|
||||
// It should only get sources when they are based on git
|
||||
var gitUrl string
|
||||
var mainBranch string
|
||||
switch object {
|
||||
case connectors.ARGO_APPLICATION:
|
||||
argoapp := &applicationv1alpha1.Application{}
|
||||
if err := r.Client.Get(ctx, types.NamespacedName{
|
||||
Namespace: source.Spec.Resource.Namespace,
|
||||
Name: source.Spec.Resource.Kind,
|
||||
}, argoapp); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
gitUrl = argoapp.Spec.Source.RepoURL
|
||||
mainBranch = argoapp.Spec.Source.TargetRevision
|
||||
}
|
||||
sshKeys, err := ssh.NewPublicKeysFromFile("git", "/var/ssh-key", "")
|
||||
// TODO: Shoulen't be hardcoded
|
||||
repo, err := git.PlainClone("/tmp", false, &git.CloneOptions{
|
||||
URL: gitUrl,
|
||||
Auth: sshKeys,
|
||||
})
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
refs := []string{}
|
||||
rawRefs, err := repo.References()
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
defer rawRefs.Close()
|
||||
|
||||
rawRefs.ForEach(func(ref *plumbing.Reference) error {
|
||||
refs = append(refs, ref.String())
|
||||
return nil
|
||||
})
|
||||
|
||||
source.Status.Branches = refs
|
||||
source.Status.MainBranch = mainBranch
|
||||
if err := r.Status().Update(ctx, source); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *AppSourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&k8sbadhouseplantsnetv1alpha1.AppSource{}).
|
||||
Complete(r)
|
||||
}
|
84
src/internal/controller/appsource_controller_test.go
Normal file
84
src/internal/controller/appsource_controller_test.go
Normal file
@ -0,0 +1,84 @@
|
||||
/*
|
||||
Copyright 2024.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
|
||||
k8sbadhouseplantsnetv1alpha1 "github.com/allanger/gitops-diff-operator/api/v1alpha1"
|
||||
)
|
||||
|
||||
var _ = Describe("AppSource Controller", func() {
|
||||
Context("When reconciling a resource", func() {
|
||||
const resourceName = "test-resource"
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
typeNamespacedName := types.NamespacedName{
|
||||
Name: resourceName,
|
||||
Namespace: "default", // TODO(user):Modify as needed
|
||||
}
|
||||
appsource := &k8sbadhouseplantsnetv1alpha1.AppSource{}
|
||||
|
||||
BeforeEach(func() {
|
||||
By("creating the custom resource for the Kind AppSource")
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, appsource)
|
||||
if err != nil && errors.IsNotFound(err) {
|
||||
resource := &k8sbadhouseplantsnetv1alpha1.AppSource{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: resourceName,
|
||||
Namespace: "default",
|
||||
},
|
||||
// TODO(user): Specify other spec details if needed.
|
||||
}
|
||||
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
|
||||
}
|
||||
})
|
||||
|
||||
AfterEach(func() {
|
||||
// TODO(user): Cleanup logic after each test, like removing the resource instance.
|
||||
resource := &k8sbadhouseplantsnetv1alpha1.AppSource{}
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, resource)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
By("Cleanup the specific resource instance AppSource")
|
||||
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
|
||||
})
|
||||
It("should successfully reconcile the resource", func() {
|
||||
By("Reconciling the created resource")
|
||||
controllerReconciler := &AppSourceReconciler{
|
||||
Client: k8sClient,
|
||||
Scheme: k8sClient.Scheme(),
|
||||
}
|
||||
|
||||
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{
|
||||
NamespacedName: typeNamespacedName,
|
||||
})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
// TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
|
||||
// Example: If you expect a certain status condition after reconciliation, verify it here.
|
||||
})
|
||||
})
|
||||
})
|
90
src/internal/controller/suite_test.go
Normal file
90
src/internal/controller/suite_test.go
Normal file
@ -0,0 +1,90 @@
|
||||
/*
|
||||
Copyright 2024.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
. "github.com/onsi/ginkgo/v2"
|
||||
. "github.com/onsi/gomega"
|
||||
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/client-go/rest"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
||||
logf "sigs.k8s.io/controller-runtime/pkg/log"
|
||||
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||
|
||||
k8sbadhouseplantsnetv1alpha1 "github.com/allanger/gitops-diff-operator/api/v1alpha1"
|
||||
// +kubebuilder:scaffold:imports
|
||||
)
|
||||
|
||||
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
|
||||
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
|
||||
|
||||
var cfg *rest.Config
|
||||
var k8sClient client.Client
|
||||
var testEnv *envtest.Environment
|
||||
|
||||
func TestControllers(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
|
||||
RunSpecs(t, "Controller Suite")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func() {
|
||||
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
|
||||
|
||||
By("bootstrapping test environment")
|
||||
testEnv = &envtest.Environment{
|
||||
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
|
||||
ErrorIfCRDPathMissing: true,
|
||||
|
||||
// The BinaryAssetsDirectory is only required if you want to run the tests directly
|
||||
// without call the makefile target test. If not informed it will look for the
|
||||
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
|
||||
// Note that you must have the required binaries setup under the bin directory to perform
|
||||
// the tests directly. When we run make test it will be setup and used automatically.
|
||||
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
|
||||
fmt.Sprintf("1.30.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
|
||||
}
|
||||
|
||||
var err error
|
||||
// cfg is defined in this file globally.
|
||||
cfg, err = testEnv.Start()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(cfg).NotTo(BeNil())
|
||||
|
||||
err = k8sbadhouseplantsnetv1alpha1.AddToScheme(scheme.Scheme)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
// +kubebuilder:scaffold:scheme
|
||||
|
||||
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
Expect(k8sClient).NotTo(BeNil())
|
||||
|
||||
})
|
||||
|
||||
var _ = AfterSuite(func() {
|
||||
By("tearing down the test environment")
|
||||
err := testEnv.Stop()
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
})
|
Reference in New Issue
Block a user