github.com/redhat-appstudio/release-service@v0.0.0-20240507143925-083712697924/api/v1alpha1/releaseplanadmission_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  	"sort"
    22  
    23  	"github.com/konflux-ci/operator-toolkit/conditions"
    24  	"github.com/redhat-appstudio/release-service/metadata"
    25  	tektonutils "github.com/redhat-appstudio/release-service/tekton/utils"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime"
    28  	"k8s.io/apimachinery/pkg/types"
    29  )
    30  
    31  // ReleasePlanAdmissionSpec defines the desired state of ReleasePlanAdmission.
    32  type ReleasePlanAdmissionSpec struct {
    33  	// Applications is a list of references to applications to be released in the managed namespace
    34  	// +required
    35  	Applications []string `json:"applications"`
    36  
    37  	// Data is an unstructured key used for providing data for the managed Release Pipeline
    38  	// +kubebuilder:pruning:PreserveUnknownFields
    39  	// +optional
    40  	Data *runtime.RawExtension `json:"data,omitempty"`
    41  
    42  	// Environment defines which Environment will be used to release the Application
    43  	// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
    44  	// +optional
    45  	Environment string `json:"environment,omitempty"`
    46  
    47  	// Origin references where the release requests should come from
    48  	// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
    49  	// +required
    50  	Origin string `json:"origin"`
    51  
    52  	// Pipeline contains all the information about the managed Pipeline
    53  	// +required
    54  	Pipeline *tektonutils.Pipeline `json:"pipeline"`
    55  
    56  	// Policy to validate before releasing an artifact
    57  	// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
    58  	// +required
    59  	Policy string `json:"policy"`
    60  }
    61  
    62  // MatchedReleasePlan defines the relevant information for a matched ReleasePlan.
    63  type MatchedReleasePlan struct {
    64  	// Name contains the namespaced name of the ReleasePlan
    65  	// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?\/[a-z0-9]([-a-z0-9]*[a-z0-9])?$
    66  	// +optional
    67  	Name string `json:"name,omitempty"`
    68  
    69  	// Active indicates whether the ReleasePlan is set to auto-release or not
    70  	// +kubebuilder:default:false
    71  	// +optional
    72  	Active bool `json:"active,omitempty"`
    73  }
    74  
    75  // ReleasePlanAdmissionStatus defines the observed state of ReleasePlanAdmission.
    76  type ReleasePlanAdmissionStatus struct {
    77  	// Conditions represent the latest available observations for the releasePlanAdmission
    78  	// +optional
    79  	Conditions []metav1.Condition `json:"conditions"`
    80  
    81  	// ReleasePlan is a list of releasePlans matched to the ReleasePlanAdmission
    82  	// +optional
    83  	ReleasePlans []MatchedReleasePlan `json:"releasePlans"`
    84  }
    85  
    86  // +kubebuilder:object:root=true
    87  // +kubebuilder:resource:shortName=rpa
    88  // +kubebuilder:subresource:status
    89  // +kubebuilder:printcolumn:name="Environment",type=string,JSONPath=`.spec.environment`
    90  // +kubebuilder:printcolumn:name="Origin",type=string,JSONPath=`.spec.origin`
    91  
    92  // ReleasePlanAdmission is the Schema for the ReleasePlanAdmissions API.
    93  type ReleasePlanAdmission struct {
    94  	metav1.TypeMeta   `json:",inline"`
    95  	metav1.ObjectMeta `json:"metadata,omitempty"`
    96  
    97  	Spec   ReleasePlanAdmissionSpec   `json:"spec,omitempty"`
    98  	Status ReleasePlanAdmissionStatus `json:"status,omitempty"`
    99  }
   100  
   101  // ClearMatchingInfo marks the ReleasePlanAdmission as no longer matched to any ReleasePlan.
   102  func (rpa *ReleasePlanAdmission) ClearMatchingInfo() {
   103  	rpa.Status.ReleasePlans = []MatchedReleasePlan{}
   104  	conditions.SetCondition(&rpa.Status.Conditions, MatchedConditionType, metav1.ConditionFalse, MatchedReason)
   105  }
   106  
   107  // MarkMatched marks the ReleasePlanAdmission as matched to a given ReleasePlan.
   108  func (rpa *ReleasePlanAdmission) MarkMatched(releasePlan *ReleasePlan) {
   109  	pairedReleasePlan := MatchedReleasePlan{
   110  		Name:   fmt.Sprintf("%s%c%s", releasePlan.GetNamespace(), types.Separator, releasePlan.GetName()),
   111  		Active: (releasePlan.GetLabels()[metadata.AutoReleaseLabel] == "true"),
   112  	}
   113  
   114  	rpa.Status.ReleasePlans = append(rpa.Status.ReleasePlans, pairedReleasePlan)
   115  	sort.Slice(rpa.Status.ReleasePlans, func(i, j int) bool {
   116  		return rpa.Status.ReleasePlans[i].Name < rpa.Status.ReleasePlans[j].Name
   117  	})
   118  
   119  	// Update the condition every time one is added so lastTransitionTime updates
   120  	conditions.SetCondition(&rpa.Status.Conditions, MatchedConditionType, metav1.ConditionTrue, MatchedReason)
   121  }
   122  
   123  // +kubebuilder:object:root=true
   124  
   125  // ReleasePlanAdmissionList contains a list of ReleasePlanAdmission.
   126  type ReleasePlanAdmissionList struct {
   127  	metav1.TypeMeta `json:",inline"`
   128  	metav1.ListMeta `json:"metadata,omitempty"`
   129  	Items           []ReleasePlanAdmission `json:"items"`
   130  }
   131  
   132  func init() {
   133  	SchemeBuilder.Register(&ReleasePlanAdmission{}, &ReleasePlanAdmissionList{})
   134  }