github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/orbiter/kinds/clusters/kubernetes/software.go (about)

     1  package kubernetes
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  	"strings"
     7  
     8  	"github.com/caos/orbos/internal/operator/nodeagent/dep"
     9  
    10  	"github.com/caos/orbos/internal/operator/nodeagent/dep/sysctl"
    11  
    12  	"github.com/caos/orbos/internal/operator/common"
    13  	"github.com/caos/orbos/mntr"
    14  )
    15  
    16  type KubernetesVersion int
    17  
    18  const (
    19  	Unknown KubernetesVersion = iota
    20  	V1x15x0
    21  	V1x15x1
    22  	V1x15x2
    23  	V1x15x3
    24  	V1x15x4
    25  	V1x15x5
    26  	V1x15x6
    27  	V1x15x7
    28  	V1x15x8
    29  	V1x15x9
    30  	V1x15x10
    31  	V1x15x11
    32  	V1x15x12
    33  	V1x16x0
    34  	V1x16x1
    35  	V1x16x2
    36  	V1x16x3
    37  	V1x16x4
    38  	V1x16x5
    39  	V1x16x6
    40  	V1x16x7
    41  	V1x16x8
    42  	V1x16x9
    43  	V1x16x10
    44  	V1x16x11
    45  	V1x16x12
    46  	V1x16x13
    47  	V1x16x14
    48  	V1x16x15
    49  	V1x17x0
    50  	V1x17x1
    51  	V1x17x2
    52  	V1x17x3
    53  	V1x17x4
    54  	V1x17x5
    55  	V1x17x6
    56  	V1x17x7
    57  	V1x17x8
    58  	V1x17x9
    59  	V1x17x10
    60  	V1x17x11
    61  	V1x17x12
    62  	V1x17x13
    63  	V1x17x14
    64  	V1x17x15
    65  	V1x17x16
    66  	V1x17x17
    67  	V1x18x0
    68  	V1x18x1
    69  	V1x18x2
    70  	V1x18x3
    71  	V1x18x4
    72  	V1x18x5
    73  	V1x18x6
    74  	V1x18x7
    75  	V1x18x8
    76  	V1x18x9
    77  	V1x18x10
    78  	V1x18x11
    79  	V1x18x12
    80  	V1x18x13
    81  	V1x18x14
    82  	V1x18x15
    83  	V1x18x16
    84  	V1x18x17
    85  	V1x18x18
    86  	V1x18x19
    87  	V1x19x0
    88  	V1x19x1
    89  	V1x19x2
    90  	V1x19x3
    91  	V1x19x4
    92  	V1x19x5
    93  	V1x19x6
    94  	V1x19x7
    95  	V1x19x8
    96  	V1x19x9
    97  	V1x19x10
    98  	V1x20x0
    99  	V1x20x1
   100  	V1x20x2
   101  	V1x20x3
   102  	V1x20x4
   103  	V1x20x5
   104  	V1x20x6
   105  	V1x21x0
   106  )
   107  
   108  var kubernetesVersions = []string{
   109  	"unknown",
   110  	"v1.15.0", "v1.15.1", "v1.15.2", "v1.15.3", "v1.15.4", "v1.15.5", "v1.15.6", "v1.15.7", "v1.15.8", "v1.15.9", "v1.15.10", "v1.15.11", "v1.15.12",
   111  	"v1.16.0", "v1.16.1", "v1.16.2", "v1.16.3", "v1.16.4", "v1.16.5", "v1.16.6", "v1.16.7", "v1.16.8", "v1.16.9", "v1.16.10", "v1.16.11", "v1.16.12", "v1.16.13", "v1.16.14", "v1.16.15",
   112  	"v1.17.0", "v1.17.1", "v1.17.2", "v1.17.3", "v1.17.4", "v1.17.5", "v1.17.6", "v1.17.7", "v1.17.8", "v1.17.9", "v1.17.10", "v1.17.11", "v1.17.12", "v1.17.13", "v1.17.14", "v1.17.15", "v1.17.16", "v1.17.17",
   113  	"v1.18.0", "v1.18.1", "v1.18.2", "v1.18.3", "v1.18.4", "v1.18.5", "v1.18.6", "v1.18.7", "v1.18.8", "v1.18.9", "v1.18.10", "v1.18.11", "v1.18.12", "v1.18.13", "v1.18.14", "v1.18.15", "v1.18.16", "v1.18.17", "v1.18.18", "v1.18.19",
   114  	"v1.19.0", "v1.19.1", "v1.19.2", "v1.19.3", "v1.19.4", "v1.19.5", "v1.19.6", "v1.19.7", "v1.19.8", "v1.19.9", "v1.19.10",
   115  	"v1.20.0", "v1.20.1", "v1.20.2", "v1.20.3", "v1.20.4", "v1.20.5", "v1.20.6",
   116  	"v1.21.0",
   117  }
   118  
   119  func (k KubernetesVersion) String() string {
   120  	return kubernetesVersions[k]
   121  }
   122  
   123  func (k KubernetesVersion) DefineSoftware() common.Software {
   124  	sysctlPkg := common.Package{}
   125  	sysctl.Enable(&sysctlPkg, common.IpForward)
   126  	sysctl.Enable(&sysctlPkg, common.BridgeNfCallIptables)
   127  	sysctl.Enable(&sysctlPkg, common.BridgeNfCallIp6tables)
   128  	return common.Software{
   129  		Swap: common.Package{Version: "disabled"},
   130  		Containerruntime: common.Package{
   131  			Version: "docker-ce v19.03.5",
   132  			Config: map[string]string{
   133  				"daemon.json": `{
   134  	"exec-opts": ["native.cgroupdriver=systemd"],
   135  	"log-driver": "json-file",
   136  	"log-opts": {
   137  		"max-size": "100m"
   138  	},
   139  	"storage-driver": "overlay2"
   140  }`,
   141  			}},
   142  		Kubelet: common.Package{Version: k.String()},
   143  		Kubeadm: common.Package{Version: k.String()},
   144  		Kubectl: common.Package{Version: k.String()},
   145  		Sysctl:  sysctlPkg,
   146  		Kernel: common.Package{
   147  			Version: "3.10.0",
   148  			Config: map[string]string{
   149  				dep.CentOS7.String(): "1160.42.2.el7",
   150  			},
   151  		},
   152  	}
   153  }
   154  
   155  func KubernetesSoftware(current common.Software) common.Software {
   156  	return common.Software{
   157  		Swap:             current.Swap,
   158  		Containerruntime: current.Containerruntime,
   159  		Kubelet:          current.Kubelet,
   160  		Kubeadm:          current.Kubeadm,
   161  		Kubectl:          current.Kubectl,
   162  		Sysctl:           current.Sysctl,
   163  		Kernel:           current.Kernel,
   164  	}
   165  }
   166  
   167  func ParseString(version string) KubernetesVersion {
   168  	for idx, k8sVersion := range kubernetesVersions {
   169  		if k8sVersion == version {
   170  			return KubernetesVersion(idx)
   171  		}
   172  	}
   173  	return KubernetesVersion(0)
   174  }
   175  
   176  func (k KubernetesVersion) equals(other KubernetesVersion) bool {
   177  	return string(k) == string(other)
   178  }
   179  
   180  func (k KubernetesVersion) NextHighestMinor() KubernetesVersion {
   181  	switch k {
   182  	case V1x15x0, V1x15x1, V1x15x2, V1x15x3, V1x15x4, V1x15x5, V1x15x6, V1x15x7, V1x15x8, V1x15x9, V1x15x10, V1x15x11, V1x15x12:
   183  		return V1x16x15
   184  	case V1x16x0, V1x16x1, V1x16x2, V1x16x3, V1x16x4, V1x16x5, V1x16x6, V1x16x7, V1x16x8, V1x16x9, V1x16x10, V1x16x11, V1x16x12, V1x16x13, V1x16x14, V1x16x15:
   185  		return V1x17x17
   186  	case V1x17x0, V1x17x1, V1x17x2, V1x17x3, V1x17x4, V1x17x5, V1x17x6, V1x17x7, V1x17x8, V1x17x9, V1x17x10, V1x17x11, V1x17x12, V1x17x13, V1x17x14, V1x17x15, V1x17x16, V1x17x17:
   187  		return V1x18x19
   188  	case V1x18x0, V1x18x1, V1x18x2, V1x18x3, V1x18x4, V1x18x5, V1x18x6, V1x18x7, V1x18x8, V1x18x9, V1x18x10, V1x18x11, V1x18x12, V1x18x13, V1x18x14, V1x18x15, V1x18x16, V1x18x17, V1x18x18, V1x18x19:
   189  		return V1x19x10
   190  	case V1x19x0, V1x19x1, V1x19x2, V1x19x3, V1x19x4, V1x19x5, V1x19x6, V1x19x7, V1x19x8, V1x19x9, V1x19x10:
   191  		return V1x20x6
   192  	case V1x20x0, V1x20x1, V1x20x2, V1x20x3, V1x20x4, V1x20x5, V1x20x6:
   193  		return V1x21x0
   194  	default:
   195  		return Unknown
   196  	}
   197  }
   198  
   199  func (k KubernetesVersion) ExtractMinor(monitor mntr.Monitor) (int, error) {
   200  	return k.extractNumber(monitor, 1)
   201  }
   202  
   203  func (k KubernetesVersion) ExtractPatch(monitor mntr.Monitor) (int, error) {
   204  	return k.extractNumber(monitor, 2)
   205  }
   206  
   207  func (k KubernetesVersion) extractNumber(monitor mntr.Monitor, position int) (int, error) {
   208  	if k == Unknown {
   209  		return 0, errors.New("unknown kubernetes version")
   210  	}
   211  
   212  	parts := strings.Split(k.String(), ".")
   213  	version, err := strconv.ParseInt(parts[position], 10, 8)
   214  	if err != nil {
   215  		return 0, err
   216  	}
   217  
   218  	monitor.WithFields(map[string]interface{}{
   219  		"number":   version,
   220  		"position": position,
   221  		"string":   k,
   222  	}).Debug("Extracted from semantic version")
   223  
   224  	return int(version), nil
   225  }
   226  
   227  func softwareContains(this common.Software, that common.Software) bool {
   228  	return contains(this.Swap, that.Swap) &&
   229  		contains(this.Kubelet, that.Kubelet) &&
   230  		contains(this.Kubeadm, that.Kubeadm) &&
   231  		contains(this.Kubectl, that.Kubectl) &&
   232  		contains(this.Containerruntime, that.Containerruntime) &&
   233  		contains(this.KeepaliveD, that.KeepaliveD) &&
   234  		contains(this.Nginx, that.Nginx) &&
   235  		contains(this.Hostname, that.Hostname) &&
   236  		sysctl.Contains(this.Sysctl, that.Sysctl) &&
   237  		contains(this.Health, that.Health) &&
   238  		contains(this.Kernel, that.Kernel)
   239  }
   240  
   241  func contains(this, that common.Package) bool {
   242  	return that.Version == "" && that.Config == nil || common.PackageEquals(this, that)
   243  }
   244  
   245  func softwareDefines(this common.Software, that common.Software) bool {
   246  	return defines(this.Swap, that.Swap) &&
   247  		defines(this.Kubelet, that.Kubelet) &&
   248  		defines(this.Kubeadm, that.Kubeadm) &&
   249  		defines(this.Kubectl, that.Kubectl) &&
   250  		defines(this.Containerruntime, that.Containerruntime) &&
   251  		defines(this.KeepaliveD, that.KeepaliveD) &&
   252  		defines(this.Nginx, that.Nginx) &&
   253  		defines(this.Hostname, that.Hostname) &&
   254  		defines(this.Sysctl, that.Sysctl) &&
   255  		defines(this.Health, that.Health) &&
   256  		defines(this.Kernel, that.Kernel)
   257  }
   258  
   259  func defines(this, that common.Package) bool {
   260  	zeroPkg := common.Package{}
   261  	return common.PackageEquals(that, zeroPkg) || !common.PackageEquals(this, zeroPkg)
   262  }