<commit_before>package scheduler

import (
	"fmt"
	"strconv"

	"os"

	"github.com/ellcrys/crypto"
	"github.com/ellcrys/util"
	"github.com/franela/goreq"
	"github.com/ncodes/cocoon/core/common"
	logging "github.com/op/go-logging"
)

var log = logging.MustGetLogger("nomad")

// SupportedCocoonCodeLang defines the supported chaincode language
var SupportedCocoonCodeLang = []string{"go"}

// SupportedMemory represents the allowed cocoon memory choices
var SupportedMemory = map[string]int{
	"512m": 512,
	"1g":   1024,
	"2g":   2048,
}

// SupportedCPUShare represents the allowed cocoon cpu share choices
var SupportedCPUShare = map[string]int{
	"1x": 100,
	"2x": 200,
}

// SupportedDiskSpace represents the allowed cocoon disk space
var SupportedDiskSpace = map[string]int{
	"1x": 300,
	"2x": 500,
}

// Nomad defines a nomad scheduler that implements
// scheduler.Scheduler interface. Every interaction with
// the scheduler is handled here.
type Nomad struct {
	schedulerAddr    string
	API              string
	ServiceDiscovery ServiceDiscovery
}

// NewNomad creates a nomad scheduler object
func NewNomad() *Nomad {
	return &Nomad{
		ServiceDiscovery: &NomadServiceDiscovery{
			ConsulAddr: util.Env("CONSUL_ADDR", "localhost:8500"),
			Protocol:   "http",
		},
	}
}

// GetName returns the scheduler name
func (sc *Nomad) GetName() string {
	return "nomad"
}

// SetAddr sets the nomad's API endpoint
func (sc *Nomad) SetAddr(addr string, https bool) {
	scheme := "http://"
	if https {
		scheme = "https://"
	}
	sc.API = scheme + addr
}

// deployJob registers a new job
func (sc *Nomad) deployJob(jobSpec string) (string, int, error) {

	res, err := goreq.Request{
		Method: "POST",
		Uri:    sc.API + "/v1/jobs",
		Body:   jobSpec,
	}.Do()

	if err != nil {
		return "", 0, err
	}
	defer res.Body.Close()

	respStr, _ := res.Body.ToString()
	return respStr, res.StatusCode, nil
}

// Deploy a cocoon code to the scheduler
func (sc *Nomad) Deploy(jobID, lang, url, tag, buildParams, link, memory, cpuShare string) (*DeploymentInfo, error) {

	var err error

	if len(jobID) == 0 {
		return nil, fmt.Errorf("job id is required")
	}

	if err = common.ValidateDeployment(url, lang, buildParams); err != nil {
		return nil, err
	}

	log.Debugf("Deploying cocoon code with language=%s, url=%s, tag=%s", lang, url, tag)

	if len(buildParams) > 0 {
		buildParams = crypto.ToBase64([]byte(buildParams))
	}

	var img string
	switch lang {
	case "go":
		img = "ncodes/cocoon-launcher:latest"
	}

	job := NewJob(jobID, 1)
	job.GetSpec().Region = "global"
	job.GetSpec().Datacenters = []string{"dc1"}
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_CODE_URL"] = url
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_CODE_TAG"] = tag
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_CODE_LANG"] = lang
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_BUILD_PARAMS"] = buildParams
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_DISK_LIMIT"] = strconv.Itoa(SupportedDiskSpace[cpuShare])
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_LINK"] = link
	job.GetSpec().TaskGroups[0].Tasks[0].Config.Image = img
	job.GetSpec().TaskGroups[0].Tasks[0].Resources.CPU = SupportedCPUShare[cpuShare]
	job.GetSpec().TaskGroups[0].Tasks[0].Resources.MemoryMB = SupportedMemory[memory]
	job.GetSpec().TaskGroups[0].Resources.CPU = SupportedCPUShare[cpuShare]
	job.GetSpec().TaskGroups[0].Resources.MemoryMB = SupportedMemory[memory]

	jobSpec, _ := util.ToJSON(job)
	resp, status, err := sc.deployJob(string(jobSpec))
	if err != nil {
		return nil, fmt.Errorf("system: failed to deploy job spec. %s", err)
	} else if status != 200 {
		return nil, fmt.Errorf("system: failed to deploy job spec. %s", resp)
	}

	var jobInfo map[string]interface{}
	if err = util.FromJSON([]byte(resp), &jobInfo); err != nil {
		return nil, fmt.Errorf("system: %s", resp)
	}

	return &DeploymentInfo{
		ID:     jobID,
		EvalID: jobInfo["EvalID"].(string),
	}, nil
}

// Getenv returns an environment variable value based on the schedulers
// naming convention.
func Getenv(env, defaultVal string) string {
	return util.Env(os.Getenv("NOMAD_"+env), defaultVal)
}

// GetServices fetches all the instances of a service
func (sc *Nomad) GetServices(serviceID string) []Service {
	return nil
}
<commit_msg>Sigh! Removed call to os.Getenv using the value from from os.Getenv<commit_after>package scheduler

import (
	"fmt"
	"strconv"

	"github.com/ellcrys/crypto"
	"github.com/ellcrys/util"
	"github.com/franela/goreq"
	"github.com/ncodes/cocoon/core/common"
	logging "github.com/op/go-logging"
)

var log = logging.MustGetLogger("nomad")

// SupportedCocoonCodeLang defines the supported chaincode language
var SupportedCocoonCodeLang = []string{"go"}

// SupportedMemory represents the allowed cocoon memory choices
var SupportedMemory = map[string]int{
	"512m": 512,
	"1g":   1024,
	"2g":   2048,
}

// SupportedCPUShare represents the allowed cocoon cpu share choices
var SupportedCPUShare = map[string]int{
	"1x": 100,
	"2x": 200,
}

// SupportedDiskSpace represents the allowed cocoon disk space
var SupportedDiskSpace = map[string]int{
	"1x": 300,
	"2x": 500,
}

// Nomad defines a nomad scheduler that implements
// scheduler.Scheduler interface. Every interaction with
// the scheduler is handled here.
type Nomad struct {
	schedulerAddr    string
	API              string
	ServiceDiscovery ServiceDiscovery
}

// NewNomad creates a nomad scheduler object
func NewNomad() *Nomad {
	return &Nomad{
		ServiceDiscovery: &NomadServiceDiscovery{
			ConsulAddr: util.Env("CONSUL_ADDR", "localhost:8500"),
			Protocol:   "http",
		},
	}
}

// GetName returns the scheduler name
func (sc *Nomad) GetName() string {
	return "nomad"
}

// SetAddr sets the nomad's API endpoint
func (sc *Nomad) SetAddr(addr string, https bool) {
	scheme := "http://"
	if https {
		scheme = "https://"
	}
	sc.API = scheme + addr
}

// deployJob registers a new job
func (sc *Nomad) deployJob(jobSpec string) (string, int, error) {

	res, err := goreq.Request{
		Method: "POST",
		Uri:    sc.API + "/v1/jobs",
		Body:   jobSpec,
	}.Do()

	if err != nil {
		return "", 0, err
	}
	defer res.Body.Close()

	respStr, _ := res.Body.ToString()
	return respStr, res.StatusCode, nil
}

// Deploy a cocoon code to the scheduler
func (sc *Nomad) Deploy(jobID, lang, url, tag, buildParams, link, memory, cpuShare string) (*DeploymentInfo, error) {

	var err error

	if len(jobID) == 0 {
		return nil, fmt.Errorf("job id is required")
	}

	if err = common.ValidateDeployment(url, lang, buildParams); err != nil {
		return nil, err
	}

	log.Debugf("Deploying cocoon code with language=%s, url=%s, tag=%s", lang, url, tag)

	if len(buildParams) > 0 {
		buildParams = crypto.ToBase64([]byte(buildParams))
	}

	var img string
	switch lang {
	case "go":
		img = "ncodes/cocoon-launcher:latest"
	}

	job := NewJob(jobID, 1)
	job.GetSpec().Region = "global"
	job.GetSpec().Datacenters = []string{"dc1"}
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_CODE_URL"] = url
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_CODE_TAG"] = tag
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_CODE_LANG"] = lang
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_BUILD_PARAMS"] = buildParams
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_DISK_LIMIT"] = strconv.Itoa(SupportedDiskSpace[cpuShare])
	job.GetSpec().TaskGroups[0].Tasks[0].Env["COCOON_LINK"] = link
	job.GetSpec().TaskGroups[0].Tasks[0].Config.Image = img
	job.GetSpec().TaskGroups[0].Tasks[0].Resources.CPU = SupportedCPUShare[cpuShare]
	job.GetSpec().TaskGroups[0].Tasks[0].Resources.MemoryMB = SupportedMemory[memory]
	job.GetSpec().TaskGroups[0].Resources.CPU = SupportedCPUShare[cpuShare]
	job.GetSpec().TaskGroups[0].Resources.MemoryMB = SupportedMemory[memory]

	jobSpec, _ := util.ToJSON(job)
	resp, status, err := sc.deployJob(string(jobSpec))
	if err != nil {
		return nil, fmt.Errorf("system: failed to deploy job spec. %s", err)
	} else if status != 200 {
		return nil, fmt.Errorf("system: failed to deploy job spec. %s", resp)
	}

	var jobInfo map[string]interface{}
	if err = util.FromJSON([]byte(resp), &jobInfo); err != nil {
		return nil, fmt.Errorf("system: %s", resp)
	}

	return &DeploymentInfo{
		ID:     jobID,
		EvalID: jobInfo["EvalID"].(string),
	}, nil
}

// Getenv returns an environment variable value based on the schedulers
// naming convention.
func Getenv(env, defaultVal string) string {
	return util.Env("NOMAD_"+env, defaultVal)
}

// GetServices fetches all the instances of a service
func (sc *Nomad) GetServices(serviceID string) []Service {
	return nil
}
<|endoftext|>


<commit_before>package inmem

import (
	"testing"
	"time"
)

func TestSimpleSend(t *testing.T) {
	r, w := Pipe()
	onTimeout := time.After(100 * time.Millisecond)
	onRcv := make(chan bool)
	go func() {
		msg, in, out, err := r.Receive(0)
		if err != nil {
			t.Fatal(err)
		}
		if msg.Data != "hello world" {
			t.Fatalf("%#v", *msg)
		}
		if msg.Name != "print" {
			t.Fatalf("%#v", *msg)
		}
		if len(msg.Args) != 0 {
			t.Fatalf("%#v", *msg)
		}
		if in != nil {
			t.Fatalf("%#v", in)
		}
		if out != nil {
			t.Fatalf("%#v", out)
		}
		close(onRcv)
	}()
	in, out, err := w.Send(&Message{Name: "print", Data: "hello world"}, 0)
	if err != nil {
		t.Fatal(err)
	}
	if in != nil {
		t.Fatalf("%#v", in)
	}
	if out != nil {
		t.Fatalf("%#v", out)
	}
	select {
	case <-onTimeout:
		t.Fatalf("timeout")
	case <-onRcv:
	}
}
<commit_msg>inmem: use pkg/testutils<commit_after>package inmem

import (
	"github.com/dotcloud/docker/pkg/testutils"
	"testing"
)

func TestSimpleSend(t *testing.T) {
	r, w := Pipe()
	testutils.Timeout(t, func() {
		go func() {
			msg, in, out, err := r.Receive(0)
			if err != nil {
				t.Fatal(err)
			}
			if msg.Data != "hello world" {
				t.Fatalf("%#v", *msg)
			}
			if msg.Name != "print" {
				t.Fatalf("%#v", *msg)
			}
			if len(msg.Args) != 0 {
				t.Fatalf("%#v", *msg)
			}
			if in != nil {
				t.Fatalf("%#v", in)
			}
			if out != nil {
				t.Fatalf("%#v", out)
			}
		}()
		in, out, err := w.Send(&Message{Name: "print", Data: "hello world"}, 0)
		if err != nil {
			t.Fatal(err)
		}
		if in != nil {
			t.Fatalf("%#v", in)
		}
		if out != nil {
			t.Fatalf("%#v", out)
		}
	})
}
<|endoftext|>


<commit_before>// Copyright 2016 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package input

import (
	"path/filepath"
	"testing"

	"github.com/caixw/apidoc/doc"
	"github.com/issue9/assert"
)

func TestParse(t *testing.T) {
	a := assert.New(t)

	testParse(a, "go")
	testParse(a, "groovy")
	testParse(a, "java")
	testParse(a, "javascript")
	testParse(a, "pascal")
	testParse(a, "perl")
	testParse(a, "php")
	testParse(a, "python")
	testParse(a, "ruby")
	testParse(a, "rust")
	testParse(a, "swift")
}

func testParse(a *assert.Assertion, lang string) {
	o := &Options{
		Lang:      lang,
		Dir:       "./testdata/" + lang,
		Recursive: true,
	}
	a.NotError(o.Init()) // 初始化扩展名信息

	docs := doc.New()
	err := Parse(docs, o)
	a.NotError(err).
		Equal(len(docs.Apis), 2)

		// doc.xx
	a.Equal(docs.Title, "title of api").
		Equal(docs.Version, "2.9").
		Equal(docs.BaseURL, "https://api.caixw.io").
		Equal(docs.LicenseName, "MIT").
		Equal(docs.LicenseURL, "https://opensources.org/licenses/MIT").
		Equal(docs.Content, "\n line1\n line2\n")

	// test1.xx
	api0 := docs.Apis[0]
	api1 := docs.Apis[1]
	a.Equal(api0.URL, "/users/login").
		Equal(api1.URL, "/users/login").
		Equal(api0.Group, "users").
		Equal(api1.Group, "users")
}

func TestParseFile(t *testing.T) {
	a := assert.New(t)

	testParseFile(a, "go", "./testdata/go/test1.go")
	testParseFile(a, "groovy", "./testdata/groovy/test1.groovy")
	testParseFile(a, "java", "./testdata/java/test1.java")
	testParseFile(a, "javascript", "./testdata/javascript/test1.js")
	testParseFile(a, "pascal", "./testdata/pascal/test1.pas")
	testParseFile(a, "perl", "./testdata/perl/test1.pl")
	testParseFile(a, "php", "./testdata/php/test1.php")
	testParseFile(a, "python", "./testdata/python/test1.py")
	testParseFile(a, "ruby", "./testdata/ruby/test1.rb")
	testParseFile(a, "rust", "./testdata/rust/test1.rs")
	testParseFile(a, "swift", "./testdata/swift/test1.swift")
}

func testParseFile(a *assert.Assertion, lang, path string) {
	docs := doc.New()
	a.NotNil(docs)

	b, found := langs[lang]
	if !found {
		a.T().Error("不支持该语言")
	}

	parseFile(docs, path, b, nil)
	a.Equal(2, len(docs.Apis))

	api0 := docs.Apis[0]
	api1 := docs.Apis[1]

	a.T().Log(api0.Success.Examples[0])
	a.T().Log(api1.Success.Examples[0])

	a.Equal(api0.URL, "/users/login").
		Equal(api1.URL, "/users/login").
		Equal(api0.Group, "users").
		Equal(api1.Group, "users")

	if api0.Method == "POST" {
		a.Equal(api1.Method, "DELETE").
			Equal(1, len(api1.Request.Headers))
	} else {
		a.Equal(api0.Method, "DELETE").
			Equal(api1.Method, "POST").
			Equal(1, len(api0.Request.Headers))
	}
}

func TestRecursivePath(t *testing.T) {
	a := assert.New(t)

	opt := &Options{Dir: "./testdir", Recursive: false, Exts: []string{".c", ".h"}}
	paths, err := recursivePath(opt)
	a.NotError(err)
	a.Contains(paths, []string{
		filepath.Join("testdir", "testfile.c"),
		filepath.Join("testdir", "testfile.h"),
	})

	opt.Dir = "./testdir"
	opt.Recursive = true
	opt.Exts = []string{".1", ".2"}
	paths, err = recursivePath(opt)
	a.NotError(err)
	a.Contains(paths, []string{
		filepath.Join("testdir", "testdir1", "testfile.1"),
		filepath.Join("testdir", "testdir1", "testfile.2"),
		filepath.Join("testdir", "testdir2", "testfile.1"),
		filepath.Join("testdir", "testfile.1"),
	})

	opt.Dir = "./testdir/testdir1"
	opt.Recursive = true
	opt.Exts = []string{".1", ".2"}
	paths, err = recursivePath(opt)
	a.NotError(err)
	a.Contains(paths, []string{
		filepath.Join("testdir", "testdir1", "testfile.1"),
		filepath.Join("testdir", "testdir1", "testfile.2"),
	})

	opt.Dir = "./testdir"
	opt.Recursive = true
	opt.Exts = []string{".1"}
	paths, err = recursivePath(opt)
	a.NotError(err)
	a.Contains(paths, []string{
		filepath.Join("testdir", "testdir1", "testfile.1"),
		filepath.Join("testdir", "testdir2", "testfile.1"),
		filepath.Join("testdir", "testfile.1"),
	})
}

func TestOptions_Init(t *testing.T) {
	a := assert.New(t)

	o := &Options{Dir: "not exists"}
	a.Error(o.Init())

	o.Dir = "./"
	o.Lang = "not exists"
	a.Error(o.Init())

	// 未指定扩展名，则使用系统默认的
	o.Lang = "c++"
	a.NotError(o.Init())
	a.Equal(o.Exts, langExts["c++"])

	// 指定了 Exts，自动调整扩展名样式。
	o.Lang = "c++"
	o.Exts = []string{"c1", ".c2"}
	a.NotError(o.Init())
	a.Equal(o.Exts, []string{".c1", ".c2"})
}
<commit_msg>修正测试错误<commit_after>// Copyright 2016 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

package input

import (
	"path/filepath"
	"testing"

	"github.com/caixw/apidoc/doc"
	"github.com/issue9/assert"
)

func TestParse(t *testing.T) {
	a := assert.New(t)

	testParse(a, "go")
	testParse(a, "groovy")
	testParse(a, "java")
	testParse(a, "javascript")
	testParse(a, "pascal")
	testParse(a, "perl")
	testParse(a, "php")
	testParse(a, "python")
	testParse(a, "ruby")
	testParse(a, "rust")
	testParse(a, "swift")
}

func testParse(a *assert.Assertion, lang string) {
	o := &Options{
		Lang:      lang,
		Dir:       "./testdata/" + lang,
		Recursive: true,
	}
	a.NotError(o.Init()) // 初始化扩展名信息

	docs := doc.New()
	err := Parse(docs, o)
	a.NotError(err).
		Equal(len(docs.Apis), 2)

		// doc.xx
	a.Equal(docs.Title, "title of api").
		Equal(docs.Version, "2.9").
		Equal(docs.BaseURL, "https://api.caixw.io").
		Equal(docs.LicenseName, "MIT").
		Equal(docs.LicenseURL, "https://opensources.org/licenses/MIT").
		Equal(docs.Content, "\n line1\n line2\n")

	// test1.xx
	api0 := docs.Apis[0]
	api1 := docs.Apis[1]
	a.Equal(api0.URL, "/users/login").
		Equal(api1.URL, "/users/login").
		Equal(api0.Group, "users").
		Equal(api1.Group, "users")
}

func TestParseFile(t *testing.T) {
	a := assert.New(t)

	testParseFile(a, "go", "./testdata/go/test1.go")
	testParseFile(a, "groovy", "./testdata/groovy/test1.groovy")
	testParseFile(a, "java", "./testdata/java/test1.java")
	testParseFile(a, "javascript", "./testdata/javascript/test1.js")
	testParseFile(a, "pascal", "./testdata/pascal/test1.pas")
	testParseFile(a, "perl", "./testdata/perl/test1.pl")
	testParseFile(a, "php", "./testdata/php/test1.php")
	testParseFile(a, "python", "./testdata/python/test1.py")
	testParseFile(a, "ruby", "./testdata/ruby/test1.rb")
	testParseFile(a, "rust", "./testdata/rust/test1.rs")
	testParseFile(a, "swift", "./testdata/swift/test1.swift")
}

func testParseFile(a *assert.Assertion, lang, path string) {
	docs := doc.New()
	a.NotNil(docs)

	b, found := langs[lang]
	if !found {
		a.TB().Error("不支持该语言")
	}

	parseFile(docs, path, b, nil)
	a.Equal(2, len(docs.Apis))

	api0 := docs.Apis[0]
	api1 := docs.Apis[1]

	a.TB().Log(api0.Success.Examples[0])
	a.TB().Log(api1.Success.Examples[0])

	a.Equal(api0.URL, "/users/login").
		Equal(api1.URL, "/users/login").
		Equal(api0.Group, "users").
		Equal(api1.Group, "users")

	if api0.Method == "POST" {
		a.Equal(api1.Method, "DELETE").
			Equal(1, len(api1.Request.Headers))
	} else {
		a.Equal(api0.Method, "DELETE").
			Equal(api1.Method, "POST").
			Equal(1, len(api0.Request.Headers))
	}
}

func TestRecursivePath(t *testing.T) {
	a := assert.New(t)

	opt := &Options{Dir: "./testdir", Recursive: false, Exts: []string{".c", ".h"}}
	paths, err := recursivePath(opt)
	a.NotError(err)
	a.Contains(paths, []string{
		filepath.Join("testdir", "testfile.c"),
		filepath.Join("testdir", "testfile.h"),
	})

	opt.Dir = "./testdir"
	opt.Recursive = true
	opt.Exts = []string{".1", ".2"}
	paths, err = recursivePath(opt)
	a.NotError(err)
	a.Contains(paths, []string{
		filepath.Join("testdir", "testdir1", "testfile.1"),
		filepath.Join("testdir", "testdir1", "testfile.2"),
		filepath.Join("testdir", "testdir2", "testfile.1"),
		filepath.Join("testdir", "testfile.1"),
	})

	opt.Dir = "./testdir/testdir1"
	opt.Recursive = true
	opt.Exts = []string{".1", ".2"}
	paths, err = recursivePath(opt)
	a.NotError(err)
	a.Contains(paths, []string{
		filepath.Join("testdir", "testdir1", "testfile.1"),
		filepath.Join("testdir", "testdir1", "testfile.2"),
	})

	opt.Dir = "./testdir"
	opt.Recursive = true
	opt.Exts = []string{".1"}
	paths, err = recursivePath(opt)
	a.NotError(err)
	a.Contains(paths, []string{
		filepath.Join("testdir", "testdir1", "testfile.1"),
		filepath.Join("testdir", "testdir2", "testfile.1"),
		filepath.Join("testdir", "testfile.1"),
	})
}

func TestOptions_Init(t *testing.T) {
	a := assert.New(t)

	o := &Options{Dir: "not exists"}
	a.Error(o.Init())

	o.Dir = "./"
	o.Lang = "not exists"
	a.Error(o.Init())

	// 未指定扩展名，则使用系统默认的
	o.Lang = "c++"
	a.NotError(o.Init())
	a.Equal(o.Exts, langExts["c++"])

	// 指定了 Exts，自动调整扩展名样式。
	o.Lang = "c++"
	o.Exts = []string{"c1", ".c2"}
	a.NotError(o.Init())
	a.Equal(o.Exts, []string{".c1", ".c2"})
}
<|endoftext|>


<commit_before>// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.

// This binary provides sample code for using the gopacket TCP assembler and TCP
// stream reader.  It reads packets off the wire and reconstructs HTTP requests
// it sees, logging them.
package main

import (
	"code.google.com/p/gopacket"
	"code.google.com/p/gopacket/layers"
	"code.google.com/p/gopacket/pcap"
	"fmt"
	"runtime"
	"time"
)

var processedIps map[string]int64

func main() {

	processedIps = make(map[string]int64)

	ticker := time.NewTicker(time.Second * 10)
	go func() {
		for _ = range ticker.C {
			fmt.Println(time.Now(), "Checking if any ip's should be cleaned from processedIps")
			fmt.Println("ProcessedIps contains", len(processedIps), "IP's")

			cleanThreshold := time.Now().Unix() - 10

			for k, v := range processedIps {
				if v < cleanThreshold {
					delete(processedIps, k)
				}
			}
			fmt.Println("ProcessedIps contains", len(processedIps), "IP's")

		}
	}()

	if handle, err := pcap.OpenLive("eth0", 128, true, 0); err != nil {
		panic(err)
	} else if err := handle.SetBPFFilter("tcp and ip and (port 80 or port 443 or port 21 or port 20)"); err != nil { // optional
		panic(err)
	} else {
		packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
		for packet := range packetSource.Packets() {
			p := packet
			if net := p.NetworkLayer(); net == nil {
				fmt.Println("No net layer found")
			} else {
				ip4 := net.(*layers.IPv4)
				tcp := p.TransportLayer().(*layers.TCP)
				fmt.Println(ip4.SrcIP.String(), tcp.SrcPort, "->", ip4.DstIP.String(), tcp.DstPort)

				srcIp := ip4.SrcIP.String()
				dstIp := ip4.SrcIP.String()
				curTime := time.Now().Unix()

				if _, ok := processedIps[srcIp]; ok {
					fmt.Println(srcIp, "is already in processedIps")
				} else {
					processedIps[ip4.SrcIP.String()] = curTime
				}

				if _, ok := processedIps[dstIp]; ok {
					fmt.Println(dstIp, "is already in destinationIps")
				} else {
					processedIps[dstIp] = curTime
				}

				//fmt.Println(processedIps)

				_ = &runtime.MemStats{}
				//            memStats := &runtime.MemStats{}
				//            runtime.ReadMemStats(memStats)
				//            fmt.Println("Length:", len(processedIps), runtime.NumGoroutine(), memStats.Alloc, memStats.TotalAlloc)
			}
		}
	}

}
<commit_msg>Removed pcap POC<commit_after><|endoftext|>


<commit_before>package main

import (
	"database/sql"							//used for unifrom database access
	"fmt"									//print statements
	"github.com/go-martini/martini"			//extra frame work build on net/http
	_ "github.com/lib/pq"					//go sql driver
	"net/http"								//framework
)

func SetupDB() *sql.DB {
	db, err := sql.Open("postgres", "host=192.168.50.131 user=postgres dbname=postgres sslmode=disable") 		//my only lib/pq usage? login into postgres database
	PanicIf(err)

	return db
}

func PanicIf(err error) {
	if err != nil {
		panic(err)
	}
}

func ShowDB(db *sql.DB, r *http.Request, rw http.ResponseWriter) { //localhost:3000/?search=name
		search := "%" + r.URL.Query().Get("search") + "%"
		rows, err := db.Query(`SELECT fname, LName, cost, city
                           FROM custauth
                           WHERE FName ILIKE $1
                           OR LName ILIKE $1
                           OR city ILIKE $1`, search)
		PanicIf(err)
		defer rows.Close()

		var FirstName, LastName, cost, city string
		for rows.Next() {
			err := rows.Scan(&FirstName, &LastName, &cost, &city)
			PanicIf(err)
			fmt.Fprintf(rw, "First Name: %s\nLast Name: %s\nCost: %s\nCity: %s\n\n", FirstName, LastName, cost, city)
		}
	}

func InsertPur(r *http.Request, db *sql.DB){
//	_, err := db.Query("INSERT INTO custauth (custid, fname, lname, city,state, country,email,cost,errorflag) VALUES ($1, $2, $3, $4, $5, $6, $7, $8,$9)",
	_, err := db.Exec("INSERT INTO custauth (custid, fname, lname, city,state, country,email,cost,errorflag) VALUES ($1, $2, $3, $4, $5, $6, $7, $8,$9)",
	r.FormValue("custid"),
		r.FormValue("fname"),
		r.FormValue("lname"),
		r.FormValue("city"),
		r.FormValue("state"),
		r.FormValue("country"),
		r.FormValue("email"),
		r.FormValue("cost"),
		r.FormValue("errorflag"))

	PanicIf(err)
}

func main() {
	m := martini.Classic()
	m.Map(SetupDB())
	m.Get("/", func() string {
    return "192.168.50.131 !"
  })
	m.Get("/show", ShowDB)
	m.Post("/add", InsertPur)
	m.Run()
}
<commit_msg>Using hardcodes Service IP not hard coded  Minion<commit_after>package main

import (
	"database/sql"							//used for unifrom database access
	"fmt"									//print statements
	"github.com/go-martini/martini"			//extra frame work build on net/http
	_ "github.com/lib/pq"					//go sql driver
	"net/http"								//framework
)

func SetupDB() *sql.DB {
	db, err := sql.Open("postgres", "host=10.254.76.103 user=postgres dbname=postgres sslmode=disable") 		//my only lib/pq usage? login into postgres database
	PanicIf(err)

	return db
}

func PanicIf(err error) {
	if err != nil {
		panic(err)
	}
}

func ShowDB(db *sql.DB, r *http.Request, rw http.ResponseWriter) { //localhost:3000/?search=name
		search := "%" + r.URL.Query().Get("search") + "%"
		rows, err := db.Query(`SELECT fname, LName, cost, city
                           FROM custauth
                           WHERE FName ILIKE $1
                           OR LName ILIKE $1
                           OR city ILIKE $1`, search)
		PanicIf(err)
		defer rows.Close()

		var FirstName, LastName, cost, city string
		for rows.Next() {
			err := rows.Scan(&FirstName, &LastName, &cost, &city)
			PanicIf(err)
			fmt.Fprintf(rw, "First Name: %s\nLast Name: %s\nCost: %s\nCity: %s\n\n", FirstName, LastName, cost, city)
		}
	}

func InsertPur(r *http.Request, db *sql.DB){
//	_, err := db.Query("INSERT INTO custauth (custid, fname, lname, city,state, country,email,cost,errorflag) VALUES ($1, $2, $3, $4, $5, $6, $7, $8,$9)",
	_, err := db.Exec("INSERT INTO custauth (custid, fname, lname, city,state, country,email,cost,errorflag) VALUES ($1, $2, $3, $4, $5, $6, $7, $8,$9)",
	r.FormValue("custid"),
		r.FormValue("fname"),
		r.FormValue("lname"),
		r.FormValue("city"),
		r.FormValue("state"),
		r.FormValue("country"),
		r.FormValue("email"),
		r.FormValue("cost"),
		r.FormValue("errorflag"))

	PanicIf(err)
}

func main() {
	m := martini.Classic()
	m.Map(SetupDB())
	m.Get("/", func() string {
    return "192.168.50.131 !"
  })
	m.Get("/show", ShowDB)
	m.Post("/add", InsertPur)
	m.Run()
}
<|endoftext|>


<commit_before>package kd

import (
	"crypto/tls"
	b64 "encoding/base64"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/edi-design/kd-go/kd/config"
	"github.com/gorilla/mux"
)

// struct of json configuration
type MainConfig struct {
	Service struct {
		Username string
		Password string
	}
}

var (
	Config           *config.Config
	verbose          = flag.Bool("v", false, "enable verbose mode to see more debug output.")
	noCheckCertParam = flag.Bool("no-check-certificate", false, "disable root CA check for HTTP requests")
	noCache          = flag.Bool("no-cache", false, "disables playlist caching")
)

// main
func Service(ObjConfig *config.Config) {
	// write config to environment vars
	Config = ObjConfig

	// check credentials
	signIn()

	// init router
	serv := mux.NewRouter()

	subroute := serv.PathPrefix("/").Subrouter()
	subroute.HandleFunc("/", channelHandler).Methods("GET")
	subroute.HandleFunc("/{quality}", channelHandler).Methods("GET")
	subroute.HandleFunc("/{quality}/{format}", channelHandler).Methods("GET")

	// not found handler. fallback if given path is not set up.
	subroute.HandleFunc("/{path:.*}", notFoundHandler)

	// start http-handle
	http.Handle("/", serv)

	fmt.Println("== Listening ...")
	printInterfaces()
	http.ListenAndServe(Config.Service.Listen, nil)
}

// Default route-handler if no configured endpoint matches.
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	path := params["path"]

	err := errors.New("use known subroutes")
	fmt.Println(err)
	fmt.Printf("path requested: %s:", path)

	w.WriteHeader(http.StatusNotFound)
}

// Handles the root directory requests.
func channelHandler(w http.ResponseWriter, r *http.Request) {
	// init vars
	var result config.ChannelList
	var data string

	// debug output
	fmt.Println("== Get channellist")

	// get params
	params := mux.Vars(r)
	format := params["format"]
	quality := params["quality"]

	cache_file, quality_playlist := getQualityInformations(quality)

	request_url := getUrl(config.METHOD_CHANNELLIST)
	body := "{\"initObj\":" + getInitObj() + "," + config.CHANNEL_OBJECT + "}"
	err := httpRequest("POST", request_url, body, &result)

	if err != nil {
		fmt.Printf("could not fetch: %v", err)
	}

	// read cache
	cache_stat, err_cache := os.Stat(cache_file)
	if err_cache == nil && (time.Now().Unix()-cache_stat.ModTime().Unix() <= config.CACHE_LIFETIME) {
		cached_data, _ := ioutil.ReadFile(cache_file)
		data = string(cached_data[:])
	} else {
		// call backend
		data = data + config.M3U_HEAD
		for _, channel := range result {
			link, err_link := getLicensedLink(channel.Files[0].FileID, channel.Files[0].URL, quality_playlist)
			if err_link != nil {
				fmt.Println(err_link.Error())
				data = "This works only if you are using a KabelDeutschland Internet connection.\n" + err_link.Error()
				break
			}
			data = data + fmt.Sprintf(config.M3U_LINE, channel.MediaName, link)
		}

		// write cache file
		if !*noCache {
			ioutil.WriteFile(cache_file, []byte(data), 0644)
		}
	}

	// set header
	if format == "txt" {
		w.Header().Set("Content-Type", "text/plain")
	} else {
		w.Header().Set("Content-Type", "application/vnd.apple.mpegurl")
	}

	w.Header().Set("Status", "200 OK")
	w.Header().Set("Content-Disposition", "inline; filename=\"playlist.m3u\"")
	w.Header().Set("Cache-Control", "no-cache, must-revalidate")
	w.Header().Set("Expies", "Sat, 26 Jul 1997 05:00:00 GMT")

	w.Write([]byte(data))
}

// get playlist according to requested quality
func getQualityInformations(quality string) (string, string) {
	var (
		quality_file     string
		quality_playlist string
	)

	switch quality {
	case "low":
		quality_file = fmt.Sprintf(config.CACHE_FILE, quality)
		quality_playlist = config.QUALITY_LOW
	case "high":
		quality_file = fmt.Sprintf(config.CACHE_FILE, quality)
		quality_playlist = config.QUALITY_HIGH
	default:
		quality_file = fmt.Sprintf(config.CACHE_FILE, "medium")
		quality_playlist = config.QUALITY_MEDIUM
	}

	return quality_file, quality_playlist
}

// request a link with a valid session
func getLicensedLink(id string, link string, playlist string) (string, error) {
	var result config.LicensedLink

	request_url := getUrl(config.METHOD_LICENSED_LINK)
	body := "{\"initObj\":" + getInitObj() + ",\"mediaFileId\":" + id + ",\"baseLink\":\"" + string(link[:]) + "\"}"
	err := httpRequest("POST", request_url, body, &result)

	if err != nil {
		fmt.Printf("could not fetch: %v", err)
		return "", errors.New("no link")
	}

	resp, err_get := http.Get(result.MainUrl)

	if err_get != nil {
		return "", err_get
	}

	url := resp.Request.URL.String()
	i := strings.LastIndex(url, "/")

	url = url[:i] + "/" + playlist

	return url, nil
}

// concats params to return a valid API url
func getUrl(method string) string {
	return fmt.Sprintf("%s?m=%s&iOSv=%s&Appv=%s", config.GATEWAY, method, config.IOS_VERSION, config.APP_VERSION)
}

// check credentials
func signIn() {
	fmt.Println("== Checking credentials")

	var result config.SignIn

	request_url := getUrl(config.METHOD_SIGNIN)

	body :=
		"{\"initObj\":" +
			getInitObj() +
			",\"userName\":\"" + Config.Service.Username + "\"" +
			",\"password\":\"" + Config.Service.Password + "\"" +
			",\"providerID\":0" +
			"}"

	handleError(fmt.Sprint(body))
	err := httpRequest("POST", request_url, body, &result)

	switch {
	case err != nil, result.LoginStatus != 0:
		handleError(fmt.Sprint("Returned result: %v", result))
		fmt.Println("Credentials are wrong")
		os.Exit(1)
	}

	fmt.Println("done")
}

// print interfaces to know where the proxy is listening
func printInterfaces() {
	addrs, err := net.InterfaceAddrs()

	if err != nil {
		fmt.Println("Can't get interfaces. You have to have at least one network connection.")
		log.Fatal("No interface found")
	}

	for _, addr := range addrs {

		var ip net.IP
		switch v := addr.(type) {
		case *net.IPAddr:
		case *net.IPNet:
			ip = v.IP
		}

		if ip == nil || ip.IsLoopback() {
			continue
		}

		ip = ip.To4()
		if ip == nil {
			continue // not an ipv4 address
		}
		fmt.Println("http://" + ip.String() + Config.Service.Listen)
	}
}

// main helper to call any http request.
func httpRequest(method string, url string, body string, result interface{}) error {
	var (
		req *http.Request
		err error
	)

	// init client, skip cert check, because of some problems with env without root-ca
	tr := &http.Transport{}
	if *noCheckCertParam {
		tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
		handleError("= certificate check disabled")
	}
	client := &http.Client{Transport: tr}

	switch method {
	case "GET":
		req, err = http.NewRequest(method, url, nil)
	case "POST":
		req, err = http.NewRequest(method, url, strings.NewReader(body))
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
	default:
		return errors.New(method + " is not a valid method.")
	}

	if err != nil {
		handleError(fmt.Sprintf("could not stat request: %v", err))
		return err
	}

	resp, err := client.Do(req)
	if err != nil {
		handleError(fmt.Sprintf("could not fetch: %v", err))
		return err
	}

	decoder := json.NewDecoder(resp.Body)
	err = decoder.Decode(&result)
	if err != nil {
		handleError(fmt.Sprintf("could not decode response: %v", err))
	}

	return err
}

// handle verbose mode otuput
func handleError(message string) {
	if *verbose {
		fmt.Println(message)
	}
}

// init obj
func getInitObj() string {
	initObject, _ := b64.StdEncoding.DecodeString(config.INIT_OBJECT)
	return string(initObject)
}
<commit_msg>add TODO<commit_after>package kd

import (
	"crypto/tls"
	b64 "encoding/base64"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/edi-design/kd-go/kd/config"
	"github.com/gorilla/mux"
)

// struct of json configuration
type MainConfig struct {
	Service struct {
		Username string
		Password string
	}
}

var (
	Config           *config.Config
	verbose          = flag.Bool("v", false, "enable verbose mode to see more debug output.")
	noCheckCertParam = flag.Bool("no-check-certificate", false, "disable root CA check for HTTP requests")
	noCache          = flag.Bool("no-cache", false, "disables playlist caching")
)

// main
func Service(ObjConfig *config.Config) {
	// write config to environment vars
	Config = ObjConfig

	// check credentials
	signIn()

	// init router
	serv := mux.NewRouter()

	subroute := serv.PathPrefix("/").Subrouter()
	subroute.HandleFunc("/", channelHandler).Methods("GET")
	subroute.HandleFunc("/{quality}", channelHandler).Methods("GET")
	subroute.HandleFunc("/{quality}/{format}", channelHandler).Methods("GET")

	// not found handler. fallback if given path is not set up.
	subroute.HandleFunc("/{path:.*}", notFoundHandler)

	// start http-handle
	http.Handle("/", serv)

	fmt.Println("== Listening ...")
	printInterfaces()
	http.ListenAndServe(Config.Service.Listen, nil)
}

// Default route-handler if no configured endpoint matches.
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
	params := mux.Vars(r)
	path := params["path"]

	err := errors.New("use known subroutes")
	fmt.Println(err)
	fmt.Printf("path requested: %s:", path)

	w.WriteHeader(http.StatusNotFound)
}

// Handles the root directory requests.
func channelHandler(w http.ResponseWriter, r *http.Request) {
	// init vars
	var result config.ChannelList
	var data string

	// debug output
	fmt.Println("== Get channellist")

	// get params
	params := mux.Vars(r)
	format := params["format"]
	quality := params["quality"]

	cache_file, quality_playlist := getQualityInformations(quality)

	request_url := getUrl(config.METHOD_CHANNELLIST)
	body := "{\"initObj\":" + getInitObj() + "," + config.CHANNEL_OBJECT + "}"
	err := httpRequest("POST", request_url, body, &result)

	if err != nil {
		fmt.Printf("could not fetch: %v", err)
	}

	// read cache
	cache_stat, err_cache := os.Stat(cache_file)
	if err_cache == nil && (time.Now().Unix()-cache_stat.ModTime().Unix() <= config.CACHE_LIFETIME) {
		cached_data, _ := ioutil.ReadFile(cache_file)
		data = string(cached_data[:])
	} else {
		// call backend
		data = data + config.M3U_HEAD
		for _, channel := range result {
			link, err_link := getLicensedLink(channel.Files[0].FileID, channel.Files[0].URL, quality_playlist)
			if err_link != nil {
				fmt.Println(err_link.Error())
				data = "This works only if you are using a KabelDeutschland Internet connection.\n" + err_link.Error()
				break
			}
			data = data + fmt.Sprintf(config.M3U_LINE, channel.MediaName, link)
		}

		// write cache file
		if !*noCache {
			ioutil.WriteFile(cache_file, []byte(data), 0644)
		}
	}

	// set header
	if format == "txt" {
		w.Header().Set("Content-Type", "text/plain")
	} else {
		w.Header().Set("Content-Type", "application/vnd.apple.mpegurl")
	}

	w.Header().Set("Status", "200 OK")
	w.Header().Set("Content-Disposition", "inline; filename=\"playlist.m3u\"")
	w.Header().Set("Cache-Control", "no-cache, must-revalidate")
	w.Header().Set("Expies", "Sat, 26 Jul 1997 05:00:00 GMT")

	w.Write([]byte(data))
}

// get playlist according to requested quality
func getQualityInformations(quality string) (string, string) {
	var (
		quality_file     string
		quality_playlist string
	)

	switch quality {
	case "low":
		quality_file = fmt.Sprintf(config.CACHE_FILE, quality)
		quality_playlist = config.QUALITY_LOW
	case "high":
		quality_file = fmt.Sprintf(config.CACHE_FILE, quality)
		quality_playlist = config.QUALITY_HIGH
	default:
		quality_file = fmt.Sprintf(config.CACHE_FILE, "medium")
		quality_playlist = config.QUALITY_MEDIUM
	}

	return quality_file, quality_playlist
}

// request a link with a valid session
func getLicensedLink(id string, link string, playlist string) (string, error) {
	var result config.LicensedLink

	request_url := getUrl(config.METHOD_LICENSED_LINK)
	body := "{\"initObj\":" + getInitObj() + ",\"mediaFileId\":" + id + ",\"baseLink\":\"" + string(link[:]) + "\"}"
	err := httpRequest("POST", request_url, body, &result)

	if err != nil {
		fmt.Printf("could not fetch: %v", err)
		return "", errors.New("no link")
	}

	resp, err_get := http.Get(result.MainUrl)

	if err_get != nil {
		return "", err_get
	}

	url := resp.Request.URL.String()
	i := strings.LastIndex(url, "/")

	url = url[:i] + "/" + playlist

	return url, nil
}

// concats params to return a valid API url
func getUrl(method string) string {
	return fmt.Sprintf("%s?m=%s&iOSv=%s&Appv=%s", config.GATEWAY, method, config.IOS_VERSION, config.APP_VERSION)
}

// check credentials
func signIn() {
	fmt.Println("== Checking credentials")

	var result config.SignIn

	request_url := getUrl(config.METHOD_SIGNIN)

	// TODO use json.Marshal
	body :=
		"{\"initObj\":" +
			getInitObj() +
			",\"userName\":\"" + Config.Service.Username + "\"" +
			",\"password\":\"" + Config.Service.Password + "\"" +
			",\"providerID\":0" +
			"}"

	handleError(fmt.Sprint(body))
	err := httpRequest("POST", request_url, body, &result)

	switch {
	case err != nil, result.LoginStatus != 0:
		handleError(fmt.Sprint("Returned result: %v", result))
		fmt.Println("Credentials are wrong")
		os.Exit(1)
	}

	fmt.Println("done")
}

// print interfaces to know where the proxy is listening
func printInterfaces() {
	addrs, err := net.InterfaceAddrs()

	if err != nil {
		fmt.Println("Can't get interfaces. You have to have at least one network connection.")
		log.Fatal("No interface found")
	}

	for _, addr := range addrs {

		var ip net.IP
		switch v := addr.(type) {
		case *net.IPAddr:
		case *net.IPNet:
			ip = v.IP
		}

		if ip == nil || ip.IsLoopback() {
			continue
		}

		ip = ip.To4()
		if ip == nil {
			continue // not an ipv4 address
		}
		fmt.Println("http://" + ip.String() + Config.Service.Listen)
	}
}

// main helper to call any http request.
func httpRequest(method string, url string, body string, result interface{}) error {
	var (
		req *http.Request
		err error
	)

	// init client, skip cert check, because of some problems with env without root-ca
	tr := &http.Transport{}
	if *noCheckCertParam {
		tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
		handleError("= certificate check disabled")
	}
	client := &http.Client{Transport: tr}

	switch method {
	case "GET":
		req, err = http.NewRequest(method, url, nil)
	case "POST":
		req, err = http.NewRequest(method, url, strings.NewReader(body))
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
	default:
		return errors.New(method + " is not a valid method.")
	}

	if err != nil {
		handleError(fmt.Sprintf("could not stat request: %v", err))
		return err
	}

	resp, err := client.Do(req)
	if err != nil {
		handleError(fmt.Sprintf("could not fetch: %v", err))
		return err
	}

	decoder := json.NewDecoder(resp.Body)
	err = decoder.Decode(&result)
	if err != nil {
		handleError(fmt.Sprintf("could not decode response: %v", err))
	}

	return err
}

// handle verbose mode otuput
func handleError(message string) {
	if *verbose {
		fmt.Println(message)
	}
}

// init obj
func getInitObj() string {
	initObject, _ := b64.StdEncoding.DecodeString(config.INIT_OBJECT)
	return string(initObject)
}
<|endoftext|>


<commit_before>/*
Copyright 2020 The Knative 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 config

import (
	"context"
	"io/ioutil"
	"time"

	"golang.org/x/oauth2/google"
	"google.golang.org/api/dns/v1"
	"google.golang.org/api/option"
	"k8s.io/apimachinery/pkg/util/wait"
)

type EnvConfig struct {
	FullHostName                  string `envconfig:"full_host_name" required:"true"`
	DomainName                    string `envconfig:"domain_name" required:"true"`
	DNSZone                       string `envconfig:"dns_zone" required:"true"`
	CloudDNSServiceAccountKeyFile string `envconfig:"cloud_dns_service_account_key_file" required:"true"`
	CloudDNSProject               string `envconfig:"cloud_dns_project" required:"true"`
	IngressIP                     string `envconfig:"ingress_ip" required:"true"`
}

type DNSRecord struct {
	IP     string
	Domain string
}

func MakeRecordSet(record *DNSRecord) *dns.ResourceRecordSet {
	dnsName := record.Domain + "."
	return &dns.ResourceRecordSet{
		Name:    dnsName,
		Rrdatas: []string{record.IP},
		// Setting TTL of DNS record to 5 seconds to make DNS become effective more quickly.
		Ttl:  int64(5),
		Type: "A",
	}
}

// DeleteDNSRecord deletes the given DNS record.
func DeleteDNSRecord(record *DNSRecord, svcAccountKeyFile, dnsProject, dnsZone string) error {
	rec := MakeRecordSet(record)
	svc, err := GetCloudDNSSvc(svcAccountKeyFile)
	if err != nil {
		return err
	}
	deletion := &dns.Change{
		Deletions: []*dns.ResourceRecordSet{rec},
	}
	return ChangeDNSRecord(deletion, svc, dnsProject, dnsZone)
}

// ChangeDNSRecord changes the given DNS record.
func ChangeDNSRecord(change *dns.Change, svc *dns.Service, dnsProject, dnsZone string) error {
	// Wait for change to be acknowledged.
	return wait.PollImmediate(time.Second, 5*time.Minute, func() (bool, error) {
		chg, err := svc.Changes.Create(dnsProject, dnsZone, change).Do()
		if err != nil {
			return false, err
		}
		return chg.Status != "pending", nil
	})
}

// GetCloudDNSSvc returns the Cloud DNS Service stub.
// reference: https://github.com/jetstack/cert-manager/blob/master/pkg/issuer/acme/dns/clouddns/clouddns.go
func GetCloudDNSSvc(svcAccountKeyFile string) (*dns.Service, error) {
	data, err := ioutil.ReadFile(svcAccountKeyFile)
	if err != nil {
		return nil, err
	}
	conf, err := google.JWTConfigFromJSON(data, dns.NdevClouddnsReadwriteScope)
	if err != nil {
		return nil, err
	}
	ctx := context.Background()
	return dns.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))
}
<commit_msg>fix the bug to create dns record (#7816)<commit_after>/*
Copyright 2020 The Knative 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 config

import (
	"context"
	"io/ioutil"
	"time"

	"golang.org/x/oauth2/google"
	"google.golang.org/api/dns/v1"
	"google.golang.org/api/option"
	"k8s.io/apimachinery/pkg/util/wait"
)

type EnvConfig struct {
	FullHostName                  string `envconfig:"full_host_name" required:"true"`
	DomainName                    string `envconfig:"domain_name" required:"true"`
	DNSZone                       string `envconfig:"dns_zone" required:"true"`
	CloudDNSServiceAccountKeyFile string `envconfig:"cloud_dns_service_account_key_file" required:"true"`
	CloudDNSProject               string `envconfig:"cloud_dns_project" required:"true"`
	IngressIP                     string `envconfig:"ingress_ip" required:"true"`
}

type DNSRecord struct {
	IP     string
	Domain string
}

func MakeRecordSet(record *DNSRecord) *dns.ResourceRecordSet {
	dnsName := record.Domain + "."
	return &dns.ResourceRecordSet{
		Name:    dnsName,
		Rrdatas: []string{record.IP},
		// Setting TTL of DNS record to 5 seconds to make DNS become effective more quickly.
		Ttl:  int64(5),
		Type: "A",
	}
}

// DeleteDNSRecord deletes the given DNS record.
func DeleteDNSRecord(record *DNSRecord, svcAccountKeyFile, dnsProject, dnsZone string) error {
	rec := MakeRecordSet(record)
	svc, err := GetCloudDNSSvc(svcAccountKeyFile)
	if err != nil {
		return err
	}
	deletion := &dns.Change{
		Deletions: []*dns.ResourceRecordSet{rec},
	}
	return ChangeDNSRecord(deletion, svc, dnsProject, dnsZone)
}

// ChangeDNSRecord changes the given DNS record.
func ChangeDNSRecord(change *dns.Change, svc *dns.Service, dnsProject, dnsZone string) error {
	chg, err := svc.Changes.Create(dnsProject, dnsZone, change).Do()
	if err != nil {
		return err
	}
	// Wait for change to be acknowledged.
	return wait.PollImmediate(time.Second, 5*time.Minute, func() (bool, error) {
		tmp, err := svc.Changes.Get(dnsProject, dnsZone, chg.Id).Do()
		if err != nil {
			return false, err
		}
		return tmp.Status != "pending", nil
	})
}

// GetCloudDNSSvc returns the Cloud DNS Service stub.
// reference: https://github.com/jetstack/cert-manager/blob/master/pkg/issuer/acme/dns/clouddns/clouddns.go
func GetCloudDNSSvc(svcAccountKeyFile string) (*dns.Service, error) {
	data, err := ioutil.ReadFile(svcAccountKeyFile)
	if err != nil {
		return nil, err
	}
	conf, err := google.JWTConfigFromJSON(data, dns.NdevClouddnsReadwriteScope)
	if err != nil {
		return nil, err
	}
	ctx := context.Background()
	return dns.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))
}
<|endoftext|>


<commit_before>/*
Copyright 2014 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 e2e

import (
	"strconv"
	"time"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/intstr"
	"k8s.io/apimachinery/pkg/util/uuid"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/apimachinery/pkg/watch"
	clientv1 "k8s.io/client-go/pkg/api/v1"
	"k8s.io/kubernetes/pkg/api/v1"
	batchv1 "k8s.io/kubernetes/pkg/apis/batch/v1"
	batchv2alpha1 "k8s.io/kubernetes/pkg/apis/batch/v2alpha1"
	"k8s.io/kubernetes/test/e2e/framework"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

func stagingClientPod(name, value string) clientv1.Pod {
	return clientv1.Pod{
		ObjectMeta: metav1.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"name": "foo",
				"time": value,
			},
		},
		Spec: clientv1.PodSpec{
			Containers: []clientv1.Container{
				{
					Name:  "nginx",
					Image: "gcr.io/google_containers/nginx-slim:0.7",
					Ports: []clientv1.ContainerPort{{ContainerPort: 80}},
				},
			},
		},
	}
}

func testingPod(name, value string) v1.Pod {
	return v1.Pod{
		ObjectMeta: metav1.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"name": "foo",
				"time": value,
			},
		},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:  "nginx",
					Image: "gcr.io/google_containers/nginx-slim:0.7",
					Ports: []v1.ContainerPort{{ContainerPort: 80}},
					LivenessProbe: &v1.Probe{
						Handler: v1.Handler{
							HTTPGet: &v1.HTTPGetAction{
								Path: "/index.html",
								Port: intstr.FromInt(8080),
							},
						},
						InitialDelaySeconds: 30,
					},
				},
			},
		},
	}
}

func observeCreation(w watch.Interface) {
	select {
	case event, _ := <-w.ResultChan():
		if event.Type != watch.Added {
			framework.Failf("Failed to observe the creation: %v", event)
		}
	case <-time.After(30 * time.Second):
		framework.Failf("Timeout while waiting for observing the creation")
	}
}

func observeObjectDeletion(w watch.Interface) (obj runtime.Object) {
	// output to give us a duration to failure.  Maybe we aren't getting the
	// full timeout for some reason.  My guess would be watch failure
	framework.Logf("Starting to observe pod deletion")
	deleted := false
	timeout := false
	timer := time.After(framework.DefaultPodDeletionTimeout)
	for !deleted && !timeout {
		select {
		case event, normal := <-w.ResultChan():
			if !normal {
				framework.Failf("The channel was closed unexpectedly")
				return
			}
			if event.Type == watch.Deleted {
				obj = event.Object
				deleted = true
			}
		case <-timer:
			timeout = true
		}
	}
	if !deleted {
		framework.Failf("Failed to observe pod deletion")
	}
	return
}

func observerUpdate(w watch.Interface, expectedUpdate func(runtime.Object) bool) {
	timer := time.After(30 * time.Second)
	updated := false
	timeout := false
	for !updated && !timeout {
		select {
		case event, _ := <-w.ResultChan():
			if event.Type == watch.Modified {
				if expectedUpdate(event.Object) {
					updated = true
				}
			}
		case <-timer:
			timeout = true
		}
	}
	if !updated {
		framework.Failf("Failed to observe pod update")
	}
	return
}

var _ = framework.KubeDescribe("Generated release_1_5 clientset", func() {
	f := framework.NewDefaultFramework("clientset")
	It("should create pods, set the deletionTimestamp and deletionGracePeriodSeconds of the pod", func() {
		podClient := f.ClientSet.Core().Pods(f.Namespace.Name)
		By("constructing the pod")
		name := "pod" + string(uuid.NewUUID())
		value := strconv.Itoa(time.Now().Nanosecond())
		podCopy := testingPod(name, value)
		pod := &podCopy
		By("setting up watch")
		selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})).String()
		options := metav1.ListOptions{LabelSelector: selector}
		pods, err := podClient.List(options)
		if err != nil {
			framework.Failf("Failed to query for pods: %v", err)
		}
		Expect(len(pods.Items)).To(Equal(0))
		options = metav1.ListOptions{
			LabelSelector:   selector,
			ResourceVersion: pods.ListMeta.ResourceVersion,
		}
		w, err := podClient.Watch(options)
		if err != nil {
			framework.Failf("Failed to set up watch: %v", err)
		}

		By("creating the pod")
		pod, err = podClient.Create(pod)
		if err != nil {
			framework.Failf("Failed to create pod: %v", err)
		}

		By("verifying the pod is in kubernetes")
		options = metav1.ListOptions{
			LabelSelector:   selector,
			ResourceVersion: pod.ResourceVersion,
		}
		pods, err = podClient.List(options)
		if err != nil {
			framework.Failf("Failed to query for pods: %v", err)
		}
		Expect(len(pods.Items)).To(Equal(1))

		By("verifying pod creation was observed")
		observeCreation(w)

		// We need to wait for the pod to be scheduled, otherwise the deletion
		// will be carried out immediately rather than gracefully.
		framework.ExpectNoError(f.WaitForPodRunning(pod.Name))

		By("deleting the pod gracefully")
		gracePeriod := int64(31)
		if err := podClient.Delete(pod.Name, metav1.NewDeleteOptions(gracePeriod)); err != nil {
			framework.Failf("Failed to delete pod: %v", err)
		}

		By("verifying the deletionTimestamp and deletionGracePeriodSeconds of the pod is set")
		observerUpdate(w, func(obj runtime.Object) bool {
			pod := obj.(*v1.Pod)
			return pod.ObjectMeta.DeletionTimestamp != nil && *pod.ObjectMeta.DeletionGracePeriodSeconds == gracePeriod
		})
	})
})

func newTestingCronJob(name string, value string) *batchv2alpha1.CronJob {
	parallelism := int32(1)
	completions := int32(1)
	return &batchv2alpha1.CronJob{
		ObjectMeta: metav1.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"time": value,
			},
		},
		Spec: batchv2alpha1.CronJobSpec{
			Schedule:          "*/1 * * * ?",
			ConcurrencyPolicy: batchv2alpha1.AllowConcurrent,
			JobTemplate: batchv2alpha1.JobTemplateSpec{
				Spec: batchv1.JobSpec{
					Parallelism: &parallelism,
					Completions: &completions,
					Template: v1.PodTemplateSpec{
						Spec: v1.PodSpec{
							RestartPolicy: v1.RestartPolicyOnFailure,
							Volumes: []v1.Volume{
								{
									Name: "data",
									VolumeSource: v1.VolumeSource{
										EmptyDir: &v1.EmptyDirVolumeSource{},
									},
								},
							},
							Containers: []v1.Container{
								{
									Name:  "c",
									Image: "gcr.io/google_containers/busybox:1.24",
									VolumeMounts: []v1.VolumeMount{
										{
											MountPath: "/data",
											Name:      "data",
										},
									},
								},
							},
						},
					},
				},
			},
		},
	}
}

var _ = framework.KubeDescribe("Generated release_1_5 clientset", func() {
	f := framework.NewDefaultFramework("clientset")
	It("should create v2alpha1 cronJobs, delete cronJobs, watch cronJobs", func() {
		var enabled bool
		groupList, err := f.ClientSet.Discovery().ServerGroups()
		framework.ExpectNoError(err)
		for _, group := range groupList.Groups {
			if group.Name == batchv2alpha1.GroupName {
				for _, version := range group.Versions {
					if version.Version == batchv2alpha1.SchemeGroupVersion.Version {
						enabled = true
						break
					}
				}
			}
		}
		if !enabled {
			framework.Logf("%s is not enabled, test skipped", batchv2alpha1.SchemeGroupVersion)
			return
		}
		cronJobClient := f.ClientSet.BatchV2alpha1().CronJobs(f.Namespace.Name)
		By("constructing the cronJob")
		name := "cronjob" + string(uuid.NewUUID())
		value := strconv.Itoa(time.Now().Nanosecond())
		cronJob := newTestingCronJob(name, value)
		By("setting up watch")
		selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})).String()
		options := metav1.ListOptions{LabelSelector: selector}
		cronJobs, err := cronJobClient.List(options)
		if err != nil {
			framework.Failf("Failed to query for cronJobs: %v", err)
		}
		Expect(len(cronJobs.Items)).To(Equal(0))
		options = metav1.ListOptions{
			LabelSelector:   selector,
			ResourceVersion: cronJobs.ListMeta.ResourceVersion,
		}
		w, err := cronJobClient.Watch(options)
		if err != nil {
			framework.Failf("Failed to set up watch: %v", err)
		}

		By("creating the cronJob")
		cronJob, err = cronJobClient.Create(cronJob)
		if err != nil {
			framework.Failf("Failed to create cronJob: %v", err)
		}

		By("verifying the cronJob is in kubernetes")
		options = metav1.ListOptions{
			LabelSelector:   selector,
			ResourceVersion: cronJob.ResourceVersion,
		}
		cronJobs, err = cronJobClient.List(options)
		if err != nil {
			framework.Failf("Failed to query for cronJobs: %v", err)
		}
		Expect(len(cronJobs.Items)).To(Equal(1))

		By("verifying cronJob creation was observed")
		observeCreation(w)

		By("deleting the cronJob")
		if err := cronJobClient.Delete(cronJob.Name, nil); err != nil {
			framework.Failf("Failed to delete cronJob: %v", err)
		}

		options = metav1.ListOptions{LabelSelector: selector}
		cronJobs, err = cronJobClient.List(options)
		if err != nil {
			framework.Failf("Failed to list cronJobs to verify deletion: %v", err)
		}
		Expect(len(cronJobs.Items)).To(Equal(0))
	})
})

var _ = framework.KubeDescribe("Staging client repo client", func() {
	f := framework.NewDefaultFramework("clientset")
	It("should create pods, delete pods, watch pods", func() {
		podClient := f.StagingClient.Core().Pods(f.Namespace.Name)
		By("constructing the pod")
		name := "pod" + string(uuid.NewUUID())
		value := strconv.Itoa(time.Now().Nanosecond())
		podCopy := stagingClientPod(name, value)
		pod := &podCopy
		By("verifying no pod exists before the test")
		pods, err := podClient.List(metav1.ListOptions{})
		if err != nil {
			framework.Failf("Failed to query for pods: %v", err)
		}
		Expect(len(pods.Items)).To(Equal(0))
		By("creating the pod")
		pod, err = podClient.Create(pod)
		if err != nil {
			framework.Failf("Failed to create pod: %v", err)
		}

		By("verifying the pod is in kubernetes")
		timeout := 1 * time.Minute
		if err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
			pods, err = podClient.List(metav1.ListOptions{})
			if err != nil {
				return false, err
			}
			if len(pods.Items) == 1 {
				return true, nil
			}
			return false, nil
		}); err != nil {
			framework.Failf("Err : %s\n. Failed to wait for 1 pod to be created", err)
		}
	})
})
<commit_msg>CronJob: Use synchronous deletion for CronJobs in e2e.<commit_after>/*
Copyright 2014 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 e2e

import (
	"strconv"
	"time"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/labels"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/util/intstr"
	"k8s.io/apimachinery/pkg/util/uuid"
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/apimachinery/pkg/watch"
	clientv1 "k8s.io/client-go/pkg/api/v1"
	"k8s.io/kubernetes/pkg/api/v1"
	batchv1 "k8s.io/kubernetes/pkg/apis/batch/v1"
	batchv2alpha1 "k8s.io/kubernetes/pkg/apis/batch/v2alpha1"
	"k8s.io/kubernetes/test/e2e/framework"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

func stagingClientPod(name, value string) clientv1.Pod {
	return clientv1.Pod{
		ObjectMeta: metav1.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"name": "foo",
				"time": value,
			},
		},
		Spec: clientv1.PodSpec{
			Containers: []clientv1.Container{
				{
					Name:  "nginx",
					Image: "gcr.io/google_containers/nginx-slim:0.7",
					Ports: []clientv1.ContainerPort{{ContainerPort: 80}},
				},
			},
		},
	}
}

func testingPod(name, value string) v1.Pod {
	return v1.Pod{
		ObjectMeta: metav1.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"name": "foo",
				"time": value,
			},
		},
		Spec: v1.PodSpec{
			Containers: []v1.Container{
				{
					Name:  "nginx",
					Image: "gcr.io/google_containers/nginx-slim:0.7",
					Ports: []v1.ContainerPort{{ContainerPort: 80}},
					LivenessProbe: &v1.Probe{
						Handler: v1.Handler{
							HTTPGet: &v1.HTTPGetAction{
								Path: "/index.html",
								Port: intstr.FromInt(8080),
							},
						},
						InitialDelaySeconds: 30,
					},
				},
			},
		},
	}
}

func observeCreation(w watch.Interface) {
	select {
	case event, _ := <-w.ResultChan():
		if event.Type != watch.Added {
			framework.Failf("Failed to observe the creation: %v", event)
		}
	case <-time.After(30 * time.Second):
		framework.Failf("Timeout while waiting for observing the creation")
	}
}

func observeObjectDeletion(w watch.Interface) (obj runtime.Object) {
	// output to give us a duration to failure.  Maybe we aren't getting the
	// full timeout for some reason.  My guess would be watch failure
	framework.Logf("Starting to observe pod deletion")
	deleted := false
	timeout := false
	timer := time.After(framework.DefaultPodDeletionTimeout)
	for !deleted && !timeout {
		select {
		case event, normal := <-w.ResultChan():
			if !normal {
				framework.Failf("The channel was closed unexpectedly")
				return
			}
			if event.Type == watch.Deleted {
				obj = event.Object
				deleted = true
			}
		case <-timer:
			timeout = true
		}
	}
	if !deleted {
		framework.Failf("Failed to observe pod deletion")
	}
	return
}

func observerUpdate(w watch.Interface, expectedUpdate func(runtime.Object) bool) {
	timer := time.After(30 * time.Second)
	updated := false
	timeout := false
	for !updated && !timeout {
		select {
		case event, _ := <-w.ResultChan():
			if event.Type == watch.Modified {
				if expectedUpdate(event.Object) {
					updated = true
				}
			}
		case <-timer:
			timeout = true
		}
	}
	if !updated {
		framework.Failf("Failed to observe pod update")
	}
	return
}

var _ = framework.KubeDescribe("Generated release_1_5 clientset", func() {
	f := framework.NewDefaultFramework("clientset")
	It("should create pods, set the deletionTimestamp and deletionGracePeriodSeconds of the pod", func() {
		podClient := f.ClientSet.Core().Pods(f.Namespace.Name)
		By("constructing the pod")
		name := "pod" + string(uuid.NewUUID())
		value := strconv.Itoa(time.Now().Nanosecond())
		podCopy := testingPod(name, value)
		pod := &podCopy
		By("setting up watch")
		selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})).String()
		options := metav1.ListOptions{LabelSelector: selector}
		pods, err := podClient.List(options)
		if err != nil {
			framework.Failf("Failed to query for pods: %v", err)
		}
		Expect(len(pods.Items)).To(Equal(0))
		options = metav1.ListOptions{
			LabelSelector:   selector,
			ResourceVersion: pods.ListMeta.ResourceVersion,
		}
		w, err := podClient.Watch(options)
		if err != nil {
			framework.Failf("Failed to set up watch: %v", err)
		}

		By("creating the pod")
		pod, err = podClient.Create(pod)
		if err != nil {
			framework.Failf("Failed to create pod: %v", err)
		}

		By("verifying the pod is in kubernetes")
		options = metav1.ListOptions{
			LabelSelector:   selector,
			ResourceVersion: pod.ResourceVersion,
		}
		pods, err = podClient.List(options)
		if err != nil {
			framework.Failf("Failed to query for pods: %v", err)
		}
		Expect(len(pods.Items)).To(Equal(1))

		By("verifying pod creation was observed")
		observeCreation(w)

		// We need to wait for the pod to be scheduled, otherwise the deletion
		// will be carried out immediately rather than gracefully.
		framework.ExpectNoError(f.WaitForPodRunning(pod.Name))

		By("deleting the pod gracefully")
		gracePeriod := int64(31)
		if err := podClient.Delete(pod.Name, metav1.NewDeleteOptions(gracePeriod)); err != nil {
			framework.Failf("Failed to delete pod: %v", err)
		}

		By("verifying the deletionTimestamp and deletionGracePeriodSeconds of the pod is set")
		observerUpdate(w, func(obj runtime.Object) bool {
			pod := obj.(*v1.Pod)
			return pod.ObjectMeta.DeletionTimestamp != nil && *pod.ObjectMeta.DeletionGracePeriodSeconds == gracePeriod
		})
	})
})

func newTestingCronJob(name string, value string) *batchv2alpha1.CronJob {
	parallelism := int32(1)
	completions := int32(1)
	return &batchv2alpha1.CronJob{
		ObjectMeta: metav1.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"time": value,
			},
		},
		Spec: batchv2alpha1.CronJobSpec{
			Schedule:          "*/1 * * * ?",
			ConcurrencyPolicy: batchv2alpha1.AllowConcurrent,
			JobTemplate: batchv2alpha1.JobTemplateSpec{
				Spec: batchv1.JobSpec{
					Parallelism: &parallelism,
					Completions: &completions,
					Template: v1.PodTemplateSpec{
						Spec: v1.PodSpec{
							RestartPolicy: v1.RestartPolicyOnFailure,
							Volumes: []v1.Volume{
								{
									Name: "data",
									VolumeSource: v1.VolumeSource{
										EmptyDir: &v1.EmptyDirVolumeSource{},
									},
								},
							},
							Containers: []v1.Container{
								{
									Name:  "c",
									Image: "gcr.io/google_containers/busybox:1.24",
									VolumeMounts: []v1.VolumeMount{
										{
											MountPath: "/data",
											Name:      "data",
										},
									},
								},
							},
						},
					},
				},
			},
		},
	}
}

var _ = framework.KubeDescribe("Generated release_1_5 clientset", func() {
	f := framework.NewDefaultFramework("clientset")
	It("should create v2alpha1 cronJobs, delete cronJobs, watch cronJobs", func() {
		var enabled bool
		groupList, err := f.ClientSet.Discovery().ServerGroups()
		framework.ExpectNoError(err)
		for _, group := range groupList.Groups {
			if group.Name == batchv2alpha1.GroupName {
				for _, version := range group.Versions {
					if version.Version == batchv2alpha1.SchemeGroupVersion.Version {
						enabled = true
						break
					}
				}
			}
		}
		if !enabled {
			framework.Logf("%s is not enabled, test skipped", batchv2alpha1.SchemeGroupVersion)
			return
		}
		cronJobClient := f.ClientSet.BatchV2alpha1().CronJobs(f.Namespace.Name)
		By("constructing the cronJob")
		name := "cronjob" + string(uuid.NewUUID())
		value := strconv.Itoa(time.Now().Nanosecond())
		cronJob := newTestingCronJob(name, value)
		By("setting up watch")
		selector := labels.SelectorFromSet(labels.Set(map[string]string{"time": value})).String()
		options := metav1.ListOptions{LabelSelector: selector}
		cronJobs, err := cronJobClient.List(options)
		if err != nil {
			framework.Failf("Failed to query for cronJobs: %v", err)
		}
		Expect(len(cronJobs.Items)).To(Equal(0))
		options = metav1.ListOptions{
			LabelSelector:   selector,
			ResourceVersion: cronJobs.ListMeta.ResourceVersion,
		}
		w, err := cronJobClient.Watch(options)
		if err != nil {
			framework.Failf("Failed to set up watch: %v", err)
		}

		By("creating the cronJob")
		cronJob, err = cronJobClient.Create(cronJob)
		if err != nil {
			framework.Failf("Failed to create cronJob: %v", err)
		}

		By("verifying the cronJob is in kubernetes")
		options = metav1.ListOptions{
			LabelSelector:   selector,
			ResourceVersion: cronJob.ResourceVersion,
		}
		cronJobs, err = cronJobClient.List(options)
		if err != nil {
			framework.Failf("Failed to query for cronJobs: %v", err)
		}
		Expect(len(cronJobs.Items)).To(Equal(1))

		By("verifying cronJob creation was observed")
		observeCreation(w)

		By("deleting the cronJob")
		// Use DeletePropagationBackground so the CronJob is really gone when the call returns.
		propagationPolicy := metav1.DeletePropagationBackground
		if err := cronJobClient.Delete(cronJob.Name, &metav1.DeleteOptions{PropagationPolicy: &propagationPolicy}); err != nil {
			framework.Failf("Failed to delete cronJob: %v", err)
		}

		options = metav1.ListOptions{LabelSelector: selector}
		cronJobs, err = cronJobClient.List(options)
		if err != nil {
			framework.Failf("Failed to list cronJobs to verify deletion: %v", err)
		}
		Expect(len(cronJobs.Items)).To(Equal(0))
	})
})

var _ = framework.KubeDescribe("Staging client repo client", func() {
	f := framework.NewDefaultFramework("clientset")
	It("should create pods, delete pods, watch pods", func() {
		podClient := f.StagingClient.Core().Pods(f.Namespace.Name)
		By("constructing the pod")
		name := "pod" + string(uuid.NewUUID())
		value := strconv.Itoa(time.Now().Nanosecond())
		podCopy := stagingClientPod(name, value)
		pod := &podCopy
		By("verifying no pod exists before the test")
		pods, err := podClient.List(metav1.ListOptions{})
		if err != nil {
			framework.Failf("Failed to query for pods: %v", err)
		}
		Expect(len(pods.Items)).To(Equal(0))
		By("creating the pod")
		pod, err = podClient.Create(pod)
		if err != nil {
			framework.Failf("Failed to create pod: %v", err)
		}

		By("verifying the pod is in kubernetes")
		timeout := 1 * time.Minute
		if err := wait.PollImmediate(time.Second, timeout, func() (bool, error) {
			pods, err = podClient.List(metav1.ListOptions{})
			if err != nil {
				return false, err
			}
			if len(pods.Items) == 1 {
				return true, nil
			}
			return false, nil
		}); err != nil {
			framework.Failf("Err : %s\n. Failed to wait for 1 pod to be created", err)
		}
	})
})
<|endoftext|>


<commit_before>/*
Copyright 2020 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 settings

import (
	"fmt"
	"strings"

	"github.com/onsi/ginkgo"
	"k8s.io/ingress-nginx/test/e2e/framework"
)

var _ = framework.DescribeSetting("keep-alive keep-alive-requests", func() {
	f := framework.NewDefaultFramework("keep-alive")

	host := "keep-alive"

	ginkgo.BeforeEach(func() {
		f.NewEchoDeployment()
		ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil)
		f.EnsureIngress(ing)
	})

	ginkgo.It("should set keepalive_timeout", func() {
		f.UpdateNginxConfigMapData("keep-alive", "140")

		f.WaitForNginxConfiguration(func(server string) bool {
			return strings.Contains(server, fmt.Sprintf(`keepalive_timeout 140s;`))
		})
	})

	ginkgo.It("should set keepalive_requests", func() {
		f.UpdateNginxConfigMapData("keep-alive-requests", "200")

		f.WaitForNginxConfiguration(func(server string) bool {
			return strings.Contains(server, fmt.Sprintf(`keepalive_requests 200;`))
		})
	})
})
<commit_msg>Add upstream keep alive tests<commit_after>/*
Copyright 2020 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 settings

import (
	"fmt"
	"regexp"
	"strings"

	"github.com/onsi/ginkgo"
	"k8s.io/ingress-nginx/test/e2e/framework"
)

var _ = framework.DescribeSetting("keep-alive keep-alive-requests", func() {
	f := framework.NewDefaultFramework("keep-alive")

	host := "keep-alive"

	ginkgo.BeforeEach(func() {
		f.NewEchoDeployment()
		ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil)
		f.EnsureIngress(ing)
	})

	ginkgo.Context("Check the keep alive", func() {
		ginkgo.It("should set keepalive_timeout", func() {
			f.UpdateNginxConfigMapData("keep-alive", "140")

			f.WaitForNginxConfiguration(func(server string) bool {
				return strings.Contains(server, fmt.Sprintf(`keepalive_timeout 140s;`))
			})
		})

		ginkgo.It("should set keepalive_requests", func() {
			f.UpdateNginxConfigMapData("keep-alive-requests", "200")

			f.WaitForNginxConfiguration(func(server string) bool {
				return strings.Contains(server, fmt.Sprintf(`keepalive_requests 200;`))
			})

		})
	})

	ginkgo.Context("Check the upstream keep alive", func() {
		ginkgo.It("should set keepalive connection to upstream server", func() {
			f.UpdateNginxConfigMapData("upstream-keepalive-connections", "128")

			f.WaitForNginxConfiguration(func(server string) bool {
				match, _ := regexp.MatchString(`upstream\supstream_balancer\s\{[\s\S]*keepalive 128;`, server)
				return match
			})
		})

		ginkgo.It("should set keep alive connection timeout to upstream server", func() {
			f.UpdateNginxConfigMapData("upstream-keepalive-timeout", "120")

			f.WaitForNginxConfiguration(func(server string) bool {
				match, _ := regexp.MatchString(`upstream\supstream_balancer\s\{[\s\S]*keepalive_timeout\s*120s;`, server)
				return match
			})
		})

		ginkgo.It("should set the request count to upstream server through one keep alive connection", func() {
			f.UpdateNginxConfigMapData("upstream-keepalive-requests", "200")

			f.WaitForNginxConfiguration(func(server string) bool {
				match, _ := regexp.MatchString(`upstream\supstream_balancer\s\{[\s\S]*keepalive_requests\s*200;`, server)
				return match
			})
		})
	})
})
<|endoftext|>


<commit_before>package winrm

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/masterzen/winrm/soap"
)

var soapXML = "application/soap+xml"

// HttpPost type func for handling http requests
type HttpPost func(*Client, *soap.SoapMessage) (string, error)

// body func reads the response body and return it as a string
func body(response *http.Response) (string, error) {

	// if we recived the content we expected
	if strings.Contains(response.Header.Get("Content-Type"), "application/soap+xml") {
		body, err := ioutil.ReadAll(response.Body)
		defer response.Body.Close()
		if err != nil {
			return "", fmt.Errorf("error while reading request body %s", err)
		}

		return string(body), nil
	}

	return "", fmt.Errorf("invalid content type")
}

// Http_post make post to the winrm soap service
func Http_post(client *Client, request *soap.SoapMessage) (response string, err error) {
	httpClient := &http.Client{Transport: client.transport}

	req, err := http.NewRequest("POST", client.url, strings.NewReader(request.String()))
	if err != nil {
		err = fmt.Errorf("impossible to create http request %s", err)
		return
	}
	req.Header.Set("Content-Type", soapXML+";charset=UTF-8")
	req.SetBasicAuth(client.username, client.password)
	resp, err := httpClient.Do(req)
	if err != nil {
		return "", fmt.Errorf("unknown error %s", err)
	}

	if resp.StatusCode != 200 {
		return "", fmt.Errorf("http error: %d", resp.StatusCode)
	}

	return body(resp)

}
<commit_msg>Improve error handling on Http_post and add settings to ensure the connection is closed and prevent stale connections.<commit_after>package winrm

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/masterzen/winrm/soap"
)

var soapXML = "application/soap+xml"

// HttpPost type func for handling http requests
type HttpPost func(*Client, *soap.SoapMessage) (string, error)

// body func reads the response body and return it as a string
func body(response *http.Response) (string, error) {

	// if we recived the content we expected
	if strings.Contains(response.Header.Get("Content-Type"), "application/soap+xml") {
		body, err := ioutil.ReadAll(response.Body)
		defer response.Body.Close()
		if err != nil {
			return "", fmt.Errorf("error while reading request body %s", err)
		}

		return string(body), nil
	}

	return "", fmt.Errorf("invalid content type")
}

// Http_post make post to the winrm soap service
func Http_post(client *Client, request *soap.SoapMessage) (response string, err error) {
	httpClient := &http.Client{Transport: client.transport}

	req, err := http.NewRequest("POST", client.url, strings.NewReader(request.String()))
	if err != nil {
		err = fmt.Errorf("impossible to create http request %s", err)
		return
	}
	req.Header.Set("Content-Type", soapXML+";charset=UTF-8")
	req.SetBasicAuth(client.username, client.password)
	resp, err := httpClient.Do(req)
	if err != nil {
		return "", fmt.Errorf("unknown error %s", err)
	}

	body, err := body(resp)
	if resp.StatusCode != 200 && err != nil {
		return "", fmt.Errorf("http error: %d - %s", resp.StatusCode, body)
	}

	return body, err
}
<|endoftext|>


<commit_before>// Copyright 2015 Andrew E. Bruno
//
// 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 khttp is a transport that authenticates all outgoing requests using
// SPNEGO (negotiate authentication) http://tools.ietf.org/html/rfc4559.
package khttp

import (
    "errors"
    "os"
    "fmt"
    "net/http"
    "strings"

    "github.com/ubccr/kerby"
)

var (
    negotiateHeader       = "Negotiate"
    wwwAuthenticateHeader = "WWW-Authenticate"
    authorizationHeader   = "Authorization"
)

// HTTP client transport that authenticates all outgoing
// requests using SPNEGO. Implements the http.RoundTripper interface
type Transport struct {
    // keytab file to use
    KeyTab  string
    // principal
    Principal  string
    // Next specifies the next transport to be used or http.DefaultTransport if nil.
    Next http.RoundTripper
}

// RoundTrip executes a single HTTP transaction performing SPNEGO negotiate
// authentication.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
    if len(t.KeyTab) > 0 {
        os.Setenv("KRB5_CLIENT_KTNAME", t.KeyTab)
    }
    service := fmt.Sprintf("HTTP@%s", req.URL.Host)
    kc := new(kerby.KerbClient)
    err := kc.Init(service, t.Principal)
    if err != nil {
        return nil, err
    }

    err = kc.Step("")
    if err != nil {
        return nil, err
    }

    req.Header.Set(authorizationHeader, negotiateHeader+" "+kc.Response())

    tr := t.Next
    if tr == nil {
        tr = http.DefaultTransport
        if tr == nil {
            return nil, errors.New("khttp: no Next transport or DefaultTransport")
        }
    }

    resp, err := tr.RoundTrip(req)
    if err != nil {
        return nil, err
    }

    authReply := strings.Split(resp.Header.Get(wwwAuthenticateHeader), " ")
    if len(authReply) != 2 || authReply[0] != negotiateHeader {
        return nil, errors.New("khttp: server replied with invalid authorization header")
    }

    // Authenticate the reply from the server
    err = kc.Step(authReply[1])
    if err != nil {
        return nil, err
    }
    kc.Clean()

    return resp, nil
}
<commit_msg>Better error message<commit_after>// Copyright 2015 Andrew E. Bruno
//
// 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 khttp is a transport that authenticates all outgoing requests using
// SPNEGO (negotiate authentication) http://tools.ietf.org/html/rfc4559.
package khttp

import (
    "errors"
    "os"
    "fmt"
    "net/http"
    "strings"

    "github.com/ubccr/kerby"
)

var (
    negotiateHeader       = "Negotiate"
    wwwAuthenticateHeader = "WWW-Authenticate"
    authorizationHeader   = "Authorization"
)

// HTTP client transport that authenticates all outgoing
// requests using SPNEGO. Implements the http.RoundTripper interface
type Transport struct {
    // keytab file to use
    KeyTab  string
    // principal
    Principal  string
    // Next specifies the next transport to be used or http.DefaultTransport if nil.
    Next http.RoundTripper
}

// RoundTrip executes a single HTTP transaction performing SPNEGO negotiate
// authentication.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
    if len(t.KeyTab) > 0 {
        os.Setenv("KRB5_CLIENT_KTNAME", t.KeyTab)
    }
    service := fmt.Sprintf("HTTP@%s", req.URL.Host)
    kc := new(kerby.KerbClient)
    err := kc.Init(service, t.Principal)
    if err != nil {
        return nil, err
    }

    err = kc.Step("")
    if err != nil {
        return nil, err
    }

    req.Header.Set(authorizationHeader, negotiateHeader+" "+kc.Response())

    tr := t.Next
    if tr == nil {
        tr = http.DefaultTransport
        if tr == nil {
            return nil, errors.New("khttp: no Next transport or DefaultTransport")
        }
    }

    resp, err := tr.RoundTrip(req)
    if err != nil {
        return nil, err
    }

    authReply := strings.Split(resp.Header.Get(wwwAuthenticateHeader), " ")
    if len(authReply) != 2 || authReply[0] != negotiateHeader {
        return nil, errors.New("khttp: server replied with invalid www-authenticate header")
    }

    // Authenticate the reply from the server
    err = kc.Step(authReply[1])
    if err != nil {
        return nil, err
    }
    kc.Clean()

    return resp, nil
}
<|endoftext|>


<commit_before>// Copyright 2019 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.

package fuzzreader

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
)

func TestCorpusSymlinks(t *testing.T) {
	fds, err := ioutil.ReadDir("corpus")
	if err != nil {
		t.Fatal(err)
	}
	if len(fds) == 0 {
		t.Fatal("no file descriptors found in corpus/")
	}

	for i := range fds {
		if fds[i].Mode()&os.ModeSymlink != 0 {
			if path, err := os.Readlink(filepath.Join("corpus", fds[i].Name())); err != nil {
				t.Errorf("broken symlink: %v", err)
			} else {
				if _, err := os.Stat(filepath.Join("corpus", path)); err != nil {
					t.Errorf("broken symlink: %v", err)
				}
			}
		} else {
			t.Errorf("%s isn't a symlink, move outside corpus/ and symlink into directory", fds[i].Name())
		}
	}
}
<commit_msg>Update reader_test.go<commit_after>// Copyright 2019 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.

package fuzzreader

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"runtime"
	"testing"
)

func TestCorpusSymlinks(t *testing.T) {

	// avoid symbolic link error on windows
	if runtime.GOOS != "windows" {
		fds, err := ioutil.ReadDir("corpus")
		if err != nil {
			t.Fatal(err)
		}
		if len(fds) == 0 {
			t.Fatal("no file descriptors found in corpus/")
		}

		for i := range fds {
			if fds[i].Mode()&os.ModeSymlink != 0 {
				if path, err := os.Readlink(filepath.Join("corpus", fds[i].Name())); err != nil {
					t.Errorf("broken symlink: %v", err)
				} else {
					if _, err := os.Stat(filepath.Join("corpus", path)); err != nil {
						t.Errorf("broken symlink: %v", err)
					}
				}
			} else {
				t.Errorf("%s isn't a symlink, move outside corpus/ and symlink into directory", fds[i].Name())
			}
		}
	}
}
<|endoftext|>