github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/types/api/v2/cluster_types.go (about)

     1  /*
     2  Copyright 2021 alibaba.
     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 v2
    18  
    19  import (
    20  	"github.com/alibaba/sealer/common"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  
    23  	v1 "github.com/alibaba/sealer/types/api/v1"
    24  )
    25  
    26  // NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.
    27  
    28  // ClusterSpec defines the desired state of Cluster
    29  type ClusterSpec struct {
    30  	// desired state of cluster
    31  	// Important: Run "make" to regenerate code after modifying this file
    32  	// Foo is an example field of Cluster. Edit Cluster_types.go to remove/update
    33  	Image string `json:"image,omitempty"`
    34  	// Why env not using map[string]string
    35  	// Because some argument is list, like: CertSANS=127.0.0.1 CertSANS=localhost, if ENV is map, will merge those two values
    36  	// but user want to config a list, using array we can convert it to {CertSANS:[127.0.0.1, localhost]}
    37  	Env     []string `json:"env,omitempty"`
    38  	CMDArgs []string `json:"cmd_args,omitempty"`
    39  	CMD     []string `json:"cmd,omitempty"`
    40  	Hosts   []Host   `json:"hosts,omitempty"`
    41  	SSH     v1.SSH   `json:"ssh,omitempty"`
    42  }
    43  
    44  type Host struct {
    45  	IPS   []string `json:"ips,omitempty"`
    46  	Roles []string `json:"roles,omitempty"`
    47  	//overwrite SSH config
    48  	SSH v1.SSH `json:"ssh,omitempty"`
    49  	//overwrite env
    50  	Env []string `json:"env,omitempty"`
    51  }
    52  
    53  // ClusterStatus defines the observed state of Cluster
    54  type ClusterStatus struct {
    55  	// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
    56  	// Important: Run "make" to regenerate code after modifying this file
    57  }
    58  
    59  // +kubebuilder:object:root=true
    60  // +kubebuilder:subresource:status
    61  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    62  
    63  // Cluster is the Schema for the clusters API
    64  type Cluster struct {
    65  	metav1.TypeMeta   `json:",inline"`
    66  	metav1.ObjectMeta `json:"metadata,omitempty"`
    67  
    68  	Spec   ClusterSpec   `json:"spec,omitempty"`
    69  	Status ClusterStatus `json:"status,omitempty"`
    70  }
    71  
    72  func (in *Cluster) GetMasterIPList() []string {
    73  	return in.GetIPSByRole(common.MASTER)
    74  }
    75  
    76  func (in *Cluster) GetNodeIPList() []string {
    77  	return in.GetIPSByRole(common.NODE)
    78  }
    79  
    80  func (in *Cluster) GetAllIPList() []string {
    81  	return append(in.GetIPSByRole(common.MASTER), in.GetIPSByRole(common.NODE)...)
    82  }
    83  
    84  func (in *Cluster) GetMaster0IP() string {
    85  	if len(in.Spec.Hosts) == 0 {
    86  		return ""
    87  	}
    88  	if len(in.Spec.Hosts[0].IPS) == 0 {
    89  		return ""
    90  	}
    91  	return in.Spec.Hosts[0].IPS[0]
    92  }
    93  
    94  func (in *Cluster) GetIPSByRole(role string) []string {
    95  	var hosts []string
    96  	for _, host := range in.Spec.Hosts {
    97  		for _, hostRole := range host.Roles {
    98  			if role == hostRole {
    99  				hosts = append(hosts, host.IPS...)
   100  				continue
   101  			}
   102  		}
   103  	}
   104  	return hosts
   105  }
   106  func (in *Cluster) GetAnnotationsByKey(key string) string {
   107  	return in.Annotations[key]
   108  }
   109  
   110  func (in *Cluster) SetAnnotations(key, value string) {
   111  	if in.Annotations == nil {
   112  		in.Annotations = make(map[string]string)
   113  	}
   114  	in.Annotations[key] = value
   115  }
   116  
   117  // +kubebuilder:object:root=true
   118  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   119  
   120  // ClusterList contains a list of Cluster
   121  type ClusterList struct {
   122  	metav1.TypeMeta `json:",inline"`
   123  	metav1.ListMeta `json:"metadata,omitempty"`
   124  	Items           []Cluster `json:"items"`
   125  }
   126  
   127  func init() {
   128  	SchemeBuilder.Register(&Cluster{}, &ClusterList{})
   129  }