github.com/redhat-appstudio/release-service@v0.0.0-20240507143925-083712697924/api/v1alpha1/releaseplan_types.go (about)

     1  /*
     2  Copyright 2022.
     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 v1alpha1
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/konflux-ci/operator-toolkit/conditions"
    23  	"github.com/redhat-appstudio/release-service/metadata"
    24  	tektonutils "github.com/redhat-appstudio/release-service/tekton/utils"
    25  	"k8s.io/apimachinery/pkg/api/meta"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/apimachinery/pkg/types"
    29  )
    30  
    31  // ReleasePlanSpec defines the desired state of ReleasePlan.
    32  type ReleasePlanSpec struct {
    33  	// Application is a reference to the application to be released in the managed namespace
    34  	// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
    35  	// +required
    36  	Application string `json:"application"`
    37  
    38  	// Data is an unstructured key used for providing data for the managed Release Pipeline
    39  	// +kubebuilder:pruning:PreserveUnknownFields
    40  	// +optional
    41  	Data *runtime.RawExtension `json:"data,omitempty"`
    42  
    43  	// PipelineRef is an optional reference to a Pipeline that would be executed
    44  	// before the managed Release Pipeline
    45  	// +optional
    46  	PipelineRef *tektonutils.PipelineRef `json:"pipelineRef,omitempty"`
    47  
    48  	// ServiceAccount is the name of the service account to use in the
    49  	// Pipeline to gain elevated privileges
    50  	// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
    51  	// +optional
    52  	ServiceAccount string `json:"serviceAccount,omitempty"`
    53  
    54  	// ReleaseGracePeriodDays is the number of days a Release should be kept
    55  	// This value is used to define the Release ExpirationTime
    56  	// +kubebuilder:default:=7
    57  	// +optional
    58  	ReleaseGracePeriodDays int `json:"releaseGracePeriodDays,omitempty"`
    59  
    60  	// Target references where to send the release requests
    61  	// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
    62  	// +required
    63  	Target string `json:"target"`
    64  }
    65  
    66  // MatchedReleasePlanAdmission defines the relevant information for a matched ReleasePlanAdmission.
    67  type MatchedReleasePlanAdmission struct {
    68  	// Name contains the namespaced name of the releasePlanAdmission
    69  	// +optional
    70  	Name string `json:"name,omitempty"`
    71  
    72  	// Active indicates whether the ReleasePlanAdmission is set to auto-release or not
    73  	// +kubebuilder:default:false
    74  	// +optional
    75  	Active bool `json:"active,omitempty"`
    76  }
    77  
    78  // ReleasePlanStatus defines the observed state of ReleasePlan.
    79  type ReleasePlanStatus struct {
    80  	// Conditions represent the latest available observations for the releasePlan
    81  	// +optional
    82  	Conditions []metav1.Condition `json:"conditions"`
    83  
    84  	// ReleasePlanAdmission contains the information of the releasePlanAdmission this ReleasePlan is
    85  	// matched to
    86  	// +optional
    87  	ReleasePlanAdmission MatchedReleasePlanAdmission `json:"releasePlanAdmission,omitempty"`
    88  }
    89  
    90  // +kubebuilder:object:root=true
    91  // +kubebuilder:resource:shortName=rp
    92  // +kubebuilder:subresource:status
    93  // +kubebuilder:printcolumn:name="Application",type=string,JSONPath=`.spec.application`
    94  // +kubebuilder:printcolumn:name="Target",type=string,JSONPath=`.spec.target`
    95  
    96  // ReleasePlan is the Schema for the ReleasePlans API.
    97  type ReleasePlan struct {
    98  	metav1.TypeMeta   `json:",inline"`
    99  	metav1.ObjectMeta `json:"metadata,omitempty"`
   100  
   101  	Spec   ReleasePlanSpec   `json:"spec,omitempty"`
   102  	Status ReleasePlanStatus `json:"status,omitempty"`
   103  }
   104  
   105  // MarkMatched marks the ReleasePlan as matched to a given ReleasePlanAdmission.
   106  func (rp *ReleasePlan) MarkMatched(releasePlanAdmission *ReleasePlanAdmission) {
   107  	rp.setMatchedStatus(releasePlanAdmission, metav1.ConditionTrue)
   108  }
   109  
   110  // MarkUnmatched marks the ReleasePlan as not matched to any ReleasePlanAdmission.
   111  func (rp *ReleasePlan) MarkUnmatched() {
   112  	if meta.IsStatusConditionPresentAndEqual(rp.Status.Conditions, MatchedConditionType.String(), metav1.ConditionFalse) {
   113  		return
   114  	}
   115  
   116  	rp.setMatchedStatus(nil, metav1.ConditionFalse)
   117  }
   118  
   119  // setMatchedStatus sets the ReleasePlan Matched condition based on the passed releasePlanAdmission and status.
   120  func (rp *ReleasePlan) setMatchedStatus(releasePlanAdmission *ReleasePlanAdmission, status metav1.ConditionStatus) {
   121  	rp.Status.ReleasePlanAdmission = MatchedReleasePlanAdmission{}
   122  
   123  	if releasePlanAdmission != nil {
   124  		rp.Status.ReleasePlanAdmission.Name = fmt.Sprintf("%s%c%s", releasePlanAdmission.GetNamespace(),
   125  			types.Separator, releasePlanAdmission.GetName())
   126  		rp.Status.ReleasePlanAdmission.Active = (releasePlanAdmission.GetLabels()[metadata.AutoReleaseLabel] == "true")
   127  	}
   128  
   129  	conditions.SetCondition(&rp.Status.Conditions, MatchedConditionType, status, MatchedReason)
   130  }
   131  
   132  // +kubebuilder:object:root=true
   133  
   134  // ReleasePlanList contains a list of ReleasePlan.
   135  type ReleasePlanList struct {
   136  	metav1.TypeMeta `json:",inline"`
   137  	metav1.ListMeta `json:"metadata,omitempty"`
   138  	Items           []ReleasePlan `json:"items"`
   139  }
   140  
   141  func init() {
   142  	SchemeBuilder.Register(&ReleasePlan{}, &ReleasePlanList{})
   143  }