Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store the hashed ApplicationSet Spec inside the ProgressiveSync state manager #135

Merged
merged 23 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
051a1a3
Calculate hash value and update the annotations list
DimitarHristov111 Sep 8, 2021
170cde9
Fix deps issues
DimitarHristov111 Sep 8, 2021
43e6cee
Fix lint errors
DimitarHristov111 Sep 8, 2021
15d99a0
Fix the URL for downloading the kubebuilder
DimitarHristov111 Sep 8, 2021
a6de00a
Add more logging
DimitarHristov111 Sep 10, 2021
6735c36
Add ApplicationSet to the scheme
DimitarHristov111 Sep 10, 2021
8bcabbc
Add permissions
DimitarHristov111 Sep 13, 2021
8c6f374
Use generic type instead of ApplicationSet
DimitarHristov111 Sep 13, 2021
29f1771
Fix when a new service is deployed
DimitarHristov111 Sep 13, 2021
fe4d983
Trigger build
DimitarHristov111 Sep 13, 2021
4ffd20b
Create ApplicationSet object as part of the tests
DimitarHristov111 Sep 14, 2021
56f314d
Keep the hash in memory instead of annotatations (for consistency)
DimitarHristov111 Sep 14, 2021
7b62885
Add tests for calculating the hash value
DimitarHristov111 Sep 15, 2021
1186af0
clean up
DimitarHristov111 Sep 15, 2021
5e2becd
Merge branch 'main' into applicationset-hash-annotation
DimitarHristov111 Sep 15, 2021
2d09720
Add a return if we fail to calculate the hash value; update tests
DimitarHristov111 Sep 16, 2021
255e327
Use argocd controls a Argo CD server
DimitarHristov111 Sep 16, 2021
637907b
Use ApplicationSet and change the way we calculate the hash value
DimitarHristov111 Sep 16, 2021
80bd256
Add missing comment
DimitarHristov111 Sep 16, 2021
eb19ec0
Update controllers/progressivesync_controller_test.go
DimitarHristov111 Sep 16, 2021
78f86c3
Update controllers/progressivesync_controller.go
DimitarHristov111 Sep 16, 2021
a8b87c7
Update controllers/progressivesync_controller.go
DimitarHristov111 Sep 16, 2021
124f63c
Change the method name
DimitarHristov111 Sep 16, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion charts/applicationset-progressive-sync/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.3.0-prealpha
version: 0.4.0-prealpha

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
Expand Down
7 changes: 7 additions & 0 deletions charts/applicationset-progressive-sync/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ rules:
- get
- patch
- update
- apiGroups:
- argoproj.io
resources:
- applicationsets
verbs:
- get
- list
{{- if or (gt ( .Values.replicaCount | int64) 1) .Values.args.enableLeaderElection }}
- apiGroups:
- ''
Expand Down
7 changes: 7 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ rules:
- get
- list
- watch
- apiGroups:
- argoproj.io
resources:
- applicationsets
verbs:
- get
- list
- apiGroups:
- argoproj.skyscanner.net
resources:
Expand Down
55 changes: 54 additions & 1 deletion controllers/progressivesync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ package controllers

import (
"context"
"crypto/md5"
"encoding/hex"

"fmt"
"strings"
"time"

"k8s.io/apimachinery/pkg/util/json"

syncv1alpha1 "github.com/Skyscanner/applicationset-progressive-sync/api/v1alpha1"
"github.com/Skyscanner/applicationset-progressive-sync/internal/consts"
"github.com/Skyscanner/applicationset-progressive-sync/internal/scheduler"
"github.com/Skyscanner/applicationset-progressive-sync/internal/utils"
applicationset "github.com/argoproj-labs/applicationset/api/v1alpha1"
applicationpkg "github.com/argoproj/argo-cd/pkg/apiclient/application"
argov1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
"github.com/go-logr/logr"
Expand All @@ -35,6 +41,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/util/retry"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -54,6 +61,7 @@ type ProgressiveSyncReconciler struct {
Scheme *runtime.Scheme
ArgoCDAppClient utils.ArgoCDAppClient
StateManager utils.ProgressiveSyncStateManager
ArgoNamespace string
}

// +kubebuilder:rbac:groups=argoproj.skyscanner.net,resources=progressivesyncs,verbs=get;list;watch;create;update;patch;delete
Expand All @@ -62,6 +70,7 @@ type ProgressiveSyncReconciler struct {
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch
// +kubebuilder:rbac:groups="argoproj.io",resources=applications,verbs=get;list;watch
// +kubebuilder:rbac:groups="argoproj.io",resources=applications/status,verbs=get;list;watch
// +kubebuilder:rbac:groups="argoproj.io",resources=applicationsets,verbs=get;list

// Reconcile performs the reconciling for a single named ProgressiveSync object
func (r *ProgressiveSyncReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down Expand Up @@ -99,11 +108,26 @@ func (r *ProgressiveSyncReconciler) Reconcile(ctx context.Context, req ctrl.Requ
return ctrl.Result{Requeue: true}, nil
}

pss, _ := r.StateManager.Get(ps.Name)

newHash, err := r.calculateHashedSpec(ctx, &ps)
if err != nil {
log.Error(err, "Failed to generate the hash value from the ApplicationSet spec")
DimitarHristov111 marked this conversation as resolved.
Show resolved Hide resolved
return ctrl.Result{}, err
} else {
currentHash := pss.GetHashedSpec()
if currentHash != newHash {
pss.SetHashedSpec(newHash)
log.Info("Successfully updated the hash value of the ApplicationSet spec", "service", ps.Name)
} else {
log.Info("Hash value is the same as nothing has changed in the ApplicationSet spec", "service", ps.Name)
}
}
DimitarHristov111 marked this conversation as resolved.
Show resolved Hide resolved

latest := ps
var result reconcile.Result
var reconcileErr error

pss, _ := r.StateManager.Get(ps.Name)
for _, stage := range ps.Spec.Stages {
log = log.WithValues("stage", stage.Name)

Expand Down Expand Up @@ -267,6 +291,35 @@ func (r *ProgressiveSyncReconciler) getClustersFromSelector(ctx context.Context,
return secrets, nil
}

// calculateHashValue returns a hash value string for the ApplicationSet spec
func (r *ProgressiveSyncReconciler) calculateHashedSpec(ctx context.Context, ps *syncv1alpha1.ProgressiveSync) (string, error) {
key := client.ObjectKeyFromObject(ps)
latest := syncv1alpha1.ProgressiveSync{}
if err := r.Client.Get(ctx, key, &latest); err != nil {
r.Log.Error(err, "Failed to retrieve the ProgressiveSync object while calculating the hash value")
return "", err
}

appSet := applicationset.ApplicationSet{}
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: r.ArgoNamespace, Name: latest.Spec.SourceRef.Name}, &appSet); err != nil {
r.Log.Error(err, "Failed to retrieve the ApplicationSet object")
return "", err
}

appSetSpec := appSet.Spec
appSetSpecInBytes, err := json.Marshal(appSetSpec)
DimitarHristov111 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
r.Log.Error(err, "Failed to encode the ApplicanSet spec")
return "", err
}

hashedSpecInBytes := md5.Sum(appSetSpecInBytes)
maruina marked this conversation as resolved.
Show resolved Hide resolved
hashedSpec := hex.EncodeToString(hashedSpecInBytes[:])

r.Log.Info("Successfully calculate the hash value of the Spec")
return hashedSpec, nil
}

// getOwnedAppsFromClusters returns a list of Applications targeting the specified clusters and owned by the specified ProgressiveSync
func (r *ProgressiveSyncReconciler) getOwnedAppsFromClusters(ctx context.Context, clusters corev1.SecretList, pr *syncv1alpha1.ProgressiveSync) ([]argov1alpha1.Application, error) {
var apps []argov1alpha1.Application
Expand Down
Loading