kubesphere.io/api@v0.0.0-20231107125330-c9a03957060c/application/v1alpha1/helmapplication_types.go (about)

     1  /*
     2  Copyright 2020 The KubeSphere 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 v1alpha1
    18  
    19  import (
    20  	"strings"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  
    24  	"kubesphere.io/api/constants"
    25  )
    26  
    27  const (
    28  	ResourceKindHelmApplication     = "HelmApplication"
    29  	ResourceSingularHelmApplication = "helmapplication"
    30  	ResourcePluralHelmApplication   = "helmapplications"
    31  )
    32  
    33  // HelmApplicationSpec defines the desired state of HelmApplication
    34  type HelmApplicationSpec struct {
    35  	// the name of the helm application
    36  	Name string `json:"name"`
    37  	// description from chart's description or frontend
    38  	Description string `json:"description,omitempty"`
    39  	// attachments id
    40  	Attachments []string `json:"attachments,omitempty"`
    41  	// info from frontend
    42  	Abstraction string `json:"abstraction,omitempty"`
    43  	AppHome     string `json:"appHome,omitempty"`
    44  	// The attachment id of the icon
    45  	Icon string `json:"icon,omitempty"`
    46  }
    47  
    48  // HelmApplicationStatus defines the observed state of HelmApplication
    49  type HelmApplicationStatus struct {
    50  	// If this application belong to appStore, latestVersion is the the latest version of the active application version.
    51  	// otherwise latestVersion is the latest version of all application version
    52  	LatestVersion string `json:"latestVersion,omitempty"`
    53  	// the state of the helm application: draft, submitted, passed, rejected, suspended, active
    54  	State      string       `json:"state,omitempty"`
    55  	UpdateTime *metav1.Time `json:"updateTime"`
    56  	StatusTime *metav1.Time `json:"statusTime"`
    57  }
    58  
    59  // +kubebuilder:resource:scope=Cluster,shortName=happ
    60  // +kubebuilder:subresource:status
    61  // +kubebuilder:printcolumn:name="application name",type=string,JSONPath=`.spec.name`
    62  // +kubebuilder:printcolumn:name="workspace",type="string",JSONPath=".metadata.labels.kubesphere\\.io/workspace"
    63  // +kubebuilder:printcolumn:name="State",type="string",JSONPath=".status.state"
    64  // +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
    65  // +genclient
    66  // +genclient:nonNamespaced
    67  // +kubebuilder:object:root=true
    68  
    69  // HelmApplication is the Schema for the helmapplications API
    70  type HelmApplication struct {
    71  	metav1.TypeMeta   `json:",inline"`
    72  	metav1.ObjectMeta `json:"metadata,omitempty"`
    73  
    74  	Spec   HelmApplicationSpec   `json:"spec,omitempty"`
    75  	Status HelmApplicationStatus `json:"status,omitempty"`
    76  }
    77  
    78  // +kubebuilder:object:root=true
    79  
    80  // HelmApplicationList contains a list of HelmApplication
    81  type HelmApplicationList struct {
    82  	metav1.TypeMeta `json:",inline"`
    83  	metav1.ListMeta `json:"metadata,omitempty"`
    84  	Items           []HelmApplication `json:"items"`
    85  }
    86  
    87  func init() {
    88  	SchemeBuilder.Register(&HelmApplication{}, &HelmApplicationList{})
    89  }
    90  
    91  func (in *HelmApplication) GetTrueName() string {
    92  	return in.Spec.Name
    93  }
    94  
    95  func (in *HelmApplication) GetHelmRepoId() string {
    96  	return getValue(in.Labels, constants.ChartRepoIdLabelKey)
    97  }
    98  
    99  func (in *HelmApplication) GetHelmApplicationId() string {
   100  	return strings.TrimSuffix(in.Name, HelmApplicationAppStoreSuffix)
   101  }
   102  func (in *HelmApplication) GetHelmCategoryId() string {
   103  	return getValue(in.Labels, constants.CategoryIdLabelKey)
   104  }
   105  
   106  func (in *HelmApplication) GetWorkspace() string {
   107  	ws := getValue(in.Labels, constants.WorkspaceLabelKey)
   108  	if ws == "" {
   109  		return getValue(in.Labels, OriginWorkspaceLabelKey)
   110  	}
   111  	return ws
   112  }
   113  
   114  func getValue(m map[string]string, key string) string {
   115  	if m == nil {
   116  		return ""
   117  	}
   118  	return m[key]
   119  }
   120  
   121  func (in *HelmApplication) GetCategoryId() string {
   122  	return getValue(in.Labels, constants.CategoryIdLabelKey)
   123  }
   124  
   125  func (in *HelmApplication) State() string {
   126  	if in.Status.State == "" {
   127  		return StateDraft
   128  	}
   129  	return in.Status.State
   130  }
   131  
   132  func (in *HelmApplication) GetCreator() string {
   133  	return getValue(in.Annotations, constants.CreatorAnnotationKey)
   134  }