Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End-to-end Tests #50

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
_output/
examples/
*.md
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea/
vendor/
pod-reaper.iml

_output/
pod-reaper.iml
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ FROM golang:1.14 AS build
WORKDIR /go/src/github.com/target/pod-reaper
ENV CGO_ENABLED=0 GOOS=linux
COPY ./ ./
RUN go test ./rules/... && \
go test ./reaper/... && \
go build -o pod-reaper -a -installsuffix go ./reaper
RUN go test ./cmd/... && \
go test ./internal/... && \
go build -o pod-reaper -a -installsuffix go ./cmd/pod-reaper

# Application
FROM scratch
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile-minikube
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# including this because default minikube drivers do not yet support multistage docker builds
# this will only be used for minikube
FROM scratch
COPY pod-reaper /
COPY _output/bin/pod-reaper /
CMD ["/pod-reaper"]
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.PHONY: test

VERSION?=$(shell git describe --tags)
REPOSITORY=target
IMAGE:=pod-reaper:$(VERSION)

CLUSTER_NAME=e2e

all: build

build:
CGO_ENABLED=0 go build -o _output/bin/pod-reaper github.com/target/pod-reaper/cmd/pod-reaper

image:
docker build -t $(IMAGE) .

clean:
rm -rf _output

test-unit:
./test/run-unit-tests.sh

test-e2e:
./test/create-cluster.sh $(CLUSTER_NAME)
./test/run-e2e-tests.sh
./test/delete-cluster.sh $(CLUSTER_NAME)
4 changes: 2 additions & 2 deletions reaper/options.go → cmd/pod-reaper/app/options.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"fmt"
Expand All @@ -10,7 +10,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"

"github.com/target/pod-reaper/rules"
"github.com/target/pod-reaper/internal/pkg/rules"
)

// environment variable names
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"os"
Expand Down
30 changes: 10 additions & 20 deletions reaper/reaper.go → cmd/pod-reaper/app/reaper.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package app

import (
"time"
Expand All @@ -9,25 +9,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

type reaper struct {
clientSet *kubernetes.Clientset
type Reaper struct {
clientSet kubernetes.Interface
options options
}

func newReaper() reaper {
config, err := rest.InClusterConfig()
if err != nil {
logrus.WithError(err).Panic("error getting in cluster kubernetes config")
panic(err)
}
clientSet, err := kubernetes.NewForConfig(config)
if err != nil {
logrus.WithError(err).Panic("unable to get client set for in cluster kubernetes config")
panic(err)
}
func NewReaper(clientSet kubernetes.Interface) Reaper {
if clientSet == nil {
message := "kubernetes client set cannot be nil"
logrus.Panic(message)
Expand All @@ -38,13 +27,14 @@ func newReaper() reaper {
logrus.WithError(err).Panic("error loading options")
panic(err)
}
return reaper{

return Reaper{
clientSet: clientSet,
options: options,
}
}

func (reaper reaper) getPods() *v1.PodList {
func (reaper Reaper) getPods() *v1.PodList {
coreClient := reaper.clientSet.CoreV1()
pods := coreClient.Pods(reaper.options.namespace)
listOptions := metav1.ListOptions{}
Expand Down Expand Up @@ -78,7 +68,7 @@ func (reaper reaper) getPods() *v1.PodList {
return podList
}

func (reaper reaper) reapPod(pod v1.Pod, reasons []string) {
func (reaper Reaper) reapPod(pod v1.Pod, reasons []string) {
deleteOptions := &metav1.DeleteOptions{
GracePeriodSeconds: reaper.options.gracePeriod,
}
Expand All @@ -102,7 +92,7 @@ func (reaper reaper) reapPod(pod v1.Pod, reasons []string) {
}
}

func (reaper reaper) scytheCycle() {
func (reaper Reaper) scytheCycle() {
logrus.Debug("starting reap cycle")
pods := reaper.getPods()
for _, pod := range pods.Items {
Expand All @@ -121,7 +111,7 @@ func cronWithOptionalSeconds() *cron.Cron {
cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)))
}

func (reaper reaper) harvest() {
func (reaper Reaper) Harvest() {
runForever := reaper.options.runDuration == 0
schedule := cronWithOptionalSeconds()
_, err := schedule.AddFunc(reaper.options.schedule, func() {
Expand Down
15 changes: 11 additions & 4 deletions reaper/main.go → cmd/pod-reaper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

joonix "github.com/joonix/log"
"github.com/sirupsen/logrus"
"github.com/target/pod-reaper/cmd/pod-reaper/app"
"github.com/target/pod-reaper/internal/pkg/client"
)

const envLogLevel = "LOG_LEVEL"
Expand All @@ -14,13 +16,18 @@ const logrusFormat = "Logrus"
const defaultLogLevel = logrus.InfoLevel

func main() {
logLevel := getLogLevel()
logrus.SetLevel(logLevel)
logFormat := getLogFormat()
logrus.SetFormatter(logFormat)
logLevel := getLogLevel()
logrus.SetLevel(logLevel)

reaper := newReaper()
reaper.harvest()
clientset, err := client.CreateClient("")
if err != nil {
logrus.WithError(err).Panic("cannot create client")
panic(err)
}
reaper := app.NewReaper(clientset)
reaper.Harvest()
logrus.Info("pod reaper is exiting")
}

Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ require (
k8s.io/api v0.17.0
k8s.io/apimachinery v0.17.3-beta.0
k8s.io/client-go v0.17.0
sigs.k8s.io/yaml v1.1.0
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
github.com/Azure/go-autorest/autorest v0.9.0 h1:MRvx8gncNaXJqOoLmhNjUAKh33JJF8LyxPhomEtOsjs=
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
github.com/Azure/go-autorest/autorest/adal v0.5.0 h1:q2gDruN08/guU9vAjuPWff0+QIrpH6ediguzdAzXAUU=
github.com/Azure/go-autorest/autorest/adal v0.5.0/go.mod h1:8Z9fGy2MpX0PvDjB1pEgQTmVqjGhiHBW7RJJEciWzS0=
github.com/Azure/go-autorest/autorest/date v0.1.0 h1:YGrhWfrgtFs84+h0o46rJrlmsZtyZRg470CqAXTZaGM=
github.com/Azure/go-autorest/autorest/date v0.1.0/go.mod h1:plvfp3oPSKwf2DNjlBjWF/7vwR+cUD/ELuzDCXwHUVA=
github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
github.com/Azure/go-autorest/logger v0.1.0 h1:ruG4BSDXONFRrZZJ2GUXDiUyVpayPmb1GnWeHDdaNKY=
github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc=
github.com/Azure/go-autorest/tracing v0.5.0 h1:TRn4WjSnkcSy5AEG3pnbtFSwNtwzjr4VYyQflFE619k=
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
Expand All @@ -17,6 +23,7 @@ github.com/davecgh/go-spew v0.0.0-20151105211317-5215b55f46b2/go.mod h1:J7Y8YcW2
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docker/spdystream v0.0.0-20160310174837-449fdfce4d96/go.mod h1:Qh8CwZgvJUkLughtfhJv5dyTYa91l1fOUCrgjqmcifM=
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
Expand Down Expand Up @@ -53,11 +60,13 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d h1:7XGaL1e6bYS1yIonGp9761ExpPPV1ui0SAC59Yube9k=
github.com/googleapis/gnostic v0.0.0-20170729233727-0c5108395e2d/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
github.com/gophercloud/gophercloud v0.1.0 h1:P/nh25+rzXouhytV2pUHBb65fnds26Ghl8/391+sT5o=
github.com/gophercloud/gophercloud v0.1.0/go.mod h1:vxM41WHh5uqHVBMZHzuwNOHh8XEoIEcSTewFxm1c5g8=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.5 h1:JboBksRwiiAJWvIYJVo46AfV+IAIKZpfrSzVKj42R4Q=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/joonix/log v0.0.0-20190130132305-f5f056244ba3 h1:Y4p9xCEVT0gnYoHQwaSb9RYxRRN4WxxOaT5h7xGZBAo=
github.com/joonix/log v0.0.0-20190130132305-f5f056244ba3/go.mod h1:9alna084PKap49x3Dl7QTGUXiS37acLi8ryAexT1SJc=
Expand Down
66 changes: 66 additions & 0 deletions internal/pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package client

import (
"fmt"

clientset "k8s.io/client-go/kubernetes"
// Ensure to load all auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

// CreateClient creates a new Kubernets clientset with the given config or in-cluster config
func CreateClient(kubeconfig string) (clientset.Interface, error) {
var cfg *rest.Config
if len(kubeconfig) != 0 {
master, err := getMasterFromKubeconfig(kubeconfig)
if err != nil {
return nil, fmt.Errorf("Failed to parse kubeconfig file: %v ", err)
}

cfg, err = clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
return nil, fmt.Errorf("Unable to build config: %v", err)
}

} else {
var err error
cfg, err = rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("Unable to build in cluster config: %v", err)
}
}

return clientset.NewForConfig(cfg)
}

func getMasterFromKubeconfig(filename string) (string, error) {
config, err := clientcmd.LoadFromFile(filename)
if err != nil {
return "", err
}

context, ok := config.Contexts[config.CurrentContext]
if !ok {
return "", fmt.Errorf("Failed to get master address from kubeconfig")
}

if val, ok := config.Clusters[context.Cluster]; ok {
return val.Server, nil
}
return "", fmt.Errorf("Failed to get master address from kubeconfig")
}
Loading