-
Notifications
You must be signed in to change notification settings - Fork 122
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 #117 from linki/jake-exec-refactor-without-exec
Refactor out a Terminator interface to support different ways to kill pods
- Loading branch information
Showing
7 changed files
with
223 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package testutil | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/sirupsen/logrus/hooks/test" | ||
|
||
"k8s.io/api/core/v1" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type TestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (suite *TestSuite) AssertPods(pods []v1.Pod, expected []map[string]string) { | ||
suite.Require().Len(pods, len(expected)) | ||
|
||
for i, pod := range pods { | ||
suite.AssertPod(pod, expected[i]) | ||
} | ||
} | ||
|
||
func (suite *TestSuite) AssertPod(pod v1.Pod, expected map[string]string) { | ||
suite.Equal(expected["namespace"], pod.Namespace) | ||
suite.Equal(expected["name"], pod.Name) | ||
} | ||
|
||
func (suite *TestSuite) AssertLog(output *test.Hook, level log.Level, msg string, fields log.Fields) { | ||
suite.Require().NotEmpty(output.Entries) | ||
|
||
lastEntry := output.LastEntry() | ||
suite.Equal(level, lastEntry.Level) | ||
suite.Equal(msg, lastEntry.Message) | ||
for k := range fields { | ||
suite.Equal(fields[k], lastEntry.Data[k]) | ||
} | ||
} |
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,45 @@ | ||
package terminator | ||
|
||
import ( | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
) | ||
|
||
// DeletePodTerminator simply asks k8s to delete the victim pod. | ||
type DeletePodTerminator struct { | ||
client kubernetes.Interface | ||
logger log.FieldLogger | ||
gracePeriod time.Duration | ||
} | ||
|
||
// NewDeletePodTerminator creates and returns a DeletePodTerminator object. | ||
func NewDeletePodTerminator(client kubernetes.Interface, logger log.FieldLogger, gracePeriod time.Duration) *DeletePodTerminator { | ||
return &DeletePodTerminator{ | ||
client: client, | ||
logger: logger.WithField("terminator", "DeletePod"), | ||
gracePeriod: gracePeriod, | ||
} | ||
} | ||
|
||
// Terminate sends a request to Kubernetes to delete the pod. | ||
func (t *DeletePodTerminator) Terminate(victim v1.Pod) error { | ||
t.logger.WithFields(log.Fields{ | ||
"namespace": victim.Namespace, | ||
"name": victim.Name, | ||
}).Debug("calling deletePod endpoint") | ||
|
||
return t.client.CoreV1().Pods(victim.Namespace).Delete(victim.Name, deleteOptions(t.gracePeriod)) | ||
} | ||
|
||
func deleteOptions(gracePeriod time.Duration) *metav1.DeleteOptions { | ||
if gracePeriod < 0 { | ||
return nil | ||
} | ||
|
||
return &metav1.DeleteOptions{GracePeriodSeconds: (*int64)(&gracePeriod)} | ||
} |
Oops, something went wrong.