Skip to content

Commit

Permalink
restart related k8s services when cloud provider config is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
moelsayed authored and Alena Prokharchyk committed Jun 13, 2018
1 parent f8bb7c1 commit 4c30f1a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
42 changes: 37 additions & 5 deletions cluster/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cluster

import (
"context"
"crypto/md5"
"fmt"
"path"
"strconv"
Expand Down Expand Up @@ -29,6 +30,7 @@ const (
CoreOS = "CoreOS"
CoreOSPrefixPath = "/opt/rke"
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) {
Expand Down Expand Up @@ -132,6 +134,11 @@ func (c *Cluster) BuildKubeAPIProcess(prefixPath string) v3.Process {
if len(c.CloudProvider.Name) > 0 && c.CloudProvider.Name != aws.AWSCloudProviderName {
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
serviceOptions := c.GetKubernetesServicesOptions()
if serviceOptions.KubeAPI != nil {
Expand Down Expand Up @@ -192,7 +199,7 @@ func (c *Cluster) BuildKubeAPIProcess(prefixPath string) v3.Process {
Args: args,
VolumesFrom: VolumesFrom,
Binds: Binds,
Env: c.Services.KubeAPI.ExtraEnv,
Env: getUniqStringList(c.Services.KubeAPI.ExtraEnv),
NetworkMode: "host",
RestartPolicy: "always",
Image: c.Services.KubeAPI.Image,
Expand Down Expand Up @@ -230,7 +237,11 @@ func (c *Cluster) BuildKubeControllerProcess(prefixPath string) v3.Process {
if len(c.CloudProvider.Name) > 0 && c.CloudProvider.Name != aws.AWSCloudProviderName {
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
serviceOptions := c.GetKubernetesServicesOptions()
if serviceOptions.KubeController != nil {
Expand Down Expand Up @@ -274,7 +285,7 @@ func (c *Cluster) BuildKubeControllerProcess(prefixPath string) v3.Process {
Args: args,
VolumesFrom: VolumesFrom,
Binds: Binds,
Env: c.Services.KubeController.ExtraEnv,
Env: getUniqStringList(c.Services.KubeController.ExtraEnv),
NetworkMode: "host",
RestartPolicy: "always",
Image: c.Services.KubeController.Image,
Expand Down Expand Up @@ -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 {
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
serviceOptions := c.GetKubernetesServicesOptions()
if serviceOptions.Kubelet != nil {
Expand Down Expand Up @@ -379,7 +394,7 @@ func (c *Cluster) BuildKubeletProcess(host *hosts.Host, prefixPath string) v3.Pr
Command: Command,
VolumesFrom: VolumesFrom,
Binds: Binds,
Env: c.Services.Kubelet.ExtraEnv,
Env: getUniqStringList(c.Services.Kubelet.ExtraEnv),
NetworkMode: "host",
RestartPolicy: "always",
Image: c.Services.Kubelet.Image,
Expand Down Expand Up @@ -705,3 +720,20 @@ func getTagMajorVersion(tag string) string {
}
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
}
21 changes: 20 additions & 1 deletion docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ func IsContainerUpgradable(ctx context.Context, dClient *client.Client, imageCfg
}
if containerInspect.Config.Image != imageCfg.Image ||
!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)
return true, nil
}
Expand Down Expand Up @@ -390,3 +391,21 @@ func convertToSemver(version string) (*semver.Version, error) {
compVersion[2] = "0"
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
}

0 comments on commit 4c30f1a

Please sign in to comment.