github.com/sealerio/sealer@v0.11.1-0.20240507115618-f4f89c5853ae/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  	"net"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  
    24  	"github.com/sealerio/sealer/common"
    25  	v1 "github.com/sealerio/sealer/types/api/v1"
    26  )
    27  
    28  // NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.
    29  
    30  // ClusterSpec defines the desired state of Cluster
    31  type ClusterSpec struct {
    32  	// desired state of cluster
    33  	// Important: Run "make" to regenerate code after modifying this file
    34  	// Foo is an example field of Cluster. Edit Cluster_types.go to remove/update
    35  	Image string `json:"image,omitempty"`
    36  	// Why env not using map[string]string
    37  	// Because some argument is list, like: CertSANS=127.0.0.1 CertSANS=localhost, if ENV is map, will merge those two values
    38  	// but user want to config a list, using array we can convert it to {CertSANS:[127.0.0.1, localhost]}
    39  	Env     []string `json:"env,omitempty"`
    40  	CMDArgs []string `json:"cmd_args,omitempty"`
    41  	CMD     []string `json:"cmd,omitempty"`
    42  	// APPNames This field allows user to specify the app name they want to run launch.
    43  	APPNames         []string               `json:"appNames,omitempty"`
    44  	Hosts            []Host                 `json:"hosts,omitempty"`
    45  	SSH              v1.SSH                 `json:"ssh,omitempty"`
    46  	ContainerRuntime ContainerRuntimeConfig `json:"containerRuntime,omitempty"`
    47  	// HostAliases holds the mapping between IP and hostnames that will be injected as an entry in the
    48  	// host's hosts file.
    49  	HostAliases []HostAlias `json:"hostAliases,omitempty"`
    50  	// Registry field contains configurations about local registry and remote registry.
    51  	Registry Registry `json:"registry,omitempty"`
    52  
    53  	// DataRoot set sealer rootfs directory path.
    54  	// if not set, default value is "/var/lib/sealer/data"
    55  	DataRoot string `json:"dataRoot,omitempty"`
    56  }
    57  
    58  type ContainerRuntimeConfig struct {
    59  	Type string `json:"type,omitempty"`
    60  }
    61  
    62  type Host struct {
    63  	IPS   []net.IP `json:"ips,omitempty"`
    64  	Roles []string `json:"roles,omitempty"`
    65  	//overwrite SSH config
    66  	SSH v1.SSH `json:"ssh,omitempty"`
    67  	//overwrite env
    68  	Env    []string          `json:"env,omitempty"`
    69  	Labels map[string]string `json:"labels,omitempty"`
    70  	Taints []string          `json:"taints,omitempty"`
    71  }
    72  
    73  // HostAlias holds the mapping between IP and hostnames that will be injected as an entry in the
    74  // pod's hosts file.
    75  type HostAlias struct {
    76  	// IP address of the host file entry.
    77  	IP string `json:"ip,omitempty"`
    78  	// Hostnames for the above IP address.
    79  	Hostnames []string `json:"hostnames,omitempty"`
    80  }
    81  
    82  type Registry struct {
    83  	// LocalRegistry is the sealer builtin registry configuration
    84  	LocalRegistry *LocalRegistry `json:"localRegistry,omitempty"`
    85  	// ExternalRegistry used to serve external registry service. do not support yet.
    86  	ExternalRegistry *ExternalRegistry `json:"externalRegistry,omitempty"`
    87  }
    88  
    89  type RegistryConfig struct {
    90  	Domain   string `json:"domain,omitempty"`
    91  	Port     int    `json:"port,omitempty"`
    92  	Username string `json:"username,omitempty"`
    93  	Password string `json:"password,omitempty"`
    94  }
    95  
    96  type ExternalRegistry struct {
    97  	RegistryConfig
    98  }
    99  
   100  type LocalRegistry struct {
   101  	RegistryConfig
   102  	// HA indicate that whether local registry will be deployed on all master nodes.
   103  	// if LocalRegistry is not specified, default value is true.
   104  	HA *bool `json:"ha,omitempty"`
   105  	// Insecure indicated that whether the local registry is exposed in HTTPS.
   106  	// if true sealer will not generate default ssl cert.
   107  	Insecure *bool   `json:"insecure,omitempty"`
   108  	Cert     TLSCert `json:"cert,omitempty"`
   109  }
   110  
   111  type TLSCert struct {
   112  	SubjectAltName *SubjectAltName `json:"subjectAltName,omitempty"`
   113  }
   114  
   115  type SubjectAltName struct {
   116  	DNSNames []string `json:"dnsNames,omitempty"`
   117  	IPs      []string `json:"ips,omitempty"`
   118  }
   119  
   120  // ClusterStatus defines the observed state of Cluster
   121  type ClusterStatus struct {
   122  	// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
   123  	// Important: Run "make" to regenerate code after modifying this file
   124  }
   125  
   126  // +kubebuilder:object:root=true
   127  // +kubebuilder:subresource:status
   128  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   129  
   130  // Cluster is the Schema for the clusters API
   131  type Cluster struct {
   132  	metav1.TypeMeta   `json:",inline"`
   133  	metav1.ObjectMeta `json:"metadata,omitempty"`
   134  
   135  	Spec   ClusterSpec   `json:"spec,omitempty"`
   136  	Status ClusterStatus `json:"status,omitempty"`
   137  }
   138  
   139  func (in *Cluster) GetMasterIPList() []net.IP {
   140  	return in.GetIPSByRole(common.MASTER)
   141  }
   142  
   143  func (in *Cluster) GetMasterIPStrList() (ipStrList []string) {
   144  	ipList := in.GetIPSByRole(common.MASTER)
   145  
   146  	for _, ip := range ipList {
   147  		ipStrList = append(ipStrList, ip.String())
   148  	}
   149  
   150  	return ipStrList
   151  }
   152  
   153  func (in *Cluster) GetNodeIPList() []net.IP {
   154  	return in.GetIPSByRole(common.NODE)
   155  }
   156  
   157  func (in *Cluster) GetAllIPList() []net.IP {
   158  	return append(in.GetIPSByRole(common.MASTER), in.GetIPSByRole(common.NODE)...)
   159  }
   160  
   161  func (in *Cluster) GetMaster0IP() net.IP {
   162  	masterIPList := in.GetIPSByRole(common.MASTER)
   163  	if len(masterIPList) == 0 {
   164  		return nil
   165  	}
   166  	return masterIPList[0]
   167  }
   168  
   169  func (in *Cluster) GetIPSByRole(role string) []net.IP {
   170  	var hosts []net.IP
   171  	for _, host := range in.Spec.Hosts {
   172  		for _, hostRole := range host.Roles {
   173  			if role == hostRole {
   174  				hosts = append(hosts, host.IPS...)
   175  				continue
   176  			}
   177  		}
   178  	}
   179  	return hosts
   180  }
   181  
   182  func (in *Cluster) GetAnnotationsByKey(key string) string {
   183  	return in.Annotations[key]
   184  }
   185  
   186  func (in *Cluster) SetAnnotations(key, value string) {
   187  	if in.Annotations == nil {
   188  		in.Annotations = make(map[string]string)
   189  	}
   190  	in.Annotations[key] = value
   191  }
   192  
   193  // +kubebuilder:object:root=true
   194  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   195  
   196  // ClusterList contains a list of Cluster
   197  type ClusterList struct {
   198  	metav1.TypeMeta `json:",inline"`
   199  	metav1.ListMeta `json:"metadata,omitempty"`
   200  	Items           []Cluster `json:"items"`
   201  }
   202  
   203  func init() {
   204  	SchemeBuilder.Register(&Cluster{}, &ClusterList{})
   205  }