mirror of
https://github.com/rancher/rke.git
synced 2025-09-16 15:10:12 +00:00
restart related k8s services when cloud provider config is updated
This commit is contained in:
committed by
Alena Prokharchyk
parent
f8bb7c1510
commit
4c30f1a28d
@@ -2,6 +2,7 @@ package cluster
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/md5"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -29,6 +30,7 @@ const (
|
|||||||
CoreOS = "CoreOS"
|
CoreOS = "CoreOS"
|
||||||
CoreOSPrefixPath = "/opt/rke"
|
CoreOSPrefixPath = "/opt/rke"
|
||||||
ContainerNameLabel = "io.rancher.rke.container.name"
|
ContainerNameLabel = "io.rancher.rke.container.name"
|
||||||
|
CloudConfigSumEnv = "RKE_CLOUD_CONFIG_CHECKSUM"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GeneratePlan(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig, hostsInfoMap map[string]types.Info) (v3.RKEPlan, error) {
|
func GeneratePlan(ctx context.Context, rkeConfig *v3.RancherKubernetesEngineConfig, hostsInfoMap map[string]types.Info) (v3.RKEPlan, error) {
|
||||||
@@ -132,6 +134,11 @@ func (c *Cluster) BuildKubeAPIProcess(prefixPath string) v3.Process {
|
|||||||
if len(c.CloudProvider.Name) > 0 && c.CloudProvider.Name != aws.AWSCloudProviderName {
|
if len(c.CloudProvider.Name) > 0 && c.CloudProvider.Name != aws.AWSCloudProviderName {
|
||||||
CommandArgs["cloud-config"] = CloudConfigPath
|
CommandArgs["cloud-config"] = CloudConfigPath
|
||||||
}
|
}
|
||||||
|
if len(c.CloudProvider.Name) > 0 {
|
||||||
|
c.Services.KubeAPI.ExtraEnv = append(
|
||||||
|
c.Services.KubeAPI.ExtraEnv,
|
||||||
|
fmt.Sprintf("%s=%s", CloudConfigSumEnv, getCloudConfigChecksum(c.CloudProvider)))
|
||||||
|
}
|
||||||
// check if our version has specific options for this component
|
// check if our version has specific options for this component
|
||||||
serviceOptions := c.GetKubernetesServicesOptions()
|
serviceOptions := c.GetKubernetesServicesOptions()
|
||||||
if serviceOptions.KubeAPI != nil {
|
if serviceOptions.KubeAPI != nil {
|
||||||
@@ -192,7 +199,7 @@ func (c *Cluster) BuildKubeAPIProcess(prefixPath string) v3.Process {
|
|||||||
Args: args,
|
Args: args,
|
||||||
VolumesFrom: VolumesFrom,
|
VolumesFrom: VolumesFrom,
|
||||||
Binds: Binds,
|
Binds: Binds,
|
||||||
Env: c.Services.KubeAPI.ExtraEnv,
|
Env: getUniqStringList(c.Services.KubeAPI.ExtraEnv),
|
||||||
NetworkMode: "host",
|
NetworkMode: "host",
|
||||||
RestartPolicy: "always",
|
RestartPolicy: "always",
|
||||||
Image: c.Services.KubeAPI.Image,
|
Image: c.Services.KubeAPI.Image,
|
||||||
@@ -230,7 +237,11 @@ func (c *Cluster) BuildKubeControllerProcess(prefixPath string) v3.Process {
|
|||||||
if len(c.CloudProvider.Name) > 0 && c.CloudProvider.Name != aws.AWSCloudProviderName {
|
if len(c.CloudProvider.Name) > 0 && c.CloudProvider.Name != aws.AWSCloudProviderName {
|
||||||
CommandArgs["cloud-config"] = CloudConfigPath
|
CommandArgs["cloud-config"] = CloudConfigPath
|
||||||
}
|
}
|
||||||
|
if len(c.CloudProvider.Name) > 0 {
|
||||||
|
c.Services.KubeController.ExtraEnv = append(
|
||||||
|
c.Services.KubeController.ExtraEnv,
|
||||||
|
fmt.Sprintf("%s=%s", CloudConfigSumEnv, getCloudConfigChecksum(c.CloudProvider)))
|
||||||
|
}
|
||||||
// check if our version has specific options for this component
|
// check if our version has specific options for this component
|
||||||
serviceOptions := c.GetKubernetesServicesOptions()
|
serviceOptions := c.GetKubernetesServicesOptions()
|
||||||
if serviceOptions.KubeController != nil {
|
if serviceOptions.KubeController != nil {
|
||||||
@@ -274,7 +285,7 @@ func (c *Cluster) BuildKubeControllerProcess(prefixPath string) v3.Process {
|
|||||||
Args: args,
|
Args: args,
|
||||||
VolumesFrom: VolumesFrom,
|
VolumesFrom: VolumesFrom,
|
||||||
Binds: Binds,
|
Binds: Binds,
|
||||||
Env: c.Services.KubeController.ExtraEnv,
|
Env: getUniqStringList(c.Services.KubeController.ExtraEnv),
|
||||||
NetworkMode: "host",
|
NetworkMode: "host",
|
||||||
RestartPolicy: "always",
|
RestartPolicy: "always",
|
||||||
Image: c.Services.KubeController.Image,
|
Image: c.Services.KubeController.Image,
|
||||||
@@ -323,7 +334,11 @@ func (c *Cluster) BuildKubeletProcess(host *hosts.Host, prefixPath string) v3.Pr
|
|||||||
if len(c.CloudProvider.Name) > 0 && c.CloudProvider.Name != aws.AWSCloudProviderName {
|
if len(c.CloudProvider.Name) > 0 && c.CloudProvider.Name != aws.AWSCloudProviderName {
|
||||||
CommandArgs["cloud-config"] = CloudConfigPath
|
CommandArgs["cloud-config"] = CloudConfigPath
|
||||||
}
|
}
|
||||||
|
if len(c.CloudProvider.Name) > 0 {
|
||||||
|
c.Services.Kubelet.ExtraEnv = append(
|
||||||
|
c.Services.Kubelet.ExtraEnv,
|
||||||
|
fmt.Sprintf("%s=%s", CloudConfigSumEnv, getCloudConfigChecksum(c.CloudProvider)))
|
||||||
|
}
|
||||||
// check if our version has specific options for this component
|
// check if our version has specific options for this component
|
||||||
serviceOptions := c.GetKubernetesServicesOptions()
|
serviceOptions := c.GetKubernetesServicesOptions()
|
||||||
if serviceOptions.Kubelet != nil {
|
if serviceOptions.Kubelet != nil {
|
||||||
@@ -379,7 +394,7 @@ func (c *Cluster) BuildKubeletProcess(host *hosts.Host, prefixPath string) v3.Pr
|
|||||||
Command: Command,
|
Command: Command,
|
||||||
VolumesFrom: VolumesFrom,
|
VolumesFrom: VolumesFrom,
|
||||||
Binds: Binds,
|
Binds: Binds,
|
||||||
Env: c.Services.Kubelet.ExtraEnv,
|
Env: getUniqStringList(c.Services.Kubelet.ExtraEnv),
|
||||||
NetworkMode: "host",
|
NetworkMode: "host",
|
||||||
RestartPolicy: "always",
|
RestartPolicy: "always",
|
||||||
Image: c.Services.Kubelet.Image,
|
Image: c.Services.Kubelet.Image,
|
||||||
@@ -705,3 +720,20 @@ func getTagMajorVersion(tag string) string {
|
|||||||
}
|
}
|
||||||
return strings.Join(splitTag[:2], ".")
|
return strings.Join(splitTag[:2], ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCloudConfigChecksum(config v3.CloudProvider) string {
|
||||||
|
configByteSum := md5.Sum([]byte(fmt.Sprintf("%s", config)))
|
||||||
|
return fmt.Sprintf("%x", configByteSum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUniqStringList(l []string) []string {
|
||||||
|
m := map[string]bool{}
|
||||||
|
ul := []string{}
|
||||||
|
for _, k := range l {
|
||||||
|
if _, ok := m[k]; !ok {
|
||||||
|
m[k] = true
|
||||||
|
ul = append(ul, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ul
|
||||||
|
}
|
||||||
|
@@ -299,7 +299,8 @@ func IsContainerUpgradable(ctx context.Context, dClient *client.Client, imageCfg
|
|||||||
}
|
}
|
||||||
if containerInspect.Config.Image != imageCfg.Image ||
|
if containerInspect.Config.Image != imageCfg.Image ||
|
||||||
!sliceEqualsIgnoreOrder(containerInspect.Config.Entrypoint, imageCfg.Entrypoint) ||
|
!sliceEqualsIgnoreOrder(containerInspect.Config.Entrypoint, imageCfg.Entrypoint) ||
|
||||||
!sliceEqualsIgnoreOrder(containerInspect.Config.Cmd, imageCfg.Cmd) {
|
!sliceEqualsIgnoreOrder(containerInspect.Config.Cmd, imageCfg.Cmd) ||
|
||||||
|
!isContainerRKEEnvChanged(containerInspect.Config.Env, imageCfg.Env) {
|
||||||
logrus.Debugf("[%s] Container [%s] is eligible for upgrade on host [%s]", plane, containerName, hostname)
|
logrus.Debugf("[%s] Container [%s] is eligible for upgrade on host [%s]", plane, containerName, hostname)
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@@ -390,3 +391,21 @@ func convertToSemver(version string) (*semver.Version, error) {
|
|||||||
compVersion[2] = "0"
|
compVersion[2] = "0"
|
||||||
return semver.NewVersion(strings.Join(compVersion, "."))
|
return semver.NewVersion(strings.Join(compVersion, "."))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isContainerRKEEnvChanged(containerEnv, imageConfigEnv []string) bool {
|
||||||
|
// remove PATH env from the container env
|
||||||
|
cleanedContainerEnv := getRKEEnvVars(containerEnv)
|
||||||
|
cleanedImageConfigEnv := getRKEEnvVars(imageConfigEnv)
|
||||||
|
|
||||||
|
return sliceEqualsIgnoreOrder(cleanedContainerEnv, cleanedImageConfigEnv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRKEEnvVars(env []string) []string {
|
||||||
|
tmp := []string{}
|
||||||
|
for _, e := range env {
|
||||||
|
if strings.HasPrefix("RKE_", e) {
|
||||||
|
tmp = append(tmp, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tmp
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user