mirror of
https://github.com/stakater/Reloader.git
synced 2026-08-27 14:37:17 +00:00
Implement PR-2 review comments
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stakater/Reloader/internal/pkg/common"
|
||||
"github.com/stakater/Reloader/internal/pkg/crypto"
|
||||
"github.com/stakater/Reloader/pkg/kube"
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
// ResourceUpdatedHandler contains updated objects
|
||||
type ResourceUpdatedHandler struct {
|
||||
Resource interface{}
|
||||
OldResource interface{}
|
||||
}
|
||||
|
||||
// Handle processes the updated resource
|
||||
func (r ResourceUpdatedHandler) Handle() error {
|
||||
if r.Resource == nil || r.OldResource == nil {
|
||||
logrus.Errorf("Error in Handler")
|
||||
} else {
|
||||
logrus.Infof("Detected changes in object %s", r.Resource)
|
||||
// process resource based on its type
|
||||
rollingUpgrade(r, "deployments")
|
||||
rollingUpgrade(r, "daemonsets")
|
||||
rollingUpgrade(r, "statefulSets")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func rollingUpgrade(r ResourceUpdatedHandler, rollingUpgradeType string) {
|
||||
client, err := kube.GetClient()
|
||||
if err != nil {
|
||||
logrus.Fatalf("Unable to create Kubernetes client error = %v", err)
|
||||
}
|
||||
var namespace, resourceName, shaData, oldSHAdata, envarPostfix, annotation string
|
||||
if _, ok := r.Resource.(*v1.ConfigMap); ok {
|
||||
logrus.Infof("Performing 'Updated' action for resource of type 'configmap'")
|
||||
namespace = r.Resource.(*v1.ConfigMap).Namespace
|
||||
resourceName = r.Resource.(*v1.ConfigMap).Name
|
||||
envarPostfix = common.ConfigmapEnvarPostfix
|
||||
annotation = common.ConfigmapUpdateOnChangeAnnotation
|
||||
shaData = getSHAfromConfigmapData(r.Resource.(*v1.ConfigMap).Data)
|
||||
oldSHAdata = getSHAfromConfigmapData(r.OldResource.(*v1.ConfigMap).Data)
|
||||
} else if _, ok := r.Resource.(*v1.Secret); ok {
|
||||
logrus.Infof("Performing 'Updated' action for resource of type 'secret'")
|
||||
namespace = r.Resource.(*v1.Secret).Namespace
|
||||
resourceName = r.Resource.(*v1.Secret).Name
|
||||
envarPostfix = common.SecretEnvarPostfix
|
||||
annotation = common.SecretUpdateOnChangeAnnotation
|
||||
shaData = getSHAfromSecretData(r.Resource.(*v1.Secret).Data)
|
||||
oldSHAdata = getSHAfromSecretData(r.OldResource.(*v1.Secret).Data)
|
||||
} else {
|
||||
logrus.Warnf("Invalid resource: Resource should be 'Secret' or 'Configmap' but found, %v", r.Resource)
|
||||
}
|
||||
|
||||
if shaData != oldSHAdata {
|
||||
if rollingUpgradeType == "deployments" {
|
||||
RollingUpgradeDeployment(client, namespace, resourceName, shaData, envarPostfix, annotation)
|
||||
} else if rollingUpgradeType == "daemonsets" {
|
||||
RollingUpgradeDaemonSets(client, namespace, resourceName, shaData, envarPostfix, annotation)
|
||||
} else if rollingUpgradeType == "statefulSets" {
|
||||
RollingUpgradeStatefulSets(client, namespace, resourceName, shaData, envarPostfix, annotation)
|
||||
}
|
||||
} else {
|
||||
logrus.Infof("Resource update will not happen because no data change detected")
|
||||
}
|
||||
}
|
||||
|
||||
// RollingUpgradeDeployment upgrades the deployment if there is any change in configmap or secret data
|
||||
func RollingUpgradeDeployment(client kubernetes.Interface, namespace string, resourceName string, shaData string, envarPostfix string, annotation string) error {
|
||||
deployments, err := client.ExtensionsV1beta1().Deployments(namespace).List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to list deployments %v", err)
|
||||
}
|
||||
|
||||
for _, d := range deployments.Items {
|
||||
containers := d.Spec.Template.Spec.Containers
|
||||
// match deployments with the correct annotation
|
||||
annotationValue := d.ObjectMeta.Annotations[annotation]
|
||||
updated := performRollingUpgrade(containers, resourceName, annotationValue, shaData, envarPostfix)
|
||||
if !updated {
|
||||
logrus.Warnf("Rolling upgrade did not happen")
|
||||
} else {
|
||||
// update the deployment
|
||||
_, err := client.ExtensionsV1beta1().Deployments(namespace).Update(&d)
|
||||
if err != nil {
|
||||
logrus.Errorf("Update deployment failed %v", err)
|
||||
} else {
|
||||
logrus.Infof("Updated Deployment %s", d.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RollingUpgradeDaemonSets upgrades the daemonset if there is any change in configmap or secret data
|
||||
func RollingUpgradeDaemonSets(client kubernetes.Interface, namespace string, resourceName string, shaData string, envarPostfix string, annotation string) error {
|
||||
daemonSets, err := client.ExtensionsV1beta1().DaemonSets(namespace).List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to list daemonSets %v", err)
|
||||
}
|
||||
|
||||
for _, d := range daemonSets.Items {
|
||||
containers := d.Spec.Template.Spec.Containers
|
||||
// match daemonSets with the correct annotation
|
||||
annotationValue := d.ObjectMeta.Annotations[annotation]
|
||||
updated := performRollingUpgrade(containers, resourceName, annotationValue, shaData, envarPostfix)
|
||||
if !updated {
|
||||
logrus.Warnf("Rolling upgrade did not happen")
|
||||
} else {
|
||||
// update the daemonSet
|
||||
_, err := client.ExtensionsV1beta1().DaemonSets(namespace).Update(&d)
|
||||
if err != nil {
|
||||
logrus.Errorf("Update daemonSet failed %v", err)
|
||||
} else {
|
||||
logrus.Infof("Updated daemonSet %s", d.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// RollingUpgradeStatefulSets upgrades the statefulset if there is any change in configmap or secret data
|
||||
func RollingUpgradeStatefulSets(client kubernetes.Interface, namespace string, resourceName string, shaData string, envarPostfix string, annotation string) error {
|
||||
statefulSets, err := client.AppsV1beta1().StatefulSets(namespace).List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to list statefulSets %v", err)
|
||||
}
|
||||
|
||||
for _, s := range statefulSets.Items {
|
||||
containers := s.Spec.Template.Spec.Containers
|
||||
// match statefulSets with the correct annotation
|
||||
annotationValue := s.ObjectMeta.Annotations[annotation]
|
||||
updated := performRollingUpgrade(containers, resourceName, annotationValue, shaData, envarPostfix)
|
||||
if !updated {
|
||||
logrus.Warnf("Rolling upgrade did not happen")
|
||||
} else {
|
||||
// update the statefulSet
|
||||
_, err := client.AppsV1beta1().StatefulSets(namespace).Update(&s)
|
||||
if err != nil {
|
||||
logrus.Errorf("Update statefulSet failed %v", err)
|
||||
} else {
|
||||
logrus.Infof("Updated statefulSet %s", s.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func performRollingUpgrade(containers []v1.Container, resourceName string, annotationValue string, shaData string, envarPostfix string) bool {
|
||||
updated := false
|
||||
if annotationValue != "" {
|
||||
values := strings.Split(annotationValue, ",")
|
||||
for _, value := range values {
|
||||
if value == resourceName {
|
||||
updated = updateContainers(containers, value, shaData, envarPostfix)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
func updateContainers(containers []v1.Container, annotationValue string, shaData string, envarPostfix string) bool {
|
||||
updated := false
|
||||
envar := common.EnvVarPrefix + common.ConvertToEnvVarName(annotationValue) + envarPostfix
|
||||
logrus.Infof("Generated environment variable: %s", envar)
|
||||
for i := range containers {
|
||||
envs := containers[i].Env
|
||||
|
||||
//update if env var exists
|
||||
updated = updateEnvVar(envs, envar, shaData)
|
||||
|
||||
// if no existing env var exists lets create one
|
||||
if !updated {
|
||||
e := v1.EnvVar{
|
||||
Name: envar,
|
||||
Value: shaData,
|
||||
}
|
||||
containers[i].Env = append(containers[i].Env, e)
|
||||
updated = true
|
||||
logrus.Infof("%s environment variable does not exist, creating a new env with value %s", envar, shaData)
|
||||
}
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
func updateEnvVar(envs []v1.EnvVar, envar string, shaData string) bool {
|
||||
for j := range envs {
|
||||
if envs[j].Name == envar {
|
||||
logrus.Infof("%s environment variable found", envar)
|
||||
if envs[j].Value != shaData {
|
||||
logrus.Infof("Updating %s to %s", envar, shaData)
|
||||
envs[j].Value = shaData
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getSHAfromConfigmapData(data map[string]string) string {
|
||||
logrus.Infof("Generating SHA for configmap data")
|
||||
values := []string{}
|
||||
for k, v := range data {
|
||||
values = append(values, k+"="+v)
|
||||
}
|
||||
sort.Strings(values)
|
||||
return crypto.GenerateSHA(strings.Join(values, ";"))
|
||||
}
|
||||
|
||||
func getSHAfromSecretData(data map[string][]byte) string {
|
||||
logrus.Infof("Generating SHA for secret data")
|
||||
values := []string{}
|
||||
for k, v := range data {
|
||||
values = append(values, k+"="+string(v[:]))
|
||||
}
|
||||
sort.Strings(values)
|
||||
return crypto.GenerateSHA(strings.Join(values, ";"))
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stakater/Reloader/internal/pkg/common"
|
||||
"github.com/stakater/Reloader/internal/pkg/testutil"
|
||||
"github.com/stakater/Reloader/pkg/kube"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
var (
|
||||
client = getClient()
|
||||
namespace = "test-handler"
|
||||
configmapName = "testconfigmap-handler-update-" + common.RandSeq(5)
|
||||
secretName = "testsecret-handler-update-" + common.RandSeq(5)
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
logrus.Infof("Creating namespace %s", namespace)
|
||||
testutil.CreateNamespace(namespace, client)
|
||||
|
||||
logrus.Infof("Setting up the test resources")
|
||||
setup()
|
||||
|
||||
logrus.Infof("Running Testcases")
|
||||
retCode := m.Run()
|
||||
|
||||
logrus.Infof("tearing down the test resources")
|
||||
teardown()
|
||||
|
||||
os.Exit(retCode)
|
||||
}
|
||||
|
||||
func getClient() *kubernetes.Clientset {
|
||||
newClient, err := kube.GetClient()
|
||||
if err != nil {
|
||||
logrus.Fatalf("Unable to create Kubernetes client error = %v", err)
|
||||
}
|
||||
return newClient
|
||||
}
|
||||
|
||||
func setup() {
|
||||
// Creating configmap
|
||||
_, err := testutil.CreateConfigMap(client, namespace, configmapName, "www.google.com")
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in configmap creation: %v", err)
|
||||
}
|
||||
|
||||
// Creating secret
|
||||
data := "dGVzdFNlY3JldEVuY29kaW5nRm9yUmVsb2FkZXI="
|
||||
_, err = testutil.CreateSecret(client, namespace, secretName, data)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in secret creation: %v", err)
|
||||
}
|
||||
|
||||
// Creating Deployment with configmap
|
||||
testutil.CreateDeployment(client, configmapName, namespace)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in Deployment with configmap creation: %v", err)
|
||||
}
|
||||
|
||||
// Creating Deployment with secret
|
||||
testutil.CreateDeployment(client, secretName, namespace)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in Deployment with secret creation: %v", err)
|
||||
}
|
||||
|
||||
// Creating Daemonset with configmap
|
||||
testutil.CreateDaemonset(client, configmapName, namespace)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in Daemonset with configmap creation: %v", err)
|
||||
}
|
||||
|
||||
// Creating Daemonset with secret
|
||||
testutil.CreateDaemonset(client, secretName, namespace)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in Daemonset with secret creation: %v", err)
|
||||
}
|
||||
|
||||
// Creating Statefulset with configmap
|
||||
testutil.CreateStatefulset(client, configmapName, namespace)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in Statefulset with configmap creation: %v", err)
|
||||
}
|
||||
|
||||
// Creating Statefulset with secret
|
||||
testutil.CreateStatefulset(client, secretName, namespace)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in Statefulset with secret creation: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func teardown() {
|
||||
// Deleting Deployment with configmap
|
||||
deploymentError := testutil.DeleteDeployment(client, namespace, configmapName)
|
||||
if deploymentError != nil {
|
||||
logrus.Errorf("Error while deleting deployment with configmap %v", deploymentError)
|
||||
}
|
||||
|
||||
// Deleting Deployment with secret
|
||||
deploymentError = testutil.DeleteDeployment(client, namespace, secretName)
|
||||
if deploymentError != nil {
|
||||
logrus.Errorf("Error while deleting deployment with secret %v", deploymentError)
|
||||
}
|
||||
|
||||
// Deleting Daemonset with configmap
|
||||
daemonsetError := testutil.DeleteDaemonset(client, namespace, configmapName)
|
||||
if daemonsetError != nil {
|
||||
logrus.Errorf("Error while deleting daemonset with configmap %v", daemonsetError)
|
||||
}
|
||||
|
||||
// Deleting Deployment with secret
|
||||
daemonsetError = testutil.DeleteDaemonset(client, namespace, secretName)
|
||||
if daemonsetError != nil {
|
||||
logrus.Errorf("Error while deleting daemonset with secret %v", daemonsetError)
|
||||
}
|
||||
|
||||
// Deleting Statefulset with configmap
|
||||
statefulsetError := testutil.DeleteStatefulset(client, namespace, configmapName)
|
||||
if statefulsetError != nil {
|
||||
logrus.Errorf("Error while deleting statefulset with configmap %v", statefulsetError)
|
||||
}
|
||||
|
||||
// Deleting Deployment with secret
|
||||
statefulsetError = testutil.DeleteStatefulset(client, namespace, secretName)
|
||||
if statefulsetError != nil {
|
||||
logrus.Errorf("Error while deleting statefulset with secret %v", statefulsetError)
|
||||
}
|
||||
|
||||
// Deleting Configmap
|
||||
err := testutil.DeleteConfigMap(client, namespace, configmapName)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error while deleting the configmap %v", err)
|
||||
}
|
||||
|
||||
// Deleting Secret
|
||||
err = testutil.DeleteSecret(client, namespace, secretName)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error while deleting the secret %v", err)
|
||||
}
|
||||
|
||||
// Deleting namespace
|
||||
testutil.DeleteNamespace(namespace, client)
|
||||
|
||||
}
|
||||
|
||||
func TestRollingUpgradeForDeploymentWithConfigmap(t *testing.T) {
|
||||
shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, configmapName, "www.stakater.com")
|
||||
RollingUpgradeDeployment(client, namespace, configmapName, shaData, common.ConfigmapEnvarPostfix, common.ConfigmapUpdateOnChangeAnnotation)
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
logrus.Infof("Verifying deployment update")
|
||||
updated := testutil.VerifyDeploymentUpdate(client, namespace, configmapName, common.ConfigmapEnvarPostfix, shaData, common.ConfigmapUpdateOnChangeAnnotation)
|
||||
if !updated {
|
||||
t.Errorf("Deployment was not updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRollingUpgradeForDeploymentWithSecret(t *testing.T) {
|
||||
shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, "dGVzdFVwZGF0ZWRTZWNyZXRFbmNvZGluZ0ZvclJlbG9hZGVy")
|
||||
RollingUpgradeDeployment(client, namespace, secretName, shaData, common.SecretEnvarPostfix, common.SecretUpdateOnChangeAnnotation)
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
logrus.Infof("Verifying deployment update")
|
||||
updated := testutil.VerifyDeploymentUpdate(client, namespace, secretName, common.SecretEnvarPostfix, shaData, common.SecretUpdateOnChangeAnnotation)
|
||||
if !updated {
|
||||
t.Errorf("Deployment was not updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRollingUpgradeForDaemonsetWithConfigmap(t *testing.T) {
|
||||
shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.facebook.com")
|
||||
RollingUpgradeDaemonSets(client, namespace, configmapName, shaData, common.ConfigmapEnvarPostfix, common.ConfigmapUpdateOnChangeAnnotation)
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
logrus.Infof("Verifying daemonset update")
|
||||
updated := testutil.VerifyDaemonsetUpdate(client, namespace, configmapName, common.ConfigmapEnvarPostfix, shaData, common.ConfigmapUpdateOnChangeAnnotation)
|
||||
if !updated {
|
||||
t.Errorf("Daemonset was not updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRollingUpgradeForDaemonsetWithSecret(t *testing.T) {
|
||||
shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, "d3d3LmZhY2Vib29rLmNvbQ==")
|
||||
RollingUpgradeDaemonSets(client, namespace, secretName, shaData, common.SecretEnvarPostfix, common.SecretUpdateOnChangeAnnotation)
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
logrus.Infof("Verifying daemonset update")
|
||||
updated := testutil.VerifyDaemonsetUpdate(client, namespace, secretName, common.SecretEnvarPostfix, shaData, common.SecretUpdateOnChangeAnnotation)
|
||||
if !updated {
|
||||
t.Errorf("Daemonset was not updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRollingUpgradeForStatefulsetWithConfigmap(t *testing.T) {
|
||||
shaData := testutil.ConvertResourceToSHA(testutil.ConfigmapResourceType, namespace, configmapName, "www.twitter.com")
|
||||
RollingUpgradeStatefulSets(client, namespace, configmapName, shaData, common.ConfigmapEnvarPostfix, common.ConfigmapUpdateOnChangeAnnotation)
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
logrus.Infof("Verifying statefulset update")
|
||||
updated := testutil.VerifyStatefulsetUpdate(client, namespace, configmapName, common.ConfigmapEnvarPostfix, shaData, common.ConfigmapUpdateOnChangeAnnotation)
|
||||
if !updated {
|
||||
t.Errorf("Statefulset was not updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRollingUpgradeForStatefulsetWithSecret(t *testing.T) {
|
||||
shaData := testutil.ConvertResourceToSHA(testutil.SecretResourceType, namespace, secretName, "d3d3LnR3aXR0ZXIuY29t")
|
||||
RollingUpgradeStatefulSets(client, namespace, secretName, shaData, common.SecretEnvarPostfix, common.SecretUpdateOnChangeAnnotation)
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
logrus.Infof("Verifying statefulset update")
|
||||
updated := testutil.VerifyStatefulsetUpdate(client, namespace, secretName, common.SecretEnvarPostfix, shaData, common.SecretUpdateOnChangeAnnotation)
|
||||
if !updated {
|
||||
t.Errorf("Statefulset was not updated")
|
||||
}
|
||||
}
|
||||
@@ -1,241 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stakater/Reloader/internal/pkg/helper"
|
||||
"github.com/stakater/Reloader/pkg/kube"
|
||||
"k8s.io/api/core/v1"
|
||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
const (
|
||||
configmapUpdateOnChangeAnnotation = "reloader.stakater.com/configmap.update-on-change"
|
||||
// Adding separate annotation to differentiate between configmap and secret
|
||||
secretUpdateOnChangeAnnotation = "reloader.stakater.com/secret.update-on-change"
|
||||
)
|
||||
|
||||
// ResourceUpdatedHandler contains updated objects
|
||||
type ResourceUpdatedHandler struct {
|
||||
Resource interface{}
|
||||
OldResource interface{}
|
||||
}
|
||||
|
||||
// Handle processes the updated resource
|
||||
func (r ResourceUpdatedHandler) Handle() error {
|
||||
if r.Resource == nil || r.OldResource == nil {
|
||||
logrus.Errorf("Error in Handler")
|
||||
} else {
|
||||
logrus.Infof("Detected changes in object %s", r.Resource)
|
||||
// process resource based on its type
|
||||
rollingUpgrade(r, "deployments")
|
||||
rollingUpgrade(r, "daemonsets")
|
||||
rollingUpgrade(r, "statefulSets")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func rollingUpgrade(r ResourceUpdatedHandler, rollingUpgradeType string) {
|
||||
client, err := kube.GetClient()
|
||||
if err != nil {
|
||||
logrus.Fatalf("Unable to create Kubernetes client error = %v", err)
|
||||
}
|
||||
var namespace, name, shaData, envNamePostfix string
|
||||
if _, ok := r.Resource.(*v1.ConfigMap); ok {
|
||||
logrus.Infof("Performing 'Updated' action for resource of type 'configmap'")
|
||||
namespace = r.Resource.(*v1.ConfigMap).Namespace
|
||||
name = r.Resource.(*v1.ConfigMap).Name
|
||||
shaData = helper.ConvertConfigmapToSHA(r.Resource.(*v1.ConfigMap))
|
||||
envNamePostfix = "_CONFIGMAP"
|
||||
} else if _, ok := r.Resource.(*v1.Secret); ok {
|
||||
logrus.Infof("Performing 'Updated' action for resource of type 'secret'")
|
||||
namespace = r.Resource.(*v1.Secret).Namespace
|
||||
name = r.Resource.(*v1.Secret).Name
|
||||
shaData = helper.ConvertSecretToSHA(r.Resource.(*v1.Secret))
|
||||
envNamePostfix = "_SECRET"
|
||||
} else {
|
||||
logrus.Warnf("Invalid resource: Resource should be 'Secret' or 'Configmap' but found, %v", r.Resource)
|
||||
}
|
||||
|
||||
if rollingUpgradeType == "deployments" {
|
||||
RollingUpgradeForDeployment(client, namespace, name, shaData, envNamePostfix)
|
||||
} else if rollingUpgradeType == "daemonsets" {
|
||||
RollingUpgradeForDaemonSets(client, namespace, name, shaData, envNamePostfix)
|
||||
} else if rollingUpgradeType == "statefulSets" {
|
||||
RollingUpgradeForStatefulSets(client, namespace, name, shaData, envNamePostfix)
|
||||
}
|
||||
}
|
||||
|
||||
// RollingUpgradeForDeployment upgrades the deployment if there is any change in configmap or secret data
|
||||
func RollingUpgradeForDeployment(client kubernetes.Interface, namespace string, name string, shaData string, envName string) error {
|
||||
deployments, err := client.ExtensionsV1beta1().Deployments(namespace).List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to list deployments %v", err)
|
||||
}
|
||||
var updateOnChangeAnnotation string
|
||||
if envName == "_CONFIGMAP" {
|
||||
updateOnChangeAnnotation = configmapUpdateOnChangeAnnotation
|
||||
} else if envName == "_SECRET" {
|
||||
updateOnChangeAnnotation = secretUpdateOnChangeAnnotation
|
||||
}
|
||||
for _, d := range deployments.Items {
|
||||
containers := d.Spec.Template.Spec.Containers
|
||||
// match deployments with the correct annotation
|
||||
annotationValue := d.ObjectMeta.Annotations[updateOnChangeAnnotation]
|
||||
|
||||
if annotationValue != "" {
|
||||
values := strings.Split(annotationValue, ",")
|
||||
matches := false
|
||||
for _, value := range values {
|
||||
if value == name {
|
||||
matches = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if matches {
|
||||
updated := updateContainers(containers, name, shaData, envName)
|
||||
|
||||
if !updated {
|
||||
logrus.Warnf("Rolling upgrade did not happen")
|
||||
} else {
|
||||
// update the deployment
|
||||
_, err := client.ExtensionsV1beta1().Deployments(namespace).Update(&d)
|
||||
if err != nil {
|
||||
logrus.Errorf("Update deployment failed %v", err)
|
||||
} else {
|
||||
logrus.Infof("Updated Deployment %s", d.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RollingUpgradeForDaemonSets upgrades the daemonset if there is any change in configmap or secret data
|
||||
func RollingUpgradeForDaemonSets(client kubernetes.Interface, namespace string, name string, shaData string, envName string) error {
|
||||
daemonSets, err := client.ExtensionsV1beta1().DaemonSets(namespace).List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to list daemonSets %v", err)
|
||||
}
|
||||
|
||||
var updateOnChangeAnnotation string
|
||||
if envName == "_CONFIGMAP" {
|
||||
updateOnChangeAnnotation = configmapUpdateOnChangeAnnotation
|
||||
} else if envName == "_SECRET" {
|
||||
updateOnChangeAnnotation = secretUpdateOnChangeAnnotation
|
||||
}
|
||||
for _, d := range daemonSets.Items {
|
||||
containers := d.Spec.Template.Spec.Containers
|
||||
// match daemonSets with the correct annotation
|
||||
annotationValue := d.ObjectMeta.Annotations[updateOnChangeAnnotation]
|
||||
|
||||
if annotationValue != "" {
|
||||
values := strings.Split(annotationValue, ",")
|
||||
matches := false
|
||||
for _, value := range values {
|
||||
if value == name {
|
||||
matches = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if matches {
|
||||
updated := updateContainers(containers, name, shaData, envName)
|
||||
|
||||
if !updated {
|
||||
logrus.Warnf("Rolling upgrade did not happen")
|
||||
} else {
|
||||
// update the daemonSet
|
||||
_, err := client.ExtensionsV1beta1().DaemonSets(namespace).Update(&d)
|
||||
if err != nil {
|
||||
logrus.Errorf("Update daemonSet failed %v", err)
|
||||
} else {
|
||||
logrus.Infof("Updated daemonSet %s", d.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RollingUpgradeForStatefulSets upgrades the statefulset if there is any change in configmap or secret data
|
||||
func RollingUpgradeForStatefulSets(client kubernetes.Interface, namespace string, name string, shaData string, envName string) error {
|
||||
statefulSets, err := client.AppsV1beta1().StatefulSets(namespace).List(meta_v1.ListOptions{})
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to list statefulSets %v", err)
|
||||
}
|
||||
var updateOnChangeAnnotation string
|
||||
if envName == "_CONFIGMAP" {
|
||||
updateOnChangeAnnotation = configmapUpdateOnChangeAnnotation
|
||||
} else if envName == "_SECRET" {
|
||||
updateOnChangeAnnotation = secretUpdateOnChangeAnnotation
|
||||
}
|
||||
for _, d := range statefulSets.Items {
|
||||
containers := d.Spec.Template.Spec.Containers
|
||||
// match statefulSets with the correct annotation
|
||||
annotationValue := d.ObjectMeta.Annotations[updateOnChangeAnnotation]
|
||||
|
||||
if annotationValue != "" {
|
||||
values := strings.Split(annotationValue, ",")
|
||||
matches := false
|
||||
for _, value := range values {
|
||||
if value == name {
|
||||
matches = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if matches {
|
||||
updated := updateContainers(containers, name, shaData, envName)
|
||||
|
||||
if !updated {
|
||||
logrus.Warnf("Rolling upgrade did not happen")
|
||||
} else {
|
||||
// update the statefulSet
|
||||
_, err := client.AppsV1beta1().StatefulSets(namespace).Update(&d)
|
||||
if err != nil {
|
||||
logrus.Errorf("Update statefulSet failed %v", err)
|
||||
} else {
|
||||
logrus.Infof("Updated statefulSet %s", d.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func updateContainers(containers []v1.Container, annotationValue string, shaData string, resourceType string) bool {
|
||||
updated := false
|
||||
envar := "STAKATER_" + helper.ConvertToEnvVarName(annotationValue) + resourceType
|
||||
logrus.Infof("Generated environment variable: %s", envar)
|
||||
|
||||
for i := range containers {
|
||||
envs := containers[i].Env
|
||||
matched := false
|
||||
for j := range envs {
|
||||
if envs[j].Name == envar {
|
||||
matched = true
|
||||
logrus.Infof("%s environment variable found", envar)
|
||||
if envs[j].Value != shaData {
|
||||
logrus.Infof("Updating %s to %s", envar, shaData)
|
||||
envs[j].Value = shaData
|
||||
updated = true
|
||||
}
|
||||
}
|
||||
}
|
||||
// if no existing env var exists lets create one
|
||||
if !matched {
|
||||
e := v1.EnvVar{
|
||||
Name: envar,
|
||||
Value: shaData,
|
||||
}
|
||||
containers[i].Env = append(containers[i].Env, e)
|
||||
updated = true
|
||||
logrus.Infof("%s environment variable does not found, creating a new env with value %s", envar, shaData)
|
||||
}
|
||||
}
|
||||
return updated
|
||||
}
|
||||
Reference in New Issue
Block a user