sigs.k8s.io/kueue@v0.6.2/pkg/controller/admissionchecks/provisioning/admissioncheck_reconciler.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package provisioning
    18  
    19  import (
    20  	"context"
    21  
    22  	apimeta "k8s.io/apimachinery/pkg/api/meta"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/utils/ptr"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    27  
    28  	kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
    29  )
    30  
    31  type acReconciler struct {
    32  	client client.Client
    33  	helper *provisioningConfigHelper
    34  }
    35  
    36  var _ reconcile.Reconciler = (*acReconciler)(nil)
    37  
    38  func (a *acReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
    39  	ac := &kueue.AdmissionCheck{}
    40  	if err := a.client.Get(ctx, req.NamespacedName, ac); err != nil || ac.Spec.ControllerName != ControllerName {
    41  		return reconcile.Result{}, client.IgnoreNotFound(err)
    42  	}
    43  
    44  	currentCondition := ptr.Deref(apimeta.FindStatusCondition(ac.Status.Conditions, kueue.AdmissionCheckActive), metav1.Condition{})
    45  	newCondition := metav1.Condition{
    46  		Type:    kueue.AdmissionCheckActive,
    47  		Status:  metav1.ConditionTrue,
    48  		Reason:  "Active",
    49  		Message: "The admission check is active",
    50  	}
    51  
    52  	if _, err := a.helper.ConfigFromRef(ctx, ac.Spec.Parameters); err != nil {
    53  		newCondition.Status = metav1.ConditionFalse
    54  		newCondition.Reason = "BadParametersRef"
    55  		newCondition.Message = err.Error()
    56  	}
    57  
    58  	if currentCondition.Status != newCondition.Status {
    59  		apimeta.SetStatusCondition(&ac.Status.Conditions, newCondition)
    60  		return reconcile.Result{}, a.client.Status().Update(ctx, ac)
    61  	}
    62  	return reconcile.Result{}, nil
    63  }