Skip to content

Commit

Permalink
add containers log saver extension
Browse files Browse the repository at this point in the history
Signed-off-by: Anatoly Loskutnikov <anatoly.loskutnikov@gmail.com>
  • Loading branch information
Tiberivs committed Mar 11, 2021
1 parent 5962cd9 commit cd416e8
Show file tree
Hide file tree
Showing 4 changed files with 430 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extensions/base/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ package base

import (
"github.com/networkservicemesh/integration-tests/extensions/checkout"
"github.com/networkservicemesh/integration-tests/extensions/logs"
)

// Suite is a base suite for generating tests. Contains extensions that can be used for assertion and automation goals.
type Suite struct {
checkout.Suite
logs.LogSuite
// Add other extensions here
}

func (s *Suite) SetupSuite() {
s.Repository = "networkservicemesh/deployments-k8s"
s.Version = "25830bce"
s.Dir = "../" // Note: this should be synced with input parameters in gen.go file

s.Suite.SetupSuite()
s.LogSuite.SetupSuite()
}
119 changes: 119 additions & 0 deletions extensions/logs/suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// 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 logs

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)

type LogSuite struct {
LogDir string

testStartTime time.Time
kubeClient kubernetes.Interface
}

func (s *LogSuite) SetupSuite() {
if s.LogDir == "" {
s.LogDir = "./logs"
}

kubeClient, err := newKubeClient()
if err != nil {
panic(err)
}

s.kubeClient = kubeClient
}

func (s *LogSuite) SetupTest() {
s.testStartTime = time.Now()
}

func (s *LogSuite) AfterTest(suiteName, testName string) {
logOptions := corev1.PodLogOptions{
Timestamps: true,
SinceTime: &metav1.Time{Time: s.testStartTime},
}

logPath := fmt.Sprintf("%s/%s/%s", s.LogDir, suiteName, testName)
if err := os.MkdirAll(logPath, os.ModePerm); err != nil {
panic(err)
}

list, _ := s.kubeClient.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
for nsIdx := range list.Items {
ns := &list.Items[nsIdx]

if isIgnoredNamespace(ns.Name) {
continue
}

pods, err := s.kubeClient.CoreV1().Pods(ns.Name).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err)
}

for podIdx := range pods.Items {
pod := &pods.Items[podIdx]

result, err := s.kubeClient.CoreV1().Pods(ns.Name).GetLogs(pod.Name, &logOptions).DoRaw(context.TODO())
if err != nil {
panic(err)
}

if len(result) > 0 {
err := ioutil.WriteFile(fmt.Sprintf("%s/%s.log", logPath, pod.Name), result, os.ModePerm)
if err != nil {
panic(err)
}
}
}
}
}

// newKubeClient creates new k8s client
func newKubeClient() (kubernetes.Interface, error) {
path := os.Getenv("KUBECONFIG")

if path == "" {
path = filepath.Join(os.Getenv("HOME"), ".kube", "config")
}

config, err := clientcmd.BuildConfigFromFlags("", path)
if err != nil {
return nil, err
}

return kubernetes.NewForConfig(config)
}

func isIgnoredNamespace(ns string) bool {
return strings.HasPrefix(ns, "kube-") ||
strings.HasPrefix(ns, "local-")
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ require (
github.com/sirupsen/logrus v1.8.0 // indirect
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20210301091718-77cc2087c03b // indirect
k8s.io/api v0.19.4
k8s.io/apimachinery v0.19.4
k8s.io/client-go v0.19.4
)
Loading

0 comments on commit cd416e8

Please sign in to comment.