github.com/redhat-appstudio/release-service@v0.0.0-20240507045911-a8558ef3422a/controllers/releaseplanadmission/adapter.go (about) 1 /* 2 Copyright 2023. 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 releaseplanadmission 18 19 import ( 20 "context" 21 "reflect" 22 23 "github.com/go-logr/logr" 24 "github.com/konflux-ci/operator-toolkit/controller" 25 "github.com/redhat-appstudio/release-service/api/v1alpha1" 26 "github.com/redhat-appstudio/release-service/loader" 27 "k8s.io/apimachinery/pkg/api/meta" 28 "sigs.k8s.io/controller-runtime/pkg/client" 29 ) 30 31 // adapter holds the objects needed to reconcile a ReleasePlanAdmission. 32 type adapter struct { 33 client client.Client 34 ctx context.Context 35 loader loader.ObjectLoader 36 logger *logr.Logger 37 releasePlanAdmission *v1alpha1.ReleasePlanAdmission 38 } 39 40 // newAdapter creates and returns an adapter instance. 41 func newAdapter(ctx context.Context, client client.Client, releasePlanAdmission *v1alpha1.ReleasePlanAdmission, loader loader.ObjectLoader, logger *logr.Logger) *adapter { 42 return &adapter{ 43 client: client, 44 ctx: ctx, 45 loader: loader, 46 logger: logger, 47 releasePlanAdmission: releasePlanAdmission, 48 } 49 } 50 51 // EnsureMatchingInformationIsSet is an operation that will ensure that the ReleasePlanAdmission has updated matching 52 // information in its status. 53 func (a *adapter) EnsureMatchingInformationIsSet() (controller.OperationResult, error) { 54 releasePlans, err := a.loader.GetMatchingReleasePlans(a.ctx, a.client, a.releasePlanAdmission) 55 if err != nil { 56 return controller.RequeueWithError(err) 57 } 58 59 copiedReleasePlanAdmission := a.releasePlanAdmission.DeepCopy() 60 patch := client.MergeFrom(copiedReleasePlanAdmission) 61 62 a.releasePlanAdmission.ClearMatchingInfo() 63 for i := range releasePlans.Items { 64 a.releasePlanAdmission.MarkMatched(&releasePlans.Items[i]) 65 } 66 67 // If there is no change in the matched ReleasePlans and the Matched condition is present 68 // (in case it is a new ReleasePlanAdmission going from matched to nil -> matched to nil), do not patch 69 if reflect.DeepEqual(copiedReleasePlanAdmission.Status.ReleasePlans, a.releasePlanAdmission.Status.ReleasePlans) && 70 meta.FindStatusCondition(copiedReleasePlanAdmission.Status.Conditions, 71 v1alpha1.MatchedConditionType.String()) != nil { 72 return controller.ContinueProcessing() 73 } 74 75 return controller.RequeueOnErrorOrContinue(a.client.Status().Patch(a.ctx, a.releasePlanAdmission, patch)) 76 }