From 278aeff1ed3e1f57f25c246d3200ad7f1618c9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Lipt=C3=A1k?= Date: Tue, 12 May 2020 15:34:58 -0400 Subject: [PATCH] Enable lint in Travis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gábor Lipták --- .travis.yml | 9 ++-- chaos/chaos.go | 6 --- chaos/chaos_test.go | 3 +- victims/factory/daemonsets/daemonsets.go | 8 ++-- victims/factory/daemonsets/daemonsets_test.go | 6 +-- victims/factory/deployments/deployments.go | 8 ++-- .../factory/deployments/deployments_test.go | 6 +-- .../factory/statefulsets/statefulset_test.go | 6 +-- victims/factory/statefulsets/statefulsets.go | 8 ++-- victims/victims.go | 14 +++--- victims/victims_test.go | 46 +++++++++---------- 11 files changed, 59 insertions(+), 61 deletions(-) diff --git a/.travis.yml b/.travis.yml index 498da1f6..160f5adb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,9 @@ -sudo: false +os: linux +distro: xenial language: go +go_import_path: github.com/asobti/kube-monkey + go: - "1.12.x" - "1.13.x" @@ -11,10 +14,10 @@ git: depth: 1 env: -- GOLANGCI_RELEASE="v1.4.1" +- GOLANGCI_RELEASE="v1.26.0" script: - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s ${GOLANGCI_RELEASE} -- make +- make test lint # See https://github.com/asobti/kube-monkey/issues/120 # - .travis/check_workspace.sh diff --git a/chaos/chaos.go b/chaos/chaos.go index ed75763d..64da87c6 100644 --- a/chaos/chaos.go +++ b/chaos/chaos.go @@ -148,12 +148,6 @@ func (c *Chaos) getKillValue(clientset kube.Interface) (int, error) { return killValue, nil } -// Redundant for DeleteRandomPods(clientset,1) but DeleteRandomPod is faster -// Terminates one random pod -func (c *Chaos) terminatePod(clientset kube.Interface) error { - return c.Victim().DeleteRandomPod(clientset) -} - // NewResult creates a ChaosResult instance func (c *Chaos) NewResult(e error) *Result { return &Result{ diff --git a/chaos/chaos_test.go b/chaos/chaos_test.go index 148c2f18..34bc8069 100644 --- a/chaos/chaos_test.go +++ b/chaos/chaos_test.go @@ -2,10 +2,11 @@ package chaos import ( "errors" + "testing" + "github.com/asobti/kube-monkey/config" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/suite" - "testing" kube "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" diff --git a/victims/factory/daemonsets/daemonsets.go b/victims/factory/daemonsets/daemonsets.go index 877058b8..cdf699b3 100644 --- a/victims/factory/daemonsets/daemonsets.go +++ b/victims/factory/daemonsets/daemonsets.go @@ -7,7 +7,7 @@ import ( "github.com/asobti/kube-monkey/config" "github.com/asobti/kube-monkey/victims" - "k8s.io/api/apps/v1" + appsv1 "k8s.io/api/apps/v1" ) type DaemonSet struct { @@ -15,7 +15,7 @@ type DaemonSet struct { } // New creates a new instance of DaemonSet -func New(dep *v1.DaemonSet) (*DaemonSet, error) { +func New(dep *appsv1.DaemonSet) (*DaemonSet, error) { ident, err := identifier(dep) if err != nil { return nil, err @@ -34,7 +34,7 @@ func New(dep *v1.DaemonSet) (*DaemonSet, error) { // This label should be unique to a DaemonSet, and is used to // identify the pods that belong to this DaemonSet, as pods // inherit labels from the DaemonSet -func identifier(kubekind *v1.DaemonSet) (string, error) { +func identifier(kubekind *appsv1.DaemonSet) (string, error) { identifier, ok := kubekind.Labels[config.IdentLabelKey] if !ok { return "", fmt.Errorf("%T %s does not have %s label", kubekind, kubekind.Name, config.IdentLabelKey) @@ -44,7 +44,7 @@ func identifier(kubekind *v1.DaemonSet) (string, error) { // Read the mean-time-between-failures value defined by the DaemonSet // in the label defined by config.MtbfLabelKey -func meanTimeBetweenFailures(kubekind *v1.DaemonSet) (int, error) { +func meanTimeBetweenFailures(kubekind *appsv1.DaemonSet) (int, error) { mtbf, ok := kubekind.Labels[config.MtbfLabelKey] if !ok { return -1, fmt.Errorf("%T %s does not have %s label", kubekind, kubekind.Name, config.MtbfLabelKey) diff --git a/victims/factory/daemonsets/daemonsets_test.go b/victims/factory/daemonsets/daemonsets_test.go index 56f4da8a..04cec5ff 100644 --- a/victims/factory/daemonsets/daemonsets_test.go +++ b/victims/factory/daemonsets/daemonsets_test.go @@ -5,7 +5,7 @@ import ( "github.com/asobti/kube-monkey/config" "github.com/stretchr/testify/assert" - "k8s.io/api/apps/v1" + appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -15,9 +15,9 @@ const ( NAMESPACE = metav1.NamespaceDefault ) -func newDaemonSet(name string, labels map[string]string) v1.DaemonSet { +func newDaemonSet(name string, labels map[string]string) appsv1.DaemonSet { - return v1.DaemonSet{ + return appsv1.DaemonSet{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: NAMESPACE, diff --git a/victims/factory/deployments/deployments.go b/victims/factory/deployments/deployments.go index 21610404..6612cdae 100644 --- a/victims/factory/deployments/deployments.go +++ b/victims/factory/deployments/deployments.go @@ -7,7 +7,7 @@ import ( "github.com/asobti/kube-monkey/config" "github.com/asobti/kube-monkey/victims" - "k8s.io/api/apps/v1" + appsv1 "k8s.io/api/apps/v1" ) type Deployment struct { @@ -15,7 +15,7 @@ type Deployment struct { } // New creates a new instance of Deployment -func New(dep *v1.Deployment) (*Deployment, error) { +func New(dep *appsv1.Deployment) (*Deployment, error) { ident, err := identifier(dep) if err != nil { return nil, err @@ -34,7 +34,7 @@ func New(dep *v1.Deployment) (*Deployment, error) { // This label should be unique to a deployment, and is used to // identify the pods that belong to this deployment, as pods // inherit labels from the Deployment -func identifier(kubekind *v1.Deployment) (string, error) { +func identifier(kubekind *appsv1.Deployment) (string, error) { identifier, ok := kubekind.Labels[config.IdentLabelKey] if !ok { return "", fmt.Errorf("%T %s does not have %s label", kubekind, kubekind.Name, config.IdentLabelKey) @@ -44,7 +44,7 @@ func identifier(kubekind *v1.Deployment) (string, error) { // Read the mean-time-between-failures value defined by the Deployment // in the label defined by config.MtbfLabelKey -func meanTimeBetweenFailures(kubekind *v1.Deployment) (int, error) { +func meanTimeBetweenFailures(kubekind *appsv1.Deployment) (int, error) { mtbf, ok := kubekind.Labels[config.MtbfLabelKey] if !ok { return -1, fmt.Errorf("%T %s does not have %s label", kubekind, kubekind.Name, config.MtbfLabelKey) diff --git a/victims/factory/deployments/deployments_test.go b/victims/factory/deployments/deployments_test.go index b2990eb6..950cd662 100644 --- a/victims/factory/deployments/deployments_test.go +++ b/victims/factory/deployments/deployments_test.go @@ -5,7 +5,7 @@ import ( "github.com/asobti/kube-monkey/config" "github.com/stretchr/testify/assert" - "k8s.io/api/apps/v1" + appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -15,9 +15,9 @@ const ( NAMESPACE = metav1.NamespaceDefault ) -func newDeployment(name string, labels map[string]string) v1.Deployment { +func newDeployment(name string, labels map[string]string) appsv1.Deployment { - return v1.Deployment{ + return appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: NAMESPACE, diff --git a/victims/factory/statefulsets/statefulset_test.go b/victims/factory/statefulsets/statefulset_test.go index 44983aff..1dff58fc 100644 --- a/victims/factory/statefulsets/statefulset_test.go +++ b/victims/factory/statefulsets/statefulset_test.go @@ -5,7 +5,7 @@ import ( "github.com/asobti/kube-monkey/config" "github.com/stretchr/testify/assert" - "k8s.io/api/apps/v1" + appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -15,9 +15,9 @@ const ( NAMESPACE = metav1.NamespaceDefault ) -func newStatefulSet(name string, labels map[string]string) v1.StatefulSet { +func newStatefulSet(name string, labels map[string]string) appsv1.StatefulSet { - return v1.StatefulSet{ + return appsv1.StatefulSet{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: NAMESPACE, diff --git a/victims/factory/statefulsets/statefulsets.go b/victims/factory/statefulsets/statefulsets.go index 3593648a..8f3c1005 100644 --- a/victims/factory/statefulsets/statefulsets.go +++ b/victims/factory/statefulsets/statefulsets.go @@ -7,7 +7,7 @@ import ( "github.com/asobti/kube-monkey/config" "github.com/asobti/kube-monkey/victims" - "k8s.io/api/apps/v1" + corev1 "k8s.io/api/apps/v1" ) type StatefulSet struct { @@ -15,7 +15,7 @@ type StatefulSet struct { } // New creates a new instance of StatefulSet -func New(ss *v1.StatefulSet) (*StatefulSet, error) { +func New(ss *corev1.StatefulSet) (*StatefulSet, error) { ident, err := identifier(ss) if err != nil { return nil, err @@ -34,7 +34,7 @@ func New(ss *v1.StatefulSet) (*StatefulSet, error) { // This label should be unique to a statefulset, and is used to // identify the pods that belong to this statefulset, as pods // inherit labels from the StatefulSet -func identifier(kubekind *v1.StatefulSet) (string, error) { +func identifier(kubekind *corev1.StatefulSet) (string, error) { identifier, ok := kubekind.Labels[config.IdentLabelKey] if !ok { return "", fmt.Errorf("%T %s does not have %s label", kubekind, kubekind.Name, config.IdentLabelKey) @@ -44,7 +44,7 @@ func identifier(kubekind *v1.StatefulSet) (string, error) { // Read the mean-time-between-failures value defined by the StatefulSet // in the label defined by config.MtbfLabelKey -func meanTimeBetweenFailures(kubekind *v1.StatefulSet) (int, error) { +func meanTimeBetweenFailures(kubekind *corev1.StatefulSet) (int, error) { mtbf, ok := kubekind.Labels[config.MtbfLabelKey] if !ok { return -1, fmt.Errorf("%T %s does not have %s label", kubekind, kubekind.Name, config.MtbfLabelKey) diff --git a/victims/victims.go b/victims/victims.go index c6cd4573..772d8eae 100644 --- a/victims/victims.go +++ b/victims/victims.go @@ -12,7 +12,7 @@ import ( kube "k8s.io/client-go/kubernetes" - "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/selection" @@ -45,8 +45,8 @@ type VictimSpecificAPICalls interface { type VictimAPICalls interface { // Exposed Api Calls - RunningPods(kube.Interface) ([]v1.Pod, error) - Pods(kube.Interface) ([]v1.Pod, error) + RunningPods(kube.Interface) ([]corev1.Pod, error) + Pods(kube.Interface) ([]corev1.Pod, error) DeletePod(kube.Interface, string) error DeleteRandomPod(kube.Interface) error // Deprecated, but faster than DeleteRandomPods for single pod termination DeleteRandomPods(kube.Interface, int) error @@ -95,14 +95,14 @@ func (v *VictimBase) Mtbf() int { } // RunningPods returns a list of running pods for the victim -func (v *VictimBase) RunningPods(clientset kube.Interface) (runningPods []v1.Pod, err error) { +func (v *VictimBase) RunningPods(clientset kube.Interface) (runningPods []corev1.Pod, err error) { pods, err := v.Pods(clientset) if err != nil { return nil, err } for _, pod := range pods { - if pod.Status.Phase == v1.PodRunning { + if pod.Status.Phase == corev1.PodRunning { runningPods = append(runningPods, pod) } } @@ -111,7 +111,7 @@ func (v *VictimBase) RunningPods(clientset kube.Interface) (runningPods []v1.Pod } // Pods returns a list of pods under the victim -func (v *VictimBase) Pods(clientset kube.Interface) ([]v1.Pod, error) { +func (v *VictimBase) Pods(clientset kube.Interface) ([]corev1.Pod, error) { labelSelector, err := labelFilterForPods(v.identifier) if err != nil { return nil, err @@ -246,7 +246,7 @@ func labelRequirementForPods(identifier string) (*labels.Requirement, error) { } // RandomPodName picks a random pod name from a list of Pods -func RandomPodName(pods []v1.Pod) string { +func RandomPodName(pods []corev1.Pod) string { r := rand.New(rand.NewSource(time.Now().UnixNano())) randIndex := r.Intn(len(pods)) return pods[randIndex].Name diff --git a/victims/victims_test.go b/victims/victims_test.go index db346537..57b7f674 100644 --- a/victims/victims_test.go +++ b/victims/victims_test.go @@ -10,7 +10,7 @@ import ( "github.com/asobti/kube-monkey/config" "github.com/bouk/monkey" "github.com/stretchr/testify/assert" - "k8s.io/api/core/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" kube "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" @@ -23,9 +23,9 @@ const ( NAME = "name" ) -func newPod(name string, status v1.PodPhase) v1.Pod { +func newPod(name string, status corev1.PodPhase) corev1.Pod { - return v1.Pod{ + return corev1.Pod{ TypeMeta: metav1.TypeMeta{ Kind: "Pod", APIVersion: "v1", @@ -37,13 +37,13 @@ func newPod(name string, status v1.PodPhase) v1.Pod { "kube-monkey/identifier": IDENTIFIER, }, }, - Status: v1.PodStatus{ + Status: corev1.PodStatus{ Phase: status, }, } } -func generateNPods(namePrefix string, n int, status v1.PodPhase) []runtime.Object { +func generateNPods(namePrefix string, n int, status corev1.PodPhase) []runtime.Object { var pods []runtime.Object for i := 0; i < n; i++ { pod := newPod(fmt.Sprintf("%s%d", namePrefix, i), status) @@ -54,14 +54,14 @@ func generateNPods(namePrefix string, n int, status v1.PodPhase) []runtime.Objec } func generateNRunningPods(namePrefix string, n int) []runtime.Object { - return generateNPods(namePrefix, n, v1.PodRunning) + return generateNPods(namePrefix, n, corev1.PodRunning) } func newVictimBase() *VictimBase { return New(KIND, NAME, NAMESPACE, IDENTIFIER, 1) } -func getPodList(client kube.Interface) *v1.PodList { +func getPodList(client kube.Interface) *corev1.PodList { podList, _ := client.CoreV1().Pods(NAMESPACE).List(metav1.ListOptions{}) return podList } @@ -80,8 +80,8 @@ func TestVictimBaseTemplateGetters(t *testing.T) { func TestRunningPods(t *testing.T) { v := newVictimBase() - pod1 := newPod("app1", v1.PodRunning) - pod2 := newPod("app2", v1.PodPending) + pod1 := newPod("app1", corev1.PodRunning) + pod2 := newPod("app2", corev1.PodPending) client := fake.NewSimpleClientset(&pod1, &pod2) @@ -97,8 +97,8 @@ func TestRunningPods(t *testing.T) { func TestPods(t *testing.T) { v := newVictimBase() - pod1 := newPod("app1", v1.PodRunning) - pod2 := newPod("app2", v1.PodPending) + pod1 := newPod("app1", corev1.PodRunning) + pod2 := newPod("app2", corev1.PodPending) client := fake.NewSimpleClientset(&pod1, &pod2) @@ -110,7 +110,7 @@ func TestPods(t *testing.T) { func TestDeletePod(t *testing.T) { v := newVictimBase() - pod := newPod("app", v1.PodRunning) + pod := newPod("app", corev1.PodRunning) client := fake.NewSimpleClientset(&pod) @@ -127,7 +127,7 @@ func TestDeletePodDryRun(t *testing.T) { defer monkey.Unpatch(config.DryRun) v := newVictimBase() - pod := newPod("app", v1.PodRunning) + pod := newPod("app", corev1.PodRunning) client := fake.NewSimpleClientset(&pod) @@ -141,9 +141,9 @@ func TestDeletePodDryRun(t *testing.T) { func TestDeleteRandomPods(t *testing.T) { v := newVictimBase() - pod1 := newPod("app1", v1.PodRunning) - pod2 := newPod("app2", v1.PodPending) - pod3 := newPod("app3", v1.PodRunning) + pod1 := newPod("app1", corev1.PodRunning) + pod2 := newPod("app2", corev1.PodPending) + pod3 := newPod("app3", corev1.PodRunning) client := fake.NewSimpleClientset(&pod1, &pod2, &pod3) podList := getPodList(client).Items @@ -272,8 +272,8 @@ func TestDeletePodsFixedPercentage(t *testing.T) { name: "does not count pending pods when calculating num of pods to kill", killPercentage: 80, pods: append( - generateNPods("running", 1, v1.PodRunning), - generateNPods("pending", 1, v1.PodPending)...), + generateNPods("running", 1, corev1.PodRunning), + generateNPods("pending", 1, corev1.PodPending)...), expectedNum: 0, expectedErr: false, }, @@ -298,8 +298,8 @@ func TestDeletePodsFixedPercentage(t *testing.T) { func TestDeleteRandomPod(t *testing.T) { v := newVictimBase() - pod1 := newPod("app1", v1.PodRunning) - pod2 := newPod("app2", v1.PodPending) + pod1 := newPod("app1", corev1.PodRunning) + pod2 := newPod("app2", corev1.PodPending) client := fake.NewSimpleClientset(&pod1, &pod2) @@ -338,10 +338,10 @@ func TestIsWhitelisted(t *testing.T) { func TestRandomPodName(t *testing.T) { - pod1 := newPod("app1", v1.PodRunning) - pod2 := newPod("app2", v1.PodPending) + pod1 := newPod("app1", corev1.PodRunning) + pod2 := newPod("app2", corev1.PodPending) - name := RandomPodName([]v1.Pod{pod1, pod2}) + name := RandomPodName([]corev1.Pod{pod1, pod2}) assert.Truef(t, strings.HasPrefix(name, "app"), "Pod name %s should start with 'app'", name) }