Skip to content

Commit df144e7

Browse files
Merge pull request #107 from codefresh-io/handle-app-conditions
disable sync limitation and report errors in better way
2 parents 787e955 + 0d72784 commit df144e7

File tree

10 files changed

+53
-112
lines changed

10 files changed

+53
-112
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.3.4-cap-CR-empty-desired-state
1+
2.3.4-cap-CR-report-condition-errors

controller/sync.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ func (m *appStateManager) SyncAppState(app *v1alpha1.Application, state *v1alpha
204204
if !proj.IsGroupKindPermitted(un.GroupVersionKind().GroupKind(), res.Namespaced) {
205205
return fmt.Errorf("Resource %s:%s is not permitted in project %s.", un.GroupVersionKind().Group, un.GroupVersionKind().Kind, proj.Name)
206206
}
207-
allowedApplications, _ := m.settingsMgr.GetAppsAllowedToDeliverIncluster()
208-
if res.Namespaced && !proj.IsDestinationPermitted(v1alpha1.ApplicationDestination{Namespace: un.GetNamespace(), Server: app.Spec.Destination.Server, Name: app.Spec.Destination.Name}, app.ObjectMeta.Name, allowedApplications) {
207+
if res.Namespaced && !proj.IsDestinationPermitted(v1alpha1.ApplicationDestination{Namespace: un.GetNamespace(), Server: app.Spec.Destination.Server, Name: app.Spec.Destination.Name}) {
209208
return fmt.Errorf("namespace %v is not permitted in project '%s'", un.GetNamespace(), proj.Name)
210209
}
211210
return nil

pkg/apis/application/v1alpha1/app_project_types.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (proj AppProject) IsResourcePermitted(groupKind schema.GroupKind, namespace
321321
return false
322322
}
323323
if namespace != "" {
324-
return proj.IsDestinationPermitted(ApplicationDestination{Server: dest.Server, Name: dest.Name, Namespace: namespace}, "", nil)
324+
return proj.IsDestinationPermitted(ApplicationDestination{Server: dest.Server, Name: dest.Name, Namespace: namespace})
325325
}
326326
return true
327327
}
@@ -356,24 +356,7 @@ func (proj AppProject) IsSourcePermitted(src ApplicationSource) bool {
356356
}
357357

358358
// IsDestinationPermitted validates if the provided application's destination is one of the allowed destinations for the project
359-
func (proj AppProject) IsDestinationPermitted(dst ApplicationDestination, applicationName string, appsThatAllowedRunInCluster []string) bool {
360-
applicationExistsFunc := func() bool {
361-
for _, appNameFromSettings := range appsThatAllowedRunInCluster {
362-
if appNameFromSettings == applicationName {
363-
return true
364-
}
365-
}
366-
return false
367-
}
368-
369-
// in case if application destination is in-cluster and allowed apps defined in argo-cm
370-
if (dst.Server == KubernetesInternalAPIServerAddr || dst.Name == "in-cluster") && len(appsThatAllowedRunInCluster) > 0 {
371-
applicationExists := applicationExistsFunc()
372-
if !applicationExists {
373-
return false
374-
}
375-
}
376-
359+
func (proj AppProject) IsDestinationPermitted(dst ApplicationDestination) bool {
377360
for _, item := range proj.Spec.Destinations {
378361
dstNameMatched := dst.Name != "" && globMatch(item.Name, dst.Name)
379362
dstServerMatched := dst.Server != "" && globMatch(item.Server, dst.Server)

pkg/apis/application/v1alpha1/app_project_types_test.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

pkg/apis/application/v1alpha1/types_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func TestAppProject_IsDestinationPermitted(t *testing.T) {
137137
Destinations: data.projDest,
138138
},
139139
}
140-
assert.Equal(t, proj.IsDestinationPermitted(data.appDest, "", nil), data.isPermitted)
140+
assert.Equal(t, proj.IsDestinationPermitted(data.appDest), data.isPermitted)
141141
}
142142
}
143143

server/application/application_errors_parser.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package application
33
import (
44
"strings"
55

6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
68
"github.com/argoproj/gitops-engine/pkg/health"
79
"github.com/argoproj/gitops-engine/pkg/sync/common"
810

@@ -25,6 +27,28 @@ func parseApplicationSyncResultErrors(os *appv1.OperationState) []*events.Object
2527
return errors
2628
}
2729

30+
func parseApplicationSyncResultErrorsFromConditions(conditions []appv1.ApplicationCondition) []*events.ObjectError {
31+
var errs []*events.ObjectError
32+
for _, cnd := range conditions {
33+
if !strings.Contains(strings.ToLower(cnd.Type), "error") {
34+
continue
35+
}
36+
37+
lastSeen := metav1.Now()
38+
if cnd.LastTransitionTime != nil {
39+
lastSeen = *cnd.LastTransitionTime
40+
}
41+
42+
errs = append(errs, &events.ObjectError{
43+
Type: "sync",
44+
Level: "error",
45+
Message: cnd.Message,
46+
LastSeen: lastSeen,
47+
})
48+
}
49+
return errs
50+
}
51+
2852
func parseResourceSyncResultErrors(rs *appv1.ResourceStatus, os *appv1.OperationState) []*events.ObjectError {
2953
errors := []*events.ObjectError{}
3054
if os.SyncResult == nil {

server/application/application_event_reporter.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@ func getResourceEventPayload(
390390
errors = append(errors, parseApplicationSyncResultErrors(originalApplication.Status.OperationState)...)
391391
}
392392

393+
if originalApplication != nil && originalApplication.Status.Conditions != nil {
394+
errors = append(errors, parseApplicationSyncResultErrorsFromConditions(originalApplication.Status.Conditions)...)
395+
}
396+
393397
if len(desiredState.RawManifest) == 0 && len(desiredState.CompiledManifest) != 0 {
394398
// for handling helm defined resources, etc...
395399
y, err := yaml.JSONToYAML([]byte(desiredState.CompiledManifest))
@@ -515,30 +519,11 @@ func (s *applicationEventReporter) getApplicationEventPayload(ctx context.Contex
515519
Cluster: a.Spec.Destination.Server,
516520
}
517521

518-
errs := []*events.ObjectError{}
519-
for _, cnd := range a.Status.Conditions {
520-
if !strings.Contains(strings.ToLower(cnd.Type), "error") {
521-
continue
522-
}
523-
524-
lastSeen := metav1.Now()
525-
if cnd.LastTransitionTime != nil {
526-
lastSeen = *cnd.LastTransitionTime
527-
}
528-
529-
errs = append(errs, &events.ObjectError{
530-
Type: "sync",
531-
Level: "error",
532-
Message: cnd.Message,
533-
LastSeen: lastSeen,
534-
})
535-
}
536-
537522
payload := events.EventPayload{
538523
Timestamp: ts,
539524
Object: object,
540525
Source: source,
541-
Errors: errs,
526+
Errors: parseApplicationSyncResultErrorsFromConditions(a.Status.Conditions),
542527
}
543528

544529
payloadBytes, err := json.Marshal(&payload)

server/application/applications_errors_parser_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ func TestParseResourceSyncResultErrors(t *testing.T) {
9696
})
9797
}
9898

99+
func TestParseApplicationSyncResultErrorsFromConditions(t *testing.T) {
100+
t.Run("conditions exists", func(t *testing.T) {
101+
errors := parseApplicationSyncResultErrorsFromConditions([]v1alpha1.ApplicationCondition{
102+
{
103+
Type: "error",
104+
Message: "error message",
105+
},
106+
})
107+
108+
assert.Len(t, errors, 1)
109+
assert.Equal(t, errors[0].Message, "error message")
110+
assert.Equal(t, errors[0].Type, "sync")
111+
assert.Equal(t, errors[0].Level, "error")
112+
})
113+
}
114+
99115
func TestParseAggregativeHealthErrors(t *testing.T) {
100116
t.Run("application tree is nil", func(t *testing.T) {
101117
errs := parseAggregativeHealthErrors(&v1alpha1.ResourceStatus{

server/project/project.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func (s *Server) Update(ctx context.Context, q *project.ProjectUpdateRequest) (*
341341
if oldProj.IsSourcePermitted(a.Spec.Source) {
342342
srcValidatedApps = append(srcValidatedApps, a)
343343
}
344-
if oldProj.IsDestinationPermitted(a.Spec.Destination, "", nil) {
344+
if oldProj.IsDestinationPermitted(a.Spec.Destination) {
345345
dstValidatedApps = append(dstValidatedApps, a)
346346
}
347347
}
@@ -355,7 +355,7 @@ func (s *Server) Update(ctx context.Context, q *project.ProjectUpdateRequest) (*
355355
}
356356
}
357357
for _, a := range dstValidatedApps {
358-
if !q.Project.IsDestinationPermitted(a.Spec.Destination, "", nil) {
358+
if !q.Project.IsDestinationPermitted(a.Spec.Destination) {
359359
invalidDstCount++
360360
}
361361
}

util/argo/argo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func ValidatePermissions(ctx context.Context, spec *argoappv1.ApplicationSpec, p
382382
}
383383

384384
if spec.Destination.Server != "" {
385-
if !proj.IsDestinationPermitted(spec.Destination, "", nil) {
385+
if !proj.IsDestinationPermitted(spec.Destination) {
386386
conditions = append(conditions, argoappv1.ApplicationCondition{
387387
Type: argoappv1.ApplicationConditionInvalidSpecError,
388388
Message: fmt.Sprintf("application destination {%s %s} is not permitted in project '%s'", spec.Destination.Server, spec.Destination.Namespace, spec.Project),

0 commit comments

Comments
 (0)