Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
add update and owner reference (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerKahani committed Jul 10, 2020
1 parent 0e00385 commit d9e624b
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 20 deletions.
20 changes: 17 additions & 3 deletions pkg/controllers/applicationset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"k8s.io/kubernetes/pkg/apis/core"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"

Expand Down Expand Up @@ -102,17 +103,30 @@ func (r *ApplicationSetReconciler) createApplications(ctx context.Context, appli

for _, app := range appList {
app.Namespace = applicationSetInfo.Namespace
if err := r.Client.Create(ctx, &app); err != nil {
log.Error(err, fmt.Sprintf("failed to create Application %s resource for applicationSet %s", app.Name, applicationSetInfo.Name))

found := app
action, err := ctrl.CreateOrUpdate(ctx, r.Client, &found, func() error {
found.Spec = app.Spec
return controllerutil.SetControllerReference(&applicationSetInfo, &found, r.Scheme)
})

if err != nil {
log.Error(err, fmt.Sprintf("failed to get Application %s resource for applicationSet %s", app.Name, applicationSetInfo.Name))
continue
}

r.Recorder.Eventf(&applicationSetInfo, core.EventTypeNormal, "Created", "Created Application %q", app.Name)
log.Infof("created Application %s resource for applicationSet %s", app.Name, applicationSetInfo.Name)
log.Infof("%s Application %s resource for applicationSet %s", action, app.Name, applicationSetInfo.Name)
}

return nil

}

func (r *ApplicationSetReconciler) getApplications(ctx context.Context, applicationSetInfo argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) {

return nil, nil

}

var _ handler.EventHandler = &clusterSecretEventHandler{}
102 changes: 85 additions & 17 deletions pkg/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,24 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
crtclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"testing"

argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"

"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestCreateApplications(t *testing.T) {

scheme := runtime.NewScheme()
argoprojiov1alpha1.AddToScheme(scheme)
argov1alpha1.AddToScheme(scheme)

client := fake.NewFakeClientWithScheme(scheme)

r := ApplicationSetReconciler{
Client: client,
Scheme: scheme,
Recorder: record.NewFakeRecorder(1),
}

for _, c := range []struct {
appSet argoprojiov1alpha1.ApplicationSet
apps []argov1alpha1.Application
expected []argov1alpha1.Application
appSet argoprojiov1alpha1.ApplicationSet
existsApps []argov1alpha1.Application
apps []argov1alpha1.Application
expected []argov1alpha1.Application
}{
{
appSet: argoprojiov1alpha1.ApplicationSet{
Expand All @@ -40,6 +34,7 @@ func TestCreateApplications(t *testing.T) {
Namespace: "namespace",
},
},
existsApps: nil,
apps: []argov1alpha1.Application{
{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -61,17 +56,90 @@ func TestCreateApplications(t *testing.T) {
},
},
},
{
appSet: argoprojiov1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "namespace",
},
Spec: argoprojiov1alpha1.ApplicationSetSpec{
Template: argoprojiov1alpha1.ApplicationSetTemplate{
Spec: argov1alpha1.ApplicationSpec{
Project: "project",
},
},
},
},
existsApps: []argov1alpha1.Application{
argov1alpha1.Application{
TypeMeta: metav1.TypeMeta{
Kind: "Application",
APIVersion: "argoproj.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "app1",
Namespace: "namespace",
ResourceVersion: "2",
},
Spec: argov1alpha1.ApplicationSpec{
Project: "test",
},
},
},
apps: []argov1alpha1.Application{
{
ObjectMeta: metav1.ObjectMeta{
Name: "app1",
},
Spec: argov1alpha1.ApplicationSpec{
Project: "project",
},
},
},
expected: []argov1alpha1.Application{
{
TypeMeta: metav1.TypeMeta{
Kind: "Application",
APIVersion: "argoproj.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "app1",
Namespace: "namespace",
ResourceVersion: "3",
},
Spec: argov1alpha1.ApplicationSpec{
Project: "project",
},
},
},
},
} {
initObjs := []runtime.Object{&c.appSet}
for _, a := range c.existsApps {
controllerutil.SetControllerReference(&c.appSet, &a, scheme)
initObjs = append(initObjs, &a)
}

client := fake.NewFakeClientWithScheme(scheme, initObjs...)

r := ApplicationSetReconciler{
Client: client,
Scheme: scheme,
Recorder: record.NewFakeRecorder(1),
}

r.createApplications(context.TODO(), c.appSet, c.apps)

for _, e := range c.expected {
for _, obj := range c.expected {
got := &argov1alpha1.Application{}
_ = client.Get(context.Background(), crtclient.ObjectKey{
Namespace: e.Namespace,
Name: e.Name,
Namespace: obj.Namespace,
Name: obj.Name,
}, got)

assert.Equal(t, e, *got)
controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)

assert.Equal(t, obj, *got)
}
}

Expand Down

0 comments on commit d9e624b

Please sign in to comment.