Init commit
This commit is contained in:
@ -5,3 +5,5 @@ type ArgoApp struct {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -7,3 +7,6 @@ const (
|
||||
func GetSupportedConnectors() []string {
|
||||
return []string{ARGO_APPLICATION}
|
||||
}
|
||||
|
||||
type GitOpsConnector interface {
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ package controller
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
@ -33,10 +34,10 @@ import (
|
||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||
|
||||
diffv1alpha1 "github.com/allanger/gitops-diff-operator/api/v1alpha1"
|
||||
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
|
||||
@ -50,7 +51,6 @@ type AppSourceReconciler struct {
|
||||
// +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
|
||||
@ -82,10 +82,14 @@ func (r *AppSourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
||||
var mainBranch string
|
||||
switch object {
|
||||
case connectors.ARGO_APPLICATION:
|
||||
scheme := runtime.NewScheme()
|
||||
if err := applicationv1alpha1.AddToScheme(scheme); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
argoapp := &applicationv1alpha1.Application{}
|
||||
if err := r.Client.Get(ctx, types.NamespacedName{
|
||||
Namespace: source.Spec.Resource.Namespace,
|
||||
Name: source.Spec.Resource.Kind,
|
||||
Name: source.Spec.Resource.Name,
|
||||
}, argoapp); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
@ -93,17 +97,33 @@ func (r *AppSourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
||||
mainBranch = argoapp.Spec.Source.TargetRevision
|
||||
}
|
||||
sshKeys, err := ssh.NewPublicKeysFromFile("git", "/var/ssh-key", "")
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
// TODO: Shoulen't be hardcoded
|
||||
repo, err := git.PlainClone("/tmp", false, &git.CloneOptions{
|
||||
path := fmt.Sprintf("/tmp/%s-%s-%s", req.Namespace, req.Name, time.Now().String())
|
||||
repo, err := git.PlainClone(path, false, &git.CloneOptions{
|
||||
URL: gitUrl,
|
||||
Auth: sshKeys,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
refs := []string{}
|
||||
rawRefs, err := repo.References()
|
||||
rawRefs, err := repo.Tags()
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
defer rawRefs.Close()
|
||||
|
||||
rawRefs.ForEach(func(ref *plumbing.Reference) error {
|
||||
refs = append(refs, ref.String())
|
||||
return nil
|
||||
})
|
||||
|
||||
rawRefs, err = repo.Branches()
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
@ -119,8 +139,13 @@ func (r *AppSourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
|
||||
if err := r.Status().Update(ctx, source); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
return ctrl.Result{}, nil
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
return ctrl.Result{
|
||||
Requeue: true,
|
||||
RequeueAfter: 60 * time.Second,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
|
229
src/internal/controller/diff_controller.go
Normal file
229
src/internal/controller/diff_controller.go
Normal file
@ -0,0 +1,229 @@
|
||||
/*
|
||||
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"
|
||||
"time"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/utils/ptr"
|
||||
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"
|
||||
|
||||
diffv1alpha1 "github.com/allanger/gitops-diff-operator/api/v1alpha1"
|
||||
k8sbadhouseplantsnetv1alpha1 "github.com/allanger/gitops-diff-operator/api/v1alpha1"
|
||||
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
|
||||
argocdclient "github.com/argoproj/argo-cd/v2/pkg/apiclient"
|
||||
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
|
||||
argoappv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
|
||||
"github.com/sergi/go-diff/diffmatchpatch"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
// DiffReconciler reconciles a Diff object
|
||||
type DiffReconciler struct {
|
||||
client.Client
|
||||
Scheme *runtime.Scheme
|
||||
}
|
||||
|
||||
// +kubebuilder:rbac:groups=k8s.badhouseplants.net,resources=diffs,verbs=get;list;watch;create;update;patch;delete
|
||||
// +kubebuilder:rbac:groups=k8s.badhouseplants.net,resources=diffs/status,verbs=get;update;patch
|
||||
// +kubebuilder:rbac:groups=k8s.badhouseplants.net,resources=diffs/finalizers,verbs=update
|
||||
|
||||
// Reconcile is part of the main kubernetes reconciliation loop which aims to
|
||||
// move the current state of the cluster closer to the desired state.
|
||||
// TODO(user): Modify the Reconcile function to compare the state specified by
|
||||
// the Diff object against the actual cluster state, and then
|
||||
// perform operations to make the cluster state reflect the state specified by
|
||||
// the user.
|
||||
//
|
||||
// For more details, check Reconcile and its Result here:
|
||||
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.18.2/pkg/reconcile
|
||||
func (r *DiffReconciler) 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}
|
||||
|
||||
diff := &diffv1alpha1.Diff{}
|
||||
err := r.Client.Get(ctx, types.NamespacedName{
|
||||
Namespace: req.Namespace,
|
||||
Name: req.Name,
|
||||
}, diff)
|
||||
|
||||
if err != nil {
|
||||
if k8serrors.IsNotFound(err) {
|
||||
return reconcileResult, nil
|
||||
}
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
source := &diffv1alpha1.AppSource{}
|
||||
if err := r.Client.Get(ctx, types.NamespacedName{
|
||||
Namespace: req.Namespace,
|
||||
Name: diff.Spec.AppSourceName,
|
||||
}, source); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
client, err := apiclient.NewClient(&argocdclient.ClientOptions{
|
||||
ServerAddr: "argocd-server.argo-system.svc.cluster.local:80",
|
||||
Insecure: true,
|
||||
AuthToken: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhcmdvY2QiLCJzdWIiOiJhbGljZTphcGlLZXkiLCJuYmYiOjE3MTk5MzIwOTEsImlhdCI6MTcxOTkzMjA5MSwianRpIjoiN2VmM2Y2OTYtYjU4Zi00NDIyLTk4YTktNjFlYjMyNjNhNDUxIn0.XmQThdSp_WWUbWv6Ur95URqXnY9-LCXxmcun9kBoYTQ",
|
||||
})
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
closer, appCl, err := client.NewApplicationClient()
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
defer closer.Close()
|
||||
|
||||
qMain := application.ApplicationManifestQuery{
|
||||
Name: &source.Spec.Resource.Name,
|
||||
AppNamespace: &source.Spec.Resource.Namespace,
|
||||
Revision: ptr.To(source.Status.MainBranch),
|
||||
}
|
||||
before, err := getManifestsFromArgo(ctx, qMain, appCl)
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
qNew := application.ApplicationManifestQuery{
|
||||
Name: &source.Spec.Resource.Name,
|
||||
AppNamespace: &source.Spec.Resource.Namespace,
|
||||
Revision: ptr.To(diff.Spec.TargetRef),
|
||||
}
|
||||
after, err := getManifestsFromArgo(ctx, qNew, appCl)
|
||||
if err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
dmp := diffmatchpatch.New()
|
||||
|
||||
diffs := dmp.DiffMain(before, after, true)
|
||||
|
||||
secretName := fmt.Sprintf("%s-diff", diff.Name)
|
||||
diffSecret := &corev1.Secret{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: secretName,
|
||||
Namespace: req.Namespace,
|
||||
},
|
||||
}
|
||||
|
||||
err = r.Client.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: secretName}, diffSecret)
|
||||
if err != nil {
|
||||
if !k8serrors.IsNotFound(err) {
|
||||
return reconcileResult, err
|
||||
}
|
||||
if err := r.Client.Create(ctx, diffSecret); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
if err := r.Client.Get(ctx, types.NamespacedName{Namespace: req.Namespace, Name: secretName}, diffSecret); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
}
|
||||
diffSecret.Data = map[string][]byte{}
|
||||
diffSecret.Data["diff"] = []byte(dmp.DiffPrettyText(diffs))
|
||||
if err := r.Update(ctx, diffSecret); err != nil {
|
||||
return reconcileResult, err
|
||||
}
|
||||
|
||||
return reconcileResult, nil
|
||||
}
|
||||
|
||||
func getManifestsFromArgo(ctx context.Context, q application.ApplicationManifestQuery, appCl application.ApplicationServiceClient) (string, error) {
|
||||
log := log.FromContext(ctx)
|
||||
log.Info("requesting an app", "application", q.Name, "appNamespace", q.AppNamespace)
|
||||
var resources string
|
||||
|
||||
res, err := appCl.GetManifests(ctx, &q)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var unstructureds []*unstructured.Unstructured
|
||||
for _, mfst := range res.Manifests {
|
||||
obj, err := argoappv1.UnmarshalToUnstructured(mfst)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
unstructureds = append(unstructureds, obj)
|
||||
}
|
||||
|
||||
for _, rawManifest := range unstructureds {
|
||||
if rawManifest.Object != nil {
|
||||
yamlBytes, err := yaml.Marshal(rawManifest.Object)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resources += fmt.Sprintf("---\n%s\n", string(yamlBytes))
|
||||
|
||||
app := &argoappv1.Application{}
|
||||
if err := yaml.Unmarshal(yamlBytes, app); err != nil {
|
||||
log.Info(err.Error())
|
||||
} else {
|
||||
log.Info(fmt.Sprintf("%v", app))
|
||||
app.ObjectMeta.Name = fmt.Sprintf("%s-%s", "diff", app.ObjectMeta.Name)
|
||||
app.Spec.SyncPolicy = &argoappv1.SyncPolicy{}
|
||||
|
||||
app, err := appCl.Create(ctx, &application.ApplicationCreateRequest{
|
||||
Application: app,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
output, err := getManifestsFromArgo(ctx, application.ApplicationManifestQuery{
|
||||
AppNamespace: &app.ObjectMeta.Namespace,
|
||||
Name: &app.ObjectMeta.Name,
|
||||
}, appCl)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
_, err = appCl.Delete(ctx, &application.ApplicationDeleteRequest{
|
||||
Name: app.GetMetadata().Name,
|
||||
AppNamespace: &app.ObjectMeta.Namespace,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resources += output
|
||||
}
|
||||
}
|
||||
}
|
||||
return resources, nil
|
||||
}
|
||||
|
||||
// SetupWithManager sets up the controller with the Manager.
|
||||
func (r *DiffReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
return ctrl.NewControllerManagedBy(mgr).
|
||||
For(&k8sbadhouseplantsnetv1alpha1.Diff{}).
|
||||
Complete(r)
|
||||
}
|
84
src/internal/controller/diff_controller_test.go
Normal file
84
src/internal/controller/diff_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("Diff 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
|
||||
}
|
||||
diff := &k8sbadhouseplantsnetv1alpha1.Diff{}
|
||||
|
||||
BeforeEach(func() {
|
||||
By("creating the custom resource for the Kind Diff")
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, diff)
|
||||
if err != nil && errors.IsNotFound(err) {
|
||||
resource := &k8sbadhouseplantsnetv1alpha1.Diff{
|
||||
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.Diff{}
|
||||
err := k8sClient.Get(ctx, typeNamespacedName, resource)
|
||||
Expect(err).NotTo(HaveOccurred())
|
||||
|
||||
By("Cleanup the specific resource instance Diff")
|
||||
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
|
||||
})
|
||||
It("should successfully reconcile the resource", func() {
|
||||
By("Reconciling the created resource")
|
||||
controllerReconciler := &DiffReconciler{
|
||||
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.
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user