Skip to content

Commit

Permalink
Add subresource test
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhny committed Nov 29, 2023
1 parent 452d43e commit 675acf1
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 17 deletions.
34 changes: 17 additions & 17 deletions controllers/application/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,19 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req reconcile.Req
if err != nil {
return reconcile.Result{}, err
}
/*
statusDiff, err := util.GetObjectDiff(tmpApplication.Status, application.Status)
if err != nil {
return reconcile.Result{}, err
}

// If we update the Application initially on applied defaults before starting reconciling resources we allow all
// updates to be visible even though the controllerDuties may take some time.
if len(statusDiff) > 0 {
err := r.GetClient().Status().Update(ctx, application)
return reconcile.Result{Requeue: true}, err
}
*/
statusDiff, err := util.GetObjectDiff(tmpApplication.Status, application.Status)
if err != nil {
return reconcile.Result{}, err
}

// If we update the Application initially on applied defaults before starting reconciling resources we allow all
// updates to be visible even though the controllerDuties may take some time.
if len(statusDiff) > 0 {
err := r.GetClient().Status().Update(ctx, application)
return reconcile.Result{Requeue: true}, err
}

// Finalizer check is due to a bug when updating using controller-runtime
// See https://github.com/kubernetes-sigs/controller-runtime/issues/2453
if len(specDiff) > 0 || (!ctrlutil.ContainsFinalizer(tmpApplication, applicationFinalizer) && ctrlutil.ContainsFinalizer(application, applicationFinalizer)) {
Expand Down Expand Up @@ -174,7 +174,7 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req reconcile.Req
return res, nil
}
}
r.GetClient().Status().Update(ctx, application)
//r.GetClient().Status().Update(ctx, application)
r.EmitNormalEvent(application, "ReconcileEnd", fmt.Sprintf("Application %v has finished reconciliation loop", application.Name))

return reconcile.Result{}, err
Expand Down Expand Up @@ -241,10 +241,10 @@ func ValidateIngresses(application *skiperatorv1alpha1.Application) error {

func (r *ApplicationReconciler) manageControllerStatus(context context.Context, app *skiperatorv1alpha1.Application, controller string, statusName skiperatorv1alpha1.StatusNames, message string) (reconcile.Result, error) {
app.UpdateControllerStatus(controller, message, statusName)
//err := r.GetClient().Status().Update(context, app)
//if err != nil {
// return reconcile.Result{Requeue: true}, err
//}
err := r.GetClient().Status().Update(context, app)
if err != nil {
return reconcile.Result{Requeue: true}, err
}
return reconcile.Result{Requeue: true}, nil
}

Expand Down
56 changes: 56 additions & 0 deletions tests/application/subresource-status/application-error-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: skiperator.kartverket.no/v1alpha1
kind: Application
metadata:
finalizers:
- skip.statkart.no/finalizer
name: borked
spec:
enablePDB: true
image: image
port: 8080
priority: medium
redirectToHTTPS: true
replicas: "2"
strategy:
type: RollingUpdate
status:
application:
message: One of the controllers is in a failed state
status: Error
controllers:
AuthorizationPolicy:
message: AuthorizationPolicy has finished synchronizing
status: Synced
Certificate:
message: Certificate has finished synchronizing
status: Synced
ConfigMap:
message: ConfigMap has finished synchronizing
status: Synced
EgressServiceEntry:
message: EgressServiceEntry has finished synchronizing
status: Synced
HorizontalPodAutoScaler:
message: HorizontalPodAutoScaler has finished synchronizing
status: Synced
IngressGateway:
message: IngressGateway has finished synchronizing
status: Synced
IngressVirtualService:
message: IngressVirtualService has finished synchronizing
status: Synced
NetworkPolicy:
message: NetworkPolicy has finished synchronizing
status: Synced
PeerAuthentication:
message: PeerAuthentication has finished synchronizing
status: Synced
PodDisruptionBudget:
message: 'json: cannot unmarshal string into Go value of type v1alpha1.Replicas'
status: Error
Service:
message: Service has finished synchronizing
status: Synced
ServiceAccount:
message: ServiceAccount has finished synchronizing
status: Synced
8 changes: 8 additions & 0 deletions tests/application/subresource-status/application-error.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: skiperator.kartverket.no/v1alpha1
kind: Application
metadata:
name: borked
spec:
image: image
port: 8080
replicas: "2"
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apiVersion: skiperator.kartverket.no/v1alpha1
kind: Application
metadata:
finalizers:
- skip.statkart.no/finalizer
name: working
spec:
enablePDB: true
image: image
port: 8080
priority: medium
redirectToHTTPS: true
replicas:
max: 5
min: 2
targetCpuUtilization: 80
strategy:
type: RollingUpdate
status:
application:
message: All controllers synced
status: Synced
controllers:
AuthorizationPolicy:
message: AuthorizationPolicy has finished synchronizing
status: Synced
Certificate:
message: Certificate has finished synchronizing
status: Synced
ConfigMap:
message: ConfigMap has finished synchronizing
status: Synced
Deployment:
message: Deployment has finished synchronizing
status: Synced
EgressServiceEntry:
message: EgressServiceEntry has finished synchronizing
status: Synced
HorizontalPodAutoScaler:
message: HorizontalPodAutoScaler has finished synchronizing
status: Synced
IngressGateway:
message: IngressGateway has finished synchronizing
status: Synced
IngressVirtualService:
message: IngressVirtualService has finished synchronizing
status: Synced
NetworkPolicy:
message: NetworkPolicy has finished synchronizing
status: Synced
PeerAuthentication:
message: PeerAuthentication has finished synchronizing
status: Synced
PodDisruptionBudget:
message: PodDisruptionBudget has finished synchronizing
status: Synced
Service:
message: Service has finished synchronizing
status: Synced
ServiceAccount:
message: ServiceAccount has finished synchronizing
status: Synced
ServiceMonitor:
message: ServiceMonitor has finished synchronizing
status: Synced
7 changes: 7 additions & 0 deletions tests/application/subresource-status/application-synced.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: skiperator.kartverket.no/v1alpha1
kind: Application
metadata:
name: working
spec:
image: image
port: 8080
19 changes: 19 additions & 0 deletions tests/application/subresource-status/chainsaw-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: chainsaw.kyverno.io/v1alpha1
kind: Test
metadata:
name: subresource-status
spec:
skip: false
concurrent: true
skipDelete: false
steps:
- try:
- create:
file: application-synced.yaml
- assert:
file: application-synced-assert.yaml
- try:
- create:
file: application-error.yaml
- assert:
file: application-error-assert.yaml

0 comments on commit 675acf1

Please sign in to comment.