-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature reload-on-delete implemented, test cases enhanced
- Loading branch information
Showing
12 changed files
with
568 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/sirupsen/logrus" | ||
"github.com/stakater/Reloader/internal/pkg/callbacks" | ||
"github.com/stakater/Reloader/internal/pkg/constants" | ||
"github.com/stakater/Reloader/internal/pkg/metrics" | ||
"github.com/stakater/Reloader/internal/pkg/options" | ||
"github.com/stakater/Reloader/internal/pkg/testutil" | ||
"github.com/stakater/Reloader/internal/pkg/util" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/tools/record" | ||
) | ||
|
||
// ResourceDeleteHandler contains new objects | ||
type ResourceDeleteHandler struct { | ||
Resource interface{} | ||
Collectors metrics.Collectors | ||
Recorder record.EventRecorder | ||
} | ||
|
||
// Handle processes resources being deleted | ||
func (r ResourceDeleteHandler) Handle() error { | ||
if r.Resource == nil { | ||
logrus.Errorf("Resource delete handler received nil resource") | ||
} else { | ||
config, _ := r.GetConfig() | ||
// Send webhook | ||
if options.WebhookUrl != "" { | ||
return sendUpgradeWebhook(config, options.WebhookUrl) | ||
} | ||
// process resource based on its type | ||
return doRollingUpgrade(config, r.Collectors, r.Recorder, invokeDeleteStrategy) | ||
} | ||
return nil | ||
} | ||
|
||
// GetConfig gets configurations containing SHA, annotations, namespace and resource name | ||
func (r ResourceDeleteHandler) GetConfig() (util.Config, string) { | ||
var oldSHAData string | ||
var config util.Config | ||
if _, ok := r.Resource.(*v1.ConfigMap); ok { | ||
config = util.GetConfigmapConfig(r.Resource.(*v1.ConfigMap)) | ||
} else if _, ok := r.Resource.(*v1.Secret); ok { | ||
config = util.GetSecretConfig(r.Resource.(*v1.Secret)) | ||
} else { | ||
logrus.Warnf("Invalid resource: Resource should be 'Secret' or 'Configmap' but found, %v", r.Resource) | ||
} | ||
return config, oldSHAData | ||
} | ||
|
||
func invokeDeleteStrategy(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) constants.Result { | ||
if options.ReloadStrategy == constants.AnnotationsReloadStrategy { | ||
return removePodAnnotations(upgradeFuncs, item, config, autoReload) | ||
} | ||
|
||
return removeContainerEnvVars(upgradeFuncs, item, config, autoReload) | ||
} | ||
|
||
func removePodAnnotations(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) constants.Result { | ||
config.SHAValue = testutil.GetSHAfromEmptyData() | ||
return updatePodAnnotations(upgradeFuncs, item, config, autoReload) | ||
} | ||
|
||
func removeContainerEnvVars(upgradeFuncs callbacks.RollingUpgradeFuncs, item runtime.Object, config util.Config, autoReload bool) constants.Result { | ||
envVar := getEnvVarName(config.ResourceName, config.Type) | ||
container := getContainerUsingResource(upgradeFuncs, item, config, autoReload) | ||
|
||
if container == nil { | ||
return constants.NoContainerFound | ||
} | ||
|
||
//remove if env var exists | ||
containers := upgradeFuncs.ContainersFunc(item) | ||
for i := range containers { | ||
envs := containers[i].Env | ||
index := -1 | ||
for j := range envs { | ||
if envs[j].Name == envVar { | ||
index = j | ||
break | ||
} | ||
} | ||
if index != -1 { | ||
containers[i].Env = append(containers[i].Env[:index], containers[i].Env[index+1:]...) | ||
return constants.Updated | ||
} | ||
} | ||
|
||
return constants.NotUpdated | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.