Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
Annotations (#58)
Browse files Browse the repository at this point in the history
* fix: add template annotations to rendered Application

Signed-off-by: Michael Goodness <michael.goodness@mlb.com>

* feat(cluster-generator): make AppSet annotations available for templating

Signed-off-by: Michael Goodness <michael.goodness@mlb.com>

* chore: auto-formatting

Signed-off-by: Michael Goodness <michael.goodness@mlb.com>

* fix(cluster.go): ensure params map is long enough for annotations

Signed-off-by: Michael Goodness <michael.goodness@mlb.com>

* chore(generators): update cluster_test

Signed-off-by: Michael Goodness <michael.goodness@mlb.com>
  • Loading branch information
mgoodness authored Nov 11, 2020
1 parent 357090e commit 717fc3c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
25 changes: 13 additions & 12 deletions pkg/controllers/applicationset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ package controllers
import (
"context"
"fmt"
"reflect"
"time"

"github.com/argoproj-labs/applicationset/pkg/generators"
"github.com/argoproj-labs/applicationset/pkg/utils"
argov1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"k8s.io/kubernetes/pkg/apis/core"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"
"time"

log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -43,8 +44,8 @@ import (
// ApplicationSetReconciler reconciles a ApplicationSet object
type ApplicationSetReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
Scheme *runtime.Scheme
Recorder record.EventRecorder
Generators map[string]generators.Generator
utils.Policy
utils.Renderer
Expand Down Expand Up @@ -99,7 +100,7 @@ func (r *ApplicationSetReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
}, nil
}

func (r *ApplicationSetReconciler) GetRelevantGenerators(requestedGenerator *argoprojiov1alpha1.ApplicationSetGenerator) []generators.Generator{
func (r *ApplicationSetReconciler) GetRelevantGenerators(requestedGenerator *argoprojiov1alpha1.ApplicationSetGenerator) []generators.Generator {
var res []generators.Generator

v := reflect.Indirect(reflect.ValueOf(requestedGenerator))
Expand All @@ -109,15 +110,15 @@ func (r *ApplicationSetReconciler) GetRelevantGenerators(requestedGenerator *arg
continue
}

if !reflect.ValueOf(field.Interface()).IsNil() {
if !reflect.ValueOf(field.Interface()).IsNil() {
res = append(res, r.Generators[v.Type().Field(i).Name])
}
}

return res
}

func (r *ApplicationSetReconciler) getMinRequeueAfter(applicationSetInfo *argoprojiov1alpha1.ApplicationSet) time.Duration{
func (r *ApplicationSetReconciler) getMinRequeueAfter(applicationSetInfo *argoprojiov1alpha1.ApplicationSet) time.Duration {
var res time.Duration
for _, requestedGenerator := range applicationSetInfo.Spec.Generators {

Expand All @@ -137,8 +138,9 @@ func (r *ApplicationSetReconciler) getMinRequeueAfter(applicationSetInfo *argopr
return res
}

func getTempApplication(applicationSetTemplate argoprojiov1alpha1.ApplicationSetTemplate) *argov1alpha1.Application{
func getTempApplication(applicationSetTemplate argoprojiov1alpha1.ApplicationSetTemplate) *argov1alpha1.Application {
var tmplApplication argov1alpha1.Application
tmplApplication.Annotations = applicationSetTemplate.Annotations
tmplApplication.Labels = applicationSetTemplate.Labels
tmplApplication.Namespace = applicationSetTemplate.Namespace
tmplApplication.Name = applicationSetTemplate.Name
Expand Down Expand Up @@ -250,13 +252,12 @@ func (r *ApplicationSetReconciler) createOrUpdateInCluster(ctx context.Context,
return firstError
}


// createInCluster will filter from the desiredApplications only the application that needs to be created
// Then it will call createOrUpdateInCluster to do the actual create
func (r *ApplicationSetReconciler) createInCluster(ctx context.Context, applicationSet argoprojiov1alpha1.ApplicationSet, desiredApplications []argov1alpha1.Application) error {

var createApps []argov1alpha1.Application
current, err := r.getCurrentApplications(ctx,applicationSet)
current, err := r.getCurrentApplications(ctx, applicationSet)
if err != nil {
return err
}
Expand All @@ -279,7 +280,7 @@ func (r *ApplicationSetReconciler) createInCluster(ctx context.Context, applicat
return r.createOrUpdateInCluster(ctx, applicationSet, createApps)
}

func (r *ApplicationSetReconciler) getCurrentApplications(ctx context.Context, applicationSet argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error){
func (r *ApplicationSetReconciler) getCurrentApplications(ctx context.Context, applicationSet argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) {
var current argov1alpha1.ApplicationList
err := r.Client.List(context.Background(), &current, client.MatchingFields{".metadata.controller": applicationSet.Name})

Expand All @@ -295,7 +296,7 @@ func (r *ApplicationSetReconciler) getCurrentApplications(ctx context.Context, a
func (r *ApplicationSetReconciler) deleteInCluster(ctx context.Context, applicationSet argoprojiov1alpha1.ApplicationSet, desiredApplications []argov1alpha1.Application) error {

// Save current applications to be able to delete the ones that are not in appList
current,err := r.getCurrentApplications(ctx,applicationSet)
current, err := r.getCurrentApplications(ctx, applicationSet)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/generators/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ type ClusterGenerator struct {
client.Client
}


func NewClusterGenerator(c client.Client) Generator {
g := &ClusterGenerator{
Client: c,
}
return g
}

func (g * ClusterGenerator) GetRequeueAfter(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator) time.Duration {
func (g *ClusterGenerator) GetRequeueAfter(appSetGenerator *argoprojiov1alpha1.ApplicationSetGenerator) time.Duration {
return NoRequeueAfter
}

Expand Down Expand Up @@ -65,9 +64,12 @@ func (g *ClusterGenerator) GenerateParams(

res := make([]map[string]string, len(clusterSecretList.Items))
for i, cluster := range clusterSecretList.Items {
params := make(map[string]string, len(cluster.ObjectMeta.Labels) + 2)
params := make(map[string]string, len(cluster.ObjectMeta.Annotations)+len(cluster.ObjectMeta.Labels)+2)
params["name"] = string(cluster.Data["name"])
params["server"] = string(cluster.Data["server"])
for key, value := range cluster.ObjectMeta.Annotations {
params[fmt.Sprintf("metadata.annotations.%s", key)] = value
}
for key, value := range cluster.ObjectMeta.Labels {
params[fmt.Sprintf("metadata.labels.%s", key)] = value
}
Expand Down
22 changes: 15 additions & 7 deletions pkg/generators/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"context"
"encoding/base64"
"errors"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

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

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

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -44,6 +46,9 @@ func TestGenerateParams(t *testing.T) {
"environment": "staging",
"org": "foo",
},
Annotations: map[string]string{
"foo.argoproj.io": "staging",
},
},
Data: map[string][]byte{
"config": []byte(base64.StdEncoding.EncodeToString([]byte("foo"))),
Expand All @@ -65,6 +70,9 @@ func TestGenerateParams(t *testing.T) {
"environment": "production",
"org": "bar",
},
Annotations: map[string]string{
"foo.argoproj.io": "production",
},
},
Data: map[string][]byte{
"config": []byte(base64.StdEncoding.EncodeToString([]byte("foo"))),
Expand All @@ -83,8 +91,8 @@ func TestGenerateParams(t *testing.T) {
{
metav1.LabelSelector{},
[]map[string]string{
{"name": "c3RhZ2luZy0wMQ==", "server": "https://staging-01.example.com", "metadata.labels.environment": "staging", "metadata.labels.org":"foo", "metadata.labels.argocd.argoproj.io/secret-type": "cluster"},
{"name": "cHJvZHVjdGlvbi0wMQ==", "server": "https://production-01.example.com", "metadata.labels.environment": "production", "metadata.labels.org":"bar", "metadata.labels.argocd.argoproj.io/secret-type": "cluster"},
{"name": "c3RhZ2luZy0wMQ==", "server": "https://staging-01.example.com", "metadata.labels.environment": "staging", "metadata.labels.org": "foo", "metadata.labels.argocd.argoproj.io/secret-type": "cluster", "metadata.annotations.foo.argoproj.io": "staging"},
{"name": "cHJvZHVjdGlvbi0wMQ==", "server": "https://production-01.example.com", "metadata.labels.environment": "production", "metadata.labels.org": "bar", "metadata.labels.argocd.argoproj.io/secret-type": "cluster", "metadata.annotations.foo.argoproj.io": "production"},
},
false,
nil,
Expand All @@ -96,7 +104,7 @@ func TestGenerateParams(t *testing.T) {
},
},
[]map[string]string{
{"name": "cHJvZHVjdGlvbi0wMQ==", "server": "https://production-01.example.com", "metadata.labels.environment": "production", "metadata.labels.org":"bar", "metadata.labels.argocd.argoproj.io/secret-type": "cluster"},
{"name": "cHJvZHVjdGlvbi0wMQ==", "server": "https://production-01.example.com", "metadata.labels.environment": "production", "metadata.labels.org": "bar", "metadata.labels.argocd.argoproj.io/secret-type": "cluster", "metadata.annotations.foo.argoproj.io": "production"},
},
false,
nil,
Expand All @@ -115,8 +123,8 @@ func TestGenerateParams(t *testing.T) {
},
},
[]map[string]string{
{"name": "c3RhZ2luZy0wMQ==", "server": "https://staging-01.example.com", "metadata.labels.argocd.argoproj.io/secret-type":"cluster", "metadata.labels.environment": "staging", "metadata.labels.org":"foo"},
{"name": "cHJvZHVjdGlvbi0wMQ==", "server": "https://production-01.example.com", "metadata.labels.argocd.argoproj.io/secret-type":"cluster", "metadata.labels.environment": "production", "metadata.labels.org":"bar"},
{"name": "c3RhZ2luZy0wMQ==", "server": "https://staging-01.example.com", "metadata.labels.environment": "staging", "metadata.labels.org": "foo", "metadata.labels.argocd.argoproj.io/secret-type": "cluster", "metadata.annotations.foo.argoproj.io": "staging"},
{"name": "cHJvZHVjdGlvbi0wMQ==", "server": "https://production-01.example.com", "metadata.labels.environment": "production", "metadata.labels.org": "bar", "metadata.labels.argocd.argoproj.io/secret-type": "cluster", "metadata.annotations.foo.argoproj.io": "production"},
},
false,
nil,
Expand All @@ -138,7 +146,7 @@ func TestGenerateParams(t *testing.T) {
},
},
[]map[string]string{
{"name": "c3RhZ2luZy0wMQ==", "server": "https://staging-01.example.com", "metadata.labels.environment": "staging", "metadata.labels.org":"foo", "metadata.labels.argocd.argoproj.io/secret-type":"cluster"},
{"name": "c3RhZ2luZy0wMQ==", "server": "https://staging-01.example.com", "metadata.labels.environment": "staging", "metadata.labels.org": "foo", "metadata.labels.argocd.argoproj.io/secret-type": "cluster", "metadata.annotations.foo.argoproj.io": "staging"},
},
false,
nil,
Expand Down

0 comments on commit 717fc3c

Please sign in to comment.