This repository has been archived by the owner on Oct 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from rebuy-de/add-pre-stop-sleep
add preStop sleep
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package prestopsleep | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
|
||
log "github.com/sirupsen/logrus" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/pkg/api/v1" | ||
v1beta1apps "k8s.io/client-go/pkg/apis/apps/v1beta1" | ||
v1beta1extensions "k8s.io/client-go/pkg/apis/extensions/v1beta1" | ||
) | ||
|
||
const SleepSeconds = 5 | ||
|
||
type Interceptor struct { | ||
} | ||
|
||
func New() *Interceptor { | ||
return &Interceptor{} | ||
} | ||
|
||
func (i *Interceptor) ManifestRendered(obj runtime.Object) (runtime.Object, error) { | ||
switch typed := obj.(type) { | ||
case *v1beta1extensions.Deployment: | ||
AddToDeployment(typed) | ||
return typed, nil | ||
|
||
case *v1beta1apps.StatefulSet: | ||
AddToStatefulSet(typed) | ||
return typed, nil | ||
|
||
default: | ||
log.WithFields(log.Fields{ | ||
"type": reflect.TypeOf(obj), | ||
}).Debug("type doesn't support adding preStop hook") | ||
} | ||
return obj, nil | ||
} | ||
|
||
func AddToContainer(container *v1.Container) { | ||
if container.Lifecycle == nil { | ||
container.Lifecycle = &v1.Lifecycle{} | ||
} | ||
|
||
if container.Lifecycle.PreStop != nil { | ||
return | ||
} | ||
|
||
container.Lifecycle.PreStop = &v1.Handler{ | ||
Exec: &v1.ExecAction{ | ||
Command: []string{"sleep", fmt.Sprintf("%d", SleepSeconds)}, | ||
}, | ||
} | ||
} | ||
|
||
func AddToPodTemplace(tpl *v1.PodTemplateSpec) { | ||
for i := range tpl.Spec.Containers { | ||
AddToContainer(&tpl.Spec.Containers[i]) | ||
} | ||
} | ||
|
||
func AddToDeployment(deployment *v1beta1extensions.Deployment) { | ||
AddToPodTemplace(&deployment.Spec.Template) | ||
} | ||
|
||
func AddToStatefulSet(set *v1beta1apps.StatefulSet) { | ||
AddToPodTemplace(&set.Spec.Template) | ||
} |
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,37 @@ | ||
package prestopsleep | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rebuy-de/kubernetes-deployment/pkg/interceptors" | ||
"github.com/rebuy-de/kubernetes-deployment/pkg/testutil" | ||
"k8s.io/client-go/pkg/api/v1" | ||
v1beta1extensions "k8s.io/client-go/pkg/apis/extensions/v1beta1" | ||
) | ||
|
||
func TestType(t *testing.T) { | ||
var inter interceptors.ManifestRendered | ||
inter = New() | ||
_ = inter | ||
} | ||
|
||
func TestModify(t *testing.T) { | ||
deployment := &v1beta1extensions.Deployment{ | ||
Spec: v1beta1extensions.DeploymentSpec{ | ||
Template: v1.PodTemplateSpec{ | ||
Spec: v1.PodSpec{ | ||
InitContainers: []v1.Container{ | ||
v1.Container{}, | ||
}, | ||
Containers: []v1.Container{ | ||
v1.Container{}, | ||
v1.Container{}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
AddToDeployment(deployment) | ||
|
||
testutil.AssertGoldenFile(t, "test-fixtures/deployment-golden.json", deployment) | ||
} |
52 changes: 52 additions & 0 deletions
52
pkg/interceptors/prestopsleep/test-fixtures/deployment-golden.json
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,52 @@ | ||
{ | ||
"metadata": { | ||
"creationTimestamp": null | ||
}, | ||
"spec": { | ||
"template": { | ||
"metadata": { | ||
"creationTimestamp": null | ||
}, | ||
"spec": { | ||
"initContainers": [ | ||
{ | ||
"name": "", | ||
"resources": {} | ||
} | ||
], | ||
"containers": [ | ||
{ | ||
"name": "", | ||
"resources": {}, | ||
"lifecycle": { | ||
"preStop": { | ||
"exec": { | ||
"command": [ | ||
"sleep", | ||
"5" | ||
] | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "", | ||
"resources": {}, | ||
"lifecycle": { | ||
"preStop": { | ||
"exec": { | ||
"command": [ | ||
"sleep", | ||
"5" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"strategy": {} | ||
}, | ||
"status": {} | ||
} |