mirror of
https://github.com/stakater/Reloader.git
synced 2026-02-14 18:09:50 +00:00
Implement PR-1 review comments
This commit is contained in:
@@ -48,22 +48,22 @@ func NewController(
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
// Add function to add a 'create' event to the queue in case of creating a resource
|
||||
// Add function to add a new object to the queue in case of creating a resource
|
||||
func (c *Controller) Add(obj interface{}) {
|
||||
c.queue.Add(handler.ResourceCreatedHandler{
|
||||
NewResource: obj,
|
||||
Resource: obj,
|
||||
})
|
||||
}
|
||||
|
||||
// Update function to add an 'update' event to the queue in case of updating a resource
|
||||
// Update function to add an old object and a new object to the queue in case of updating a resource
|
||||
func (c *Controller) Update(old interface{}, new interface{}) {
|
||||
c.queue.Add(handler.ResourceUpdatedHandler{
|
||||
NewResource: new,
|
||||
Resource: new,
|
||||
OldResource: old,
|
||||
})
|
||||
}
|
||||
|
||||
// Delete function to add an 'update' event to the queue in case of deleting a resource
|
||||
// Delete function to add an object to the queue in case of deleting a resource
|
||||
func (c *Controller) Delete(old interface{}) {
|
||||
// TODO Added this function for future usecase
|
||||
logrus.Infof("Deleted resource has been added to queue")
|
||||
|
||||
@@ -10,10 +10,10 @@ import (
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/api/extensions/v1beta1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
var (
|
||||
client, _ = kube.GetClient()
|
||||
configmapNamePrefix = "testconfigmap-reloader"
|
||||
secretNamePrefix = "testsecret-reloader"
|
||||
letters = []rune("abcdefghijklmnopqrstuvwxyz")
|
||||
@@ -30,9 +30,14 @@ func randSeq(n int) string {
|
||||
|
||||
// Creating a Controller to do a rolling upgrade upon updating the configmap or secret
|
||||
func TestControllerForUpdatingConfigmapShouldUpdateDeployment(t *testing.T) {
|
||||
client, err := kube.GetClient()
|
||||
if err != nil {
|
||||
logrus.Infof("Unable to create Kubernetes client error = %v", err)
|
||||
return
|
||||
}
|
||||
namespace := "test-reloader"
|
||||
createNamespace(t, namespace)
|
||||
defer deleteNamespace(t, namespace)
|
||||
createNamespace(t, namespace, client)
|
||||
defer deleteNamespace(t, namespace, client)
|
||||
|
||||
controller, err := NewController(client, "configMaps", namespace)
|
||||
if err != nil {
|
||||
@@ -52,7 +57,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateDeployment(t *testing.T) {
|
||||
}
|
||||
logrus.Infof("Created Configmap %q.\n", configmapName)
|
||||
time.Sleep(10 * time.Second)
|
||||
deployment := createDeployement(configmapName, namespace)
|
||||
deployment := createDeployement(configmapName, namespace, client)
|
||||
|
||||
logrus.Infof("Updating Configmap %q.\n", configmapName)
|
||||
_, err = configmapClient.Get(configmapName, metav1.GetOptions{})
|
||||
@@ -84,7 +89,7 @@ func TestControllerForUpdatingConfigmapShouldUpdateDeployment(t *testing.T) {
|
||||
time.Sleep(15 * time.Second)
|
||||
}
|
||||
|
||||
func createDeployement(deploymentName string, namespace string) *v1beta1.Deployment {
|
||||
func createDeployement(deploymentName string, namespace string, client kubernetes.Interface) *v1beta1.Deployment {
|
||||
deploymentClient := client.ExtensionsV1beta1().Deployments(namespace)
|
||||
deployment := initDeployment(namespace, deploymentName)
|
||||
deployment, err := deploymentClient.Create(deployment)
|
||||
@@ -96,9 +101,14 @@ func createDeployement(deploymentName string, namespace string) *v1beta1.Deploym
|
||||
}
|
||||
|
||||
func TestControllerForUpdatingSecretShouldUpdateDeployment(t *testing.T) {
|
||||
client, err := kube.GetClient()
|
||||
if err != nil {
|
||||
logrus.Infof("Unable to create Kubernetes client error = %v", err)
|
||||
return
|
||||
}
|
||||
namespace := "test-reloader-secrets"
|
||||
createNamespace(t, namespace)
|
||||
defer deleteNamespace(t, namespace)
|
||||
createNamespace(t, namespace, client)
|
||||
defer deleteNamespace(t, namespace, client)
|
||||
|
||||
controller, err := NewController(client, "secrets", namespace)
|
||||
if err != nil {
|
||||
@@ -203,7 +213,7 @@ func initSecret(namespace string, secretName string) *v1.Secret {
|
||||
}
|
||||
}
|
||||
|
||||
func createNamespace(t *testing.T, namespace string) {
|
||||
func createNamespace(t *testing.T, namespace string, client kubernetes.Interface) {
|
||||
_, err := client.CoreV1().Namespaces().Create(&v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: namespace}})
|
||||
if err != nil {
|
||||
t.Error("Failed to create namespace for testing", err)
|
||||
@@ -212,7 +222,7 @@ func createNamespace(t *testing.T, namespace string) {
|
||||
}
|
||||
}
|
||||
|
||||
func deleteNamespace(t *testing.T, namespace string) {
|
||||
func deleteNamespace(t *testing.T, namespace string, client kubernetes.Interface) {
|
||||
err := client.CoreV1().Namespaces().Delete(namespace, &metav1.DeleteOptions{})
|
||||
if err != nil {
|
||||
t.Error("Failed to delete namespace that was created for testing", err)
|
||||
|
||||
6
internal/pkg/handler/created-handler.go
Normal file
6
internal/pkg/handler/created-handler.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package handler
|
||||
|
||||
// ResourceCreatedHandler contains new objects
|
||||
type ResourceCreatedHandler struct {
|
||||
Resource interface{}
|
||||
}
|
||||
@@ -10,27 +10,16 @@ type ResourceHandler interface {
|
||||
Handle() error
|
||||
}
|
||||
|
||||
// ResourceCreatedHandler contains new objects
|
||||
type ResourceCreatedHandler struct {
|
||||
NewResource interface{}
|
||||
}
|
||||
|
||||
// ResourceUpdatedHandler contains updated objects
|
||||
type ResourceUpdatedHandler struct {
|
||||
NewResource interface{}
|
||||
OldResource interface{}
|
||||
}
|
||||
|
||||
// Handle processes the newly created resource
|
||||
func (r ResourceCreatedHandler) Handle() error {
|
||||
if r.NewResource == nil {
|
||||
if r.Resource == nil {
|
||||
logrus.Infof("Error in Handler")
|
||||
} else {
|
||||
logrus.Infof("Detected changes in object %s", r.NewResource)
|
||||
logrus.Infof("Detected changes in object %s", r.Resource)
|
||||
// process resource based on its type
|
||||
if _, ok := r.NewResource.(*v1.ConfigMap); ok {
|
||||
if _, ok := r.Resource.(*v1.ConfigMap); ok {
|
||||
logrus.Infof("Performing 'Added' action for resource of type 'configmap'")
|
||||
} else if _, ok := r.NewResource.(*v1.Secret); ok {
|
||||
} else if _, ok := r.Resource.(*v1.Secret); ok {
|
||||
logrus.Infof("Performing 'Added' action for resource of type 'secret'")
|
||||
} else {
|
||||
logrus.Infof("Invalid resource")
|
||||
@@ -41,14 +30,14 @@ func (r ResourceCreatedHandler) Handle() error {
|
||||
|
||||
// Handle processes the updated resource
|
||||
func (r ResourceUpdatedHandler) Handle() error {
|
||||
if r.NewResource == nil || r.OldResource == nil {
|
||||
if r.Resource == nil || r.OldResource == nil {
|
||||
logrus.Infof("Error in Handler")
|
||||
} else {
|
||||
logrus.Infof("Detected changes in object %s", r.NewResource)
|
||||
logrus.Infof("Detected changes in object %s", r.Resource)
|
||||
// process resource based on its type
|
||||
if _, ok := r.NewResource.(*v1.ConfigMap); ok {
|
||||
if _, ok := r.Resource.(*v1.ConfigMap); ok {
|
||||
logrus.Infof("Performing 'Updated' action for resource of type 'configmap'")
|
||||
} else if _, ok := r.NewResource.(*v1.Secret); ok {
|
||||
} else if _, ok := r.Resource.(*v1.Secret); ok {
|
||||
logrus.Infof("Performing 'Updated' action for resource of type 'secret'")
|
||||
} else {
|
||||
logrus.Infof("Invalid resource")
|
||||
7
internal/pkg/handler/updated-handler.go
Normal file
7
internal/pkg/handler/updated-handler.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package handler
|
||||
|
||||
// ResourceUpdatedHandler contains updated objects
|
||||
type ResourceUpdatedHandler struct {
|
||||
Resource interface{}
|
||||
OldResource interface{}
|
||||
}
|
||||
Reference in New Issue
Block a user